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