module vmakepkg import os fn test_recipe_phases_load_helpers_and_receive_arguments() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_phases_${os.getpid()}') os.mkdir_all(dir)! cwd := os.getwd() old_pkgdir := os.getenv_opt('VPAC_PKGDIR') old_startdir := os.getenv_opt('VPAC_STARTDIR') defer { os.chdir(cwd) or { eprintln('restore test directory: ${err}') } if value := old_pkgdir { os.setenv('VPAC_PKGDIR', value, true) } else { os.unsetenv('VPAC_PKGDIR') } if value := old_startdir { os.setenv('VPAC_STARTDIR', value, true) } else { os.unsetenv('VPAC_STARTDIR') } os.rmdir_all(dir) or { eprintln('remove test package: ${err}') } } os.setenv('VPAC_PKGDIR', dir, true) os.setenv('VPAC_STARTDIR', dir, true) os.chdir(dir)! os.write_file('helper.scm', '(define (destination name) (string-append (pkgdir) "/" name))')! os.write_file('one.txt', 'first\n')! os.write_file('two.txt', 'second\n')! recipe := os.join_path(dir, 'PKGBUILD.scm') os.write_file(recipe, '(load "helper.scm") (defpkgbuild phases #:pkgname "phases" #:pkgver "1.0" #:pkgrel 1 #:pkgdesc "phase execution" #:arch (list "any") #:package (lambda () (for-each (lambda (file) (run "cp" file (destination (string-append file ".copy")))) (glob "*.txt"))) #:post-install (lambda (version) (run "touch" (destination version))))')! recipe_load(recipe)! assert !os.exists('one.txt.copy') recipe_run_phase(recipe, 'package', [])! assert os.read_file('one.txt.copy')! == 'first\n' assert os.read_file('two.txt.copy')! == 'second\n' version := '1.0; touch injected' recipe_run_phase(recipe, 'post-install', [version])! assert os.is_file(version) assert !os.exists('injected') recipe_run_phase(recipe, 'post-install', []) or { return } assert false, 'a hook ran without its version argument' } fn test_recipe_rejects_malformed_metadata_calls() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_metadata_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln('remove test metadata: ${err}') } } path := os.join_path(dir, 'PKGBUILD.scm') for source in ['(vpac-defpkgbuild)', '(vpac-make-pkgbuild #() #())', '(vpac-make-pkgbuild #("name") #())'] { os.write_file(path, source)! recipe_load(path) or { continue } assert false, 'malformed package metadata loaded: ${source}' } } // architecture and option spellings in package metadata fn test_recipe_arch() { assert recipe_arch('x86-64') == 'x86_64' assert recipe_arch('any') == 'any' assert recipe_arch('x86') == 'x86' } fn test_recipe_option() { assert recipe_option('no-strip') == '!strip' assert recipe_option('docs') == 'docs' assert recipe_option('zipman') == 'zipman' assert recipe_option('no-docs') == '!docs' } fn test_recipe_hook_arity() { assert recipe_hook_arity('pre-install') == 1 assert recipe_hook_arity('post-remove') == 1 assert recipe_hook_arity('pre-upgrade') == 2 assert recipe_hook_arity('post-upgrade') == 2 } fn test_shell_quote() { assert shell_quote("a 'b' c") == "'a '\\''b'\\'' c'" assert shell_quote('plain') == "'plain'" } fn test_shell_quote_round_trips_through_sh() { // the property that matters: whatever the value holds, a shell reads it // back as the one word it was — no expansion, no split, no command for value in ["it's", '\$HOME', '`id`', '\$(id)', 'a b\tc', 'semi;colon', 'new\nline', 'back\\slash', '"double"', "'", "''", '*', '-n', '', 'ünï'] { res := os.exec(['sh', '-c', 'printf %s ' + shell_quote(value)]) assert res.exit_code == 0, value assert res.output == value, value } } // recipe_defpkg_count counts real (defpkgbuild ...) forms: comments of either // kind stay out, block comments stay out, a string's mention of the form // stays out, and a longer symbol whose name begins equally is no form fn test_recipe_defpkg_count() { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_count_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } p := os.join_path(dir, 'count.scm') os.write_file(p, '(defpkgbuild foo\n)')! assert recipe_defpkg_count(p)! == 1 os.write_file(p, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')! assert recipe_defpkg_count(p)! == 2 os.write_file(p, ';; a comment names (defpkgbuild x)\n(defpkgbuild foo\n)')! assert recipe_defpkg_count(p)! == 1 os.write_file(p, '(defpkgbuild foo ; inline ends the text (defpkgbuild y)\n)')! assert recipe_defpkg_count(p)! == 1 os.write_file(p, '#| (defpkgbuild z) reduces the block |#\n(defpkgbuild foo\n)')! assert recipe_defpkg_count(p)! == 1 // a string's mention of the form stays out: the ('no form counts') text // spells one whole (defpkgbuild ...) inside a string os.write_file(p, '(defpkgbuild foo\n #:defpkgdesc "the string names (defpkgbuild x) whole"\n)')! assert recipe_defpkg_count(p)! == 1 os.write_file(p, '(defpkg-build foo\n)')! assert recipe_defpkg_count(p)! == 0 os.write_file(p, '(defpkgbuildy foo\n)')! assert recipe_defpkg_count(p)! == 0 os.write_file(p, '#;(defpkgbuild ignored)\n( defpkgbuild foo)')! assert recipe_defpkg_count(p)! == 1 os.write_file(p, '(begin ( defpkgbuild foo) (\n defpkgbuild bar))')! assert recipe_defpkg_count(p)! == 2 os.write_file(p, "'(defpkgbuild quoted)\n#((defpkgbuild literal))\n(|defpkgbuild| foo)")! assert recipe_defpkg_count(p)! == 1 os.write_file(p, '#\\(\n#0=(begin . #0#)')! assert recipe_defpkg_count(p)! == 0 } fn test_recipe_load_uses_scheme_declarations() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_reader_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln('remove test package: ${err}') } } path := os.join_path(dir, 'PKGBUILD.scm') source := valid_recipe.join('\n').replace('(defpkgbuild', '(\n defpkgbuild') os.write_file(path, '#;(defpkgbuild ignored)\n' + source)! assert recipe_load(path)!.items('pkgname')! == ['corpus'] os.write_file(path, source + '\n' + source)! recipe_load(path) or { assert err.msg().contains('one (defpkgbuild ...) form per file, not 2'), err.msg() return } assert false, 'whitespace hid a second package declaration' } fn test_native_recipe_loads_helpers_and_validates_declarations() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_vile_recipe_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln('remove test recipe: ${err}') } } path := os.join_path(dir, 'PKGBUILD.vile') os.write_file(os.join_path(dir, 'helper.vile'), 'version := "1.2"')! source := 'load("helper.vile") defpkgbuild(native, #:pkgname, "native", #:pkgver, version, #:pkgrel, 1, #:pkgdesc, "native recipe", #:arch, [#:any], #:package, fn() {})' os.write_file(path, source)! assert native_description(recipe_load(path)!)!.spec.pkgver == '1.2' os.write_file(path, source.replace('fn() {}', 'fn(unexpected) {}'))! if _ := recipe_load(path) { assert false, 'a package phase with an argument was accepted' } else { assert err.msg().contains('#:package takes 0 parameters'), err.msg() } os.write_file(path, source + '\n' + source)! recipe_load(path) or { assert err.msg().contains('form per file, not 2'), err.msg() return } assert false, 'a second native package declaration was accepted' } fn test_recipe_load_unreadable() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_unreadable_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } // no file at all, and a directory where the file should be: each fails // with the read's reason and the path, not a later, other complaint for p in [os.join_path(dir, 'missing.scm'), dir] { recipe_load(p) or { assert err.msg().starts_with('${p}: '), err.msg() assert !err.msg().contains('defpkgbuild'), err.msg() continue } assert false, '${p} loaded' } // two forms in a readable file are still the one-form error two := os.join_path(dir, 'two.scm') os.write_file(two, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')! recipe_load(two) or { assert err.msg().contains('one (defpkgbuild ...) form per file, not 2'), err.msg() return } assert false, 'two forms loaded' } // read_items reads what recipe_items_of makes of a scheme expression's value fn read_items(src string) ![]string { mut m := recipe_new_machine()! return recipe_items_of(m.eval(src)!, 'depends')! } fn test_recipe_items_of_every_spelling() ! { // one item, as a string, a keyword or a symbol assert read_items('"glibc"')! == ['glibc'] assert read_items('#:any')! == ['any'] assert read_items("'zsh")! == ['zsh'] // a quoted list, a vector, and the empty one assert read_items('\'("a" "b" #:c)')! == ['a', 'b', 'c'] assert read_items('#("a" "b" #:c)')! == ['a', 'b', 'c'] assert read_items("'()")! == []string{} assert read_items('#()')! == []string{} // a list's items are plain: a number among them is said, in both kinds for src in ['\'("a" 1)', '#("a" 1)'] { read_items(src) or { assert err.msg().contains('items must be strings or keywords'), err.msg() continue } assert false, '${src} was read' } // a dotted tail is no list: its last item would be dropped silently. // a cons with a non-list tail exercises the same rejection. read_items('(cons "a" "b")') or { assert err.msg().contains('a list ends in ()'), err.msg() return } assert false, 'a dotted list was read' } fn test_recipe_vector_lists_reach_the_pkgbuild() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_vector_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } // the lists spelled as vectors, where the golden recipe quotes them recipe := os.join_path(dir, 'vec.scm') lines := [ '(defpkgbuild vec', ' #:pkgname "vec"', ' #:pkgver "1.0"', ' #:pkgrel 1', ' #:pkgdesc "vector lists"', ' #:arch #(#:any)', ' #:depends #("glibc" "zsh")', ' #:license #("MIT")', ' #:build (lambda () (run "true"))', ' #:package (lambda () (run "true")))', ] os.write_file(recipe, lines.join('\n'))! desc := native_description(recipe_load(recipe)!)! assert desc.spec.depends == ['glibc', 'zsh'] assert desc.spec.arch == ['any'] assert desc.licenses == ['MIT'] } // valid_recipe is a recipe every field of which is right; the corpus below breaks // one thing at a time const valid_recipe = ['(defpkgbuild corpus', ' #:pkgname "corpus"', ' #:pkgver "1.0"', ' #:pkgrel 1', ' #:pkgdesc "a corpus base"', " #:arch '(#:any)", ' #:build (lambda () (run "true"))', ' #:package (lambda () (run "true")))'] // RecipeCase is a recipe broken one way, and what the validator says about it struct RecipeCase { what string text string says string } // broken replaces the line of valid_recipe that starts with key's spelling, or // appends extra before the closing parenthesis when key is '' fn broken(key string, line string) string { mut lines := valid_recipe.clone() if key == '' { // the last line ends in defpkgbuild's own ')': the new line takes it last := lines[lines.len - 1] lines[lines.len - 1] = last[..last.len - 1] lines << line + ')' return lines.join('\n') } for i, l in lines { if l.trim_space().starts_with('#:${key} ') { lines[i] = line if i == lines.len - 1 { lines[i] += ')' } } } return lines.join('\n') } fn test_recipe_validation_corpus() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_corpus_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } // the base itself is good base := os.join_path(dir, 'base.scm') os.write_file(base, valid_recipe.join('\n'))! recipe_load(base)! hex64 := 'ab'.repeat(32) for c in [ RecipeCase{'no name', '(defpkgbuild)', 'needs the name'}, RecipeCase{'a number for a name', valid_recipe.join('\n').replace('(defpkgbuild corpus', '(defpkgbuild 7'), 'the name is a symbol or a string, not'}, RecipeCase{'a key without its value', broken('', ' #:url'), '#:key and value come in pairs'}, RecipeCase{'a value where a key goes', broken('', ' "stray" "pair"'), 'expected a #:key, got "stray"'}, RecipeCase{'a key twice', broken('', ' #:pkgrel 2'), '#:pkgrel is set twice'}, RecipeCase{'an empty name list', broken('pkgname', " #:pkgname '()"), '#:pkgname must not be empty'}, RecipeCase{'a name with a space', broken('pkgname', ' #:pkgname "cor pus"'), 'has whitespace in it'}, RecipeCase{'no arch', broken('arch', " #:arch '()"), '#:arch must not be empty'}, RecipeCase{'a short b2sum', broken('', ' #:b2sums \'("abc")'), 'b2sums: abc is 3 characters; b2sums entries are 128 hex characters or SKIP'}, RecipeCase{'a sha256 that is not hex', broken('', ' #:sha256sums \'("${'zz'.repeat(32)}")'), 'has a character that is not hex (z)'}, RecipeCase{'a short fingerprint', broken('', ' #:validpgpkeys \'("ABCDEF")'), 'validpgpkeys: entry 0 is 6 characters'}, RecipeCase{'a lowercase fingerprint', broken('', ' #:validpgpkeys \'("${'ab'.repeat(20)}")'), 'uppercase fingerprints only'}, RecipeCase{'an absolute backup', broken('', ' #:backup \'("/etc/x.conf")'), 'is absolute; backup paths are relative'}, RecipeCase{'a phase that is no lambda', broken('build', ' #:build "make"'), '#:build must be a (lambda ...)'}, RecipeCase{'a phase with a parameter', broken('build', ' #:build (lambda (x) (run "true"))'), '#:build takes 0 parameters, not 1'}, RecipeCase{'a hook with no version', broken('', ' #:post-install (lambda () (run "true"))'), '#:post-install takes 1 parameter, not 0'}, RecipeCase{'an upgrade hook with one version', broken('', ' #:post-upgrade (lambda (new) (run "true"))'), '#:post-upgrade takes 2 parameters, not 1'}, RecipeCase{'a pkgver that is a number', broken('pkgver', ' #:pkgver 3'), '#:pkgver must be a string or a (lambda ...)'}, RecipeCase{'a pkgrel in quotes', broken('pkgrel', ' #:pkgrel "1"'), '#:pkgrel must be an integer'}, RecipeCase{'a negative epoch', broken('', ' #:epoch -1'), 'epoch is negative'}, RecipeCase{'a pkgdesc that is a number', broken('pkgdesc', ' #:pkgdesc 5'), '#:pkgdesc must be a string'}, RecipeCase{'an unknown key', broken('', ' #:pkgdescription "x"'), 'has no key #:pkgdescription'}, RecipeCase{'a dotted list', broken('', ' #:depends (cons "a" "b")'), 'a list ends in ()'}, RecipeCase{'no pkgdesc', broken('pkgdesc', ''), '#:pkgdesc is required'}, RecipeCase{'no package', broken('package', ' #:url "https://example.org"'), '#:package is required'}, ] { p := os.join_path(dir, 'case.scm') os.write_file(p, c.text)! recipe_load(p) or { assert err.msg().contains(c.says), '${c.what}: ${err.msg()}' continue } assert false, '${c.what} was taken:\n${c.text}' } // the right lengths and SKIP pass ok := os.join_path(dir, 'sums.scm') os.write_file(ok, broken('', ' #:sha256sums \'("${hex64}" "SKIP")'))! recipe_load(ok)! } fn test_split_recipe_metadata() ! { // the split smoke package: two pkgnames, one shared package lambda that // asks (this-pkg) which split it runs for recipe := os.join_path(@VMODROOT, 'pkgs', 'schemesplit', 'PKGBUILD.scm') desc := native_description(recipe_load(recipe)!)! assert desc.spec.pkgnames == ['schemesplit', 'schemesplit-doc'] assert desc.phases == ['package'] assert desc.spec.hooks.len == 0 } fn test_recipe_without_a_build_phase() ! { // data-only recipes only declare the package phase dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_nobuild_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } p := os.join_path(dir, 'data.scm') os.write_file(p, broken('build', ''))! assert native_description(recipe_load(p)!)!.phases == ['package'] } fn test_native_hook_keeps_helpers_and_literal_process_arguments() ! { dir := private_dir(os.vtmp_dir())! defer { os.rmdir_all(dir) or { eprintln('remove hook test: ${err}') } } source := os.join_path(dir, 'recipe') os.mkdir(source)! path := os.join_path(source, 'PKGBUILD.scm') os.write_file(os.join_path(source, 'helper.scm'), '(define (record value) (run "touch" value))')! os.write_file(path, '(load "helper.scm") (defpkgbuild demo #:pkgname "demo" #:pkgver "1" #:pkgrel 1 #:pkgdesc "test" #:arch (list "any") #:package (lambda () #t) #:post-install (lambda (v) (record v)))')! hook := write_recipe_hook(path, dir, ['helper.scm'])! os.rmdir_all(source)! message := os.join_path(dir, 'space ; $HOME *') old := os.getenv_opt('VPAC_PKGNAME') defer { if val := old { os.setenv('VPAC_PKGNAME', val, true) } else { os.unsetenv('VPAC_PKGNAME') } } package_hook([hook, '--nspkg-hook', 'post-install', 'demo', message])! assert os.is_file(message) } fn test_native_sources_reject_checksum_before_unpacking() ! { dir := private_dir(os.vtmp_dir())! defer { os.rmdir_all(dir) or { eprintln('remove source test: ${err}') } } src := os.join_path(dir, 'src') os.mkdir(src)! os.write_file(os.join_path(dir, 'one'), 'package contents')! native_sources(Spec{ source: ['one'], sha256sums: ['0'.repeat(64)] }, dir, src) or { assert err.msg().contains('sha256 mismatch') assert os.ls(src)!.len == 0 return } assert false, 'bad checksum accepted' }