| 1 | module vmakepkg |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | // the generated head's spellings, checked against makepkg |
| 6 | fn test_lip_arch() { |
| 7 | assert lip_arch('x86-64') == 'x86_64' |
| 8 | assert lip_arch('any') == 'any' |
| 9 | assert lip_arch('x86') == 'x86' |
| 10 | } |
| 11 | |
| 12 | fn test_lip_option() { |
| 13 | assert lip_option('no-strip') == '!strip' |
| 14 | assert lip_option('docs') == 'docs' |
| 15 | assert lip_option('zipman') == 'zipman' |
| 16 | assert lip_option('no-docs') == '!docs' |
| 17 | } |
| 18 | |
| 19 | fn test_lip_hook_arity() { |
| 20 | assert lip_hook_arity('pre-install') == 1 |
| 21 | assert lip_hook_arity('post-remove') == 1 |
| 22 | assert lip_hook_arity('pre-upgrade') == 2 |
| 23 | assert lip_hook_arity('post-upgrade') == 2 |
| 24 | } |
| 25 | |
| 26 | fn test_lip_fn_name() { |
| 27 | assert lip_fn_name('pre-install') == 'pre_install' |
| 28 | } |
| 29 | |
| 30 | fn test_shell_quote() { |
| 31 | assert shell_quote("a 'b' c") == "'a '\\''b'\\'' c'" |
| 32 | assert shell_quote('plain') == "'plain'" |
| 33 | } |
| 34 | |
| 35 | fn test_shell_quote_round_trips_through_sh() { |
| 36 | // the property that matters: whatever the value holds, a shell reads it |
| 37 | // back as the one word it was — no expansion, no split, no command |
| 38 | for value in ["it's", '\$HOME', '`id`', '\$(id)', 'a b\tc', 'semi;colon', 'new\nline', 'back\\slash', |
| 39 | '"double"', "'", "''", '*', '-n', '', 'ünï'] { |
| 40 | res := os.exec(['sh', '-c', 'printf %s ' + shell_quote(value)]) |
| 41 | assert res.exit_code == 0, value |
| 42 | assert res.output == value, value |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // lip_make turns one lip into the PKGBUILD (and the .install) makepkg runs: |
| 47 | // its head is the head a real PKGBUILD spells, its phases delegate to the |
| 48 | // runner, and pkgver stays a static one when the lip's is |
| 49 | fn test_lip_make_golden() ! { |
| 50 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_make_golden_${os.getpid()}') |
| 51 | os.mkdir_all(dir)! |
| 52 | defer { |
| 53 | os.rmdir_all(dir) or {} |
| 54 | } |
| 55 | lip := os.join_path(dir, 'tiny.lip') |
| 56 | lines := [ |
| 57 | '(defpkgbuild tiny', |
| 58 | ' :pkgname "tiny"', |
| 59 | ' :pkgver "1.0"', |
| 60 | ' :pkgrel 3', |
| 61 | ' :pkgdesc "tiny golden package"', |
| 62 | " :arch '(:any)", |
| 63 | ' :depends \'("bash")', |
| 64 | " :options '(:no-strip :docs)", |
| 65 | ' :build', |
| 66 | ' (lambda () (run "true"))', |
| 67 | ' :package', |
| 68 | ' (lambda () (run "true"))', |
| 69 | ' :post-install', |
| 70 | ' (lambda (newver) (echo "done")))', |
| 71 | ] |
| 72 | os.write_file(lip, lines.join('\n'))! |
| 73 | |
| 74 | bb := lip_make(lip, '/vpac/runner')! |
| 75 | assert bb.base == 'tiny' |
| 76 | assert bb.dynamic == false |
| 77 | // the identity, spelled the way makepkg reads it |
| 78 | assert bb.text.contains("pkgname='tiny'") |
| 79 | assert bb.text.contains("pkgver='1.0'") |
| 80 | assert bb.text.contains('pkgrel=3') |
| 81 | assert bb.text.contains("pkgdesc='tiny golden package'") |
| 82 | assert bb.text.contains("arch=(\n 'any'\n)") |
| 83 | // lists carry one quoted entry a line, and the lip's :no-x becomes |
| 84 | // makepkg's !x |
| 85 | assert bb.text.contains('depends=(') && bb.text.contains(" 'bash'") |
| 86 | assert bb.text.contains("options=(\n '!strip'\n 'docs'\n)") |
| 87 | // the phases call this binary back with the lip they came from |
| 88 | assert bb.text.contains("VPAC_BLIP_RUN='/vpac/runner'") |
| 89 | assert bb.text.contains("VPAC_PKG_LIP='${lip}'") |
| 90 | assert bb.text.contains('build() {\n export') |
| 91 | assert bb.text.contains('package() {\n export') |
| 92 | // a static pkgver runs no pkgver() |
| 93 | assert !bb.text.contains('pkgver()') |
| 94 | // the hooks are the .install the head names, run through the runner |
| 95 | assert bb.install.starts_with('#!/bin/sh') |
| 96 | assert bb.text.contains("install='tiny.install'") |
| 97 | // libalpm sources the .install in a shell of its own: the runner and |
| 98 | // the lip stand in it literally, nothing is read from the PKGBUILD's |
| 99 | // variables, and a missing one is said |
| 100 | assert bb.install.contains("VPAC_PKGNAME='tiny' '/vpac/runner' --lip-run '${lip}' post-install \"\$1\"\n"), bb.install |
| 101 | assert bb.install.contains("if [ ! -x '/vpac/runner' ] || [ ! -r '${lip}' ]; then") |
| 102 | assert !bb.install.contains('\$VPAC_') && !bb.install.contains('\$pkgdir') |
| 103 | // and the function is sh that runs: with no runner there it fails aloud |
| 104 | install_file := os.join_path(dir, 'tiny.install') |
| 105 | os.write_file(install_file, bb.install)! |
| 106 | hook := os.exec(['sh', '-c', '. "\$0"; post_install 1.0 2>&1', install_file]) |
| 107 | assert hook.exit_code == 1 && hook.output.contains('one of them is gone'), hook.output |
| 108 | } |
| 109 | |
| 110 | // a pkgver lambda makes the package a VCS one: pkgver() is generated for |
| 111 | // makepkg to call, and the head carries the placeholder it replaces |
| 112 | fn test_lip_make_dynamic_pkgver() ! { |
| 113 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_make_dyn_${os.getpid()}') |
| 114 | os.mkdir_all(dir)! |
| 115 | defer { |
| 116 | os.rmdir_all(dir) or {} |
| 117 | } |
| 118 | lip := os.join_path(dir, 'tiny.lip') |
| 119 | lines := [ |
| 120 | '(defpkgbuild tiny', |
| 121 | ' :pkgname "tiny"', |
| 122 | ' :pkgver (lambda () "unused")', |
| 123 | ' :pkgrel 1', |
| 124 | ' :pkgdesc "tiny vcs package"', |
| 125 | " :arch '(:any)", |
| 126 | ' :build', |
| 127 | ' (lambda () (run "true"))', |
| 128 | ' :package', |
| 129 | ' (lambda () (run "true")))', |
| 130 | ] |
| 131 | os.write_file(lip, lines.join('\n'))! |
| 132 | bb := lip_make(lip, '/vpac/runner')! |
| 133 | assert bb.dynamic |
| 134 | assert bb.text.contains("pkgver='0'") |
| 135 | assert bb.text.contains('pkgver() {\n export') |
| 136 | } |
| 137 | |
| 138 | // lip_defpkg_count counts real (defpkgbuild ...) forms: comments of either |
| 139 | // kind stay out, block comments stay out, a string's mention of the form |
| 140 | // stays out, and a longer symbol whose name begins equally is no form |
| 141 | fn test_lip_defpkg_count() { |
| 142 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_count_${os.getpid()}') |
| 143 | os.mkdir_all(dir)! |
| 144 | defer { |
| 145 | os.rmdir_all(dir) or {} |
| 146 | } |
| 147 | p := os.join_path(dir, 'count.lip') |
| 148 | os.write_file(p, '(defpkgbuild foo\n)')! |
| 149 | assert lip_defpkg_count(p)! == 1 |
| 150 | os.write_file(p, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')! |
| 151 | assert lip_defpkg_count(p)! == 2 |
| 152 | os.write_file(p, ';; a comment names (defpkgbuild x)\n(defpkgbuild foo\n)')! |
| 153 | assert lip_defpkg_count(p)! == 1 |
| 154 | os.write_file(p, '(defpkgbuild foo ; inline ends the text (defpkgbuild y)\n)')! |
| 155 | assert lip_defpkg_count(p)! == 1 |
| 156 | os.write_file(p, '#| (defpkgbuild z) reduces the block |#\n(defpkgbuild foo\n)')! |
| 157 | assert lip_defpkg_count(p)! == 1 |
| 158 | // a string's mention of the form stays out: the ('no form counts') text |
| 159 | // spells one whole (defpkgbuild ...) inside a string |
| 160 | os.write_file(p, '(defpkgbuild foo\n :defpkgdesc "the string names (defpkgbuild x) whole"\n)')! |
| 161 | assert lip_defpkg_count(p)! == 1 |
| 162 | os.write_file(p, '(defpkg-build foo\n)')! |
| 163 | assert lip_defpkg_count(p)! == 0 |
| 164 | os.write_file(p, '(defpkgbuildy foo\n)')! |
| 165 | assert lip_defpkg_count(p)! == 0 |
| 166 | } |
| 167 | |
| 168 | // the name pre-bound is the real form's: one named in a comment or a string |
| 169 | // before it binds nothing, and the name runs to the symbol's end |
| 170 | fn test_lip_defpkg_name() { |
| 171 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_name_${os.getpid()}') |
| 172 | os.mkdir_all(dir)! |
| 173 | defer { |
| 174 | os.rmdir_all(dir) or {} |
| 175 | } |
| 176 | p := os.join_path(dir, 'name.lip') |
| 177 | os.write_file(p, '(defpkgbuild foo-bar\n)')! |
| 178 | assert lip_defpkg_name(p) == 'foo-bar' |
| 179 | os.write_file(p, ';; write (defpkgbuild wrong ...) below\n#| (defpkgbuild also-wrong) |#\n(defpkgbuild right\n)')! |
| 180 | assert lip_defpkg_name(p) == 'right' |
| 181 | os.write_file(p, '(defpkgbuild lib.so+x@1)')! |
| 182 | assert lip_defpkg_name(p) == 'lib.so+x@1' |
| 183 | os.write_file(p, '(defpkgbuild "quoted"\n)')! |
| 184 | assert lip_defpkg_name(p) == '' |
| 185 | os.write_file(p, ';; no form at all')! |
| 186 | assert lip_defpkg_name(p) == '' |
| 187 | } |
| 188 | |
| 189 | fn test_lip_load_unreadable() ! { |
| 190 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_unreadable_${os.getpid()}') |
| 191 | os.mkdir_all(dir)! |
| 192 | defer { |
| 193 | os.rmdir_all(dir) or {} |
| 194 | } |
| 195 | // no file at all, and a directory where the file should be: each fails |
| 196 | // with the read's reason and the path, not a later, other complaint |
| 197 | for p in [os.join_path(dir, 'missing.lip'), dir] { |
| 198 | lip_load(p) or { |
| 199 | assert err.msg().starts_with('${p}: '), err.msg() |
| 200 | assert !err.msg().contains('defpkgbuild'), err.msg() |
| 201 | continue |
| 202 | } |
| 203 | assert false, '${p} loaded' |
| 204 | } |
| 205 | // two forms in a readable file are still the one-form error |
| 206 | two := os.join_path(dir, 'two.lip') |
| 207 | os.write_file(two, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')! |
| 208 | lip_load(two) or { |
| 209 | assert err.msg().contains('one (defpkgbuild ...) form per file, not 2'), err.msg() |
| 210 | return |
| 211 | } |
| 212 | assert false, 'two forms loaded' |
| 213 | } |
| 214 | |
| 215 | // read_items reads what lip_items_of makes of a blip expression's value |
| 216 | fn read_items(src string) ![]string { |
| 217 | mut m := lip_new_machine()! |
| 218 | return lip_items_of(m.run_str(src)!, 'depends')! |
| 219 | } |
| 220 | |
| 221 | fn test_lip_items_of_every_spelling() ! { |
| 222 | // one item, as a string, a keyword or a symbol |
| 223 | assert read_items('"glibc"')! == ['glibc'] |
| 224 | assert read_items(':any')! == ['any'] |
| 225 | assert read_items("'zsh")! == ['zsh'] |
| 226 | // a quoted list, a vector, and the empty one |
| 227 | assert read_items('\'("a" "b" :c)')! == ['a', 'b', 'c'] |
| 228 | assert read_items('["a" "b" :c]')! == ['a', 'b', 'c'] |
| 229 | assert read_items("'()")! == []string{} |
| 230 | assert read_items('[]')! == []string{} |
| 231 | // a list's items are plain: a number among them is said, in both kinds |
| 232 | for src in ['\'("a" 1)', '["a" 1]'] { |
| 233 | read_items(src) or { |
| 234 | assert err.msg().contains('items must be strings or keywords'), err.msg() |
| 235 | continue |
| 236 | } |
| 237 | assert false, '${src} was read' |
| 238 | } |
| 239 | // a dotted tail is no list: its last item would be dropped silently. |
| 240 | // blip's reader has no dotted syntax ('.' is a symbol), so cons makes one |
| 241 | read_items('(cons "a" "b")') or { |
| 242 | assert err.msg().contains('a list ends in ()'), err.msg() |
| 243 | return |
| 244 | } |
| 245 | assert false, 'a dotted list was read' |
| 246 | } |
| 247 | |
| 248 | fn test_lip_vector_lists_reach_the_pkgbuild() ! { |
| 249 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_vector_${os.getpid()}') |
| 250 | os.mkdir_all(dir)! |
| 251 | defer { |
| 252 | os.rmdir_all(dir) or {} |
| 253 | } |
| 254 | // the lists spelled as vectors, where the golden lip quotes them |
| 255 | lip := os.join_path(dir, 'vec.lip') |
| 256 | lines := [ |
| 257 | '(defpkgbuild vec', |
| 258 | ' :pkgname "vec"', |
| 259 | ' :pkgver "1.0"', |
| 260 | ' :pkgrel 1', |
| 261 | ' :pkgdesc "vector lists"', |
| 262 | ' :arch [:any]', |
| 263 | ' :depends ["glibc" "zsh"]', |
| 264 | ' :license ["MIT"]', |
| 265 | ' :build (lambda () (run "true"))', |
| 266 | ' :package (lambda () (run "true")))', |
| 267 | ] |
| 268 | os.write_file(lip, lines.join('\n'))! |
| 269 | bb := lip_make(lip, '/vpac/runner')! |
| 270 | // the generator writes an array an item a line |
| 271 | assert bb.text.contains("depends=(\n 'glibc'\n 'zsh'\n)"), bb.text |
| 272 | assert bb.text.contains("arch=(\n 'any'\n)"), bb.text |
| 273 | assert bb.text.contains("license=(\n 'MIT'\n)"), bb.text |
| 274 | } |
| 275 | |
| 276 | // valid_lip is a lip every field of which is right; the corpus below breaks |
| 277 | // one thing at a time |
| 278 | const valid_lip = ['(defpkgbuild corpus', ' :pkgname "corpus"', ' :pkgver "1.0"', ' :pkgrel 1', |
| 279 | ' :pkgdesc "a corpus base"', " :arch '(:any)", ' :build (lambda () (run "true"))', |
| 280 | ' :package (lambda () (run "true")))'] |
| 281 | |
| 282 | // LipCase is a lip broken one way, and what the validator says about it |
| 283 | struct LipCase { |
| 284 | what string |
| 285 | text string |
| 286 | says string |
| 287 | } |
| 288 | |
| 289 | // broken replaces the line of valid_lip that starts with key's spelling, or |
| 290 | // appends extra before the closing parenthesis when key is '' |
| 291 | fn broken(key string, line string) string { |
| 292 | mut lines := valid_lip.clone() |
| 293 | if key == '' { |
| 294 | // the last line ends in defpkgbuild's own ')': the new line takes it |
| 295 | last := lines[lines.len - 1] |
| 296 | lines[lines.len - 1] = last[..last.len - 1] |
| 297 | lines << line + ')' |
| 298 | return lines.join('\n') |
| 299 | } |
| 300 | for i, l in lines { |
| 301 | if l.trim_space().starts_with(':${key} ') { |
| 302 | lines[i] = line |
| 303 | if i == lines.len - 1 { |
| 304 | lines[i] += ')' |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | return lines.join('\n') |
| 309 | } |
| 310 | |
| 311 | fn test_lip_validation_corpus() ! { |
| 312 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_corpus_${os.getpid()}') |
| 313 | os.mkdir_all(dir)! |
| 314 | defer { |
| 315 | os.rmdir_all(dir) or {} |
| 316 | } |
| 317 | // the base itself is good |
| 318 | base := os.join_path(dir, 'base.lip') |
| 319 | os.write_file(base, valid_lip.join('\n'))! |
| 320 | lip_make(base, '/vpac/runner')! |
| 321 | |
| 322 | hex64 := 'ab'.repeat(32) |
| 323 | for c in [ |
| 324 | LipCase{'no name', '(defpkgbuild)', 'needs the name'}, |
| 325 | LipCase{'a number for a name', valid_lip.join('\n').replace('(defpkgbuild corpus', '(defpkgbuild 7'), 'the name is a symbol or a string, not'}, |
| 326 | LipCase{'a key without its value', broken('', ' :url'), ':key and value come in pairs'}, |
| 327 | LipCase{'a value where a key goes', broken('', ' "stray" "pair"'), 'expected a :key, got "stray"'}, |
| 328 | LipCase{'a key twice', broken('', ' :pkgrel 2'), ':pkgrel is set twice'}, |
| 329 | LipCase{'an empty name list', broken('pkgname', " :pkgname '()"), ':pkgname must not be empty'}, |
| 330 | LipCase{'a name with a space', broken('pkgname', ' :pkgname "cor pus"'), 'has whitespace in it'}, |
| 331 | LipCase{'no arch', broken('arch', " :arch '()"), ':arch must not be empty'}, |
| 332 | LipCase{'a short b2sum', broken('', ' :b2sums \'("abc")'), 'b2sums: abc is 3 characters; b2sums entries are 128 hex characters or SKIP'}, |
| 333 | LipCase{'a sha256 that is not hex', broken('', ' :sha256sums \'("${'zz'.repeat(32)}")'), 'has a character that is not hex (z)'}, |
| 334 | LipCase{'a short fingerprint', broken('', ' :validpgpkeys \'("ABCDEF")'), 'validpgpkeys: entry 0 is 6 characters'}, |
| 335 | LipCase{'a lowercase fingerprint', broken('', ' :validpgpkeys \'("${'ab'.repeat(20)}")'), 'uppercase fingerprints only'}, |
| 336 | LipCase{'an absolute backup', broken('', ' :backup \'("/etc/x.conf")'), 'is absolute; backup paths are relative'}, |
| 337 | LipCase{'a phase that is no lambda', broken('build', ' :build "make"'), ':build must be a (lambda ...)'}, |
| 338 | LipCase{'a phase with a parameter', broken('build', ' :build (lambda (x) (run "true"))'), ':build takes 0 parameters, not 1'}, |
| 339 | LipCase{'a hook with no version', broken('', ' :post-install (lambda () (run "true"))'), ':post-install takes 1 parameter, not 0'}, |
| 340 | LipCase{'an upgrade hook with one version', broken('', ' :post-upgrade (lambda (new) (run "true"))'), ':post-upgrade takes 2 parameters, not 1'}, |
| 341 | LipCase{'a pkgver that is a number', broken('pkgver', ' :pkgver 3'), ':pkgver must be a string or a (lambda ...)'}, |
| 342 | LipCase{'a pkgrel in quotes', broken('pkgrel', ' :pkgrel "1"'), ':pkgrel must be an integer'}, |
| 343 | LipCase{'a negative epoch', broken('', ' :epoch -1'), 'epoch is negative'}, |
| 344 | LipCase{'a pkgdesc that is a number', broken('pkgdesc', ' :pkgdesc 5'), ':pkgdesc must be a string'}, |
| 345 | LipCase{'an unknown key', broken('', ' :pkgdescription "x"'), 'has no key :pkgdescription'}, |
| 346 | LipCase{'a dotted list', broken('', ' :depends (cons "a" "b")'), 'a list ends in ()'}, |
| 347 | LipCase{'no pkgdesc', broken('pkgdesc', ''), ':pkgdesc is required'}, |
| 348 | LipCase{'no package', broken('package', ' :url "https://example.org"'), ':package is required'}, |
| 349 | ] { |
| 350 | p := os.join_path(dir, 'case.lip') |
| 351 | os.write_file(p, c.text)! |
| 352 | lip_make(p, '/vpac/runner') or { |
| 353 | assert err.msg().contains(c.says), '${c.what}: ${err.msg()}' |
| 354 | continue |
| 355 | } |
| 356 | assert false, '${c.what} was taken:\n${c.text}' |
| 357 | } |
| 358 | // the right lengths and SKIP pass |
| 359 | ok := os.join_path(dir, 'sums.lip') |
| 360 | os.write_file(ok, broken('', ' :sha256sums \'("${hex64}" "SKIP")'))! |
| 361 | lip_make(ok, '/vpac/runner')! |
| 362 | } |
| 363 | |
| 364 | fn test_lip_make_split_golden() ! { |
| 365 | // the split smoke package: two pkgnames, one shared package lambda that |
| 366 | // asks (this-pkg) which split it runs for |
| 367 | lip := os.join_path(@VMODROOT, 'pkgs', 'lipsplit', 'PKGBUILD.lip') |
| 368 | bb := lip_make(lip, '/vpac/runner')! |
| 369 | assert bb.base == 'lipsplit' |
| 370 | assert bb.text.contains("pkgbase='lipsplit'"), bb.text |
| 371 | assert bb.text.contains("pkgname=(\n 'lipsplit'\n 'lipsplit-doc'\n)"), bb.text |
| 372 | // one package function per split, named as makepkg calls it |
| 373 | // (package_$pkgname, the hyphen and all), each running the shared lambda |
| 374 | for name in ['lipsplit', 'lipsplit-doc'] { |
| 375 | assert bb.text.contains('package_${name}() {'), bb.text |
| 376 | } |
| 377 | assert !bb.text.contains('\npackage() {'), bb.text |
| 378 | // no build phase in the lip: none in the PKGBUILD, which makepkg allows |
| 379 | assert !bb.text.contains('build() {'), bb.text |
| 380 | assert bb.install == '' |
| 381 | } |
| 382 | |
| 383 | fn test_lip_without_a_build_phase() ! { |
| 384 | // a data package: no :build, as makepkg allows; the PKGBUILD gets no |
| 385 | // build() wrapper, and still its package() |
| 386 | dir := os.join_path(os.vtmp_dir(), 'vpac_lip_nobuild_${os.getpid()}') |
| 387 | os.mkdir_all(dir)! |
| 388 | defer { |
| 389 | os.rmdir_all(dir) or {} |
| 390 | } |
| 391 | p := os.join_path(dir, 'data.lip') |
| 392 | os.write_file(p, broken('build', ''))! |
| 393 | bb := lip_make(p, '/vpac/runner')! |
| 394 | assert !bb.text.contains('build() {'), bb.text |
| 395 | assert bb.text.contains('package() {'), bb.text |
| 396 | } |
| 397 | |