module coracle import nspkg.engine import os struct PreparationTrace { mut: events []string } fn reference_preparation_event(context voidptr, event &Event) { mut trace := unsafe { &PreparationTrace(context) } match unsafe { event.type_ } { .checkdeps_start { trace.events << 'check:start' } .checkdeps_done { trace.events << 'check:done' } .optdep_removal { removed := unsafe { event.optdep_removal } trace.events << 'optional:${removed.pkg.get_name()}:${removed.optdep.compute_string()}' } else { assert false, 'unexpected preparation event' } } } fn native_preparation_event(context voidptr, event engine.PreparationEvent) { mut trace := unsafe { &PreparationTrace(context) } match event.kind { .check_dependencies_start { trace.events << 'check:start' } .check_dependencies_done { trace.events << 'check:done' } .optional_dependency_removed { trace.events << 'optional:${event.package.name}:${event.dependency.compute_string()}' } else { assert false, 'unexpected preparation event' } } } fn removal_fixture(path string, name string, depends []string, provides []string, optional []string, reason int) ! { os.mkdir_all(os.join_path(path, 'local', '${name}-1-1'))! text := '%REASON%\n${reason}\n\n%DEPENDS%\n${depends.join('\n')}\n\n%PROVIDES%\n${provides.join('\n')}\n\n%OPTDEPENDS%\n${optional.join('\n')}\n\n' os.write_file(os.join_path(path, 'local', '${name}-1-1', 'desc'), text)! os.write_file(os.join_path(path, 'local', '${name}-1-1', 'files'), '')! } fn test_native_removal_preparation_matches_libalpm_flag_matrix() ! { dir := os.join_path(os.vtmp_dir(), 'alpm-removal-prepare-parity-${os.getpid()}') os.mkdir_all(os.join_path(dir, 'local'))! defer { os.rmdir_all(dir) or { eprintln(err) } } os.write_file(os.join_path(dir, 'local', 'ALPM_DB_VERSION'), '9\n')! removal_fixture(dir, 'base', [], ['virtual=2'], [], 1)! removal_fixture(dir, 'shared', ['base'], [], [], 1)! removal_fixture(dir, 'top', ['shared'], [], [], 0)! removal_fixture(dir, 'consumer', ['virtual>=2'], [], [], 0)! removal_fixture(dir, 'obsolete', ['virtual>=3'], [], [], 0)! removal_fixture(dir, 'broken', ['already-missing'], [], [], 0)! removal_fixture(dir, 'optional', [], [], ['virtual>=2: integration', 'leaf'], 0)! removal_fixture(dir, 'leaf', [], [], [], 1)! removal_fixture(dir, 'cycle-a', ['cycle-b'], [], [], 1)! removal_fixture(dir, 'cycle-b', ['cycle-a'], [], [], 1)! removal_fixture(dir, 'cycle-top', ['cycle-a'], [], [], 0)! old := initialize('/', dir)! defer { old.release() or { eprintln(err) } } refdb := old.get_localdb()! mut native := engine.initialize('/', dir)! defer { native.release() or { eprintln(err) } } mut db := native.local_database()! mut ref_trace := &PreparationTrace{} mut native_trace := &PreparationTrace{} old.option_set_eventcb(reference_preparation_event, ref_trace)! native.set_preparation_callback(native_preparation_event, native_trace)! flag_bits := [C.ALPM_TRANS_FLAG_NODEPS, C.ALPM_TRANS_FLAG_CASCADE, C.ALPM_TRANS_FLAG_RECURSE, C.ALPM_TRANS_FLAG_RECURSEALL, C.ALPM_TRANS_FLAG_UNNEEDED, C.ALPM_TRANS_FLAG_NODEPVERSION] for combination in 0 .. 64 { mut flags := int(C.ALPM_TRANS_FLAG_NOLOCK) for bit, mask in flag_bits { if combination & (1 << bit) != 0 { flags |= mask } } for targets in [['top'], ['base'], ['leaf'], ['base', 'shared'], ['cycle-top']] { assert C.oracle_alpm_trans_init(old, flags) == 0 native.start_transaction(unsafe { engine.TransactionFlag(flags) })! for name in targets { old.remove_pkg(refdb.get_pkg(name) or { return error('missing C target') })! native.remove_package(db.get_pkg(name) or { return error('missing native target') })! } ref_trace.events = []string{} native_trace.events = []string{} problems, problem := old.trans_prepare_problems() want_kind := old.errno() result := native.prepare_transaction()! assert result.success == (problem == none), '${combination}: ${targets}' if !result.success { assert int(result.kind) == int(want_kind) assert result.missing.map('${it.target}|${it.dependency.compute_string()}|${it.causing_package}') == problems.map('${it.target}|${it.depend}|${it.causing}') assert native.transaction_state()! == .initialized } else { assert native.transaction_state()! == .prepared } assert native.transaction_remove()!.map(it.name) == old.trans_get_remove().pkgs().map(it.get_name()), '${combination}: ${targets}' assert native_trace.events == ref_trace.events, '${combination}: ${targets}' native.release_transaction()! old.trans_release()! } } } fn test_native_and_libalpm_agree_on_lock_ownership() ! { dir := os.join_path(os.vtmp_dir(), 'alpm-lock-parity-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } old := initialize('/', dir)! defer { old.release() or { eprintln(err) } } mut native := engine.initialize('/', dir)! defer { native.release() or { eprintln(err) } } old.trans_init(trans_flag_0)! if _ := native.start_transaction(engine.transaction_flag_0) { assert false } assert native.errno() == .handle_lock if _ := native.transaction_remove() { assert false, 'a failed start cannot leave a transaction' } assert old.unlock_async() == 0 assert old.unlock_async() == 0 native.start_transaction(engine.transaction_flag_0)! old.trans_release()! assert os.exists(native.lockfile), 'the old owner must not unlink the new owner’s lock' native.unlock()! old.trans_init(trans_flag_0)! native.release_transaction()! assert os.exists(native.lockfile) old.trans_release()! assert !os.exists(native.lockfile) old.trans_init(.nolock)! native.start_transaction(.nolock)! assert native.prepare_transaction()!.success old.trans_prepare()! if _ := old.trans_interrupt() { assert false } if _ := native.interrupt_transaction() { assert false } assert int(native.errno()) == int(old.errno()) native.release_transaction()! old.trans_release()! assert unsafe { (&Handle(nil)).unlock_async() } == -1 }