| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import os |
| 5 | |
| 6 | fn test_package_find_preserves_each_owners_error_and_matches_c_strings() ! { |
| 7 | root := os.join_path(os.vtmp_dir(), 'alpm-package-find-${os.getpid()}') |
| 8 | os.mkdir_all(os.join_path(root, 'local'))! |
| 9 | defer { os.rmdir_all(root) or { eprintln(err) } } |
| 10 | os.write_file(os.join_path(root, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 11 | for name in ['alpha', 'beta'] { |
| 12 | path := os.join_path(root, 'local', '${name}-1-1', 'desc') |
| 13 | os.mkdir_all(os.dir(path))! |
| 14 | os.write_file(path, '%NAME%\n${name}\n\n%VERSION%\n1-1\n\n%PROVIDES%\n${name}-virtual=2\n\n')! |
| 15 | } |
| 16 | first := initialize('/', root)! |
| 17 | defer { first.release() or { eprintln(err) } } |
| 18 | second := initialize('/', root)! |
| 19 | defer { second.release() or { eprintln(err) } } |
| 20 | public_first := facade.initialize('/', root)! |
| 21 | defer { public_first.release() or { eprintln(err) } } |
| 22 | public_second := facade.initialize('/', root)! |
| 23 | defer { public_second.release() or { eprintln(err) } } |
| 24 | first_db := first.get_localdb()! |
| 25 | second_db := second.get_localdb()! |
| 26 | public_first_db := public_first.get_localdb()! |
| 27 | public_second_db := public_second.get_localdb()! |
| 28 | alpha := first_db.get_pkg('alpha') or { return error('missing C alpha') } |
| 29 | beta := second_db.get_pkg('beta') or { return error('missing C beta') } |
| 30 | public_alpha := public_first_db.get_pkg('alpha') or { return error('missing native alpha') } |
| 31 | public_beta := public_second_db.get_pkg('beta') or { return error('missing native beta') } |
| 32 | mut list := &List(unsafe { nil }) |
| 33 | mut public_list := &facade.List(unsafe { nil }) |
| 34 | defer { list.free() } |
| 35 | defer { public_list.free() } |
| 36 | for item in [voidptr(unsafe { nil }), voidptr(alpha), voidptr(unsafe { nil }), voidptr(beta), |
| 37 | voidptr(alpha)] { |
| 38 | assert (&list).append(item) != unsafe { nil } |
| 39 | } |
| 40 | for item in [voidptr(unsafe { nil }), voidptr(public_alpha), voidptr(unsafe { nil }), |
| 41 | voidptr(public_beta), voidptr(public_alpha)] { |
| 42 | assert (&public_list).append(item) != unsafe { nil } |
| 43 | } |
| 44 | mut mismatches := []string{} |
| 45 | for empty in [false, true] { |
| 46 | for prior in 0 .. 4 { |
| 47 | for needle in ['', 'alpha', 'beta', 'absent', 'Alpha', 'alpha\x00suffix', 'beta\x00suffix', |
| 48 | '\x00alpha', 'absent\x00alpha'] { |
| 49 | first.option_get_root() |
| 50 | second.option_get_root() |
| 51 | public_first.option_get_root() |
| 52 | public_second.option_get_root() |
| 53 | if prior & 1 != 0 { |
| 54 | if _ := first.register_syncdb('', 0) { |
| 55 | assert false |
| 56 | } |
| 57 | if _ := public_first.register_syncdb('', 0) { |
| 58 | assert false |
| 59 | } |
| 60 | } |
| 61 | if prior & 2 != 0 { |
| 62 | if _ := second.register_syncdb('', 0) { |
| 63 | assert false |
| 64 | } |
| 65 | if _ := public_second.register_syncdb('', 0) { |
| 66 | assert false |
| 67 | } |
| 68 | } |
| 69 | haystack := if empty { &List(unsafe { nil }) } else { list } |
| 70 | public_haystack := if empty { &facade.List(unsafe { nil }) } else { public_list } |
| 71 | mut expected := 0 |
| 72 | if result := haystack.pkg_find(needle) { |
| 73 | expected = if voidptr(result) == voidptr(alpha) { 1 } else { 2 } |
| 74 | assert voidptr(result) == voidptr(alpha) || voidptr(result) == voidptr(beta) |
| 75 | } |
| 76 | mut actual := 0 |
| 77 | if result := public_haystack.pkg_find(needle) { |
| 78 | actual = if voidptr(result) == voidptr(public_alpha) { 1 } else { 2 } |
| 79 | assert voidptr(result) == voidptr(public_alpha) || voidptr(result) == voidptr(public_beta) |
| 80 | } |
| 81 | // package.c compares stored names directly, without CHECK_HANDLE getters. |
| 82 | expected_errors := [int(first.errno()), int(second.errno())] |
| 83 | actual_errors := [int(public_first.errno()), int(public_second.errno())] |
| 84 | assert expected_errors == [if prior & 1 != 0 { 6 } else { 0 }, |
| 85 | if prior & 2 != 0 { 6 } else { 0 }] |
| 86 | if actual != expected || actual_errors != expected_errors { |
| 87 | mismatches << '${empty}/${prior}/${needle.bytes()}: ${actual}:${actual_errors} != ${expected}:${expected_errors}' |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | assert mismatches.len == 0, mismatches[..int_min(8, mismatches.len)].join('\n') |
| 93 | assert voidptr(public_first.find_local_pkg('alpha\x00suffix') or { return error('missing native helper result') }) == voidptr(public_alpha) |
| 94 | assert voidptr(first.find_local_pkg('alpha\x00suffix') or { return error('missing C helper result') }) == voidptr(alpha) |
| 95 | compare_package_satisfaction(first, second, public_first, public_second, alpha, beta, |
| 96 | public_alpha, public_beta)! |
| 97 | } |
| 98 | |
| 99 | fn compare_package_satisfaction(first &Handle, second &Handle, public_first &facade.Handle, |
| 100 | public_second &facade.Handle, alpha &Package, beta &Package, public_alpha &facade.Package, |
| 101 | public_beta &facade.Package) ! { |
| 102 | list := unsafe { (&List(nil)).add(alpha).add(beta) } |
| 103 | public_list := unsafe { (&facade.List(nil)).add(public_alpha).add(public_beta) } |
| 104 | defer { list.free() } |
| 105 | defer { public_list.free() } |
| 106 | for prior in 0 .. 4 { |
| 107 | for query in ['alpha', 'alpha>=1', 'alpha<1', 'beta', 'beta>=1', 'alpha-virtual', |
| 108 | 'alpha-virtual>=2', 'alpha-virtual>2', 'beta-virtual=2', 'missing', 'alpha\x00suffix'] { |
| 109 | dep := dep_from_string(query) |
| 110 | public_dep := facade.dep_from_string(query) |
| 111 | defer { dep.free() } |
| 112 | defer { public_dep.free() } |
| 113 | for mode in ['literal', 'provider', 'list'] { |
| 114 | first.option_get_root() |
| 115 | second.option_get_root() |
| 116 | public_first.option_get_root() |
| 117 | public_second.option_get_root() |
| 118 | if prior & 1 != 0 { |
| 119 | if _ := first.register_syncdb('', 0) { |
| 120 | assert false |
| 121 | } |
| 122 | if _ := public_first.register_syncdb('', 0) { |
| 123 | assert false |
| 124 | } |
| 125 | } |
| 126 | if prior & 2 != 0 { |
| 127 | if _ := second.register_syncdb('', 0) { |
| 128 | assert false |
| 129 | } |
| 130 | if _ := public_second.register_syncdb('', 0) { |
| 131 | assert false |
| 132 | } |
| 133 | } |
| 134 | if mode == 'list' { |
| 135 | mut expected := 0 |
| 136 | if found := list.find_satisfier(query) { |
| 137 | expected = if voidptr(found) == voidptr(alpha) { 1 } else { 2 } |
| 138 | assert voidptr(found) == voidptr(alpha) || voidptr(found) == voidptr(beta) |
| 139 | } |
| 140 | mut actual := 0 |
| 141 | if found := public_list.find_satisfier(query) { |
| 142 | actual = if voidptr(found) == voidptr(public_alpha) { 1 } else { 2 } |
| 143 | assert voidptr(found) == voidptr(public_alpha) || voidptr(found) == voidptr(public_beta) |
| 144 | } |
| 145 | assert actual == expected, '${prior}/${query}/${mode}' |
| 146 | } else { |
| 147 | expected := if mode == 'literal' { |
| 148 | alpha.satisfies(dep) |
| 149 | } else { |
| 150 | beta.satisfies(dep) |
| 151 | } |
| 152 | actual := if mode == 'literal' { |
| 153 | public_alpha.satisfies(public_dep) |
| 154 | } else { |
| 155 | public_beta.satisfies(public_dep) |
| 156 | } |
| 157 | assert actual == expected, '${prior}/${query}/${mode}' |
| 158 | } |
| 159 | assert [int(public_first.errno()), int(public_second.errno())] == [ |
| 160 | int(first.errno()), |
| 161 | int(second.errno()), |
| 162 | ], '${prior}/${query}/${mode}' |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | // A frontend can edit the provision records it already borrowed. |
| 167 | assert list.find_satisfier('alpha-virtual>=2') != none |
| 168 | assert public_list.find_satisfier('alpha-virtual>=2') != none |
| 169 | mut provision := alpha.get_provides().get_depend() or { return error('missing C provision') } |
| 170 | mut public_provision := public_alpha.get_provides().get_depend() or { return error('missing native provision') } |
| 171 | provision.mod = .any |
| 172 | public_provision.mod = .any |
| 173 | assert list.find_satisfier('alpha-virtual>=2') == none |
| 174 | assert public_list.find_satisfier('alpha-virtual>=2') == none |
| 175 | assert list.find_satisfier('alpha-virtual') != none |
| 176 | assert public_list.find_satisfier('alpha-virtual') != none |
| 177 | } |
| 178 | |