vpac / src / build_repo_test.v
113 lines · 104 sloc · 3.97 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module main
2
3import os
4
5fn test_build_recipe_precedence() ! {
6 dir := os.join_path(os.vtmp_dir(), 'vpac_recipe_choice_${os.getpid()}')
7 os.mkdir_all(dir)!
8 defer { os.rmdir_all(dir) or { went_on('remove the test recipes', err) } }
9 if _ := build_recipe_name(dir) {
10 assert false, 'an empty directory has no recipe'
11 }
12 for name in ['PKGBUILD', 'PKGBUILD.vsh', 'PKGBUILD.scm', 'PKGBUILD.vile'] {
13 os.write_file(os.join_path(dir, name), '')!
14 assert build_recipe_name(dir)! == name
15 }
16}
17
18fn test_repo_name() {
19 // the last part of the path or url, without a .git suffix. trim_right
20 // would have cut a set of characters: digit came out as d, gist as gis
21 cases := {
22 'https://example.org/mything.git': 'mything'
23 'https://example.org/mything': 'mything'
24 'https://example.org/mything.git/': 'mything'
25 'https://example.org/digit': 'digit'
26 'https://example.org/gist.git': 'gist'
27 'git@github.com:foo/bar.git': 'bar'
28 'git@host:bar.git': 'bar'
29 '/home/u/src/thing': 'thing'
30 '/home/u/src/thing/': 'thing'
31 '/': ''
32 '.': ''
33 '': ''
34 }
35 for source, want in cases {
36 got := repo_name(source)
37 assert got == want, 'repo_name("${source}") is "${got}", not "${want}"'
38 }
39}
40
41fn test_repo_work_dir_urls() ! {
42 old := pkgbuild_dir
43 pkgbuild_dir = '/builds'
44 defer { pkgbuild_dir = old }
45
46 name, one := repo_work_dir('u', 'https://one.example/mything.git')!
47 _, two := repo_work_dir('u', 'https://two.example/mything.git')!
48 _, one_slash := repo_work_dir('u', 'https://one.example/mything.git/')!
49 assert name == 'mything'
50 hash := one.all_after('/builds/mything-')
51 assert one.starts_with('/builds/mything-') && hash.len == 8
52 assert hash.bytes().all(it.is_hex_digit())
53 // the same name from another source is another dir; a trailing slash is
54 // the same source
55 assert one != two
56 assert one == one_slash
57 // and a pkgbuild package of that name builds elsewhere
58 assert one != pkgbuild_build_dir('u', 'mything')!
59 if _, _ := repo_work_dir('u', '/') {
60 assert false, '/ names no repo'
61 }
62}
63
64fn test_is_git_url() {
65 // the transfer urls and the .git names, as always
66 assert is_git_url('https://example.org/mything.git')
67 assert is_git_url('git://example.org/mything')
68 // scp-style: user@host:path — a colon before any slash, the way git
69 // spells a clone it will fetch over ssh
70 assert is_git_url('git@github.com:foo/bar')
71 assert is_git_url('git@host:bar.git')
72 assert is_scp_remote('git@host:bar.git')
73 // a path's slash comes first; a bare file with a colon in its name has
74 // no slash before it either, but hosts come before the colon in scp
75 for not in ['/home/u/mything', '/home/u/we:ird', '/dev/null:too'] {
76 assert !is_scp_remote(not), not
77 }
78}
79
80fn test_repo_work_dir_directories() ! {
81 old := pkgbuild_dir
82 pkgbuild_dir = '/builds'
83 defer { pkgbuild_dir = old }
84 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_build_repo_${os.getpid()}')
85 os.mkdir_all(os.join_path(tmp, 'a', 'mything'))!
86 os.mkdir_all(os.join_path(tmp, 'b', 'mything'))!
87 defer { os.rmdir_all(tmp) or {} }
88 cwd := os.getwd()
89 defer { os.chdir(cwd) or {} }
90
91 _, a := repo_work_dir('u', os.join_path(tmp, 'a', 'mything'))!
92 _, b := repo_work_dir('u', os.join_path(tmp, 'b', 'mything'))!
93 assert a != b
94 // a directory is keyed on its real path: relative, trailing slash and .
95 // all name the same checkout
96 os.chdir(os.join_path(tmp, 'a'))!
97 _, rel := repo_work_dir('u', 'mything/')!
98 os.chdir(os.join_path(tmp, 'a', 'mything'))!
99 dot_name, dot := repo_work_dir('u', '.')!
100 assert rel == a
101 assert dot == a
102 assert dot_name == 'mything'
103}
104
105fn test_build_refuses_noconfirm_alone() {
106 // the build question's preset is no: --noconfirm alone would build
107 // nothing and exit clean, so it is refused before anything is pulled
108 build_repo(ConfigBuild{ noconfirm: true, query: ['/nonexistent/repo'] }) or {
109 assert err.msg().contains('--nopkgbuildconfirm'), err.msg()
110 return
111 }
112 assert false, 'built under --noconfirm'
113}
114