module main import os fn test_repo_name() { // the last part of the path or url, without a .git suffix. trim_right // would have cut a set of characters: digit came out as d, gist as gis cases := { 'https://example.org/mything.git': 'mything' 'https://example.org/mything': 'mything' 'https://example.org/mything.git/': 'mything' 'https://example.org/digit': 'digit' 'https://example.org/gist.git': 'gist' 'git@github.com:foo/bar.git': 'bar' 'git@host:bar.git': 'bar' '/home/u/src/thing': 'thing' '/home/u/src/thing/': 'thing' '/': '' '.': '' '': '' } for source, want in cases { got := repo_name(source) assert got == want, 'repo_name("${source}") is "${got}", not "${want}"' } } fn test_repo_work_dir_urls() ! { old := pkgbuild_dir pkgbuild_dir = '/builds' defer { pkgbuild_dir = old } name, one := repo_work_dir('u', 'https://one.example/mything.git')! _, two := repo_work_dir('u', 'https://two.example/mything.git')! _, one_slash := repo_work_dir('u', 'https://one.example/mything.git/')! assert name == 'mything' hash := one.all_after('/builds/mything-') assert one.starts_with('/builds/mything-') && hash.len == 8 assert hash.bytes().all(it.is_hex_digit()) // the same name from another source is another dir; a trailing slash is // the same source assert one != two assert one == one_slash // and a pkgbuild package of that name builds elsewhere assert one != pkgbuild_build_dir('u', 'mything')! if _, _ := repo_work_dir('u', '/') { assert false, '/ names no repo' } } fn test_is_git_url() { // the transfer urls and the .git names, as always assert is_git_url('https://example.org/mything.git') assert is_git_url('git://example.org/mything') // scp-style: user@host:path — a colon before any slash, the way git // spells a clone it will fetch over ssh assert is_git_url('git@github.com:foo/bar') assert is_git_url('git@host:bar.git') assert is_scp_remote('git@host:bar.git') // a path's slash comes first; a bare file with a colon in its name has // no slash before it either, but hosts come before the colon in scp for not in ['/home/u/mything', '/home/u/we:ird', '/dev/null:too'] { assert !is_scp_remote(not), not } } fn test_repo_work_dir_directories() ! { old := pkgbuild_dir pkgbuild_dir = '/builds' defer { pkgbuild_dir = old } tmp := os.join_path(os.vtmp_dir(), 'vpac_test_build_repo_${os.getpid()}') os.mkdir_all(os.join_path(tmp, 'a', 'mything'))! os.mkdir_all(os.join_path(tmp, 'b', 'mything'))! defer { os.rmdir_all(tmp) or {} } cwd := os.getwd() defer { os.chdir(cwd) or {} } _, a := repo_work_dir('u', os.join_path(tmp, 'a', 'mything'))! _, b := repo_work_dir('u', os.join_path(tmp, 'b', 'mything'))! assert a != b // a directory is keyed on its real path: relative, trailing slash and . // all name the same checkout os.chdir(os.join_path(tmp, 'a'))! _, rel := repo_work_dir('u', 'mything/')! os.chdir(os.join_path(tmp, 'a', 'mything'))! dot_name, dot := repo_work_dir('u', '.')! assert rel == a assert dot == a assert dot_name == 'mything' } fn test_install_name_of() ! { for text, want in { 'pkgname=x\ninstall=x.install\n': 'x.install' 'install = "x.install"': 'x.install' "install='x.install'": 'x.install' // bash reads these; this reader takes them as written 'install="\${pkgname}.install"': '\${pkgname}.install' 'install=x.install # the hooks': 'x.install # the hooks' // none named, or a command in a function body, which is no assignment 'pkgname=x\n': '' 'package() {\n install -Dm755 x "$pkgdir/x"\n}': '' 'installdir=/opt': '' // the last assignment is the one bash keeps 'install=a.install\ninstall=b.install': 'b.install' } { assert install_name_of(text)! == want, text } for escape in ['install=/etc/x.install', 'install=../x.install', 'install=a/../../x.install', 'install=..'] { install_name_of(escape) or { assert err.msg().contains('is not a plain name beside the PKGBUILD'), err.msg() continue } assert false, '${escape} was taken' } } fn test_review_the_rest_shows_every_install_file() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_test_review_rest_${os.getpid()}') os.rmdir_all(dir) or {} os.mkdir_all(os.join_path(dir, '.git'))! defer { os.rmdir_all(dir) or {} } pkg_text := 'pkgname=x\ninstall="\${pkgname}.install"\n' os.write_file(os.join_path(dir, 'PKGBUILD'), pkg_text)! os.write_file(os.join_path(dir, 'x.install'), 'post_install() { rm -rf /; }')! os.write_file(os.join_path(dir, 'other.install'), 'post_upgrade() { :; }')! os.write_file(os.join_path(dir, 'x.patch'), 'patch')! // the generated head spells its install file with a variable: the reader // cannot resolve it, and the scriptlet makepkg packs must still be shown name := install_name_of(pkg_text)! mut review := generated_review('x', 'PKGBUILD.vsh', 'script', pkg_text, name, '') review_the_rest(mut review, dir, name, false)! files := review.files.map(it.name) assert 'x.install' in files && 'other.install' in files, files.str() assert review.files.filter(it.name == 'x.install')[0].text == 'post_install() { rm -rf /; }' assert review.listing == ['PKGBUILD', 'other.install', 'x.install', 'x.patch'] assert review.warnings.len == 1 assert review.warnings[0].contains('"\${pkgname}.install"'), review.warnings[0] // a name the reader resolved is shown once, and nothing is warned of mut plain := generated_review('x', 'PKGBUILD.vsh', 'script', 'install=x.install', 'x.install', 'post_install() { rm -rf /; }') review_the_rest(mut plain, dir, 'x.install', true)! assert plain.files.filter(it.name == 'x.install').len == 1 assert plain.files.any(it.name == 'other.install') assert plain.warnings.len == 0 } fn test_build_refuses_noconfirm_alone() { // the build question's preset is no: --noconfirm alone would build // nothing and exit clean, so it is refused before anything is pulled build_repo(ConfigBuild{ noconfirm: true, query: ['/nonexistent/repo'] }) or { assert err.msg().contains('--nopkgbuildconfirm'), err.msg() return } assert false, 'built under --noconfirm' }