module coracle import nspkg.engine import nspkg.tests.archivefixtures import os fn install_parity_entry(name string, version string, fields string) archivefixtures.Entry { return archivefixtures.Entry{ name: '${name}-${version}/desc', text: '%FILENAME%\n${name}-${version}.pkg.tar.zst\n\n%CSIZE%\n100\n\n' + fields } } struct InstallParityAnswers { mut: allow bool trace []string } fn install_parity_c_event(context voidptr, event &Event) { mut answers := unsafe { &InstallParityAnswers(context) } match unsafe { event.type_ } { .resolvedeps_start { answers.trace << 'resolve:start' } .resolvedeps_done { answers.trace << 'resolve:done' } .interconflicts_start { answers.trace << 'conflicts:start' } .interconflicts_done { answers.trace << 'conflicts:done' } else { assert false, 'unexpected install event' } } } fn install_parity_native_event(context voidptr, event engine.PreparationEvent) { mut answers := unsafe { &InstallParityAnswers(context) } match event.kind { .resolve_dependencies_start { answers.trace << 'resolve:start' } .resolve_dependencies_done { answers.trace << 'resolve:done' } .conflicts_start { answers.trace << 'conflicts:start' } .conflicts_done { answers.trace << 'conflicts:done' } else { assert false, 'unexpected install event' } } } fn install_parity_c_question(context voidptr, question &Question) { mut answers := unsafe { &InstallParityAnswers(context) } mut answer := unsafe { &QuestionAny(question) } match unsafe { question.type_ } { .remove_pkgs { mut packages := unsafe { question.remove_pkgs.packages } answers.trace << 'skip:' + packages.pkgs().map(it.get_name()).join(',') answer.answer = i32(answers.allow) } .conflict_pkg { conflict := unsafe { question.conflict.conflict } answers.trace << 'conflict:${conflict.package1.get_name()}:${conflict.package2.get_name()}:${conflict.reason.compute_string()}' answer.answer = i32(answers.allow) } .replace_pkg { replacement := unsafe { question.replace } answers.trace << 'replace:${replacement.oldpkg.get_name()}:${replacement.newpkg.get_name()}:${replacement.newdb.get_name()}' answer.answer = i32(answers.allow) } .select_provider { mut providers := unsafe { question.select_provider.providers } answers.trace << 'providers:' + providers.pkgs().map(it.get_name()).join(',') answer.answer = i32(answers.allow) } .install_ignorepkg { answers.trace << 'ignore:' + unsafe { question.install_ignorepkg.pkg }.get_name() answer.answer = i32(answers.allow) } else { assert false, 'unexpected install question' } } } fn install_parity_skip(context voidptr, packages []&engine.Package) bool { mut answers := unsafe { &InstallParityAnswers(context) } answers.trace << 'skip:' + packages.map(it.name).join(',') return answers.allow } fn install_parity_conflict(context voidptr, conflict engine.PackageConflict) bool { mut answers := unsafe { &InstallParityAnswers(context) } answers.trace << 'conflict:${conflict.package1.name}:${conflict.package2.name}:${conflict.reason.compute_string()}' return answers.allow } fn install_parity_replace(context voidptr, old &engine.Package, new &engine.Package, db &engine.Database) bool { mut answers := unsafe { &InstallParityAnswers(context) } answers.trace << 'replace:${old.name}:${new.name}:${db.name}' return answers.allow } fn install_parity_provider(context voidptr, dependency engine.Dependency, packages []&engine.Package) int { mut answers := unsafe { &InstallParityAnswers(context) } assert dependency.name() == 'virtual' answers.trace << 'providers:' + packages.map(it.name).join(',') return int(answers.allow) } fn install_parity_ignore(context voidptr, pkg &engine.Package) bool { mut answers := unsafe { &InstallParityAnswers(context) } answers.trace << 'ignore:' + pkg.name return answers.allow } fn install_parity_problems(result engine.PreparationResult) []string { return match result.kind { .unsatisfied_deps { result.missing.map('${it.target}|${it.dependency.compute_string()}|${it.causing_package}') } .conflicting_deps { result.conflicts.map('${it.package1.name}|${it.reason.compute_string()}|${it.package2.name}') } .pkg_invalid_arch { result.invalid_architectures.map('${it}||') } else { []string{} } } } fn test_native_install_and_upgrade_prepare_match_libalpm() ! { dir := os.join_path(os.vtmp_dir(), 'alpm-install-prepare-parity-${os.getpid()}') os.mkdir_all(os.join_path(dir, 'sync'))! os.mkdir_all(os.join_path(dir, 'cache'))! 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')! for entry in [install_parity_entry('old', '1-1', '%REASON%\n1\n\n'), install_parity_entry('consumer', '1-1', '%DEPENDS%\nold>=1\n\n'), install_parity_entry('library', '3-1', '%REASON%\n1\n\n')] { path := os.join_path(dir, 'local', entry.name) os.mkdir_all(os.dir(path))! os.write_file(path, entry.text)! os.write_file(os.join_path(os.dir(path), 'files'), '')! } archivefixtures.write(os.join_path(dir, 'sync', 'core.db'), [ install_parity_entry('app', '1-1', '%DEPENDS%\nvirtual>=2\n\n'), install_parity_entry('provider-a', '1-1', '%PROVIDES%\nvirtual=2\n\n%DEPENDS%\nleaf\n\n'), install_parity_entry('provider-b', '1-1', '%PROVIDES%\nvirtual=2\n\n'), install_parity_entry('leaf', '1-1', ''), install_parity_entry('bad', '1-1', '%DEPENDS%\nabsent\n\n'), install_parity_entry('replacement', '1-1', '%PROVIDES%\nold=1\n\n%CONFLICTS%\nold\n\n%REPLACES%\nold\n\n'), install_parity_entry('incomplete', '1-1', '%CONFLICTS%\nold\n\n'), install_parity_entry('alias-a', '1-1', '%PROVIDES%\nalias-b\n\n%CONFLICTS%\nalias-b\n\n'), install_parity_entry('alias-b', '1-1', ''), install_parity_entry('alias-consumer', '1-1', '%DEPENDS%\nalias-b>=1\n\n'), install_parity_entry('clash-a', '1-1', '%CONFLICTS%\nclash-b\n\n'), install_parity_entry('clash-b', '1-1', ''), install_parity_entry('duplicate-a', '1-1', '%FILENAME%\nshared.pkg.tar.zst\n\n'), install_parity_entry('duplicate-b', '1-1', '%FILENAME%\nshared.pkg.tar.zst\n\n'), install_parity_entry('wrong', '1-1', '%ARCH%\nother\n\n'), install_parity_entry('empty-arch', '1-1', '%ARCH%\n\n'), install_parity_entry('old', '2-1', ''), install_parity_entry('library', '2-1', ''), archivefixtures.Entry{ name: 'missing-filename-1-1/desc', text: '%DESC%\nMissing filename\n\n' }, ], false)! archivefixtures.write(os.join_path(dir, 'sync', 'extra.db'), [ install_parity_entry('old', '3-1', ''), install_parity_entry('library', '4-1', ''), ], false)! os.write_file_array(os.join_path(dir, 'cache', 'app-1-1.pkg.tar.zst.part'), []u8{len: 25})! old := initialize('/', dir)! defer { old.release() or { eprintln(err) } } ref_local := old.get_localdb()! ref_core := old.register_syncdb('core', 0)! old.register_syncdb('extra', 0)! old.option_add_architecture('x86_64')! old.option_add_cachedir(os.join_path(dir, 'cache'))! mut native := engine.initialize('/', dir)! defer { native.release() or { eprintln(err) } } mut local := native.local_database()! mut core := native.register_syncdb('core', 0)! native.register_syncdb('extra', 0)! native.add_string_list(.architectures, 'x86_64')! native.add_string_list(.cache_dirs, os.join_path(dir, 'cache'))! mut ref_answers := &InstallParityAnswers{} mut answers := &InstallParityAnswers{} old.option_set_eventcb(install_parity_c_event, ref_answers)! old.option_set_questioncb(install_parity_c_question, ref_answers)! native.set_preparation_callback(install_parity_native_event, answers)! native.set_dependency_questions(engine.DependencyQuestions{ context: answers, install_ignored: install_parity_ignore, select_provider: install_parity_provider, skip_unresolvable: install_parity_skip, remove_conflict: install_parity_conflict, replace_package: install_parity_replace })! for combination in 0 .. 8 { mut flags := int(C.ALPM_TRANS_FLAG_NOLOCK) if combination & 1 != 0 { flags |= C.ALPM_TRANS_FLAG_NODEPS } if combination & 2 != 0 { flags |= C.ALPM_TRANS_FLAG_NODEPVERSION } if combination & 4 != 0 { flags |= C.ALPM_TRANS_FLAG_NOCONFLICTS } for allowed in [false, true] { for scenario in ['expand', 'skip', 'conflict', 'reverse', 'alias', 'alias-version', 'inner-conflict', 'duplicate', 'architecture', 'empty-architecture', 'missing-filename', 'remove', 'upgrade', 'downgrade'] { assert C.oracle_alpm_trans_init(old, flags) == 0 native.start_transaction(unsafe { engine.TransactionFlag(flags) })! ref_answers.allow = allowed ref_answers.trace = []string{} answers.allow = allowed answers.trace = []string{} if scenario == 'remove' { old.remove_pkg(ref_local.get_pkg('old') or { return error('C victim missing') })! native.remove_package(local.get_pkg('old') or { return error('native victim missing') })! } if scenario in ['upgrade', 'downgrade'] { old.sync_sysupgrade(scenario == 'downgrade')! native.plan_system_upgrade(scenario == 'downgrade')! } else { targets := match scenario { 'expand', 'remove' { ['app'] } 'skip' { ['bad', 'app'] } 'conflict' { ['replacement'] } 'reverse' { ['incomplete'] } 'alias' { ['alias-a', 'alias-b'] } 'alias-version' { ['alias-a', 'alias-b', 'alias-consumer'] } 'inner-conflict' { ['clash-a', 'clash-b'] } 'duplicate' { ['duplicate-a', 'duplicate-b'] } 'architecture' { ['wrong'] } 'empty-architecture' { ['empty-arch'] } else { ['missing-filename'] } } for name in targets { old.add_pkg(ref_core.get_pkg(name) or { return error('C target missing') })! native.add_package(core.get_pkg(name) or { return error('native target missing') })! } } problems, failure := old.trans_prepare_problems() want_kind := if problem := failure { unsafe { Errno(problem.code()) } } else { Errno.ok } actual := native.prepare_transaction() or { engine.PreparationResult{ kind: native.errno() } } context := '${combination}, ${allowed}, ${scenario}' assert actual.success == (failure == none), context if !actual.success { assert int(actual.kind) == int(want_kind), context assert install_parity_problems(actual) == problems.map('${it.target}|${it.depend}|${it.causing}'), context } mut added := native.transaction_add()! ref_added := old.trans_get_add().pkgs() assert added.map('${it.name}|${it.version}|${int(it.reason)}') == ref_added.map('${it.get_name()}|${it.get_version()}|${int(it.get_reason())}'), context assert native.transaction_remove()!.map(it.name) == old.trans_get_remove().pkgs().map(it.get_name()), context assert answers.trace == ref_answers.trace, context if actual.success { for index, mut pkg in added { assert native.package_download_size(mut pkg)! == ref_added[index].download_size(), context } } native.release_transaction()! old.trans_release()! } } } }