module coracle import nspkg as facade import nspkg.tests.installfixtures import os struct PublicInstallTrace { root string logfile string interrupt bool mut: items []string action_count int reference &Handle = unsafe { nil } native &facade.Handle = unsafe { nil } } fn public_install_reference_event(context voidptr, event &Event) { mut trace := unsafe { &PublicInstallTrace(context) } trace.capture_actions() type_ := unsafe { event.type_ } match type_ { .package_operation_start, .package_operation_done { item := unsafe { event.package_operation } old := if item.oldpkg == unsafe { nil } { '' } else { item.oldpkg.get_name() + ':' + item.oldpkg.get_version() } new := if item.newpkg == unsafe { nil } { '' } else { item.newpkg.get_name() + ':' + item.newpkg.get_version() + ':' + u32(item.newpkg.get_reason()).str() } trace.items << '${type_}:${item.operation}:${old}:${new}' if trace.interrupt && type_ == .package_operation_start && item.operation != .remove { trace.reference.trans_interrupt() or { assert false, err.msg() } } } .pacnew_created { item := unsafe { event.pacnew_created } trace.items << 'pacnew:${item.from_noupgrade}:${unsafe { cstring_to_vstring(item.file) }[trace.root.len..]}' } .pacsave_created { trace.items << 'pacsave:${unsafe { cstring_to_vstring(event.pacsave_created.file) }[trace.root.len..]}' } .transaction_start, .transaction_done, .fileconflicts_start, .fileconflicts_done, .keyring_start, .keyring_done, .integrity_start, .integrity_done, .load_start, .load_done, .diskspace_start, .diskspace_done { trace.items << type_.str() } else {} } } fn public_install_native_event(context voidptr, event &facade.Event) { mut trace := unsafe { &PublicInstallTrace(context) } trace.capture_actions() type_ := unsafe { event.type_ } match type_ { .package_operation_start, .package_operation_done { item := unsafe { event.package_operation } old := if item.oldpkg == unsafe { nil } { '' } else { item.oldpkg.get_name() + ':' + item.oldpkg.get_version() } new := if item.newpkg == unsafe { nil } { '' } else { item.newpkg.get_name() + ':' + item.newpkg.get_version() + ':' + u32(item.newpkg.get_reason()).str() } trace.items << '${type_}:${item.operation}:${old}:${new}' if trace.interrupt && type_ == .package_operation_start && item.operation != .remove { trace.native.trans_interrupt() or { assert false, err.msg() } } } .pacnew_created { item := unsafe { event.pacnew_created } trace.items << 'pacnew:${item.from_noupgrade}:${unsafe { cstring_to_vstring(item.file) }[trace.root.len..]}' } .pacsave_created { trace.items << 'pacsave:${unsafe { cstring_to_vstring(event.pacsave_created.file) }[trace.root.len..]}' } .transaction_start, .transaction_done, .fileconflicts_start, .fileconflicts_done, .keyring_start, .keyring_done, .integrity_start, .integrity_done, .load_start, .load_done, .diskspace_start, .diskspace_done { trace.items << type_.str() } else {} } } fn public_install_reference_progress(context voidptr, kind Progress, name &u8, percent i32, total uint, current uint) { mut trace := unsafe { &PublicInstallTrace(context) } trace.capture_actions() trace.items << 'progress:${kind.str().all_before('_start')}:${unsafe { cstring_to_vstring(name) }}:${percent}:${total}:${current}' } fn public_install_native_progress(context voidptr, kind facade.Progress, name &u8, percent i32, total uint, current uint) { mut trace := unsafe { &PublicInstallTrace(context) } trace.capture_actions() trace.items << 'progress:${kind.str().all_before('_start')}:${unsafe { cstring_to_vstring(name) }}:${percent}:${total}:${current}' } fn public_install_reference(fixture installfixtures.Fixture, scenario installfixtures.Scenario, download_only bool) ![]string { handle := initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.option_add_cachedir(fixture.cache)! logfile := os.join_path(os.dir(fixture.db), 'actions.log') handle.option_set_logfile(logfile)! mut trace := &PublicInstallTrace{ root: handle.option_get_root(), logfile: logfile, reference: handle, interrupt: scenario.interrupt } handle.option_set_eventcb(public_install_reference_event, trace)! handle.option_set_progresscb(public_install_reference_progress, trace)! for pattern in scenario.no_upgrade { handle.option_add_noupgrade(pattern)! } for pattern in scenario.no_extract { handle.option_add_no_extract(pattern)! } handle.option_set_checkspace(if scenario.check_space { 1 } else { 0 })! mut flags := TransFlag.no_deps | .no_conflicts | .no_hooks | .no_scriptlet if scenario.db_only { flags.set(.db_only) } if download_only { flags.set(.download_only) } if scenario.no_save { flags.set(.no_save) } if scenario.all_deps { flags.set(.all_deps) } if scenario.all_explicit { flags.set(.all_explicit) } handle.trans_init(flags)! defer { handle.trans_release() or { eprintln(err) } } if scenario.remove_victim { db := handle.get_localdb()! handle.remove_pkg(db.get_pkg('victim') or { return error('missing victim') })! } for path in fixture.archives { handle.add_pkg(handle.pkg_load(path, if scenario.partial { 0 } else { 1 }, 0)!)! } handle.trans_prepare()!.free() handle.trans_commit()!.free() trace.capture_actions() return trace.items.clone() } fn public_install_native(fixture installfixtures.Fixture, scenario installfixtures.Scenario, download_only bool) ![]string { handle := facade.initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.option_add_cachedir(fixture.cache)! logfile := os.join_path(os.dir(fixture.db), 'actions.log') handle.option_set_logfile(logfile)! mut trace := &PublicInstallTrace{ root: handle.option_get_root(), logfile: logfile, native: handle, interrupt: scenario.interrupt } handle.option_set_eventcb(public_install_native_event, trace)! handle.option_set_progresscb(public_install_native_progress, trace)! for pattern in scenario.no_upgrade { handle.option_add_noupgrade(pattern)! } for pattern in scenario.no_extract { handle.option_add_no_extract(pattern)! } handle.option_set_checkspace(if scenario.check_space { 1 } else { 0 })! mut flags := facade.TransFlag.no_deps | .no_conflicts | .no_hooks | .no_scriptlet if scenario.db_only { flags.set(.db_only) } if download_only { flags.set(.download_only) } if scenario.no_save { flags.set(.no_save) } if scenario.all_deps { flags.set(.all_deps) } if scenario.all_explicit { flags.set(.all_explicit) } handle.trans_init(flags)! defer { handle.trans_release() or { eprintln(err) } } if scenario.remove_victim { db := handle.get_localdb()! handle.remove_pkg(db.get_pkg('victim') or { return error('missing victim') })! } for path in fixture.archives { handle.add_pkg(handle.pkg_load(path, if scenario.partial { 0 } else { 1 }, 0)!)! } handle.trans_prepare()!.free() handle.trans_commit()!.free() trace.capture_actions() return trace.items.clone() } fn test_public_install_upgrade_backups_database_and_progress_match_libalpm() ! { dir := os.join_path(os.vtmp_dir(), 'alpm-public-install-execution-${os.getpid()}') defer { os.rmdir_all(dir) or { eprintln(err) } } previous := os.getenv_opt('SOURCE_DATE_EPOCH') assert os.setenv('SOURCE_DATE_EPOCH', '1700000000', true) == 0 defer { if value := previous { os.setenv('SOURCE_DATE_EPOCH', value, true) } else { os.unsetenv('SOURCE_DATE_EPOCH') } } mut cases := []installfixtures.Scenario{} for local in ['original', 'incoming', 'custom'] { for incoming in ['original', 'incoming', 'custom'] { for pacnew in [false, true] { cases << installfixtures.Scenario{ name: 'hash-${local}-${incoming}-${pacnew}', local_text: local, incoming: incoming, pacnew: pacnew } } } } cases << [ installfixtures.Scenario{ name: 'fresh', installed: false, missing_local: true }, installfixtures.Scenario{ name: 'fresh-unowned', installed: false }, installfixtures.Scenario{ name: 'reinstall', version: '1-1' }, installfixtures.Scenario{ name: 'downgrade', version: '0.5-1' }, installfixtures.Scenario{ name: 'missing-local', missing_local: true }, installfixtures.Scenario{ name: 'missing-hash', missing_hash: true }, installfixtures.Scenario{ name: 'add-backup', old_backup: false }, installfixtures.Scenario{ name: 'drop-backup', new_backup: false, local_text: 'custom' }, installfixtures.Scenario{ name: 'drop-backup-nosave', new_backup: false, local_text: 'custom', no_save: true }, installfixtures.Scenario{ name: 'local-symlink', local_symlink: true, local_text: 'custom' }, installfixtures.Scenario{ name: 'noupgrade', no_upgrade: ['etc/*'] }, installfixtures.Scenario{ name: 'noupgrade-negated', no_upgrade: ['etc/*', '!etc/app.conf'] }, installfixtures.Scenario{ name: 'noextract', no_extract: ['etc/*'] }, installfixtures.Scenario{ name: 'noextract-negated', no_extract: ['etc/*', '!etc/app.conf'] }, installfixtures.Scenario{ name: 'db-only', db_only: true }, installfixtures.Scenario{ name: 'all-deps', all_deps: true }, installfixtures.Scenario{ name: 'all-explicit', all_explicit: true }, installfixtures.Scenario{ name: 'both-reasons', all_deps: true, all_explicit: true }, installfixtures.Scenario{ name: 'check-space', check_space: true }, installfixtures.Scenario{ name: 'db-only-space', db_only: true, check_space: true }, installfixtures.Scenario{ name: 'two-targets', second: true }, installfixtures.Scenario{ name: 'interrupt-last', interrupt: true }, installfixtures.Scenario{ name: 'interrupt-first', second: true, interrupt: true }, installfixtures.Scenario{ name: 'remove-victim', remove_victim: true }, installfixtures.Scenario{ name: 'mtree', mtree: true }, installfixtures.Scenario{ name: 'directory-mode' }, ] for scenario in cases { base := os.join_path(dir, scenario.name) reference := installfixtures.create(os.join_path(base, 'reference'), scenario)! native := installfixtures.create(os.join_path(base, 'native'), scenario)! if scenario.name == 'directory-mode' { os.chmod(os.join_path(reference.root, 'usr/bin'), 0o700)! os.chmod(os.join_path(native.root, 'usr/bin'), 0o700)! } expected := public_install_reference(reference, scenario, false)! actual := public_install_native(native, scenario, false)! assert actual == expected, '${scenario.name}\nnative: ${actual}\nreference: ${expected}' assert installfixtures.snapshot(native.root)! == installfixtures.snapshot(reference.root)!, scenario.name assert installfixtures.snapshot(native.db)! == installfixtures.snapshot(reference.db)!, scenario.name } } fn (mut trace PublicInstallTrace) capture_actions() { if !os.exists(trace.logfile) { return } lines := os.read_lines(trace.logfile) or { assert false, err.msg() return } assert lines.len >= trace.action_count for line in lines[trace.action_count..] { trace.items << 'action:' + line.all_after('] ').replace(trace.root, 'ROOT/') } trace.action_count = lines.len } fn test_public_download_only_writes_no_transaction_action_records() ! { base := os.join_path(os.vtmp_dir(), 'alpm-public-download-only-actions-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } scenario := installfixtures.Scenario{ name: 'download-only' } reference := installfixtures.create(os.join_path(base, 'reference'), scenario)! native := installfixtures.create(os.join_path(base, 'native'), scenario)! before := installfixtures.snapshot(native.root)! expected := public_install_reference(reference, scenario, true)! actual := public_install_native(native, scenario, true)! assert actual == expected assert !os.exists(os.join_path(os.dir(native.db), 'actions.log')) assert !os.exists(os.join_path(os.dir(reference.db), 'actions.log')) assert installfixtures.snapshot(native.root)! == before assert installfixtures.snapshot(native.root)! == installfixtures.snapshot(reference.root)! assert installfixtures.snapshot(native.db)! == installfixtures.snapshot(reference.db)! }