vpac / vmakepkg / vsh_test.v
358 lines · 342 sloc · 9.58 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module vmakepkg
2
3import os
4
5// spec_names is the bare pkgname or the split list: both spellings together
6// name one intent, so nothing is named twice, and the first mention's place
7// is kept
8fn test_spec_names() ! {
9 // the usual spellings, alone and split
10 assert spec_names(&Spec{ pkgname: 'foobar' })! == ['foobar']
11 assert spec_names(&Spec{ pkgnames: ['foo', 'bar'] })! == ['foo', 'bar']
12 // a bare name repeated in the split list is one name
13 assert spec_names(&Spec{ pkgname: 'foo', pkgnames: ['foo', 'bar'] })! == ['foo', 'bar']
14 // and a split list of repeats is one each, in first-mention order
15 assert spec_names(&Spec{ pkgnames: ['a', 'b', 'a', 'c', 'b'] })! == ['a', 'b', 'c']
16 // white space in a name is a mistake the lip format refuses too
17 if _ := spec_names(&Spec{ pkgname: 'foo bar' }) {
18 assert false, 'a name with whitespace was taken'
19 } else {
20 assert err.msg().contains('whitespace'), err.msg()
21 }
22}
23
24// no name at all is no package
25fn test_spec_names_requires_a_name() {
26 if _ := spec_names(&Spec{ pkgnames: []string{} }) {
27 assert false, 'a nameless spec was taken'
28 } else {
29 assert err.msg().contains('pkgname is required'), err.msg()
30 }
31}
32
33// spec_meta's name is the pkgbase, the first of the canonical names: for a
34// split-only spec the bare pkgname spelling is empty, and this is what
35// carries one
36fn test_spec_meta_name_is_the_pkgbase() ! {
37 mut s := Spec{
38 pkgnames: ['pkg-a', 'pkg-b']
39 pkgver: '2.5'
40 pkgrel: 2
41 pkgdesc: 'a split package'
42 arch: ['any']
43 }
44 have := map[string]bool{
45 'build': true
46 'package': true
47 }
48 meta := spec_meta(&s, have)!
49 st := meta.as_struct()
50 // the head walks a list for :pkgname; name stays the base
51 names := lip_items_of(st.get('pkgname'), 'pkgname')!
52 assert names == ['pkg-a', 'pkg-b']
53 assert st.get('name').as_string() == 'pkg-a'
54}
55
56// Calls records which of an entry's functions ran, with what
57@[heap]
58struct Calls {
59mut:
60 ran []string
61 args []string
62}
63
64// recording_entry is an entry whose every phase and hook records itself;
65// fail names the one phase that fails instead
66fn recording_entry(mut c Calls, fail string) VshEntry {
67 return VshEntry{
68 spec: fn () !Spec {
69 return Spec{
70 pkgname: 'rec'
71 pkgver: '1.0'
72 pkgrel: 1
73 pkgdesc: 'records its phases'
74 arch: ['any']
75 hooks: ['post-install']
76 }
77 }
78 prepare: fn [mut c, fail] () ! {
79 c.ran << 'prepare'
80 if fail == 'prepare' {
81 return error('prepare broke')
82 }
83 }
84 build: fn [mut c, fail] () ! {
85 c.ran << 'build'
86 if fail == 'build' {
87 return error('build broke')
88 }
89 }
90 check: fn [mut c] () ! {
91 c.ran << 'check'
92 }
93 package: fn [mut c] () ! {
94 c.ran << 'package'
95 }
96 hooks: {
97 'post-install': fn [mut c] (args []string) ! {
98 c.ran << 'post-install'
99 c.args << args
100 }
101 }
102 }
103}
104
105fn test_vsh_run_dispatches_every_phase() ! {
106 for phase in ['prepare', 'build', 'check', 'package'] {
107 mut c := &Calls{}
108 vsh_run(recording_entry(mut c, ''), [phase], '')!
109 // the phase asked for, and nothing else
110 assert c.ran == [phase]
111 }
112 // a hook takes the arguments after its name, as pacman hands them
113 mut c := &Calls{}
114 vsh_run(recording_entry(mut c, ''), ['post-install', '1.0-1', '0.9-1'], '')!
115 assert c.ran == ['post-install']
116 assert c.args == ['1.0-1', '0.9-1']
117}
118
119fn test_vsh_run_phase_failures() {
120 // a failing phase fails the run, so makepkg stops as it would
121 mut c := &Calls{}
122 vsh_run(recording_entry(mut c, 'build'), ['build'], '') or {
123 assert err.msg() == 'build broke'
124 assert c.ran == ['build']
125 return
126 }
127 assert false, 'a failing build passed'
128}
129
130fn test_vsh_run_missing_and_unknown() {
131 // a phase the entry holds no function for, a name that is nothing, and
132 // no name at all: each says so, nothing runs
133 empty := VshEntry{}
134 for phase, says in {
135 'prepare': 'prepare: the entry holds no prepare function'
136 'build': 'build: the entry holds no build function'
137 'check': 'check: the entry holds no check function'
138 'package': 'package: the entry holds no package function'
139 'pkgver': 'pkgver: the entry holds no pkgver function'
140 'post-remove': 'vsh: "post-remove" is neither generate, a phase, nor a hook'
141 '': 'vsh: give an argument'
142 } {
143 args := if phase == '' { []string{} } else { [phase] }
144 vsh_run(empty, args, '') or {
145 assert err.msg().starts_with(says), err.msg()
146 continue
147 }
148 assert false, '${phase} ran on an empty entry'
149 }
150}
151
152fn test_vsh_run_pkgver() ! {
153 // pkgver prints the version the function computes; an empty one is a
154 // mistake makepkg would turn into a broken package name
155 out := capture_stdout(fn () ! {
156 vsh_run(VshEntry{
157 pkgver: fn () string {
158 return '2.1.r3.gabc'
159 }
160 }, ['pkgver'], '')!
161 })!
162 assert out == '2.1.r3.gabc\n'
163 vsh_run(VshEntry{
164 pkgver: fn () string {
165 return ''
166 }
167 }, ['pkgver'], '') or {
168 assert err.msg() == 'pkgver: it returned an empty version'
169 return
170 }
171 assert false, 'an empty pkgver passed'
172}
173
174fn test_vsh_run_generate() ! {
175 dir := os.join_path(os.vtmp_dir(), 'vpac_vsh_generate_${os.getpid()}')
176 os.mkdir_all(dir)!
177 defer { os.rmdir_all(dir) or {} }
178 script := os.join_path(dir, 'PKGBUILD.vsh')
179 os.write_file(script, '// the script')!
180 saved := os.getenv('VPAC_V')
181 defer { os.setenv('VPAC_V', saved, true) }
182 os.setenv('VPAC_V', '/usr/bin/v', true)
183 mut c := &Calls{}
184 entry := recording_entry(mut c, '')
185 // generate writes the PKGBUILD, and the .install its hooks need, beside
186 // the script, and runs no phase
187 capture_stdout(fn [entry, script] () ! {
188 vsh_run(entry, ['generate'], script)!
189 })!
190 assert c.ran.len == 0
191 pkgbuild := os.read_file(os.join_path(dir, 'PKGBUILD'))!
192 assert pkgbuild.contains("pkgname='rec'"), pkgbuild
193 for phase in ['prepare', 'build', 'check', 'package'] {
194 assert pkgbuild.contains('${phase}() {'), phase
195 }
196 assert os.exists(os.join_path(dir, 'rec.install'))
197 // without the script's path there is nowhere to write
198 vsh_run(entry, ['generate'], '') or {
199 assert err.msg().contains('VPAC_VSH_PATH')
200 return
201 }
202 assert false, 'generate ran without a script path'
203}
204
205// BadEntry is an entry generate refuses, and what it says
206struct BadEntry {
207 e VshEntry
208 says string
209}
210
211fn test_vsh_run_generate_checks_the_entry() ! {
212 dir := os.join_path(os.vtmp_dir(), 'vpac_vsh_generate_bad_${os.getpid()}')
213 os.mkdir_all(dir)!
214 defer { os.rmdir_all(dir) or {} }
215 script := os.join_path(dir, 'PKGBUILD.vsh')
216 saved := os.getenv('VPAC_V')
217 defer { os.setenv('VPAC_V', saved, true) }
218 os.setenv('VPAC_V', '/usr/bin/v', true)
219 spec := fn () !Spec {
220 return Spec{
221 pkgname: 'bad'
222 pkgver: '1.0'
223 pkgrel: 1
224 pkgdesc: 'missing parts'
225 arch: ['any']
226 }
227 }
228 listed := fn () !Spec {
229 return Spec{
230 pkgname: 'bad'
231 pkgver: '1.0'
232 pkgrel: 1
233 pkgdesc: 'a hook with no body'
234 arch: ['any']
235 hooks: ['post-install']
236 }
237 }
238 nop := fn () ! {}
239 hook := fn (_ []string) ! {}
240 for t in [
241 BadEntry{VshEntry{
242 build: nop
243 package: nop
244 }, 'a :spec is required'},
245 BadEntry{VshEntry{
246 spec: spec
247 build: nop
248 }, 'a :package phase is required'},
249 BadEntry{VshEntry{
250 spec: spec
251 build: nop
252 package: nop
253 hooks: {
254 'post-install': hook
255 }
256 }, 'the spec does not list :post-install'},
257 BadEntry{VshEntry{
258 spec: listed
259 build: nop
260 package: nop
261 }, 'the entry holds no post-install body'},
262 ] {
263 vsh_run(t.e, ['generate'], script) or {
264 assert err.msg().contains(t.says), err.msg()
265 continue
266 }
267 assert false, 'generate took an entry it should refuse: ${t.says}'
268 }
269 // and it wrote nothing for any of them
270 assert !os.exists(os.join_path(dir, 'PKGBUILD'))
271}
272
273// capture_stdout runs f with fd 1 going to a file, and returns what came
274fn capture_stdout(f fn () !) !string {
275 path := os.join_path(os.vtmp_dir(), 'vpac_vsh_out_${os.getpid()}')
276 mut file := os.create(path)!
277 defer { os.rm(path) or {} }
278 saved := C.dup(1)
279 if saved < 0 {
280 return error('could not save stdout')
281 }
282 C.fflush(C.stdout)
283 C.dup2(file.fd, 1)
284 mut failure := ''
285 f() or { failure = err.msg() }
286 C.fflush(C.stdout)
287 C.dup2(saved, 1)
288 C.close(saved)
289 file.close()
290 if failure != '' {
291 return error(failure)
292 }
293 return os.read_file(path)!
294}
295
296fn test_vsh_run_generate_without_build() ! {
297 // no build phase, as makepkg allows: the PKGBUILD has no build() and
298 // still its package()
299 dir := os.join_path(os.vtmp_dir(), 'vpac_vsh_nobuild_${os.getpid()}')
300 os.mkdir_all(dir)!
301 defer { os.rmdir_all(dir) or {} }
302 script := os.join_path(dir, 'PKGBUILD.vsh')
303 saved := os.getenv('VPAC_V')
304 defer { os.setenv('VPAC_V', saved, true) }
305 os.setenv('VPAC_V', '/usr/bin/v', true)
306 entry := VshEntry{
307 spec: fn () !Spec {
308 return Spec{
309 pkgname: 'data'
310 pkgver: '1.0'
311 pkgrel: 1
312 pkgdesc: 'files only'
313 arch: ['any']
314 }
315 }
316 package: fn () ! {}
317 }
318 capture_stdout(fn [entry, script] () ! {
319 vsh_run(entry, ['generate'], script)!
320 })!
321 pkgbuild := os.read_file(os.join_path(dir, 'PKGBUILD'))!
322 assert !pkgbuild.contains('build() {'), pkgbuild
323 assert pkgbuild.contains('package() {'), pkgbuild
324}
325
326fn test_spec_epoch_edges() ! {
327 base := Spec{
328 pkgname: 'ep'
329 pkgver: '1.0'
330 pkgrel: 1
331 pkgdesc: 'epochs'
332 arch: ['any']
333 }
334 have := {
335 'package': true
336 }
337 // absent and 0 are the same: no epoch in the head
338 zero := spec_meta(&base, have)!
339 assert !zero.as_struct().has('epoch')
340 // a positive one is carried as it is
341 three := spec_meta(&Spec{
342 ...base
343 epoch: 3
344 }, have)!
345 assert three.as_struct().get('epoch').as_int() == 3
346 // a negative one is said, as the lip's :epoch says it, not dropped
347 for bad in [-1, -100] {
348 if _ := spec_meta(&Spec{
349 ...base
350 epoch: bad
351 }, have)
352 {
353 assert false, 'epoch ${bad} was taken'
354 } else {
355 assert err.msg() == 'pkgbuild :epoch is negative; 0 means absent', err.msg()
356 }
357 }
358}
359