module vmakepkg import os // the generated head's spellings, checked against makepkg fn test_lip_arch() { assert lip_arch('x86-64') == 'x86_64' assert lip_arch('any') == 'any' assert lip_arch('x86') == 'x86' } fn test_lip_option() { assert lip_option('no-strip') == '!strip' assert lip_option('docs') == 'docs' assert lip_option('zipman') == 'zipman' assert lip_option('no-docs') == '!docs' } fn test_lip_hook_arity() { assert lip_hook_arity('pre-install') == 1 assert lip_hook_arity('post-remove') == 1 assert lip_hook_arity('pre-upgrade') == 2 assert lip_hook_arity('post-upgrade') == 2 } fn test_lip_fn_name() { assert lip_fn_name('pre-install') == 'pre_install' } 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 } } // lip_make turns one lip into the PKGBUILD (and the .install) makepkg runs: // its head is the head a real PKGBUILD spells, its phases delegate to the // runner, and pkgver stays a static one when the lip's is fn test_lip_make_golden() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_make_golden_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } lip := os.join_path(dir, 'tiny.lip') lines := [ '(defpkgbuild tiny', ' :pkgname "tiny"', ' :pkgver "1.0"', ' :pkgrel 3', ' :pkgdesc "tiny golden package"', " :arch '(:any)", ' :depends \'("bash")', " :options '(:no-strip :docs)", ' :build', ' (lambda () (run "true"))', ' :package', ' (lambda () (run "true"))', ' :post-install', ' (lambda (newver) (echo "done")))', ] os.write_file(lip, lines.join('\n'))! bb := lip_make(lip, '/vpac/runner')! assert bb.base == 'tiny' assert bb.dynamic == false // the identity, spelled the way makepkg reads it assert bb.text.contains("pkgname='tiny'") assert bb.text.contains("pkgver='1.0'") assert bb.text.contains('pkgrel=3') assert bb.text.contains("pkgdesc='tiny golden package'") assert bb.text.contains("arch=(\n 'any'\n)") // lists carry one quoted entry a line, and the lip's :no-x becomes // makepkg's !x assert bb.text.contains('depends=(') && bb.text.contains(" 'bash'") assert bb.text.contains("options=(\n '!strip'\n 'docs'\n)") // the phases call this binary back with the lip they came from assert bb.text.contains("VPAC_BLIP_RUN='/vpac/runner'") assert bb.text.contains("VPAC_PKG_LIP='${lip}'") assert bb.text.contains('build() {\n export') assert bb.text.contains('package() {\n export') // a static pkgver runs no pkgver() assert !bb.text.contains('pkgver()') // the hooks are the .install the head names, run through the runner assert bb.install.starts_with('#!/bin/sh') assert bb.text.contains("install='tiny.install'") // libalpm sources the .install in a shell of its own: the runner and // the lip stand in it literally, nothing is read from the PKGBUILD's // variables, and a missing one is said assert bb.install.contains("VPAC_PKGNAME='tiny' '/vpac/runner' --lip-run '${lip}' post-install \"\$1\"\n"), bb.install assert bb.install.contains("if [ ! -x '/vpac/runner' ] || [ ! -r '${lip}' ]; then") assert !bb.install.contains('\$VPAC_') && !bb.install.contains('\$pkgdir') // and the function is sh that runs: with no runner there it fails aloud install_file := os.join_path(dir, 'tiny.install') os.write_file(install_file, bb.install)! hook := os.exec(['sh', '-c', '. "\$0"; post_install 1.0 2>&1', install_file]) assert hook.exit_code == 1 && hook.output.contains('one of them is gone'), hook.output } // a pkgver lambda makes the package a VCS one: pkgver() is generated for // makepkg to call, and the head carries the placeholder it replaces fn test_lip_make_dynamic_pkgver() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_make_dyn_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } lip := os.join_path(dir, 'tiny.lip') lines := [ '(defpkgbuild tiny', ' :pkgname "tiny"', ' :pkgver (lambda () "unused")', ' :pkgrel 1', ' :pkgdesc "tiny vcs package"', " :arch '(:any)", ' :build', ' (lambda () (run "true"))', ' :package', ' (lambda () (run "true")))', ] os.write_file(lip, lines.join('\n'))! bb := lip_make(lip, '/vpac/runner')! assert bb.dynamic assert bb.text.contains("pkgver='0'") assert bb.text.contains('pkgver() {\n export') } // lip_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_lip_defpkg_count() { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_count_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } p := os.join_path(dir, 'count.lip') os.write_file(p, '(defpkgbuild foo\n)')! assert lip_defpkg_count(p)! == 1 os.write_file(p, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')! assert lip_defpkg_count(p)! == 2 os.write_file(p, ';; a comment names (defpkgbuild x)\n(defpkgbuild foo\n)')! assert lip_defpkg_count(p)! == 1 os.write_file(p, '(defpkgbuild foo ; inline ends the text (defpkgbuild y)\n)')! assert lip_defpkg_count(p)! == 1 os.write_file(p, '#| (defpkgbuild z) reduces the block |#\n(defpkgbuild foo\n)')! assert lip_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 lip_defpkg_count(p)! == 1 os.write_file(p, '(defpkg-build foo\n)')! assert lip_defpkg_count(p)! == 0 os.write_file(p, '(defpkgbuildy foo\n)')! assert lip_defpkg_count(p)! == 0 } // the name pre-bound is the real form's: one named in a comment or a string // before it binds nothing, and the name runs to the symbol's end fn test_lip_defpkg_name() { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_name_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } p := os.join_path(dir, 'name.lip') os.write_file(p, '(defpkgbuild foo-bar\n)')! assert lip_defpkg_name(p) == 'foo-bar' os.write_file(p, ';; write (defpkgbuild wrong ...) below\n#| (defpkgbuild also-wrong) |#\n(defpkgbuild right\n)')! assert lip_defpkg_name(p) == 'right' os.write_file(p, '(defpkgbuild lib.so+x@1)')! assert lip_defpkg_name(p) == 'lib.so+x@1' os.write_file(p, '(defpkgbuild "quoted"\n)')! assert lip_defpkg_name(p) == '' os.write_file(p, ';; no form at all')! assert lip_defpkg_name(p) == '' } fn test_lip_load_unreadable() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_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.lip'), dir] { lip_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.lip') os.write_file(two, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')! lip_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 lip_items_of makes of a blip expression's value fn read_items(src string) ![]string { mut m := lip_new_machine()! return lip_items_of(m.run_str(src)!, 'depends')! } fn test_lip_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. // blip's reader has no dotted syntax ('.' is a symbol), so cons makes one 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_lip_vector_lists_reach_the_pkgbuild() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_vector_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } // the lists spelled as vectors, where the golden lip quotes them lip := os.join_path(dir, 'vec.lip') 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(lip, lines.join('\n'))! bb := lip_make(lip, '/vpac/runner')! // the generator writes an array an item a line assert bb.text.contains("depends=(\n 'glibc'\n 'zsh'\n)"), bb.text assert bb.text.contains("arch=(\n 'any'\n)"), bb.text assert bb.text.contains("license=(\n 'MIT'\n)"), bb.text } // valid_lip is a lip every field of which is right; the corpus below breaks // one thing at a time const valid_lip = ['(defpkgbuild corpus', ' :pkgname "corpus"', ' :pkgver "1.0"', ' :pkgrel 1', ' :pkgdesc "a corpus base"', " :arch '(:any)", ' :build (lambda () (run "true"))', ' :package (lambda () (run "true")))'] // LipCase is a lip broken one way, and what the validator says about it struct LipCase { what string text string says string } // broken replaces the line of valid_lip 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_lip.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_lip_validation_corpus() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_lip_corpus_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } // the base itself is good base := os.join_path(dir, 'base.lip') os.write_file(base, valid_lip.join('\n'))! lip_make(base, '/vpac/runner')! hex64 := 'ab'.repeat(32) for c in [ LipCase{'no name', '(defpkgbuild)', 'needs the name'}, LipCase{'a number for a name', valid_lip.join('\n').replace('(defpkgbuild corpus', '(defpkgbuild 7'), 'the name is a symbol or a string, not'}, LipCase{'a key without its value', broken('', ' :url'), ':key and value come in pairs'}, LipCase{'a value where a key goes', broken('', ' "stray" "pair"'), 'expected a :key, got "stray"'}, LipCase{'a key twice', broken('', ' :pkgrel 2'), ':pkgrel is set twice'}, LipCase{'an empty name list', broken('pkgname', " :pkgname '()"), ':pkgname must not be empty'}, LipCase{'a name with a space', broken('pkgname', ' :pkgname "cor pus"'), 'has whitespace in it'}, LipCase{'no arch', broken('arch', " :arch '()"), ':arch must not be empty'}, LipCase{'a short b2sum', broken('', ' :b2sums \'("abc")'), 'b2sums: abc is 3 characters; b2sums entries are 128 hex characters or SKIP'}, LipCase{'a sha256 that is not hex', broken('', ' :sha256sums \'("${'zz'.repeat(32)}")'), 'has a character that is not hex (z)'}, LipCase{'a short fingerprint', broken('', ' :validpgpkeys \'("ABCDEF")'), 'validpgpkeys: entry 0 is 6 characters'}, LipCase{'a lowercase fingerprint', broken('', ' :validpgpkeys \'("${'ab'.repeat(20)}")'), 'uppercase fingerprints only'}, LipCase{'an absolute backup', broken('', ' :backup \'("/etc/x.conf")'), 'is absolute; backup paths are relative'}, LipCase{'a phase that is no lambda', broken('build', ' :build "make"'), ':build must be a (lambda ...)'}, LipCase{'a phase with a parameter', broken('build', ' :build (lambda (x) (run "true"))'), ':build takes 0 parameters, not 1'}, LipCase{'a hook with no version', broken('', ' :post-install (lambda () (run "true"))'), ':post-install takes 1 parameter, not 0'}, LipCase{'an upgrade hook with one version', broken('', ' :post-upgrade (lambda (new) (run "true"))'), ':post-upgrade takes 2 parameters, not 1'}, LipCase{'a pkgver that is a number', broken('pkgver', ' :pkgver 3'), ':pkgver must be a string or a (lambda ...)'}, LipCase{'a pkgrel in quotes', broken('pkgrel', ' :pkgrel "1"'), ':pkgrel must be an integer'}, LipCase{'a negative epoch', broken('', ' :epoch -1'), 'epoch is negative'}, LipCase{'a pkgdesc that is a number', broken('pkgdesc', ' :pkgdesc 5'), ':pkgdesc must be a string'}, LipCase{'an unknown key', broken('', ' :pkgdescription "x"'), 'has no key :pkgdescription'}, LipCase{'a dotted list', broken('', ' :depends (cons "a" "b")'), 'a list ends in ()'}, LipCase{'no pkgdesc', broken('pkgdesc', ''), ':pkgdesc is required'}, LipCase{'no package', broken('package', ' :url "https://example.org"'), ':package is required'}, ] { p := os.join_path(dir, 'case.lip') os.write_file(p, c.text)! lip_make(p, '/vpac/runner') 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.lip') os.write_file(ok, broken('', ' :sha256sums \'("${hex64}" "SKIP")'))! lip_make(ok, '/vpac/runner')! } fn test_lip_make_split_golden() ! { // the split smoke package: two pkgnames, one shared package lambda that // asks (this-pkg) which split it runs for lip := os.join_path(@VMODROOT, 'pkgs', 'lipsplit', 'PKGBUILD.lip') bb := lip_make(lip, '/vpac/runner')! assert bb.base == 'lipsplit' assert bb.text.contains("pkgbase='lipsplit'"), bb.text assert bb.text.contains("pkgname=(\n 'lipsplit'\n 'lipsplit-doc'\n)"), bb.text // one package function per split, named as makepkg calls it // (package_$pkgname, the hyphen and all), each running the shared lambda for name in ['lipsplit', 'lipsplit-doc'] { assert bb.text.contains('package_${name}() {'), bb.text } assert !bb.text.contains('\npackage() {'), bb.text // no build phase in the lip: none in the PKGBUILD, which makepkg allows assert !bb.text.contains('build() {'), bb.text assert bb.install == '' } fn test_lip_without_a_build_phase() ! { // a data package: no :build, as makepkg allows; the PKGBUILD gets no // build() wrapper, and still its package() dir := os.join_path(os.vtmp_dir(), 'vpac_lip_nobuild_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } p := os.join_path(dir, 'data.lip') os.write_file(p, broken('build', ''))! bb := lip_make(p, '/vpac/runner')! assert !bb.text.contains('build() {'), bb.text assert bb.text.contains('package() {'), bb.text }