module main import alpm import os import testdb fn check_tmp(name string) !(string, string) { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_dbcheck_${name}_${os.getpid()}') os.mkdir_all(os.join_path(tmp, 'root'))! return tmp, os.join_path(tmp, 'db') + '/' } fn test_db_local_files() ! { tmp, dbpath := check_tmp('files')! defer { os.rmdir_all(tmp) or {} } testdb.write_local(dbpath, [testdb.Pkg{ name: 'whole' }])! os.mkdir_all(os.join_path(dbpath, 'local', 'nodesc-1.0-1'))! os.write_file(os.join_path(dbpath, 'local', 'nodesc-1.0-1', 'files'), '%FILES%\n\n')! os.mkdir_all(os.join_path(dbpath, 'local', 'nofiles-1.0-1'))! os.write_file(os.join_path(dbpath, 'local', 'nofiles-1.0-1', 'desc'), '%NAME%\nnofiles\n\n')! assert db_local_files(dbpath)! == ["'nodesc-1.0-1': description file is missing", "'nofiles-1.0-1': file list is missing"] } fn test_check_db_local() ! { tmp, dbpath := check_tmp('local')! defer { os.rmdir_all(tmp) or {} } testdb.write_local(dbpath, [ testdb.Pkg{ name: 'app' depends: ['lib>=1.0', 'gone', 'old<1.0'] files: ['usr/', 'usr/bin/', 'usr/bin/app', 'usr/share/shared'] }, testdb.Pkg{ name: 'lib' files: ['usr/', 'usr/lib/libx.so'] }, testdb.Pkg{ name: 'old' }, testdb.Pkg{ name: 'rival' conflicts: ['app'] files: ['usr/', 'usr/share/shared'] }, ])! handle := alpm.initialize(os.join_path(tmp, 'root') + '/', dbpath)! defer { handle.release() or {} } errors := check_db_local(handle, dbpath)! // a dependency nothing provides, one whose version does not match, two // packages in conflict, and a file two packages own. the shared usr/ // directory is no clash assert "missing 'gone' dependency for 'app'" in errors assert "missing 'old<1.0' dependency for 'app'" in errors assert errors.any(it.contains('conflicts with') && it.contains("'app'") && it.contains("'rival'")) assert "file owned by 'app' and 'rival': 'usr/share/shared'" in errors assert errors.len == 4, errors.str() } fn test_check_db_local_clean() ! { tmp, dbpath := check_tmp('clean')! defer { os.rmdir_all(tmp) or {} } testdb.write_local(dbpath, [ testdb.Pkg{ name: 'app' depends: ['lib'] files: ['usr/', 'usr/bin/app'] }, testdb.Pkg{ name: 'lib' files: ['usr/', 'usr/lib/lib.so'] }, ])! handle := alpm.initialize(os.join_path(tmp, 'root') + '/', dbpath)! defer { handle.release() or {} } assert check_db_local(handle, dbpath)! == [] } fn test_db_local_files_edges() ! { tmp, dbpath := check_tmp('fileedges')! defer { os.rmdir_all(tmp) or {} } // a local db that is not there cannot be checked, which is an error if got := db_local_files(dbpath) { assert false, 'a missing local db checked clean: ${got}' } else { assert err.msg().contains('could not open the local database'), err.msg() } // the version file alone is an empty db: it is no package entry, and // nothing is missing from it testdb.write_local(dbpath, [])! assert os.exists(os.join_path(dbpath, 'local', 'ALPM_DB_VERSION')) assert db_local_files(dbpath)! == [] // an entry with neither file is named for both, in order os.mkdir_all(os.join_path(dbpath, 'local', 'bare-1.0-1'))! assert db_local_files(dbpath)! == ["'bare-1.0-1': description file is missing", "'bare-1.0-1': file list is missing"] // whole entries beside it stay unnamed, and the order is the names' testdb.write_local(dbpath, [testdb.Pkg{ name: 'aaa' }, testdb.Pkg{ name: 'zzz' }])! os.rm(os.join_path(dbpath, 'local', 'zzz-1.0-1', 'files'))! assert db_local_files(dbpath)! == ["'bare-1.0-1': description file is missing", "'bare-1.0-1': file list is missing", "'zzz-1.0-1': file list is missing"] }