| 1 | module coracle |
| 2 | |
| 3 | import nspkg.engine |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import os |
| 6 | |
| 7 | fn install_parity_entry(name string, version string, fields string) archivefixtures.Entry { |
| 8 | return archivefixtures.Entry{ name: '${name}-${version}/desc', text: '%FILENAME%\n${name}-${version}.pkg.tar.zst\n\n%CSIZE%\n100\n\n' + fields } |
| 9 | } |
| 10 | |
| 11 | struct InstallParityAnswers { |
| 12 | mut: |
| 13 | allow bool |
| 14 | trace []string |
| 15 | } |
| 16 | |
| 17 | fn install_parity_c_event(context voidptr, event &Event) { |
| 18 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 19 | match unsafe { event.type_ } { |
| 20 | .resolvedeps_start { answers.trace << 'resolve:start' } |
| 21 | .resolvedeps_done { answers.trace << 'resolve:done' } |
| 22 | .interconflicts_start { answers.trace << 'conflicts:start' } |
| 23 | .interconflicts_done { answers.trace << 'conflicts:done' } |
| 24 | else { |
| 25 | assert false, 'unexpected install event' |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn install_parity_native_event(context voidptr, event engine.PreparationEvent) { |
| 31 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 32 | match event.kind { |
| 33 | .resolve_dependencies_start { answers.trace << 'resolve:start' } |
| 34 | .resolve_dependencies_done { answers.trace << 'resolve:done' } |
| 35 | .conflicts_start { answers.trace << 'conflicts:start' } |
| 36 | .conflicts_done { answers.trace << 'conflicts:done' } |
| 37 | else { |
| 38 | assert false, 'unexpected install event' |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn install_parity_c_question(context voidptr, question &Question) { |
| 44 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 45 | mut answer := unsafe { &QuestionAny(question) } |
| 46 | match unsafe { question.type_ } { |
| 47 | .remove_pkgs { |
| 48 | mut packages := unsafe { question.remove_pkgs.packages } |
| 49 | answers.trace << 'skip:' + packages.pkgs().map(it.get_name()).join(',') |
| 50 | answer.answer = i32(answers.allow) |
| 51 | } |
| 52 | .conflict_pkg { |
| 53 | conflict := unsafe { question.conflict.conflict } |
| 54 | answers.trace << 'conflict:${conflict.package1.get_name()}:${conflict.package2.get_name()}:${conflict.reason.compute_string()}' |
| 55 | answer.answer = i32(answers.allow) |
| 56 | } |
| 57 | .replace_pkg { |
| 58 | replacement := unsafe { question.replace } |
| 59 | answers.trace << 'replace:${replacement.oldpkg.get_name()}:${replacement.newpkg.get_name()}:${replacement.newdb.get_name()}' |
| 60 | answer.answer = i32(answers.allow) |
| 61 | } |
| 62 | .select_provider { |
| 63 | mut providers := unsafe { question.select_provider.providers } |
| 64 | answers.trace << 'providers:' + providers.pkgs().map(it.get_name()).join(',') |
| 65 | answer.answer = i32(answers.allow) |
| 66 | } |
| 67 | .install_ignorepkg { |
| 68 | answers.trace << 'ignore:' + unsafe { question.install_ignorepkg.pkg }.get_name() |
| 69 | answer.answer = i32(answers.allow) |
| 70 | } |
| 71 | else { |
| 72 | assert false, 'unexpected install question' |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn install_parity_skip(context voidptr, packages []&engine.Package) bool { |
| 78 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 79 | answers.trace << 'skip:' + packages.map(it.name).join(',') |
| 80 | return answers.allow |
| 81 | } |
| 82 | |
| 83 | fn install_parity_conflict(context voidptr, conflict engine.PackageConflict) bool { |
| 84 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 85 | answers.trace << 'conflict:${conflict.package1.name}:${conflict.package2.name}:${conflict.reason.compute_string()}' |
| 86 | return answers.allow |
| 87 | } |
| 88 | |
| 89 | fn install_parity_replace(context voidptr, old &engine.Package, new &engine.Package, db &engine.Database) bool { |
| 90 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 91 | answers.trace << 'replace:${old.name}:${new.name}:${db.name}' |
| 92 | return answers.allow |
| 93 | } |
| 94 | |
| 95 | fn install_parity_provider(context voidptr, dependency engine.Dependency, packages []&engine.Package) int { |
| 96 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 97 | assert dependency.name() == 'virtual' |
| 98 | answers.trace << 'providers:' + packages.map(it.name).join(',') |
| 99 | return int(answers.allow) |
| 100 | } |
| 101 | |
| 102 | fn install_parity_ignore(context voidptr, pkg &engine.Package) bool { |
| 103 | mut answers := unsafe { &InstallParityAnswers(context) } |
| 104 | answers.trace << 'ignore:' + pkg.name |
| 105 | return answers.allow |
| 106 | } |
| 107 | |
| 108 | fn install_parity_problems(result engine.PreparationResult) []string { |
| 109 | return match result.kind { |
| 110 | .unsatisfied_deps { |
| 111 | result.missing.map('${it.target}|${it.dependency.compute_string()}|${it.causing_package}') |
| 112 | } |
| 113 | .conflicting_deps { |
| 114 | result.conflicts.map('${it.package1.name}|${it.reason.compute_string()}|${it.package2.name}') |
| 115 | } |
| 116 | .pkg_invalid_arch { result.invalid_architectures.map('${it}||') } |
| 117 | else { []string{} } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn test_native_install_and_upgrade_prepare_match_libalpm() ! { |
| 122 | dir := os.join_path(os.vtmp_dir(), 'alpm-install-prepare-parity-${os.getpid()}') |
| 123 | os.mkdir_all(os.join_path(dir, 'sync'))! |
| 124 | os.mkdir_all(os.join_path(dir, 'cache'))! |
| 125 | os.mkdir_all(os.join_path(dir, 'local'))! |
| 126 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 127 | os.write_file(os.join_path(dir, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 128 | for entry in [install_parity_entry('old', '1-1', '%REASON%\n1\n\n'), |
| 129 | install_parity_entry('consumer', '1-1', '%DEPENDS%\nold>=1\n\n'), |
| 130 | install_parity_entry('library', '3-1', '%REASON%\n1\n\n')] { |
| 131 | path := os.join_path(dir, 'local', entry.name) |
| 132 | os.mkdir_all(os.dir(path))! |
| 133 | os.write_file(path, entry.text)! |
| 134 | os.write_file(os.join_path(os.dir(path), 'files'), '')! |
| 135 | } |
| 136 | archivefixtures.write(os.join_path(dir, 'sync', 'core.db'), [ |
| 137 | install_parity_entry('app', '1-1', '%DEPENDS%\nvirtual>=2\n\n'), |
| 138 | install_parity_entry('provider-a', '1-1', '%PROVIDES%\nvirtual=2\n\n%DEPENDS%\nleaf\n\n'), |
| 139 | install_parity_entry('provider-b', '1-1', '%PROVIDES%\nvirtual=2\n\n'), |
| 140 | install_parity_entry('leaf', '1-1', ''), |
| 141 | install_parity_entry('bad', '1-1', '%DEPENDS%\nabsent\n\n'), |
| 142 | install_parity_entry('replacement', '1-1', '%PROVIDES%\nold=1\n\n%CONFLICTS%\nold\n\n%REPLACES%\nold\n\n'), |
| 143 | install_parity_entry('incomplete', '1-1', '%CONFLICTS%\nold\n\n'), |
| 144 | install_parity_entry('alias-a', '1-1', '%PROVIDES%\nalias-b\n\n%CONFLICTS%\nalias-b\n\n'), |
| 145 | install_parity_entry('alias-b', '1-1', ''), |
| 146 | install_parity_entry('alias-consumer', '1-1', '%DEPENDS%\nalias-b>=1\n\n'), |
| 147 | install_parity_entry('clash-a', '1-1', '%CONFLICTS%\nclash-b\n\n'), |
| 148 | install_parity_entry('clash-b', '1-1', ''), |
| 149 | install_parity_entry('duplicate-a', '1-1', '%FILENAME%\nshared.pkg.tar.zst\n\n'), |
| 150 | install_parity_entry('duplicate-b', '1-1', '%FILENAME%\nshared.pkg.tar.zst\n\n'), |
| 151 | install_parity_entry('wrong', '1-1', '%ARCH%\nother\n\n'), |
| 152 | install_parity_entry('empty-arch', '1-1', '%ARCH%\n\n'), |
| 153 | install_parity_entry('old', '2-1', ''), |
| 154 | install_parity_entry('library', '2-1', ''), |
| 155 | archivefixtures.Entry{ name: 'missing-filename-1-1/desc', text: '%DESC%\nMissing filename\n\n' }, |
| 156 | ], false)! |
| 157 | archivefixtures.write(os.join_path(dir, 'sync', 'extra.db'), [ |
| 158 | install_parity_entry('old', '3-1', ''), |
| 159 | install_parity_entry('library', '4-1', ''), |
| 160 | ], false)! |
| 161 | os.write_file_array(os.join_path(dir, 'cache', 'app-1-1.pkg.tar.zst.part'), []u8{len: 25})! |
| 162 | old := initialize('/', dir)! |
| 163 | defer { old.release() or { eprintln(err) } } |
| 164 | ref_local := old.get_localdb()! |
| 165 | ref_core := old.register_syncdb('core', 0)! |
| 166 | old.register_syncdb('extra', 0)! |
| 167 | old.option_add_architecture('x86_64')! |
| 168 | old.option_add_cachedir(os.join_path(dir, 'cache'))! |
| 169 | mut native := engine.initialize('/', dir)! |
| 170 | defer { native.release() or { eprintln(err) } } |
| 171 | mut local := native.local_database()! |
| 172 | mut core := native.register_syncdb('core', 0)! |
| 173 | native.register_syncdb('extra', 0)! |
| 174 | native.add_string_list(.architectures, 'x86_64')! |
| 175 | native.add_string_list(.cache_dirs, os.join_path(dir, 'cache'))! |
| 176 | mut ref_answers := &InstallParityAnswers{} |
| 177 | mut answers := &InstallParityAnswers{} |
| 178 | old.option_set_eventcb(install_parity_c_event, ref_answers)! |
| 179 | old.option_set_questioncb(install_parity_c_question, ref_answers)! |
| 180 | native.set_preparation_callback(install_parity_native_event, answers)! |
| 181 | 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 })! |
| 182 | for combination in 0 .. 8 { |
| 183 | mut flags := int(C.ALPM_TRANS_FLAG_NOLOCK) |
| 184 | if combination & 1 != 0 { flags |= C.ALPM_TRANS_FLAG_NODEPS } |
| 185 | if combination & 2 != 0 { flags |= C.ALPM_TRANS_FLAG_NODEPVERSION } |
| 186 | if combination & 4 != 0 { flags |= C.ALPM_TRANS_FLAG_NOCONFLICTS } |
| 187 | for allowed in [false, true] { |
| 188 | for scenario in ['expand', 'skip', 'conflict', 'reverse', 'alias', 'alias-version', |
| 189 | 'inner-conflict', 'duplicate', 'architecture', 'empty-architecture', 'missing-filename', |
| 190 | 'remove', 'upgrade', 'downgrade'] { |
| 191 | assert C.oracle_alpm_trans_init(old, flags) == 0 |
| 192 | native.start_transaction(unsafe { engine.TransactionFlag(flags) })! |
| 193 | ref_answers.allow = allowed |
| 194 | ref_answers.trace = []string{} |
| 195 | answers.allow = allowed |
| 196 | answers.trace = []string{} |
| 197 | if scenario == 'remove' { |
| 198 | old.remove_pkg(ref_local.get_pkg('old') or { return error('C victim missing') })! |
| 199 | native.remove_package(local.get_pkg('old') or { return error('native victim missing') })! |
| 200 | } |
| 201 | if scenario in ['upgrade', 'downgrade'] { |
| 202 | old.sync_sysupgrade(scenario == 'downgrade')! |
| 203 | native.plan_system_upgrade(scenario == 'downgrade')! |
| 204 | } else { |
| 205 | targets := match scenario { |
| 206 | 'expand', 'remove' { ['app'] } |
| 207 | 'skip' { ['bad', 'app'] } |
| 208 | 'conflict' { ['replacement'] } |
| 209 | 'reverse' { ['incomplete'] } |
| 210 | 'alias' { ['alias-a', 'alias-b'] } |
| 211 | 'alias-version' { ['alias-a', 'alias-b', 'alias-consumer'] } |
| 212 | 'inner-conflict' { ['clash-a', 'clash-b'] } |
| 213 | 'duplicate' { ['duplicate-a', 'duplicate-b'] } |
| 214 | 'architecture' { ['wrong'] } |
| 215 | 'empty-architecture' { ['empty-arch'] } |
| 216 | else { ['missing-filename'] } |
| 217 | } |
| 218 | for name in targets { |
| 219 | old.add_pkg(ref_core.get_pkg(name) or { return error('C target missing') })! |
| 220 | native.add_package(core.get_pkg(name) or { return error('native target missing') })! |
| 221 | } |
| 222 | } |
| 223 | problems, failure := old.trans_prepare_problems() |
| 224 | want_kind := if problem := failure { |
| 225 | unsafe { Errno(problem.code()) } |
| 226 | } else { |
| 227 | Errno.ok |
| 228 | } |
| 229 | actual := native.prepare_transaction() or { engine.PreparationResult{ kind: native.errno() } } |
| 230 | context := '${combination}, ${allowed}, ${scenario}' |
| 231 | assert actual.success == (failure == none), context |
| 232 | if !actual.success { |
| 233 | assert int(actual.kind) == int(want_kind), context |
| 234 | assert install_parity_problems(actual) == problems.map('${it.target}|${it.depend}|${it.causing}'), context |
| 235 | } |
| 236 | mut added := native.transaction_add()! |
| 237 | ref_added := old.trans_get_add().pkgs() |
| 238 | assert added.map('${it.name}|${it.version}|${int(it.reason)}') == ref_added.map('${it.get_name()}|${it.get_version()}|${int(it.get_reason())}'), context |
| 239 | assert native.transaction_remove()!.map(it.name) == old.trans_get_remove().pkgs().map(it.get_name()), context |
| 240 | assert answers.trace == ref_answers.trace, context |
| 241 | if actual.success { |
| 242 | for index, mut pkg in added { |
| 243 | assert native.package_download_size(mut pkg)! == ref_added[index].download_size(), context |
| 244 | } |
| 245 | } |
| 246 | native.release_transaction()! |
| 247 | old.trans_release()! |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |