| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import testdb |
| 5 | |
| 6 | fn test_dep_name() { |
| 7 | cases := { |
| 8 | 'glibc': 'glibc' |
| 9 | 'glibc>=2.27': 'glibc' |
| 10 | 'foo<1.0': 'foo' |
| 11 | 'foo<=1.0': 'foo' |
| 12 | 'foo=1.0-2': 'foo' |
| 13 | 'foo>1:2.0': 'foo' |
| 14 | 'gcc-libs': 'gcc-libs' |
| 15 | 'python>=3.12': 'python' |
| 16 | 'libfoo.so=1-64': 'libfoo.so' |
| 17 | // stray spaces, as a published dependency list occasionally spells |
| 18 | ' glibc >=2.27 ': 'glibc' |
| 19 | ' glibc ': 'glibc' |
| 20 | 'git >2.0': 'git' |
| 21 | } |
| 22 | for dep, want in cases { |
| 23 | assert dep_name(dep) == want, dep |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn test_review() ! { |
| 28 | old := color_flag |
| 29 | defer { color_flag = old } |
| 30 | color_flag = 'never' |
| 31 | dir := os.join_path(os.vtmp_dir(), 'vpac_test_review_${os.getpid()}') |
| 32 | os.mkdir_all(os.join_path(dir, '.git'))! |
| 33 | defer { os.rmdir_all(dir) or {} } |
| 34 | os.write_file(os.join_path(dir, 'PKGBUILD'), 'pkgname=foo\n')! |
| 35 | os.write_file(os.join_path(dir, 'foo.install'), 'post_install() { :; }')! |
| 36 | os.write_file(os.join_path(dir, 'a.patch'), '')! |
| 37 | |
| 38 | // the PKGBUILD first, then the .install scripts, then every name but .git |
| 39 | mut r := read_review('foo', dir)! |
| 40 | assert r.files.map(it.name) == ['PKGBUILD', 'foo.install'] |
| 41 | assert r.listing == ['PKGBUILD', 'a.patch', 'foo.install'] |
| 42 | r.warnings = pkgbuild_warnings(PackagePkgbuild{ name: 'foo', maintainer: 'me', num_votes: 1 }) |
| 43 | assert review_text(r, 'review foo') == [ |
| 44 | 'warning: maintainer: me, votes: 1', |
| 45 | 'review foo', |
| 46 | 'PKGBUILD', |
| 47 | 'pkgname=foo\n', |
| 48 | 'foo.install', |
| 49 | 'post_install() { :; }', |
| 50 | 'files in the package', |
| 51 | 'PKGBUILD a.patch foo.install', |
| 52 | ].join('\n') |
| 53 | |
| 54 | // a generated package shows its source, then what was generated from it |
| 55 | g := generated_review('foo', 'PKGBUILD.lip', '(lip)', 'pkgname=foo', 'foo.install', 'x') |
| 56 | assert review_text(g, '') == 'PKGBUILD.lip\n(lip)\nPKGBUILD the build runs\npkgname=foo\nfoo.install\nx' |
| 57 | assert generated_review('foo', 'PKGBUILD.vsh', 'v', 'p', 'foo.install', '').files.len == 2 |
| 58 | } |
| 59 | |
| 60 | fn test_makepkg_line() ! { |
| 61 | // the dir is one word to the shell whatever it is called; the build |
| 62 | // command is the config's text, run in it |
| 63 | dir := os.join_path(os.vtmp_dir(), "vpac test's \$dir `x` ${os.getpid()}") |
| 64 | os.mkdir_all(dir)! |
| 65 | defer { os.rmdir_all(dir) or {} } |
| 66 | saved := pkgbuild_makepkg |
| 67 | defer { pkgbuild_makepkg = saved } |
| 68 | pkgbuild_makepkg = 'pwd -P' |
| 69 | res := os.exec(['sh', '-c', makepkg_line(dir)]) |
| 70 | assert res.exit_code == 0, res.output |
| 71 | assert res.output.trim_right('\n') == os.real_path(dir) |
| 72 | // the default is makepkg's own, forcing a rebuild of what is there |
| 73 | reset_config() |
| 74 | assert makepkg_line('/x').ends_with(' && makepkg -f') |
| 75 | } |
| 76 | |
| 77 | fn test_build_dir_takes_a_plain_base_only() ! { |
| 78 | // the dir is removed before the clone, so a base that climbs out of the |
| 79 | // build root is refused before any path is made of it |
| 80 | saved := pkgbuild_dir |
| 81 | defer { pkgbuild_dir = saved } |
| 82 | pkgbuild_dir = '/var/tmp/vpac-builds' |
| 83 | assert pkgbuild_build_dir('u', 'foo-git')! == '/var/tmp/vpac-builds/foo-git' |
| 84 | for bad in ['', '.', '..', '../x', 'a/b', '/etc', '-rf'] { |
| 85 | if d := pkgbuild_build_dir('u', bad) { |
| 86 | assert false, 'accepted "${bad}" as ${d}' |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn test_packagelist_files() ! { |
| 92 | dir := os.join_path(os.vtmp_dir(), 'vpac_test_pkglist_${os.getpid()}') |
| 93 | os.mkdir_all(dir)! |
| 94 | defer { os.rmdir_all(dir) or {} } |
| 95 | built := os.join_path(dir, 'foo-1.0-1-any.pkg.tar') |
| 96 | os.write_file(built, '')! |
| 97 | // makepkg names every package it can make, built or not: a debug package |
| 98 | // that was not made, and blank or odd lines, are not archives |
| 99 | out := '${built}\n${dir}/foo-debug-1.0-1-any.pkg.tar\n\n ${built} \nwarning: x\n' |
| 100 | assert packagelist_files(out) == [built, built] |
| 101 | } |
| 102 | |
| 103 | fn test_archives_in() ! { |
| 104 | dir := os.join_path(os.vtmp_dir(), 'vpac_test_archives_in_${os.getpid()}') |
| 105 | os.mkdir_all(dir)! |
| 106 | defer { os.rmdir_all(dir) or {} } |
| 107 | for f in ['foo-1.0-1-any.pkg.tar', 'bar-2-1-x86_64.pkg.tar.zst', 'bar-2-1-x86_64.pkg.tar.zst.sig', |
| 108 | 'PKGBUILD', 'notes.tar'] { |
| 109 | os.write_file(os.join_path(dir, f), '')! |
| 110 | } |
| 111 | mut got := archives_in(dir)!.map(os.base(it)) |
| 112 | got.sort() |
| 113 | // an uncompressed archive counts, a signature and other files do not |
| 114 | assert got == ['bar-2-1-x86_64.pkg.tar.zst', 'foo-1.0-1-any.pkg.tar'] |
| 115 | } |
| 116 | |
| 117 | fn test_makepkg_packagelist_names_what_is_built() ! { |
| 118 | if !os.exists_in_system_path('makepkg') { |
| 119 | // no makepkg on this system: the assumption cannot be checked here |
| 120 | return |
| 121 | } |
| 122 | if os.geteuid() == 0 { |
| 123 | // makepkg refuses root |
| 124 | return |
| 125 | } |
| 126 | dir := os.join_path(os.vtmp_dir(), 'vpac_test_packagelist_${os.getpid()}') |
| 127 | out_dir := os.join_path(dir, 'out') |
| 128 | os.mkdir_all(out_dir)! |
| 129 | defer { os.rmdir_all(dir) or {} } |
| 130 | os.write_file(os.join_path(dir, 'PKGBUILD'), [ |
| 131 | 'pkgbase=split', |
| 132 | 'pkgname=(split-a split-b)', |
| 133 | 'pkgver=1.0', |
| 134 | 'pkgrel=1', |
| 135 | "arch=('any')", |
| 136 | 'package_split-a() { :; }', |
| 137 | 'package_split-b() { :; }', |
| 138 | ].join('\n') + '\n')! |
| 139 | // an archive an older build left in the checkout: not this build's |
| 140 | os.write_file(os.join_path(dir, 'split-a-0.9-1-any.pkg.tar.zst'), '')! |
| 141 | saved := pkgbuild_makepkg |
| 142 | defer { pkgbuild_makepkg = saved } |
| 143 | pkgbuild_makepkg = 'makepkg -f' |
| 144 | // the line built_packages runs, as this user rather than through runuser: |
| 145 | // PKGDEST and PKGEXT as a makepkg.conf would set them |
| 146 | res := os.exec(['env', 'PKGDEST=${out_dir}', 'PKGEXT=.pkg.tar', 'sh', '-c', |
| 147 | '${makepkg_line(dir)} --packagelist']) |
| 148 | assert res.exit_code == 0, res.output |
| 149 | listed := res.output.split_into_lines().map(it.trim_space()).filter(it.starts_with('/')) |
| 150 | assert listed == ['${out_dir}/split-a-1.0-1-any.pkg.tar', '${out_dir}/split-b-1.0-1-any.pkg.tar'], res.output |
| 151 | // nothing built yet: nothing to install, the stale archive not among it |
| 152 | assert packagelist_files(res.output) == [] |
| 153 | // built: exactly this version's two, from PKGDEST, uncompressed |
| 154 | for f in listed { |
| 155 | os.write_file(f, '')! |
| 156 | } |
| 157 | assert packagelist_files(res.output) == listed |
| 158 | } |
| 159 | |
| 160 | fn test_run_process_drains_both_pipes() ! { |
| 161 | // more on stderr than a pipe holds, before stdout says anything: a |
| 162 | // reader of stdout alone would wait on a child that waits on it |
| 163 | out, code := run_process(['sh', '-c', 'head -c 300000 /dev/zero >&2; echo ok; exit 3'], |
| 164 | true)! |
| 165 | assert out == 'ok\n' |
| 166 | assert code == 3 |
| 167 | _, plain := run_process(['true'], false)! |
| 168 | assert plain == 0 |
| 169 | if _, _ := run_process(['vpac-no-such-tool'], false) { |
| 170 | assert false |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | fn test_built_install_config_carries_the_command_line() { |
| 175 | outer := ConfigInstall{ |
| 176 | noconfirm: true |
| 177 | asexplicit: true |
| 178 | dbonly: true |
| 179 | nodeps: true |
| 180 | nodeps_level: 2 |
| 181 | overwrite: ['usr/*'] |
| 182 | assume_installed: ['x=1'] |
| 183 | ignore: ['held'] |
| 184 | ignoregroup: ['grp'] |
| 185 | no_upgrade_deps: true |
| 186 | disable_timeout: true |
| 187 | pkgbuild: true |
| 188 | no_pkgbuild_confirm: true |
| 189 | needed: true |
| 190 | query: ['the-target'] |
| 191 | } |
| 192 | target := built_install_config(outer, ['/b/a.pkg.tar.zst', '/b/b.pkg.tar.zst'], true) |
| 193 | // the archives are the targets, not the name that was asked for |
| 194 | assert target.query == ['/b/a.pkg.tar.zst', '/b/b.pkg.tar.zst'] |
| 195 | assert target.asexplicit && !target.asdeps |
| 196 | assert target.ignore == ['held'] && target.ignoregroup == ['grp'] |
| 197 | assert target.no_upgrade_deps && target.disable_timeout && target.noconfirm |
| 198 | assert target.dbonly && target.nodeps && target.nodeps_level == 2 |
| 199 | assert target.overwrite == ['usr/*'] && target.assume_installed == ['x=1'] |
| 200 | // the outer run's own flags stay behind: an archive is not looked up in |
| 201 | // the pkgbuild repo, and --needed was settled before the build |
| 202 | assert !target.pkgbuild && !target.no_pkgbuild_confirm && !target.needed |
| 203 | // a dependency node is a dependency whatever the target was to be |
| 204 | dep := built_install_config(outer, ['/b/dep.pkg.tar.zst'], false) |
| 205 | assert dep.asdeps && !dep.asexplicit |
| 206 | assert dep.ignore == ['held'] && dep.no_upgrade_deps |
| 207 | // and nothing appears that the command line did not say |
| 208 | plain := built_install_config(ConfigInstall{}, [], true) |
| 209 | assert plain == ConfigInstall{} |
| 210 | } |
| 211 | |
| 212 | // node_db writes a local db for the node decisions and points the config at it |
| 213 | fn node_db(name string, pkgs []testdb.Pkg) !string { |
| 214 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_node_${name}_${os.getpid()}') |
| 215 | os.rmdir_all(tmp) or {} |
| 216 | os.mkdir_all(os.join_path(tmp, 'root'))! |
| 217 | testdb.write_local(os.join_path(tmp, 'db'), pkgs)! |
| 218 | root_path = os.join_path(tmp, 'root') + '/' |
| 219 | db_path = os.join_path(tmp, 'db') + '/' |
| 220 | return tmp |
| 221 | } |
| 222 | |
| 223 | fn node_of(name string, version string, wanted []string, repo_deps []string) PkgbuildNode { |
| 224 | return PkgbuildNode{ |
| 225 | info: PackagePkgbuild{ |
| 226 | name: name |
| 227 | package_base: name |
| 228 | version: version |
| 229 | } |
| 230 | wanted: wanted |
| 231 | repo_deps: repo_deps |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | fn test_node_step_is_decided_on_what_is_installed_now() ! { |
| 236 | saved_root, saved_db := root_path, db_path |
| 237 | tmp := node_db('step', [ |
| 238 | testdb.Pkg{ |
| 239 | name: 'lib' |
| 240 | version: '1.0-1' |
| 241 | }, |
| 242 | testdb.Pkg{ |
| 243 | name: 'impl' |
| 244 | version: '3.0-1' |
| 245 | provides: ['virt=3'] |
| 246 | }, |
| 247 | ])! |
| 248 | defer { |
| 249 | root_path, db_path = saved_root, saved_db |
| 250 | os.rmdir_all(tmp) or {} |
| 251 | } |
| 252 | // a dependency node everything wanted is satisfied for is not built: |
| 253 | // by its own name, by a version it meets, by something that provides it |
| 254 | for wanted in [['lib'], ['lib>=1.0'], ['lib', 'lib<2'], ['virt'], ['virt>=3', 'virt']] { |
| 255 | name := dep_name(wanted[0]) |
| 256 | step := node_step_now(node_of(name, '9-1', wanted, []), false, false)! |
| 257 | assert step.skip, '${wanted} was still to build' |
| 258 | assert step.why.contains('installed now'), step.why |
| 259 | } |
| 260 | // one requirement of several that is not met keeps the build |
| 261 | for wanted in [['lib>=2'], ['lib', 'lib>=2'], ['virt=4'], ['gone'], ['lib', 'gone']] { |
| 262 | step := node_step_now(node_of(dep_name(wanted[0]), '9-1', wanted, []), false, false)! |
| 263 | assert !step.skip, '${wanted} was skipped' |
| 264 | } |
| 265 | // a package of the name at the node's very version does not skip a |
| 266 | // dependency node whose requirement it does not meet, with or without |
| 267 | // --needed: the name being there is not the requirement being met |
| 268 | for needed in [false, true] { |
| 269 | same := node_step_now(node_of('lib', '1.0-1', ['lib>=2'], []), needed, false)! |
| 270 | assert !same.skip |
| 271 | } |
| 272 | // the target is never dropped for being satisfied: it was asked for. |
| 273 | // only --needed skips it, and only at the version it would install |
| 274 | assert !node_step_now(node_of('lib', '1.0-1', [], []), false, true)!.skip |
| 275 | assert !node_step_now(node_of('lib', '1.0-1', ['lib'], []), false, true)!.skip |
| 276 | up := node_step_now(node_of('lib', '1.0-1', [], []), true, true)! |
| 277 | assert up.skip && up.why == 'lib-1.0-1 is up to date, skipped' |
| 278 | assert !node_step_now(node_of('lib', '1.0-2', [], []), true, true)!.skip |
| 279 | assert !node_step_now(node_of('lib', '0.9-1', [], []), true, true)!.skip |
| 280 | assert !node_step_now(node_of('absent', '1.0-1', [], []), true, true)!.skip |
| 281 | // a node nothing wanted and that is no target (never planned so, but |
| 282 | // the rule is spelled) is built |
| 283 | assert !node_step_now(node_of('lib', '1.0-1', [], []), false, false)!.skip |
| 284 | // the repo dependencies left are the ones not installed, in order; |
| 285 | // a provider under another name does not count as the package |
| 286 | deps := node_step_now(node_of('app', '1-1', [], ['new', 'lib', 'virt', 'other']), false, |
| 287 | true)! |
| 288 | assert deps.repo_deps == ['new', 'virt', 'other'] |
| 289 | assert node_step_now(node_of('app', '1-1', [], []), false, true)!.repo_deps.len == 0 |
| 290 | // a skipped node asks for no repo dependencies |
| 291 | assert node_step_now(node_of('lib', '9-1', ['lib'], ['new']), false, false)!.repo_deps.len == 0 |
| 292 | } |
| 293 | |
| 294 | fn test_local_now_sees_what_an_older_handle_does_not() ! { |
| 295 | saved_root, saved_db := root_path, db_path |
| 296 | tmp := node_db('fresh', [testdb.Pkg{ |
| 297 | name: 'first' |
| 298 | }])! |
| 299 | defer { |
| 300 | root_path, db_path = saved_root, saved_db |
| 301 | os.rmdir_all(tmp) or {} |
| 302 | } |
| 303 | // the run's handle reads the local db once |
| 304 | old_handle, old_pkgs := local_now()! |
| 305 | defer { old_handle.release() or {} } |
| 306 | assert old_pkgs.pkg_find('first') != none |
| 307 | // a build's install goes in through another handle: here, written |
| 308 | testdb.write_local(os.join_path(tmp, 'db'), [ |
| 309 | testdb.Pkg{ |
| 310 | name: 'built' |
| 311 | version: '2.0-1' |
| 312 | provides: ['virt=2'] |
| 313 | }, |
| 314 | ])! |
| 315 | // the old list is the past: the staleness the plan must not be made on |
| 316 | assert old_pkgs.pkg_find('built') == none |
| 317 | assert old_pkgs.find_satisfier('virt') == none |
| 318 | // a view opened now has it, and a node waiting for it is dropped |
| 319 | view, now := local_now()! |
| 320 | assert now.pkg_find('built') != none && now.pkg_find('first') != none |
| 321 | assert now.find_satisfier('virt>=2') != none |
| 322 | view.release()! |
| 323 | assert node_step(old_pkgs, node_of('built', '2.0-1', ['virt'], []), false, false).skip == false |
| 324 | assert node_step_now(node_of('built', '2.0-1', ['virt'], []), false, false)!.skip |
| 325 | // an empty local db is a view of nothing, not an error |
| 326 | os.rmdir_all(os.join_path(tmp, 'db', 'local'))! |
| 327 | testdb.write_local(os.join_path(tmp, 'db'), [])! |
| 328 | e_view, empty := local_now()! |
| 329 | assert empty.pkg_find('first') == none |
| 330 | e_view.release()! |
| 331 | assert !node_step_now(node_of('x', '1-1', ['x'], ['dep']), false, false)!.skip |
| 332 | // a db that is not there is an error, with nothing left open |
| 333 | db_path = os.join_path(tmp, 'nowhere') + '/' |
| 334 | if _, _ := local_now() { |
| 335 | assert false, 'a missing db opened' |
| 336 | } |
| 337 | // nothing to install asks for no transaction at all |
| 338 | install_repo_deps([])! |
| 339 | } |
| 340 | |