| 1 | module main |
| 2 | |
| 3 | import alpm |
| 4 | import os |
| 5 | import testdb |
| 6 | |
| 7 | fn check_tmp(name string) !(string, string) { |
| 8 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_dbcheck_${name}_${os.getpid()}') |
| 9 | os.mkdir_all(os.join_path(tmp, 'root'))! |
| 10 | return tmp, os.join_path(tmp, 'db') + '/' |
| 11 | } |
| 12 | |
| 13 | fn test_db_local_files() ! { |
| 14 | tmp, dbpath := check_tmp('files')! |
| 15 | defer { os.rmdir_all(tmp) or {} } |
| 16 | testdb.write_local(dbpath, [testdb.Pkg{ |
| 17 | name: 'whole' |
| 18 | }])! |
| 19 | os.mkdir_all(os.join_path(dbpath, 'local', 'nodesc-1.0-1'))! |
| 20 | os.write_file(os.join_path(dbpath, 'local', 'nodesc-1.0-1', 'files'), '%FILES%\n\n')! |
| 21 | os.mkdir_all(os.join_path(dbpath, 'local', 'nofiles-1.0-1'))! |
| 22 | os.write_file(os.join_path(dbpath, 'local', 'nofiles-1.0-1', 'desc'), '%NAME%\nnofiles\n\n')! |
| 23 | assert db_local_files(dbpath)! == ["'nodesc-1.0-1': description file is missing", |
| 24 | "'nofiles-1.0-1': file list is missing"] |
| 25 | } |
| 26 | |
| 27 | fn test_check_db_local() ! { |
| 28 | tmp, dbpath := check_tmp('local')! |
| 29 | defer { os.rmdir_all(tmp) or {} } |
| 30 | testdb.write_local(dbpath, [ |
| 31 | testdb.Pkg{ |
| 32 | name: 'app' |
| 33 | depends: ['lib>=1.0', 'gone', 'old<1.0'] |
| 34 | files: ['usr/', 'usr/bin/', 'usr/bin/app', 'usr/share/shared'] |
| 35 | }, |
| 36 | testdb.Pkg{ |
| 37 | name: 'lib' |
| 38 | files: ['usr/', 'usr/lib/libx.so'] |
| 39 | }, |
| 40 | testdb.Pkg{ |
| 41 | name: 'old' |
| 42 | }, |
| 43 | testdb.Pkg{ |
| 44 | name: 'rival' |
| 45 | conflicts: ['app'] |
| 46 | files: ['usr/', 'usr/share/shared'] |
| 47 | }, |
| 48 | ])! |
| 49 | handle := alpm.initialize(os.join_path(tmp, 'root') + '/', dbpath)! |
| 50 | defer { handle.release() or {} } |
| 51 | errors := check_db_local(handle, dbpath)! |
| 52 | // a dependency nothing provides, one whose version does not match, two |
| 53 | // packages in conflict, and a file two packages own. the shared usr/ |
| 54 | // directory is no clash |
| 55 | assert "missing 'gone' dependency for 'app'" in errors |
| 56 | assert "missing 'old<1.0' dependency for 'app'" in errors |
| 57 | assert errors.any(it.contains('conflicts with') && it.contains("'app'") |
| 58 | && it.contains("'rival'")) |
| 59 | assert "file owned by 'app' and 'rival': 'usr/share/shared'" in errors |
| 60 | assert errors.len == 4, errors.str() |
| 61 | } |
| 62 | |
| 63 | fn test_check_db_local_clean() ! { |
| 64 | tmp, dbpath := check_tmp('clean')! |
| 65 | defer { os.rmdir_all(tmp) or {} } |
| 66 | testdb.write_local(dbpath, [ |
| 67 | testdb.Pkg{ |
| 68 | name: 'app' |
| 69 | depends: ['lib'] |
| 70 | files: ['usr/', 'usr/bin/app'] |
| 71 | }, |
| 72 | testdb.Pkg{ |
| 73 | name: 'lib' |
| 74 | files: ['usr/', 'usr/lib/lib.so'] |
| 75 | }, |
| 76 | ])! |
| 77 | handle := alpm.initialize(os.join_path(tmp, 'root') + '/', dbpath)! |
| 78 | defer { handle.release() or {} } |
| 79 | assert check_db_local(handle, dbpath)! == [] |
| 80 | } |
| 81 | |
| 82 | fn test_db_local_files_edges() ! { |
| 83 | tmp, dbpath := check_tmp('fileedges')! |
| 84 | defer { os.rmdir_all(tmp) or {} } |
| 85 | // a local db that is not there cannot be checked, which is an error |
| 86 | if got := db_local_files(dbpath) { |
| 87 | assert false, 'a missing local db checked clean: ${got}' |
| 88 | } else { |
| 89 | assert err.msg().contains('could not open the local database'), err.msg() |
| 90 | } |
| 91 | // the version file alone is an empty db: it is no package entry, and |
| 92 | // nothing is missing from it |
| 93 | testdb.write_local(dbpath, [])! |
| 94 | assert os.exists(os.join_path(dbpath, 'local', 'ALPM_DB_VERSION')) |
| 95 | assert db_local_files(dbpath)! == [] |
| 96 | // an entry with neither file is named for both, in order |
| 97 | os.mkdir_all(os.join_path(dbpath, 'local', 'bare-1.0-1'))! |
| 98 | assert db_local_files(dbpath)! == ["'bare-1.0-1': description file is missing", |
| 99 | "'bare-1.0-1': file list is missing"] |
| 100 | // whole entries beside it stay unnamed, and the order is the names' |
| 101 | testdb.write_local(dbpath, [testdb.Pkg{ |
| 102 | name: 'aaa' |
| 103 | }, testdb.Pkg{ |
| 104 | name: 'zzz' |
| 105 | }])! |
| 106 | os.rm(os.join_path(dbpath, 'local', 'zzz-1.0-1', 'files'))! |
| 107 | assert db_local_files(dbpath)! == ["'bare-1.0-1': description file is missing", |
| 108 | "'bare-1.0-1': file list is missing", "'zzz-1.0-1': file list is missing"] |
| 109 | } |
| 110 | |