vpac / vmakepkg / recipe_test.v
413 lines · 389 sloc · 16.51 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module vmakepkg
2
3import os
4
5fn test_recipe_phases_load_helpers_and_receive_arguments() ! {
6 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_phases_${os.getpid()}')
7 os.mkdir_all(dir)!
8 cwd := os.getwd()
9 old_pkgdir := os.getenv_opt('VPAC_PKGDIR')
10 old_startdir := os.getenv_opt('VPAC_STARTDIR')
11 defer {
12 os.chdir(cwd) or { eprintln('restore test directory: ${err}') }
13 if value := old_pkgdir {
14 os.setenv('VPAC_PKGDIR', value, true)
15 } else {
16 os.unsetenv('VPAC_PKGDIR')
17 }
18 if value := old_startdir {
19 os.setenv('VPAC_STARTDIR', value, true)
20 } else {
21 os.unsetenv('VPAC_STARTDIR')
22 }
23 os.rmdir_all(dir) or { eprintln('remove test package: ${err}') }
24 }
25 os.setenv('VPAC_PKGDIR', dir, true)
26 os.setenv('VPAC_STARTDIR', dir, true)
27 os.chdir(dir)!
28 os.write_file('helper.scm', '(define (destination name) (string-append (pkgdir) "/" name))')!
29 os.write_file('one.txt', 'first\n')!
30 os.write_file('two.txt', 'second\n')!
31 recipe := os.join_path(dir, 'PKGBUILD.scm')
32 os.write_file(recipe, '(load "helper.scm")
33(defpkgbuild phases
34 #:pkgname "phases" #:pkgver "1.0" #:pkgrel 1
35 #:pkgdesc "phase execution" #:arch (list "any")
36 #:package (lambda ()
37 (for-each (lambda (file) (run "cp" file (destination (string-append file ".copy"))))
38 (glob "*.txt")))
39 #:post-install (lambda (version)
40 (run "touch" (destination version))))')!
41 recipe_load(recipe)!
42 assert !os.exists('one.txt.copy')
43 recipe_run_phase(recipe, 'package', [])!
44 assert os.read_file('one.txt.copy')! == 'first\n'
45 assert os.read_file('two.txt.copy')! == 'second\n'
46 version := '1.0; touch injected'
47 recipe_run_phase(recipe, 'post-install', [version])!
48 assert os.is_file(version)
49 assert !os.exists('injected')
50 recipe_run_phase(recipe, 'post-install', []) or { return }
51 assert false, 'a hook ran without its version argument'
52}
53
54fn test_recipe_rejects_malformed_metadata_calls() ! {
55 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_metadata_${os.getpid()}')
56 os.mkdir_all(dir)!
57 defer { os.rmdir_all(dir) or { eprintln('remove test metadata: ${err}') } }
58 path := os.join_path(dir, 'PKGBUILD.scm')
59 for source in ['(vpac-defpkgbuild)', '(vpac-make-pkgbuild #() #())',
60 '(vpac-make-pkgbuild #("name") #())'] {
61 os.write_file(path, source)!
62 recipe_load(path) or { continue }
63 assert false, 'malformed package metadata loaded: ${source}'
64 }
65}
66
67// architecture and option spellings in package metadata
68fn test_recipe_arch() {
69 assert recipe_arch('x86-64') == 'x86_64'
70 assert recipe_arch('any') == 'any'
71 assert recipe_arch('x86') == 'x86'
72}
73
74fn test_recipe_option() {
75 assert recipe_option('no-strip') == '!strip'
76 assert recipe_option('docs') == 'docs'
77 assert recipe_option('zipman') == 'zipman'
78 assert recipe_option('no-docs') == '!docs'
79}
80
81fn test_recipe_hook_arity() {
82 assert recipe_hook_arity('pre-install') == 1
83 assert recipe_hook_arity('post-remove') == 1
84 assert recipe_hook_arity('pre-upgrade') == 2
85 assert recipe_hook_arity('post-upgrade') == 2
86}
87
88fn test_shell_quote() {
89 assert shell_quote("a 'b' c") == "'a '\\''b'\\'' c'"
90 assert shell_quote('plain') == "'plain'"
91}
92
93fn test_shell_quote_round_trips_through_sh() {
94 // the property that matters: whatever the value holds, a shell reads it
95 // back as the one word it was — no expansion, no split, no command
96 for value in ["it's", '\$HOME', '`id`', '\$(id)', 'a b\tc', 'semi;colon', 'new\nline', 'back\\slash',
97 '"double"', "'", "''", '*', '-n', '', 'ünï'] {
98 res := os.exec(['sh', '-c', 'printf %s ' + shell_quote(value)])
99 assert res.exit_code == 0, value
100 assert res.output == value, value
101 }
102}
103
104// recipe_defpkg_count counts real (defpkgbuild ...) forms: comments of either
105// kind stay out, block comments stay out, a string's mention of the form
106// stays out, and a longer symbol whose name begins equally is no form
107fn test_recipe_defpkg_count() {
108 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_count_${os.getpid()}')
109 os.mkdir_all(dir)!
110 defer {
111 os.rmdir_all(dir) or {}
112 }
113 p := os.join_path(dir, 'count.scm')
114 os.write_file(p, '(defpkgbuild foo\n)')!
115 assert recipe_defpkg_count(p)! == 1
116 os.write_file(p, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')!
117 assert recipe_defpkg_count(p)! == 2
118 os.write_file(p, ';; a comment names (defpkgbuild x)\n(defpkgbuild foo\n)')!
119 assert recipe_defpkg_count(p)! == 1
120 os.write_file(p, '(defpkgbuild foo ; inline ends the text (defpkgbuild y)\n)')!
121 assert recipe_defpkg_count(p)! == 1
122 os.write_file(p, '#| (defpkgbuild z) reduces the block |#\n(defpkgbuild foo\n)')!
123 assert recipe_defpkg_count(p)! == 1
124 // a string's mention of the form stays out: the ('no form counts') text
125 // spells one whole (defpkgbuild ...) inside a string
126 os.write_file(p, '(defpkgbuild foo\n #:defpkgdesc "the string names (defpkgbuild x) whole"\n)')!
127 assert recipe_defpkg_count(p)! == 1
128 os.write_file(p, '(defpkg-build foo\n)')!
129 assert recipe_defpkg_count(p)! == 0
130 os.write_file(p, '(defpkgbuildy foo\n)')!
131 assert recipe_defpkg_count(p)! == 0
132 os.write_file(p, '#;(defpkgbuild ignored)\n( defpkgbuild foo)')!
133 assert recipe_defpkg_count(p)! == 1
134 os.write_file(p, '(begin ( defpkgbuild foo) (\n defpkgbuild bar))')!
135 assert recipe_defpkg_count(p)! == 2
136 os.write_file(p, "'(defpkgbuild quoted)\n#((defpkgbuild literal))\n(|defpkgbuild| foo)")!
137 assert recipe_defpkg_count(p)! == 1
138 os.write_file(p, '#\\(\n#0=(begin . #0#)')!
139 assert recipe_defpkg_count(p)! == 0
140}
141
142fn test_recipe_load_uses_scheme_declarations() ! {
143 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_reader_${os.getpid()}')
144 os.mkdir_all(dir)!
145 defer { os.rmdir_all(dir) or { eprintln('remove test package: ${err}') } }
146 path := os.join_path(dir, 'PKGBUILD.scm')
147 source := valid_recipe.join('\n').replace('(defpkgbuild', '(\n defpkgbuild')
148 os.write_file(path, '#;(defpkgbuild ignored)\n' + source)!
149 assert recipe_load(path)!.items('pkgname')! == ['corpus']
150 os.write_file(path, source + '\n' + source)!
151 recipe_load(path) or {
152 assert err.msg().contains('one (defpkgbuild ...) form per file, not 2'), err.msg()
153 return
154 }
155 assert false, 'whitespace hid a second package declaration'
156}
157
158fn test_native_recipe_loads_helpers_and_validates_declarations() ! {
159 dir := os.join_path(os.vtmp_dir(), 'vpac_vile_recipe_${os.getpid()}')
160 os.mkdir_all(dir)!
161 defer { os.rmdir_all(dir) or { eprintln('remove test recipe: ${err}') } }
162 path := os.join_path(dir, 'PKGBUILD.vile')
163 os.write_file(os.join_path(dir, 'helper.vile'), 'version := "1.2"')!
164 source := 'load("helper.vile")
165defpkgbuild(native, #:pkgname, "native", #:pkgver, version,
166 #:pkgrel, 1, #:pkgdesc, "native recipe", #:arch, [#:any],
167 #:package, fn() {})'
168 os.write_file(path, source)!
169 assert native_description(recipe_load(path)!)!.spec.pkgver == '1.2'
170 os.write_file(path, source.replace('fn() {}', 'fn(unexpected) {}'))!
171 if _ := recipe_load(path) {
172 assert false, 'a package phase with an argument was accepted'
173 } else {
174 assert err.msg().contains('#:package takes 0 parameters'), err.msg()
175 }
176 os.write_file(path, source + '\n' + source)!
177 recipe_load(path) or {
178 assert err.msg().contains('form per file, not 2'), err.msg()
179 return
180 }
181 assert false, 'a second native package declaration was accepted'
182}
183
184fn test_recipe_load_unreadable() ! {
185 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_unreadable_${os.getpid()}')
186 os.mkdir_all(dir)!
187 defer {
188 os.rmdir_all(dir) or {}
189 }
190 // no file at all, and a directory where the file should be: each fails
191 // with the read's reason and the path, not a later, other complaint
192 for p in [os.join_path(dir, 'missing.scm'), dir] {
193 recipe_load(p) or {
194 assert err.msg().starts_with('${p}: '), err.msg()
195 assert !err.msg().contains('defpkgbuild'), err.msg()
196 continue
197 }
198 assert false, '${p} loaded'
199 }
200 // two forms in a readable file are still the one-form error
201 two := os.join_path(dir, 'two.scm')
202 os.write_file(two, '(defpkgbuild foo\n)\n(defpkgbuild bar\n)')!
203 recipe_load(two) or {
204 assert err.msg().contains('one (defpkgbuild ...) form per file, not 2'), err.msg()
205 return
206 }
207 assert false, 'two forms loaded'
208}
209
210// read_items reads what recipe_items_of makes of a scheme expression's value
211fn read_items(src string) ![]string {
212 mut m := recipe_new_machine()!
213 return recipe_items_of(m.eval(src)!, 'depends')!
214}
215
216fn test_recipe_items_of_every_spelling() ! {
217 // one item, as a string, a keyword or a symbol
218 assert read_items('"glibc"')! == ['glibc']
219 assert read_items('#:any')! == ['any']
220 assert read_items("'zsh")! == ['zsh']
221 // a quoted list, a vector, and the empty one
222 assert read_items('\'("a" "b" #:c)')! == ['a', 'b', 'c']
223 assert read_items('#("a" "b" #:c)')! == ['a', 'b', 'c']
224 assert read_items("'()")! == []string{}
225 assert read_items('#()')! == []string{}
226 // a list's items are plain: a number among them is said, in both kinds
227 for src in ['\'("a" 1)', '#("a" 1)'] {
228 read_items(src) or {
229 assert err.msg().contains('items must be strings or keywords'), err.msg()
230 continue
231 }
232 assert false, '${src} was read'
233 }
234 // a dotted tail is no list: its last item would be dropped silently.
235 // a cons with a non-list tail exercises the same rejection.
236 read_items('(cons "a" "b")') or {
237 assert err.msg().contains('a list ends in ()'), err.msg()
238 return
239 }
240 assert false, 'a dotted list was read'
241}
242
243fn test_recipe_vector_lists_reach_the_pkgbuild() ! {
244 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_vector_${os.getpid()}')
245 os.mkdir_all(dir)!
246 defer {
247 os.rmdir_all(dir) or {}
248 }
249 // the lists spelled as vectors, where the golden recipe quotes them
250 recipe := os.join_path(dir, 'vec.scm')
251 lines := [
252 '(defpkgbuild vec',
253 ' #:pkgname "vec"',
254 ' #:pkgver "1.0"',
255 ' #:pkgrel 1',
256 ' #:pkgdesc "vector lists"',
257 ' #:arch #(#:any)',
258 ' #:depends #("glibc" "zsh")',
259 ' #:license #("MIT")',
260 ' #:build (lambda () (run "true"))',
261 ' #:package (lambda () (run "true")))',
262 ]
263 os.write_file(recipe, lines.join('\n'))!
264 desc := native_description(recipe_load(recipe)!)!
265 assert desc.spec.depends == ['glibc', 'zsh']
266 assert desc.spec.arch == ['any']
267 assert desc.licenses == ['MIT']
268}
269
270// valid_recipe is a recipe every field of which is right; the corpus below breaks
271// one thing at a time
272const valid_recipe = ['(defpkgbuild corpus', ' #:pkgname "corpus"', ' #:pkgver "1.0"', ' #:pkgrel 1',
273 ' #:pkgdesc "a corpus base"', " #:arch '(#:any)", ' #:build (lambda () (run "true"))',
274 ' #:package (lambda () (run "true")))']
275
276// RecipeCase is a recipe broken one way, and what the validator says about it
277struct RecipeCase {
278 what string
279 text string
280 says string
281}
282
283// broken replaces the line of valid_recipe that starts with key's spelling, or
284// appends extra before the closing parenthesis when key is ''
285fn broken(key string, line string) string {
286 mut lines := valid_recipe.clone()
287 if key == '' {
288 // the last line ends in defpkgbuild's own ')': the new line takes it
289 last := lines[lines.len - 1]
290 lines[lines.len - 1] = last[..last.len - 1]
291 lines << line + ')'
292 return lines.join('\n')
293 }
294 for i, l in lines {
295 if l.trim_space().starts_with('#:${key} ') {
296 lines[i] = line
297 if i == lines.len - 1 {
298 lines[i] += ')'
299 }
300 }
301 }
302 return lines.join('\n')
303}
304
305fn test_recipe_validation_corpus() ! {
306 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_corpus_${os.getpid()}')
307 os.mkdir_all(dir)!
308 defer {
309 os.rmdir_all(dir) or {}
310 }
311 // the base itself is good
312 base := os.join_path(dir, 'base.scm')
313 os.write_file(base, valid_recipe.join('\n'))!
314 recipe_load(base)!
315
316 hex64 := 'ab'.repeat(32)
317 for c in [
318 RecipeCase{'no name', '(defpkgbuild)', 'needs the name'},
319 RecipeCase{'a number for a name', valid_recipe.join('\n').replace('(defpkgbuild corpus', '(defpkgbuild 7'), 'the name is a symbol or a string, not'},
320 RecipeCase{'a key without its value', broken('', ' #:url'), '#:key and value come in pairs'},
321 RecipeCase{'a value where a key goes', broken('', ' "stray" "pair"'), 'expected a #:key, got "stray"'},
322 RecipeCase{'a key twice', broken('', ' #:pkgrel 2'), '#:pkgrel is set twice'},
323 RecipeCase{'an empty name list', broken('pkgname', " #:pkgname '()"), '#:pkgname must not be empty'},
324 RecipeCase{'a name with a space', broken('pkgname', ' #:pkgname "cor pus"'), 'has whitespace in it'},
325 RecipeCase{'no arch', broken('arch', " #:arch '()"), '#:arch must not be empty'},
326 RecipeCase{'a short b2sum', broken('', ' #:b2sums \'("abc")'), 'b2sums: abc is 3 characters; b2sums entries are 128 hex characters or SKIP'},
327 RecipeCase{'a sha256 that is not hex', broken('', ' #:sha256sums \'("${'zz'.repeat(32)}")'), 'has a character that is not hex (z)'},
328 RecipeCase{'a short fingerprint', broken('', ' #:validpgpkeys \'("ABCDEF")'), 'validpgpkeys: entry 0 is 6 characters'},
329 RecipeCase{'a lowercase fingerprint', broken('', ' #:validpgpkeys \'("${'ab'.repeat(20)}")'), 'uppercase fingerprints only'},
330 RecipeCase{'an absolute backup', broken('', ' #:backup \'("/etc/x.conf")'), 'is absolute; backup paths are relative'},
331 RecipeCase{'a phase that is no lambda', broken('build', ' #:build "make"'), '#:build must be a (lambda ...)'},
332 RecipeCase{'a phase with a parameter', broken('build', ' #:build (lambda (x) (run "true"))'), '#:build takes 0 parameters, not 1'},
333 RecipeCase{'a hook with no version', broken('', ' #:post-install (lambda () (run "true"))'), '#:post-install takes 1 parameter, not 0'},
334 RecipeCase{'an upgrade hook with one version', broken('', ' #:post-upgrade (lambda (new) (run "true"))'), '#:post-upgrade takes 2 parameters, not 1'},
335 RecipeCase{'a pkgver that is a number', broken('pkgver', ' #:pkgver 3'), '#:pkgver must be a string or a (lambda ...)'},
336 RecipeCase{'a pkgrel in quotes', broken('pkgrel', ' #:pkgrel "1"'), '#:pkgrel must be an integer'},
337 RecipeCase{'a negative epoch', broken('', ' #:epoch -1'), 'epoch is negative'},
338 RecipeCase{'a pkgdesc that is a number', broken('pkgdesc', ' #:pkgdesc 5'), '#:pkgdesc must be a string'},
339 RecipeCase{'an unknown key', broken('', ' #:pkgdescription "x"'), 'has no key #:pkgdescription'},
340 RecipeCase{'a dotted list', broken('', ' #:depends (cons "a" "b")'), 'a list ends in ()'},
341 RecipeCase{'no pkgdesc', broken('pkgdesc', ''), '#:pkgdesc is required'},
342 RecipeCase{'no package', broken('package', ' #:url "https://example.org"'), '#:package is required'},
343 ] {
344 p := os.join_path(dir, 'case.scm')
345 os.write_file(p, c.text)!
346 recipe_load(p) or {
347 assert err.msg().contains(c.says), '${c.what}: ${err.msg()}'
348 continue
349 }
350 assert false, '${c.what} was taken:\n${c.text}'
351 }
352 // the right lengths and SKIP pass
353 ok := os.join_path(dir, 'sums.scm')
354 os.write_file(ok, broken('', ' #:sha256sums \'("${hex64}" "SKIP")'))!
355 recipe_load(ok)!
356}
357
358fn test_split_recipe_metadata() ! {
359 // the split smoke package: two pkgnames, one shared package lambda that
360 // asks (this-pkg) which split it runs for
361 recipe := os.join_path(@VMODROOT, 'pkgs', 'schemesplit', 'PKGBUILD.scm')
362 desc := native_description(recipe_load(recipe)!)!
363 assert desc.spec.pkgnames == ['schemesplit', 'schemesplit-doc']
364 assert desc.phases == ['package']
365 assert desc.spec.hooks.len == 0
366}
367
368fn test_recipe_without_a_build_phase() ! {
369 // data-only recipes only declare the package phase
370 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_nobuild_${os.getpid()}')
371 os.mkdir_all(dir)!
372 defer {
373 os.rmdir_all(dir) or {}
374 }
375 p := os.join_path(dir, 'data.scm')
376 os.write_file(p, broken('build', ''))!
377 assert native_description(recipe_load(p)!)!.phases == ['package']
378}
379
380fn test_native_hook_keeps_helpers_and_literal_process_arguments() ! {
381 dir := private_dir(os.vtmp_dir())!
382 defer { os.rmdir_all(dir) or { eprintln('remove hook test: ${err}') } }
383 source := os.join_path(dir, 'recipe')
384 os.mkdir(source)!
385 path := os.join_path(source, 'PKGBUILD.scm')
386 os.write_file(os.join_path(source, 'helper.scm'), '(define (record value) (run "touch" value))')!
387 os.write_file(path, '(load "helper.scm")
388(defpkgbuild demo #:pkgname "demo" #:pkgver "1" #:pkgrel 1 #:pkgdesc "test"
389 #:arch (list "any") #:package (lambda () #t) #:post-install (lambda (v) (record v)))')!
390 hook := write_recipe_hook(path, dir, ['helper.scm'])!
391 os.rmdir_all(source)!
392 message := os.join_path(dir, 'space ; $HOME *')
393 old := os.getenv_opt('VPAC_PKGNAME')
394 defer {
395 if val := old { os.setenv('VPAC_PKGNAME', val, true) } else { os.unsetenv('VPAC_PKGNAME') }
396 }
397 package_hook([hook, '--nspkg-hook', 'post-install', 'demo', message])!
398 assert os.is_file(message)
399}
400
401fn test_native_sources_reject_checksum_before_unpacking() ! {
402 dir := private_dir(os.vtmp_dir())!
403 defer { os.rmdir_all(dir) or { eprintln('remove source test: ${err}') } }
404 src := os.join_path(dir, 'src')
405 os.mkdir(src)!
406 os.write_file(os.join_path(dir, 'one'), 'package contents')!
407 native_sources(Spec{ source: ['one'], sha256sums: ['0'.repeat(64)] }, dir, src) or {
408 assert err.msg().contains('sha256 mismatch')
409 assert os.ls(src)!.len == 0
410 return
411 }
412 assert false, 'bad checksum accepted'
413}
414