module main import os import testdb fn test_dep_name() { cases := { 'glibc': 'glibc' 'glibc>=2.27': 'glibc' 'foo<1.0': 'foo' 'foo<=1.0': 'foo' 'foo=1.0-2': 'foo' 'foo>1:2.0': 'foo' 'gcc-libs': 'gcc-libs' 'python>=3.12': 'python' 'libfoo.so=1-64': 'libfoo.so' // stray spaces, as a published dependency list occasionally spells ' glibc >=2.27 ': 'glibc' ' glibc ': 'glibc' 'git >2.0': 'git' } for dep, want in cases { assert dep_name(dep) == want, dep } } fn test_review() ! { old := color_flag defer { color_flag = old } color_flag = 'never' dir := os.join_path(os.vtmp_dir(), 'vpac_test_review_${os.getpid()}') os.mkdir_all(os.join_path(dir, '.git'))! defer { os.rmdir_all(dir) or {} } os.write_file(os.join_path(dir, 'PKGBUILD'), 'pkgname=foo\n')! os.write_file(os.join_path(dir, 'foo.install'), 'post_install() { :; }')! os.write_file(os.join_path(dir, 'a.patch'), '')! // the PKGBUILD first, then the .install scripts, then every name but .git mut r := read_review('foo', dir)! assert r.files.map(it.name) == ['PKGBUILD', 'foo.install'] assert r.listing == ['PKGBUILD', 'a.patch', 'foo.install'] r.warnings = pkgbuild_warnings(PackagePkgbuild{ name: 'foo', maintainer: 'me', num_votes: 1 }) assert review_text(r, 'review foo') == [ 'warning: maintainer: me, votes: 1', 'review foo', 'PKGBUILD', 'pkgname=foo\n', 'foo.install', 'post_install() { :; }', 'files in the package', 'PKGBUILD a.patch foo.install', ].join('\n') // a generated package shows its source, then what was generated from it g := generated_review('foo', 'PKGBUILD.lip', '(lip)', 'pkgname=foo', 'foo.install', 'x') assert review_text(g, '') == 'PKGBUILD.lip\n(lip)\nPKGBUILD the build runs\npkgname=foo\nfoo.install\nx' assert generated_review('foo', 'PKGBUILD.vsh', 'v', 'p', 'foo.install', '').files.len == 2 } fn test_makepkg_line() ! { // the dir is one word to the shell whatever it is called; the build // command is the config's text, run in it dir := os.join_path(os.vtmp_dir(), "vpac test's \$dir `x` ${os.getpid()}") os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } saved := pkgbuild_makepkg defer { pkgbuild_makepkg = saved } pkgbuild_makepkg = 'pwd -P' res := os.exec(['sh', '-c', makepkg_line(dir)]) assert res.exit_code == 0, res.output assert res.output.trim_right('\n') == os.real_path(dir) // the default is makepkg's own, forcing a rebuild of what is there reset_config() assert makepkg_line('/x').ends_with(' && makepkg -f') } fn test_build_dir_takes_a_plain_base_only() ! { // the dir is removed before the clone, so a base that climbs out of the // build root is refused before any path is made of it saved := pkgbuild_dir defer { pkgbuild_dir = saved } pkgbuild_dir = '/var/tmp/vpac-builds' assert pkgbuild_build_dir('u', 'foo-git')! == '/var/tmp/vpac-builds/foo-git' for bad in ['', '.', '..', '../x', 'a/b', '/etc', '-rf'] { if d := pkgbuild_build_dir('u', bad) { assert false, 'accepted "${bad}" as ${d}' } } } fn test_packagelist_files() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_test_pkglist_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } built := os.join_path(dir, 'foo-1.0-1-any.pkg.tar') os.write_file(built, '')! // makepkg names every package it can make, built or not: a debug package // that was not made, and blank or odd lines, are not archives out := '${built}\n${dir}/foo-debug-1.0-1-any.pkg.tar\n\n ${built} \nwarning: x\n' assert packagelist_files(out) == [built, built] } fn test_archives_in() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_test_archives_in_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } 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', 'PKGBUILD', 'notes.tar'] { os.write_file(os.join_path(dir, f), '')! } mut got := archives_in(dir)!.map(os.base(it)) got.sort() // an uncompressed archive counts, a signature and other files do not assert got == ['bar-2-1-x86_64.pkg.tar.zst', 'foo-1.0-1-any.pkg.tar'] } fn test_makepkg_packagelist_names_what_is_built() ! { if !os.exists_in_system_path('makepkg') { // no makepkg on this system: the assumption cannot be checked here return } if os.geteuid() == 0 { // makepkg refuses root return } dir := os.join_path(os.vtmp_dir(), 'vpac_test_packagelist_${os.getpid()}') out_dir := os.join_path(dir, 'out') os.mkdir_all(out_dir)! defer { os.rmdir_all(dir) or {} } os.write_file(os.join_path(dir, 'PKGBUILD'), [ 'pkgbase=split', 'pkgname=(split-a split-b)', 'pkgver=1.0', 'pkgrel=1', "arch=('any')", 'package_split-a() { :; }', 'package_split-b() { :; }', ].join('\n') + '\n')! // an archive an older build left in the checkout: not this build's os.write_file(os.join_path(dir, 'split-a-0.9-1-any.pkg.tar.zst'), '')! saved := pkgbuild_makepkg defer { pkgbuild_makepkg = saved } pkgbuild_makepkg = 'makepkg -f' // the line built_packages runs, as this user rather than through runuser: // PKGDEST and PKGEXT as a makepkg.conf would set them res := os.exec(['env', 'PKGDEST=${out_dir}', 'PKGEXT=.pkg.tar', 'sh', '-c', '${makepkg_line(dir)} --packagelist']) assert res.exit_code == 0, res.output listed := res.output.split_into_lines().map(it.trim_space()).filter(it.starts_with('/')) assert listed == ['${out_dir}/split-a-1.0-1-any.pkg.tar', '${out_dir}/split-b-1.0-1-any.pkg.tar'], res.output // nothing built yet: nothing to install, the stale archive not among it assert packagelist_files(res.output) == [] // built: exactly this version's two, from PKGDEST, uncompressed for f in listed { os.write_file(f, '')! } assert packagelist_files(res.output) == listed } fn test_run_process_drains_both_pipes() ! { // more on stderr than a pipe holds, before stdout says anything: a // reader of stdout alone would wait on a child that waits on it out, code := run_process(['sh', '-c', 'head -c 300000 /dev/zero >&2; echo ok; exit 3'], true)! assert out == 'ok\n' assert code == 3 _, plain := run_process(['true'], false)! assert plain == 0 if _, _ := run_process(['vpac-no-such-tool'], false) { assert false } } fn test_built_install_config_carries_the_command_line() { outer := ConfigInstall{ noconfirm: true asexplicit: true dbonly: true nodeps: true nodeps_level: 2 overwrite: ['usr/*'] assume_installed: ['x=1'] ignore: ['held'] ignoregroup: ['grp'] no_upgrade_deps: true disable_timeout: true pkgbuild: true no_pkgbuild_confirm: true needed: true query: ['the-target'] } target := built_install_config(outer, ['/b/a.pkg.tar.zst', '/b/b.pkg.tar.zst'], true) // the archives are the targets, not the name that was asked for assert target.query == ['/b/a.pkg.tar.zst', '/b/b.pkg.tar.zst'] assert target.asexplicit && !target.asdeps assert target.ignore == ['held'] && target.ignoregroup == ['grp'] assert target.no_upgrade_deps && target.disable_timeout && target.noconfirm assert target.dbonly && target.nodeps && target.nodeps_level == 2 assert target.overwrite == ['usr/*'] && target.assume_installed == ['x=1'] // the outer run's own flags stay behind: an archive is not looked up in // the pkgbuild repo, and --needed was settled before the build assert !target.pkgbuild && !target.no_pkgbuild_confirm && !target.needed // a dependency node is a dependency whatever the target was to be dep := built_install_config(outer, ['/b/dep.pkg.tar.zst'], false) assert dep.asdeps && !dep.asexplicit assert dep.ignore == ['held'] && dep.no_upgrade_deps // and nothing appears that the command line did not say plain := built_install_config(ConfigInstall{}, [], true) assert plain == ConfigInstall{} } // node_db writes a local db for the node decisions and points the config at it fn node_db(name string, pkgs []testdb.Pkg) !string { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_node_${name}_${os.getpid()}') os.rmdir_all(tmp) or {} os.mkdir_all(os.join_path(tmp, 'root'))! testdb.write_local(os.join_path(tmp, 'db'), pkgs)! root_path = os.join_path(tmp, 'root') + '/' db_path = os.join_path(tmp, 'db') + '/' return tmp } fn node_of(name string, version string, wanted []string, repo_deps []string) PkgbuildNode { return PkgbuildNode{ info: PackagePkgbuild{ name: name package_base: name version: version } wanted: wanted repo_deps: repo_deps } } fn test_node_step_is_decided_on_what_is_installed_now() ! { saved_root, saved_db := root_path, db_path tmp := node_db('step', [ testdb.Pkg{ name: 'lib' version: '1.0-1' }, testdb.Pkg{ name: 'impl' version: '3.0-1' provides: ['virt=3'] }, ])! defer { root_path, db_path = saved_root, saved_db os.rmdir_all(tmp) or {} } // a dependency node everything wanted is satisfied for is not built: // by its own name, by a version it meets, by something that provides it for wanted in [['lib'], ['lib>=1.0'], ['lib', 'lib<2'], ['virt'], ['virt>=3', 'virt']] { name := dep_name(wanted[0]) step := node_step_now(node_of(name, '9-1', wanted, []), false, false)! assert step.skip, '${wanted} was still to build' assert step.why.contains('installed now'), step.why } // one requirement of several that is not met keeps the build for wanted in [['lib>=2'], ['lib', 'lib>=2'], ['virt=4'], ['gone'], ['lib', 'gone']] { step := node_step_now(node_of(dep_name(wanted[0]), '9-1', wanted, []), false, false)! assert !step.skip, '${wanted} was skipped' } // a package of the name at the node's very version does not skip a // dependency node whose requirement it does not meet, with or without // --needed: the name being there is not the requirement being met for needed in [false, true] { same := node_step_now(node_of('lib', '1.0-1', ['lib>=2'], []), needed, false)! assert !same.skip } // the target is never dropped for being satisfied: it was asked for. // only --needed skips it, and only at the version it would install assert !node_step_now(node_of('lib', '1.0-1', [], []), false, true)!.skip assert !node_step_now(node_of('lib', '1.0-1', ['lib'], []), false, true)!.skip up := node_step_now(node_of('lib', '1.0-1', [], []), true, true)! assert up.skip && up.why == 'lib-1.0-1 is up to date, skipped' assert !node_step_now(node_of('lib', '1.0-2', [], []), true, true)!.skip assert !node_step_now(node_of('lib', '0.9-1', [], []), true, true)!.skip assert !node_step_now(node_of('absent', '1.0-1', [], []), true, true)!.skip // a node nothing wanted and that is no target (never planned so, but // the rule is spelled) is built assert !node_step_now(node_of('lib', '1.0-1', [], []), false, false)!.skip // the repo dependencies left are the ones not installed, in order; // a provider under another name does not count as the package deps := node_step_now(node_of('app', '1-1', [], ['new', 'lib', 'virt', 'other']), false, true)! assert deps.repo_deps == ['new', 'virt', 'other'] assert node_step_now(node_of('app', '1-1', [], []), false, true)!.repo_deps.len == 0 // a skipped node asks for no repo dependencies assert node_step_now(node_of('lib', '9-1', ['lib'], ['new']), false, false)!.repo_deps.len == 0 } fn test_local_now_sees_what_an_older_handle_does_not() ! { saved_root, saved_db := root_path, db_path tmp := node_db('fresh', [testdb.Pkg{ name: 'first' }])! defer { root_path, db_path = saved_root, saved_db os.rmdir_all(tmp) or {} } // the run's handle reads the local db once old_handle, old_pkgs := local_now()! defer { old_handle.release() or {} } assert old_pkgs.pkg_find('first') != none // a build's install goes in through another handle: here, written testdb.write_local(os.join_path(tmp, 'db'), [ testdb.Pkg{ name: 'built' version: '2.0-1' provides: ['virt=2'] }, ])! // the old list is the past: the staleness the plan must not be made on assert old_pkgs.pkg_find('built') == none assert old_pkgs.find_satisfier('virt') == none // a view opened now has it, and a node waiting for it is dropped view, now := local_now()! assert now.pkg_find('built') != none && now.pkg_find('first') != none assert now.find_satisfier('virt>=2') != none view.release()! assert node_step(old_pkgs, node_of('built', '2.0-1', ['virt'], []), false, false).skip == false assert node_step_now(node_of('built', '2.0-1', ['virt'], []), false, false)!.skip // an empty local db is a view of nothing, not an error os.rmdir_all(os.join_path(tmp, 'db', 'local'))! testdb.write_local(os.join_path(tmp, 'db'), [])! e_view, empty := local_now()! assert empty.pkg_find('first') == none e_view.release()! assert !node_step_now(node_of('x', '1-1', ['x'], ['dep']), false, false)!.skip // a db that is not there is an error, with nothing left open db_path = os.join_path(tmp, 'nowhere') + '/' if _, _ := local_now() { assert false, 'a missing db opened' } // nothing to install asks for no transaction at all install_repo_deps([])! } fn test_upgrade_pkgbuild_config_carries_the_transaction() { up := upgrade_pkgbuild_config(ConfigUpgrade{ noconfirm: true no_pkgbuild_confirm: true nodeps: true nodeps_level: 2 overwrite: ['usr/*'] ignore: ['held'] ignoregroup: ['grp'] disable_timeout: true }) // the questions are review_and_build's own, the gate's word written by // the pass: what the command line said of asking is carried as it was assert up.pkgbuild && up.no_pkgbuild_confirm assert up.noconfirm && up.nodeps && up.nodeps_level == 2 assert up.overwrite == ['usr/*'] && up.ignore == ['held'] && up.ignoregroup == ['grp'] assert up.disable_timeout // what the outer run settles itself stays behind assert !up.needed && !up.print_only && !up.download_only // without the command line's say, the questions stand plain := upgrade_pkgbuild_config(ConfigUpgrade{}) assert plain.pkgbuild && !plain.no_pkgbuild_confirm && !plain.noconfirm }