vpac / src / build_repo_test.v
158 lines · 147 sloc · 6.27 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import os
4
5fn test_repo_name() {
6 // the last part of the path or url, without a .git suffix. trim_right
7 // would have cut a set of characters: digit came out as d, gist as gis
8 cases := {
9 'https://example.org/mything.git': 'mything'
10 'https://example.org/mything': 'mything'
11 'https://example.org/mything.git/': 'mything'
12 'https://example.org/digit': 'digit'
13 'https://example.org/gist.git': 'gist'
14 'git@github.com:foo/bar.git': 'bar'
15 'git@host:bar.git': 'bar'
16 '/home/u/src/thing': 'thing'
17 '/home/u/src/thing/': 'thing'
18 '/': ''
19 '.': ''
20 '': ''
21 }
22 for source, want in cases {
23 got := repo_name(source)
24 assert got == want, 'repo_name("${source}") is "${got}", not "${want}"'
25 }
26}
27
28fn test_repo_work_dir_urls() ! {
29 old := pkgbuild_dir
30 pkgbuild_dir = '/builds'
31 defer { pkgbuild_dir = old }
32
33 name, one := repo_work_dir('u', 'https://one.example/mything.git')!
34 _, two := repo_work_dir('u', 'https://two.example/mything.git')!
35 _, one_slash := repo_work_dir('u', 'https://one.example/mything.git/')!
36 assert name == 'mything'
37 hash := one.all_after('/builds/mything-')
38 assert one.starts_with('/builds/mything-') && hash.len == 8
39 assert hash.bytes().all(it.is_hex_digit())
40 // the same name from another source is another dir; a trailing slash is
41 // the same source
42 assert one != two
43 assert one == one_slash
44 // and a pkgbuild package of that name builds elsewhere
45 assert one != pkgbuild_build_dir('u', 'mything')!
46 if _, _ := repo_work_dir('u', '/') {
47 assert false, '/ names no repo'
48 }
49}
50
51fn test_is_git_url() {
52 // the transfer urls and the .git names, as always
53 assert is_git_url('https://example.org/mything.git')
54 assert is_git_url('git://example.org/mything')
55 // scp-style: user@host:path — a colon before any slash, the way git
56 // spells a clone it will fetch over ssh
57 assert is_git_url('git@github.com:foo/bar')
58 assert is_git_url('git@host:bar.git')
59 assert is_scp_remote('git@host:bar.git')
60 // a path's slash comes first; a bare file with a colon in its name has
61 // no slash before it either, but hosts come before the colon in scp
62 for not in ['/home/u/mything', '/home/u/we:ird', '/dev/null:too'] {
63 assert !is_scp_remote(not), not
64 }
65}
66
67fn test_repo_work_dir_directories() ! {
68 old := pkgbuild_dir
69 pkgbuild_dir = '/builds'
70 defer { pkgbuild_dir = old }
71 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_build_repo_${os.getpid()}')
72 os.mkdir_all(os.join_path(tmp, 'a', 'mything'))!
73 os.mkdir_all(os.join_path(tmp, 'b', 'mything'))!
74 defer { os.rmdir_all(tmp) or {} }
75 cwd := os.getwd()
76 defer { os.chdir(cwd) or {} }
77
78 _, a := repo_work_dir('u', os.join_path(tmp, 'a', 'mything'))!
79 _, b := repo_work_dir('u', os.join_path(tmp, 'b', 'mything'))!
80 assert a != b
81 // a directory is keyed on its real path: relative, trailing slash and .
82 // all name the same checkout
83 os.chdir(os.join_path(tmp, 'a'))!
84 _, rel := repo_work_dir('u', 'mything/')!
85 os.chdir(os.join_path(tmp, 'a', 'mything'))!
86 dot_name, dot := repo_work_dir('u', '.')!
87 assert rel == a
88 assert dot == a
89 assert dot_name == 'mything'
90}
91
92fn test_install_name_of() ! {
93 for text, want in {
94 'pkgname=x\ninstall=x.install\n': 'x.install'
95 'install = "x.install"': 'x.install'
96 "install='x.install'": 'x.install'
97 // bash reads these; this reader takes them as written
98 'install="\${pkgname}.install"': '\${pkgname}.install'
99 'install=x.install # the hooks': 'x.install # the hooks'
100 // none named, or a command in a function body, which is no assignment
101 'pkgname=x\n': ''
102 'package() {\n install -Dm755 x "$pkgdir/x"\n}': ''
103 'installdir=/opt': ''
104 // the last assignment is the one bash keeps
105 'install=a.install\ninstall=b.install': 'b.install'
106 } {
107 assert install_name_of(text)! == want, text
108 }
109 for escape in ['install=/etc/x.install', 'install=../x.install', 'install=a/../../x.install',
110 'install=..'] {
111 install_name_of(escape) or {
112 assert err.msg().contains('is not a plain name beside the PKGBUILD'), err.msg()
113 continue
114 }
115 assert false, '${escape} was taken'
116 }
117}
118
119fn test_review_the_rest_shows_every_install_file() ! {
120 dir := os.join_path(os.vtmp_dir(), 'vpac_test_review_rest_${os.getpid()}')
121 os.rmdir_all(dir) or {}
122 os.mkdir_all(os.join_path(dir, '.git'))!
123 defer { os.rmdir_all(dir) or {} }
124 pkg_text := 'pkgname=x\ninstall="\${pkgname}.install"\n'
125 os.write_file(os.join_path(dir, 'PKGBUILD'), pkg_text)!
126 os.write_file(os.join_path(dir, 'x.install'), 'post_install() { rm -rf /; }')!
127 os.write_file(os.join_path(dir, 'other.install'), 'post_upgrade() { :; }')!
128 os.write_file(os.join_path(dir, 'x.patch'), 'patch')!
129 // the generated head spells its install file with a variable: the reader
130 // cannot resolve it, and the scriptlet makepkg packs must still be shown
131 name := install_name_of(pkg_text)!
132 mut review := generated_review('x', 'PKGBUILD.vsh', 'script', pkg_text, name, '')
133 review_the_rest(mut review, dir, name, false)!
134 files := review.files.map(it.name)
135 assert 'x.install' in files && 'other.install' in files, files.str()
136 assert review.files.filter(it.name == 'x.install')[0].text == 'post_install() { rm -rf /; }'
137 assert review.listing == ['PKGBUILD', 'other.install', 'x.install', 'x.patch']
138 assert review.warnings.len == 1
139 assert review.warnings[0].contains('"\${pkgname}.install"'), review.warnings[0]
140
141 // a name the reader resolved is shown once, and nothing is warned of
142 mut plain := generated_review('x', 'PKGBUILD.vsh', 'script', 'install=x.install', 'x.install',
143 'post_install() { rm -rf /; }')
144 review_the_rest(mut plain, dir, 'x.install', true)!
145 assert plain.files.filter(it.name == 'x.install').len == 1
146 assert plain.files.any(it.name == 'other.install')
147 assert plain.warnings.len == 0
148}
149
150fn test_build_refuses_noconfirm_alone() {
151 // the build question's preset is no: --noconfirm alone would build
152 // nothing and exit clean, so it is refused before anything is pulled
153 build_repo(ConfigBuild{ noconfirm: true, query: ['/nonexistent/repo'] }) or {
154 assert err.msg().contains('--nopkgbuildconfirm'), err.msg()
155 return
156 }
157 assert false, 'built under --noconfirm'
158}
159