| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import os |
| 6 | |
| 7 | fn convenience_entry(name string, version string, fields string) archivefixtures.Entry { |
| 8 | return archivefixtures.Entry{ |
| 9 | name: '${name}-${version}/desc' |
| 10 | text: '%NAME%\n${name}\n\n%VERSION%\n${version}\n\n' + fields |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | fn convenience_fixture(root string, variant string) ! { |
| 15 | os.mkdir_all(os.join_path(root, 'local'))! |
| 16 | os.mkdir_all(os.join_path(root, 'sync'))! |
| 17 | os.write_file(os.join_path(root, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 18 | dependency := '%REASON%\n1\n\n' |
| 19 | for entry in [ |
| 20 | convenience_entry('app', '1-1', '%DEPENDS%\nvirtual>=1\n\n%OPTDEPENDS%\noptional: integration\n\n'), |
| 21 | convenience_entry('engine', '1-1', dependency + '%PROVIDES%\nvirtual=1\n\n%DEPENDS%\ndeep>=1\n\n'), |
| 22 | convenience_entry('deep', '1-1', dependency + '%DEPENDS%\nengine>=1\n\n'), |
| 23 | convenience_entry('leaf', '1-1', dependency + '%DEPENDS%\nhelper\n\n'), |
| 24 | convenience_entry('helper', '1-1', dependency), |
| 25 | convenience_entry('optconsumer', '1-1', '%OPTDEPENDS%\nhelper: integration\n\n'), |
| 26 | convenience_entry('orphan', '1-1', dependency), |
| 27 | convenience_entry('optional', '1-1', dependency), |
| 28 | convenience_entry('cyclea', '1-1', dependency + '%DEPENDS%\ncycleb\n\n'), |
| 29 | convenience_entry('cycleb', '1-1', dependency + '%DEPENDS%\ncyclea\n\n'), |
| 30 | convenience_entry('legacy', '1-1', dependency), |
| 31 | convenience_entry('client', '1-1', '%DEPENDS%\nlegacy=1-1\n\n'), |
| 32 | convenience_entry('addon', '1-1', '%DEPENDS%\nclient=1-1\n\n'), |
| 33 | ] { |
| 34 | path := os.join_path(root, 'local', entry.name) |
| 35 | os.mkdir_all(os.dir(path))! |
| 36 | os.write_file(path, entry.text)! |
| 37 | } |
| 38 | mut entries := [ |
| 39 | convenience_entry('app', '2-1', '%DEPENDS%\nvirtual>=2\nmissing\n\n'), |
| 40 | convenience_entry('engine', '2-1', '%PROVIDES%\nvirtual=2\n\n%DEPENDS%\ndeep>=2\n\n'), |
| 41 | convenience_entry('deep', '2-1', '%DEPENDS%\nengine>=2\n\n'), |
| 42 | convenience_entry('legacy', '2-1', ''), |
| 43 | convenience_entry('addon', '2-1', '%DEPENDS%\nclient>=2\n\n'), |
| 44 | ] |
| 45 | if variant != 'missing' { |
| 46 | constraint := if variant == 'stale' { 'legacy=1-1' } else { 'legacy>=2' } |
| 47 | entries << convenience_entry('client', '2-1', '%DEPENDS%\n${constraint}\n\n') |
| 48 | } |
| 49 | archivefixtures.write(os.join_path(root, 'sync', 'core.db'), entries, false)! |
| 50 | } |
| 51 | |
| 52 | fn compare_convenience_packages(actual []&facade.Package, expected []&Package) { |
| 53 | assert actual.map(it.get_name() + ':' + it.get_version()) == expected.map(it.get_name() + ':' + it.get_version()) |
| 54 | } |
| 55 | |
| 56 | fn test_convenience_dependency_walks_and_orphans_match_original_facade() ! { |
| 57 | root := os.join_path(os.vtmp_dir(), 'alpm-convenience-${os.getpid()}') |
| 58 | convenience_fixture(root, 'good')! |
| 59 | defer { os.rmdir_all(root) or { eprintln(err) } } |
| 60 | old := initialize('/', root)! |
| 61 | defer { old.release() or { eprintln(err) } } |
| 62 | public := facade.initialize('/', root)! |
| 63 | defer { public.release() or { eprintln(err) } } |
| 64 | old.register_syncdb('core', 0)! |
| 65 | public.register_syncdb('core', 0)! |
| 66 | old_local := old.get_localdb()! |
| 67 | public_local := public.get_localdb()! |
| 68 | mut installed := old_local.get_pkgcache() or { return error('missing C cache') } |
| 69 | mut public_installed := public_local.get_pkgcache() or { return error('missing native cache') } |
| 70 | mut dbs := old.get_syncdbs()! |
| 71 | mut public_dbs := public.get_syncdbs()! |
| 72 | for target in ['app', 'local/app', 'core/app', 'unknown/app', 'local/', 'core/', 'core/local/app', |
| 73 | 'core/app\x00suffix', '/app'] { |
| 74 | assert public.strip_db_prefix(target) == old.strip_db_prefix(target) |
| 75 | } |
| 76 | for dep in ['virtual>=1', 'virtual>=2', 'virtual>2', 'engine', 'deep>=2', 'missing', |
| 77 | 'virtual>=1\x00suffix'] { |
| 78 | mut expected := []&Package{} |
| 79 | if pkg := old.resolve_dep(installed, dbs, dep) { expected << pkg } |
| 80 | mut actual := []&facade.Package{} |
| 81 | if pkg := public.resolve_dep(public_installed, public_dbs, dep) { actual << pkg } |
| 82 | assert int(public.errno()) == int(old.errno()), dep |
| 83 | compare_convenience_packages(actual, expected) |
| 84 | if dep == 'virtual>=1' { |
| 85 | assert actual[0].get_version() == '1-1' |
| 86 | } |
| 87 | if dep == 'virtual>=2' { |
| 88 | assert actual[0].get_version() == '2-1' |
| 89 | } |
| 90 | } |
| 91 | mut reference_orphans := []string{} |
| 92 | mut native_orphans := []string{} |
| 93 | for pkg in installed.pkgs() { |
| 94 | if pkg.is_unrequired() { reference_orphans << pkg.get_name() } |
| 95 | } |
| 96 | for pkg in public_installed.pkgs() { |
| 97 | if pkg.is_unrequired() { native_orphans << pkg.get_name() } |
| 98 | } |
| 99 | assert native_orphans == reference_orphans |
| 100 | assert native_orphans.sorted() == ['leaf', 'orphan'] |
| 101 | for names in [[]string{}, ['leaf'], ['app', 'optconsumer', 'leaf'], ['cyclea'], ['leaf', 'leaf']] { |
| 102 | mut seed := []&Package{} |
| 103 | mut public_seed := []&facade.Package{} |
| 104 | for name in names { |
| 105 | pkg := old.find_local_pkg(name) or { return error('missing C ${name}') } |
| 106 | public_pkg := public.find_local_pkg(name) or { return error('missing native ${name}') } |
| 107 | seed << pkg |
| 108 | public_seed << public_pkg |
| 109 | } |
| 110 | expected := old.unneeded_closure(seed) |
| 111 | actual := public.unneeded_closure(public_seed) |
| 112 | assert int(public.errno()) == int(old.errno()) |
| 113 | compare_convenience_packages(actual, expected) |
| 114 | assert actual[..names.len].map(it.get_name()) == names |
| 115 | if names == ['app', 'optconsumer', 'leaf'] { |
| 116 | assert actual.map(it.get_name()).sorted() == ['app', 'helper', 'leaf', 'optconsumer', |
| 117 | 'optional', 'orphan'] |
| 118 | } |
| 119 | if names == ['cyclea'] { |
| 120 | assert actual.any(it.get_name() == 'cycleb') |
| 121 | } |
| 122 | } |
| 123 | for name in ['app', 'engine', 'deep'] { |
| 124 | pkg := dbs.dbs()[0].get_pkg(name) or { return error('missing C target') } |
| 125 | public_pkg := public_dbs.dbs()[0].get_pkg(name) or { return error('missing native target') } |
| 126 | expected := old.outdated_deps(dbs, pkg) |
| 127 | actual := public.outdated_deps(public_dbs, public_pkg) |
| 128 | assert int(public.errno()) == int(old.errno()) |
| 129 | compare_convenience_packages(actual, expected) |
| 130 | if name == 'app' { |
| 131 | assert actual.map(it.get_name()) == ['engine', 'deep'] |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn test_broken_dependents_closure_and_refusals_match_original_facade() ! { |
| 137 | base := os.join_path(os.vtmp_dir(), 'alpm-broken-dependents-${os.getpid()}') |
| 138 | os.mkdir_all(base)! |
| 139 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 140 | for variant in ['good', 'missing', 'stale'] { |
| 141 | root := os.join_path(base, variant) |
| 142 | convenience_fixture(root, variant)! |
| 143 | old := initialize('/', root)! |
| 144 | defer { old.release() or { eprintln(err) } } |
| 145 | public := facade.initialize('/', root)! |
| 146 | defer { public.release() or { eprintln(err) } } |
| 147 | repo := old.register_syncdb('core', 0)! |
| 148 | public_repo := public.register_syncdb('core', 0)! |
| 149 | dbs := old.get_syncdbs()! |
| 150 | public_dbs := public.get_syncdbs()! |
| 151 | target := repo.get_pkg('legacy') or { return error('missing C upgrade') } |
| 152 | public_target := public_repo.get_pkg('legacy') or { return error('missing native upgrade') } |
| 153 | mut expected_error := '' |
| 154 | expected := old.broken_dependents([target], dbs) or { |
| 155 | expected_error = err.msg() |
| 156 | []&Package{} |
| 157 | } |
| 158 | mut actual_error := '' |
| 159 | actual := public.broken_dependents([public_target], public_dbs) or { |
| 160 | actual_error = err.msg() |
| 161 | []&facade.Package{} |
| 162 | } |
| 163 | assert actual_error == expected_error, variant |
| 164 | assert int(public.errno()) == int(old.errno()), variant |
| 165 | compare_convenience_packages(actual, expected) |
| 166 | if variant == 'good' { |
| 167 | assert actual.map(it.get_name()).sorted() == ['addon', 'client'] |
| 168 | } else { |
| 169 | assert actual_error.contains('client') |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |