module main import alpm import json2 import os fn test_split_list() { // repeatable and comma separated, as pacman reads --ignore and friends assert split_list([]) == [] assert split_list(['a']) == ['a'] assert split_list(['a,b', 'c']) == ['a', 'b', 'c'] assert split_list([' a , b ', ',,', '']) == ['a', 'b'] assert split_list(['/usr/*,/etc/*']) == ['/usr/*', '/etc/*'] } fn test_dep_check_flag() { // pacman's -d skips the version checks, -dd every dependency check; // --nodeps is one -d assert dep_check_flag(false, 0) == alpm.trans_flag_0 assert dep_check_flag(true, 0) == alpm.TransFlag.no_depversion assert dep_check_flag(false, 1) == alpm.TransFlag.no_depversion assert dep_check_flag(false, 2) == alpm.TransFlag.no_deps assert dep_check_flag(true, 1) == alpm.TransFlag.no_deps assert dep_check_flag(false, 3) == alpm.TransFlag.no_deps } // trans_sandbox opens a handle on an empty temp db, and points the ipc // channel at a file, so what the lifecycle says can be read back fn trans_sandbox(name string) !(string, &alpm.Handle) { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_trans_${name}_${os.getpid()}') os.rmdir_all(tmp) or {} os.mkdir_all(os.join_path(tmp, 'root'))! os.mkdir_all(os.join_path(tmp, 'db', 'local'))! os.write_file(os.join_path(tmp, 'db', 'local', 'ALPM_DB_VERSION'), '9\n')! handle := alpm.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')! return tmp, handle } // said runs f with --ui ipc on and the channel in a file, and returns the // texts of the messages f sent fn said(tmp string, f fn ()) ![]string { path := os.join_path(tmp, 'channel') mut file := os.create(path)! saved_kind, saved_fd := ui_kind, ipc_fd ui_kind, ipc_fd = 'ipc', file.fd f() ui_kind, ipc_fd = saved_kind, saved_fd file.close() mut out := []string{} for line in os.read_lines(path)! { out << json2.decode[json2.Any](line)!.as_map()['text'] or { json2.Any('') }.str() } return out } fn test_trans_cleanup_releases_once() ! { tmp, handle := trans_sandbox('once')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } lpath := os.join_path(tmp, 'db', 'db.lck') trans_begin(handle, alpm.trans_flag_0)! assert os.exists(lpath) msgs := said(tmp, fn [handle] () { trans_cleanup(handle) // a second cleanup — a defer after an explicit one — is a no-op, // not a release libalpm would refuse trans_cleanup(handle) })! assert msgs.len == 0 assert !os.exists(lpath) mut open := txn_open assert open.load() == 0 } fn test_trans_cleanup_failure_is_said_and_counts_as_closed() ! { tmp, handle := trans_sandbox('failure')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } // the process believes in a transaction libalpm does not have: the // release fails, the way it would after libalpm dropped it mut open := txn_open open.store(txn_locked) msgs := said(tmp, fn [handle] () { trans_cleanup(handle) trans_cleanup(handle) })! // said once, and closed: the second cleanup did not try again assert msgs.len == 1 assert msgs[0].starts_with('failed to release the transaction: ') assert open.load() == 0 } fn test_trans_commit_releases() ! { // a committed transaction lets go of the lock through the commit's own // cleanup, and leaves the deferred one nothing to do. an empty one would // not do: libalpm's prepare returns early on nothing and its commit then // refuses as not prepared, so a package is really removed tmp, handle := trans_sandbox('commit')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } entry := os.join_path(tmp, 'db', 'local', 'gone-1.0-1') os.mkdir_all(entry)! os.write_file(os.join_path(entry, 'desc'), '%NAME%\ngone\n\n%VERSION%\n1.0-1\n\n')! os.write_file(os.join_path(entry, 'files'), '%FILES%\n\n')! trans_begin(handle, alpm.trans_flag_0)! handle.remove_pkg(handle.find_local_pkg('gone') or { return error('gone is not installed') })! trans_prepare_checked(handle)! trans_commit(handle)! assert !os.exists(os.join_path(tmp, 'db', 'db.lck')) assert !os.exists(entry) mut open := txn_open assert open.load() == 0 msgs := said(tmp, fn [handle] () { trans_cleanup(handle) })! assert msgs.len == 0 } fn test_trans_options_of_install_and_upgrade() ! { // the same four flags, read the same way from either command line want := TransOptions{ overwrite: ['/usr/*'] ignore: ['a,b'] ignoregroup: ['g'] assume_installed: ['sh=5'] } assert trans_options(ConfigInstall{ overwrite: ['/usr/*'] ignore: ['a,b'] ignoregroup: ['g'] assume_installed: ['sh=5'] }) == want assert trans_options(ConfigUpgrade{ overwrite: ['/usr/*'] ignore: ['a,b'] ignoregroup: ['g'] assume_installed: ['sh=5'] }) == want // and they reach the handle a transaction runs on, lists split tmp, handle := trans_sandbox('options')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } want.apply(handle)! mut ignored := handle.option_get_ignore_pkgs() assert ignored.strs() == ['a', 'b'] mut overwrite := handle.option_get_overwrite_files() assert overwrite.strs() == ['/usr/*'] // a spec that names no package is said, rather than assuming nothing for spec in ['=', '=1.0', '>=2'] { TransOptions{ assume_installed: [spec] }.apply(handle) or { assert err.msg().contains('names no package'), err.msg() continue } assert false, '--assume-installed ${spec} was taken' } } // killed_child forks, lets the child take the db lock of the sandbox the // way take describes, and kills it with SIGTERM through the installed // handler; it returns the child's exit status. the child runs V code — the // handlers' installation and take, a trans_begin in the tests here — on its // copy of the test's memory, and only ever leaves through _exit (the // handler's, or the 98 and 99 of a failure), so no defer, no atexit and no // test-runner report of the parent's runs a second time in it fn killed_child(handle &alpm.Handle, take fn (&alpm.Handle)) int { pid := C.fork() if pid == 0 { install_interrupt_handlers() take(handle) C.kill(C.getpid(), 15) // the handler exits; reaching here is the failure C._exit(99) } mut status := 0 C.waitpid(pid, &status, 0) return (status >> 8) & 0xff } fn test_interrupt_lets_go_of_the_lock() ! { tmp, handle := trans_sandbox('interrupt')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } lpath := os.join_path(tmp, 'db', 'db.lck') // a transaction is open, the lock held: the killed run removes it status := killed_child(handle, fn (h &alpm.Handle) { trans_begin(h, alpm.trans_flag_0) or { C._exit(98) } }) assert status == 128 + 15 assert !os.exists(lpath) } fn test_interrupt_right_after_the_lock_is_taken() ! { tmp, handle := trans_sandbox('interrupt_window')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } lpath := os.join_path(tmp, 'db', 'db.lck') // the signal lands between libalpm taking the lock and vpac recording // the transaction: the handle was published before, so the lock goes status := killed_child(handle, fn (h &alpm.Handle) { publish_for_interrupt(h) h.trans_init(alpm.trans_flag_0) or { C._exit(98) } }) assert status == 128 + 15 assert !os.exists(lpath) } fn test_interrupt_leaves_a_lock_it_does_not_hold() ! { tmp, handle := trans_sandbox('interrupt_other')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } lpath := os.join_path(tmp, 'db', 'db.lck') // another process holds the lock, and this run only previews (--print // takes none): the killed run leaves the other's lock alone os.write_file(lpath, '')! status := killed_child(handle, fn (h &alpm.Handle) { trans_begin(h, print_flags(true)) or { C._exit(98) } }) assert status == 128 + 15 assert os.exists(lpath) } fn test_trans_publishes_its_handle_while_open() ! { tmp, handle := trans_sandbox('publish')! defer { os.rmdir_all(tmp) or {} } defer { handle.release() or {} } mut published := interrupt_handle assert published.load() == 0 trans_begin(handle, alpm.trans_flag_0)! assert published.load() == u64(voidptr(handle)) trans_cleanup(handle) assert published.load() == 0 // a begin that fails publishes nothing past its failure: the lock is // someone else's here os.write_file(os.join_path(tmp, 'db', 'db.lck'), '')! if _ := trans_begin(handle, alpm.trans_flag_0) { assert false, 'a held lock was taken' } assert published.load() == 0 } // a failed prepare is worded by its kind, pacman's three shapes and // libalpm's own text for the rest fn test_prepare_problem_lines() { dep := prepare_problem_lines(.unsatisfied_deps, [ alpm.DepProblem{ target: 'user' depend: 'lib<2.0' causing: 'lib' }, alpm.DepProblem{ target: 'app' depend: 'gone' }, ]) assert dep == ['could not satisfy dependencies:', " lib breaks dependency 'lib<2.0' required by user", " unable to satisfy dependency 'gone' required by app"] conflict := prepare_problem_lines(.conflicting_deps, [ alpm.DepProblem{ target: 'p1' depend: 'p2' causing: 'p2' }, alpm.DepProblem{ target: 'p1' depend: 'virt<2' causing: 'p3' }, ]) assert conflict == ['unresolvable package conflicts:', ' p1 and p2 are in conflict', ' p1 and p3 are in conflict (virt<2)'] wrong_arch := prepare_problem_lines(.pkg_invalid_arch, [alpm.DepProblem{ target: 'foo-1.0-1-riscv64' }]) assert wrong_arch.len == 2 assert wrong_arch[1] == ' package foo-1.0-1-riscv64 does not have a valid architecture' other := prepare_problem_lines(.trans_dup_target, [alpm.DepProblem{ target: 'foo' }]) assert other.len == 2 && other[1] == ' foo' }