module vmakepkg import os // spec_names is the bare pkgname or the split list: both spellings together // name one intent, so nothing is named twice, and the first mention's place // is kept fn test_spec_names() ! { // the usual spellings, alone and split assert spec_names(&Spec{ pkgname: 'foobar' })! == ['foobar'] assert spec_names(&Spec{ pkgnames: ['foo', 'bar'] })! == ['foo', 'bar'] // a bare name repeated in the split list is one name assert spec_names(&Spec{ pkgname: 'foo', pkgnames: ['foo', 'bar'] })! == ['foo', 'bar'] // and a split list of repeats is one each, in first-mention order assert spec_names(&Spec{ pkgnames: ['a', 'b', 'a', 'c', 'b'] })! == ['a', 'b', 'c'] // white space in a name is a mistake the lip format refuses too if _ := spec_names(&Spec{ pkgname: 'foo bar' }) { assert false, 'a name with whitespace was taken' } else { assert err.msg().contains('whitespace'), err.msg() } } // no name at all is no package fn test_spec_names_requires_a_name() { if _ := spec_names(&Spec{ pkgnames: []string{} }) { assert false, 'a nameless spec was taken' } else { assert err.msg().contains('pkgname is required'), err.msg() } } // spec_meta's name is the pkgbase, the first of the canonical names: for a // split-only spec the bare pkgname spelling is empty, and this is what // carries one fn test_spec_meta_name_is_the_pkgbase() ! { mut s := Spec{ pkgnames: ['pkg-a', 'pkg-b'] pkgver: '2.5' pkgrel: 2 pkgdesc: 'a split package' arch: ['any'] } have := map[string]bool{ 'build': true 'package': true } meta := spec_meta(&s, have)! st := meta.as_struct() // the head walks a list for :pkgname; name stays the base names := lip_items_of(st.get('pkgname'), 'pkgname')! assert names == ['pkg-a', 'pkg-b'] assert st.get('name').as_string() == 'pkg-a' } // Calls records which of an entry's functions ran, with what @[heap] struct Calls { mut: ran []string args []string } // recording_entry is an entry whose every phase and hook records itself; // fail names the one phase that fails instead fn recording_entry(mut c Calls, fail string) VshEntry { return VshEntry{ spec: fn () !Spec { return Spec{ pkgname: 'rec' pkgver: '1.0' pkgrel: 1 pkgdesc: 'records its phases' arch: ['any'] hooks: ['post-install'] } } prepare: fn [mut c, fail] () ! { c.ran << 'prepare' if fail == 'prepare' { return error('prepare broke') } } build: fn [mut c, fail] () ! { c.ran << 'build' if fail == 'build' { return error('build broke') } } check: fn [mut c] () ! { c.ran << 'check' } package: fn [mut c] () ! { c.ran << 'package' } hooks: { 'post-install': fn [mut c] (args []string) ! { c.ran << 'post-install' c.args << args } } } } fn test_vsh_run_dispatches_every_phase() ! { for phase in ['prepare', 'build', 'check', 'package'] { mut c := &Calls{} vsh_run(recording_entry(mut c, ''), [phase], '')! // the phase asked for, and nothing else assert c.ran == [phase] } // a hook takes the arguments after its name, as pacman hands them mut c := &Calls{} vsh_run(recording_entry(mut c, ''), ['post-install', '1.0-1', '0.9-1'], '')! assert c.ran == ['post-install'] assert c.args == ['1.0-1', '0.9-1'] } fn test_vsh_run_phase_failures() { // a failing phase fails the run, so makepkg stops as it would mut c := &Calls{} vsh_run(recording_entry(mut c, 'build'), ['build'], '') or { assert err.msg() == 'build broke' assert c.ran == ['build'] return } assert false, 'a failing build passed' } fn test_vsh_run_missing_and_unknown() { // a phase the entry holds no function for, a name that is nothing, and // no name at all: each says so, nothing runs empty := VshEntry{} for phase, says in { 'prepare': 'prepare: the entry holds no prepare function' 'build': 'build: the entry holds no build function' 'check': 'check: the entry holds no check function' 'package': 'package: the entry holds no package function' 'pkgver': 'pkgver: the entry holds no pkgver function' 'post-remove': 'vsh: "post-remove" is neither generate, a phase, nor a hook' '': 'vsh: give an argument' } { args := if phase == '' { []string{} } else { [phase] } vsh_run(empty, args, '') or { assert err.msg().starts_with(says), err.msg() continue } assert false, '${phase} ran on an empty entry' } } fn test_vsh_run_pkgver() ! { // pkgver prints the version the function computes; an empty one is a // mistake makepkg would turn into a broken package name out := capture_stdout(fn () ! { vsh_run(VshEntry{ pkgver: fn () string { return '2.1.r3.gabc' } }, ['pkgver'], '')! })! assert out == '2.1.r3.gabc\n' vsh_run(VshEntry{ pkgver: fn () string { return '' } }, ['pkgver'], '') or { assert err.msg() == 'pkgver: it returned an empty version' return } assert false, 'an empty pkgver passed' } fn test_vsh_run_generate() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_vsh_generate_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } script := os.join_path(dir, 'PKGBUILD.vsh') os.write_file(script, '// the script')! saved := os.getenv('VPAC_V') defer { os.setenv('VPAC_V', saved, true) } os.setenv('VPAC_V', '/usr/bin/v', true) mut c := &Calls{} entry := recording_entry(mut c, '') // generate writes the PKGBUILD, and the .install its hooks need, beside // the script, and runs no phase capture_stdout(fn [entry, script] () ! { vsh_run(entry, ['generate'], script)! })! assert c.ran.len == 0 pkgbuild := os.read_file(os.join_path(dir, 'PKGBUILD'))! assert pkgbuild.contains("pkgname='rec'"), pkgbuild for phase in ['prepare', 'build', 'check', 'package'] { assert pkgbuild.contains('${phase}() {'), phase } assert os.exists(os.join_path(dir, 'rec.install')) // without the script's path there is nowhere to write vsh_run(entry, ['generate'], '') or { assert err.msg().contains('VPAC_VSH_PATH') return } assert false, 'generate ran without a script path' } // BadEntry is an entry generate refuses, and what it says struct BadEntry { e VshEntry says string } fn test_vsh_run_generate_checks_the_entry() ! { dir := os.join_path(os.vtmp_dir(), 'vpac_vsh_generate_bad_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } script := os.join_path(dir, 'PKGBUILD.vsh') saved := os.getenv('VPAC_V') defer { os.setenv('VPAC_V', saved, true) } os.setenv('VPAC_V', '/usr/bin/v', true) spec := fn () !Spec { return Spec{ pkgname: 'bad' pkgver: '1.0' pkgrel: 1 pkgdesc: 'missing parts' arch: ['any'] } } listed := fn () !Spec { return Spec{ pkgname: 'bad' pkgver: '1.0' pkgrel: 1 pkgdesc: 'a hook with no body' arch: ['any'] hooks: ['post-install'] } } nop := fn () ! {} hook := fn (_ []string) ! {} for t in [ BadEntry{VshEntry{ build: nop package: nop }, 'a :spec is required'}, BadEntry{VshEntry{ spec: spec build: nop }, 'a :package phase is required'}, BadEntry{VshEntry{ spec: spec build: nop package: nop hooks: { 'post-install': hook } }, 'the spec does not list :post-install'}, BadEntry{VshEntry{ spec: listed build: nop package: nop }, 'the entry holds no post-install body'}, ] { vsh_run(t.e, ['generate'], script) or { assert err.msg().contains(t.says), err.msg() continue } assert false, 'generate took an entry it should refuse: ${t.says}' } // and it wrote nothing for any of them assert !os.exists(os.join_path(dir, 'PKGBUILD')) } // capture_stdout runs f with fd 1 going to a file, and returns what came fn capture_stdout(f fn () !) !string { path := os.join_path(os.vtmp_dir(), 'vpac_vsh_out_${os.getpid()}') mut file := os.create(path)! defer { os.rm(path) or {} } saved := C.dup(1) if saved < 0 { return error('could not save stdout') } C.fflush(C.stdout) C.dup2(file.fd, 1) mut failure := '' f() or { failure = err.msg() } C.fflush(C.stdout) C.dup2(saved, 1) C.close(saved) file.close() if failure != '' { return error(failure) } return os.read_file(path)! } fn test_vsh_run_generate_without_build() ! { // no build phase, as makepkg allows: the PKGBUILD has no build() and // still its package() dir := os.join_path(os.vtmp_dir(), 'vpac_vsh_nobuild_${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } script := os.join_path(dir, 'PKGBUILD.vsh') saved := os.getenv('VPAC_V') defer { os.setenv('VPAC_V', saved, true) } os.setenv('VPAC_V', '/usr/bin/v', true) entry := VshEntry{ spec: fn () !Spec { return Spec{ pkgname: 'data' pkgver: '1.0' pkgrel: 1 pkgdesc: 'files only' arch: ['any'] } } package: fn () ! {} } capture_stdout(fn [entry, script] () ! { vsh_run(entry, ['generate'], script)! })! pkgbuild := os.read_file(os.join_path(dir, 'PKGBUILD'))! assert !pkgbuild.contains('build() {'), pkgbuild assert pkgbuild.contains('package() {'), pkgbuild } fn test_spec_epoch_edges() ! { base := Spec{ pkgname: 'ep' pkgver: '1.0' pkgrel: 1 pkgdesc: 'epochs' arch: ['any'] } have := { 'package': true } // absent and 0 are the same: no epoch in the head zero := spec_meta(&base, have)! assert !zero.as_struct().has('epoch') // a positive one is carried as it is three := spec_meta(&Spec{ ...base epoch: 3 }, have)! assert three.as_struct().get('epoch').as_int() == 3 // a negative one is said, as the lip's :epoch says it, not dropped for bad in [-1, -100] { if _ := spec_meta(&Spec{ ...base epoch: bad }, have) { assert false, 'epoch ${bad} was taken' } else { assert err.msg() == 'pkgbuild :epoch is negative; 0 means absent', err.msg() } } }