vpac / src / config_test.v
445 lines · 419 sloc · 18.67 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import os
4import time
5import x.progress
6
7fn config_tmp(name string) !string {
8 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_config_${name}_${os.getpid()}')
9 os.mkdir_all(os.join_path(tmp, 'db', 'local'))!
10 os.write_file(os.join_path(tmp, 'db', 'local', 'ALPM_DB_VERSION'), '9\n')!
11 return tmp
12}
13
14fn test_config_options() ! {
15 tmp := config_tmp('options')!
16 defer { os.rmdir_all(tmp) or {} }
17 cfg := os.join_path(tmp, 'config.lip')
18 os.write_file(cfg, '(path :db "${tmp}/db/")\n' +
19 '(pkg :ignoregroup ["kde-applications"] :noupgrade ["etc/foo.conf"] :noextract ["usr/share/doc/*" "usr/share/man/*"])\n' +
20 '(path :syslog true)\n' + '(network :disable-timeout true :disable-sandbox true)\n' + r'(repo :name "core" :src ["https://example.org/$repo"] :usage ["sync" "Search"])' +
21 '\n' + r'(repo :name "extra" :src ["https://example.org/$repo"])' + '\n')!
22 load_config(cfg, [])!
23
24 assert cfg_ignore_groups == ['kde-applications']
25 assert cfg_noupgrade == ['etc/foo.conf']
26 assert cfg_noextract == ['usr/share/doc/*', 'usr/share/man/*']
27 assert cfg_disable_timeout
28 assert cfg_disable_sandbox
29 assert cfg_syslog
30 core := cfg_repos.filter(it.name == 'core')[0]
31 extra := cfg_repos.filter(it.name == 'extra')[0]
32 // pacman's Usage bits: Sync 1, Search 2; every use when none is given
33 assert int(core.usage) == 1 | 2
34 assert int(extra.usage) == 15
35
36 // and libalpm gets them from init_handle
37 ui := new_trans_ui()
38 mut handle := init_handle(ui)!
39 defer { handle.release() or {} }
40 mut groups := handle.option_get_ignore_groups()
41 assert groups.strs() == ['kde-applications']
42 mut noupgrade := handle.option_get_noupgrades()
43 assert noupgrade.strs() == ['etc/foo.conf']
44 mut noextract := handle.option_get_no_extracts()
45 assert noextract.strs() == ['usr/share/doc/*', 'usr/share/man/*']
46 assert handle.option_match_no_extract('usr/share/doc/zsh/README')
47 assert !handle.option_match_no_extract('usr/bin/zsh')
48 assert handle.option_get_disable_sandbox_filesystem()
49 assert handle.option_get_disable_sandbox_syscalls()
50}
51
52// the sandbox stays on unless the config turns it off: a config that does not
53// name it, or names it false, leaves libalpm's sandbox on for the handle
54fn test_config_disable_sandbox_defaults_on() ! {
55 tmp := config_tmp('sandbox')!
56 defer {
57 os.rmdir_all(tmp) or {}
58 reset_config()
59 }
60 cfg := os.join_path(tmp, 'config.lip')
61 os.write_file(cfg, '(network :parallel 2)\n')!
62 load_config(cfg, [])!
63 assert !cfg_disable_sandbox
64 ui := new_trans_ui()
65 mut handle := init_handle(ui)!
66 defer { handle.release() or {} }
67 assert !handle.option_get_disable_sandbox_filesystem()
68 assert !handle.option_get_disable_sandbox_syscalls()
69 // a false written out is the same as leaving it out
70 os.write_file(cfg, '(network :disable-sandbox false)\n')!
71 load_config(cfg, [])!
72 assert !cfg_disable_sandbox
73}
74
75fn test_config_rejects_bad_values() ! {
76 tmp := config_tmp('bad')!
77 defer { os.rmdir_all(tmp) or {} }
78 cfg := os.join_path(tmp, 'config.lip')
79 for form, says in {
80 '(repo :name "x" :usage ["sometimes"])': 'takes sync, search, install, upgrade and all'
81 '(repo :name "x" :usage [])': 'needs at least one'
82 '(network :disable-timeout "yes")': 'must be true or false'
83 '(network :disable-sandbox "yes")': 'must be true or false'
84 // an empty one verifies nothing, and a list of no protocols accepts
85 // no transfers; a repo that names no servers has nothing to fetch
86 '(network :siglevel [])': 'needs at least one of'
87 '(network :protocols [])': 'needs at least one'
88 '(network :protocols ["ftp"])': 'takes https and http'
89 '(network :protocols ["https" "HTTPS"])': 'takes https and http'
90 '(pkg :arch "")': 'must be one plain word'
91 '(pkg :arch "x86 64")': 'must be one plain word'
92 '(pkg :arch " x86_64")': 'must be one plain word'
93 '(pkg :arch "a/b")': 'must be one plain word'
94 '(repo :name "local" :src ["http://x"])': 'cannot be named local'
95 '(repo :name "x" :src ["http://x"]) (repo :name "x" :src ["http://y"])': 'is defined twice'
96 '(repo :name "x" :src ["http://x"]) (repo :name "x" :type "pkgbuild" :src ["http://y"])': 'is defined twice'
97 '(mirrorlist :name "my os" :repo "core" :status "https://x")': 'one plain word'
98 '(mirrorlist :name "os\t" :repo "core" :status "https://x")': 'one plain word'
99 '(repo :name "x" :src [])': 'needs at least one url'
100 '(repo :name "x" :src "")': 'has an empty url in it'
101 '(repo :name "x" :type "sync")': 'takes at least one :src'
102 '(options :root " ")': 'must not be empty'
103 '(options :colour "always")': 'has no key :colour'
104 '(options :color "sometimes")': 'takes auto, always or never'
105 '(options :noconfirm "yes")': 'must be true or false'
106 '(options :suggest 2)': 'between 0 and 1'
107 '(options :sync-age -1)': 'at least 0'
108 '(search :fields ["nope"])': 'unknown field'
109 '(search :thold "high")': 'must be a number'
110 '(rank :top 0)': 'at least 1'
111 '(pkgbuild :timeout 0)': 'at least 1'
112 '(color :nope "red")': 'has no role :nope'
113 '(progress :style "round")': 'classic or block'
114 '(progress :fill "==")': 'must be one character'
115 '(progress :empty "")': 'must be one character'
116 '(spinner :frames "nope")': 'not "nope"'
117 '(spinner :frames [])': 'at least one frame'
118 '(mirrorlist :name "x" :repo "core")': 'one of :status and :list'
119 '(mirrorlist :name "x" :list "/m")': 'needs a :repo'
120 '(mirrorlist :name "a/b" :list "/m" :repo "c")': 'one plain word'
121 '(mirrorlist :name "x" :status "/m" :repo "c")': 'http or https url'
122 '(rank :dir "mirrors")': 'absolute path'
123 '(color :name "pink")': 'not "pink"'
124 } {
125 os.write_file(cfg, form + '\n')!
126 if _ := load_config(cfg, []) {
127 assert false, '${form} was accepted'
128 } else {
129 assert err.msg().contains(says), '${form}: ${err.msg()}'
130 }
131 }
132 // a list may be a single string, as everywhere in the config
133 os.write_file(cfg, '(pkg :noupgrade "etc/foo.conf")\n')!
134 load_config(cfg, [])!
135 assert cfg_noupgrade == ['etc/foo.conf']
136}
137
138// a second load answers nothing of the first's: every global is back at its
139// default before the file is read, so nothing is kept that the new file does
140// not name, and no repo of the old load survives
141fn test_config_resets_between_loads() ! {
142 tmp := config_tmp('reset')!
143 defer { os.rmdir_all(tmp) or {} }
144 cfg := os.join_path(tmp, 'config.lip')
145 os.write_file(cfg, '(repo :name "one" :src ["https://one.example/$repo"])\n
146(network :parallel 5 :siglevel ["package" "database"])\n(pkg :hold ["glibc"] :ignore ["flatpak"])\n(options :root "doas -n")\n')!
147 load_config(cfg, [])!
148 assert root_provider == 'doas -n'
149 assert cfg_repos.map(it.name) == ['one']
150 assert cfg_repos.len == 1
151 assert parallel_downloads == 5
152 assert cfg_hold == ['glibc']
153 assert cfg_ignore == ['flatpak']
154 assert cfg_disable_timeout == false
155
156 os.write_file(cfg, '(repo :name "two" :src "https://two.example/$repo")\n')!
157 load_config(cfg, [])!
158 assert cfg_repos.map(it.name) == ['two']
159 assert cfg_repos.len == 1
160 assert parallel_downloads == 3
161 assert root_provider == 'sudo'
162 assert opt_color == ''
163 assert opt_flags.len == 0
164 assert cfg_hold == []string{}
165 assert cfg_ignore == []string{}
166}
167
168// check_hold refuses a name on the hold list, the targets of a removal as
169// well as whatever a transaction pulls in beyond them
170fn test_check_hold() {
171 mut old := cfg_hold.clone()
172 defer { cfg_hold = old.clone() }
173 cfg_hold = ['held', 'base']
174 check_hold(['other', 'more']) or {
175 assert false, 'a name off the hold list was refused'
176 }
177 if _ := check_hold(['fine', 'held']) {
178 assert false, 'a held package was let through'
179 } else {
180 assert err.msg().contains('held'), err.msg()
181 }
182}
183
184// the keyring dir defaults to pacman's GPGDir, so a config that names no
185// :gpg checks signatures against the system keyring, not root's ~/.gnupg;
186// a named one overrides it for its load only
187fn test_config_gpgdir_default() ! {
188 tmp := config_tmp('gpgdir')!
189 defer { os.rmdir_all(tmp) or {} }
190 cfg := os.join_path(tmp, 'config.lip')
191 saved := cfg_gpgdir
192 defer { cfg_gpgdir = saved }
193
194 os.write_file(cfg, '(network :parallel 2)\n')!
195 load_config(cfg, [])!
196 assert cfg_gpgdir == '/etc/pacman.d/gnupg/'
197
198 os.write_file(cfg, '(path :gpg "${tmp}/keys")\n')!
199 load_config(cfg, [])!
200 assert cfg_gpgdir == '${tmp}/keys/'
201
202 os.write_file(cfg, '(network :parallel 2)\n')!
203 load_config(cfg, [])!
204 assert cfg_gpgdir == '/etc/pacman.d/gnupg/'
205}
206
207// the default reaches the handle every writing mode opens
208fn test_init_handle_sets_the_default_gpgdir() ! {
209 tmp := config_tmp('gpghandle')!
210 defer { os.rmdir_all(tmp) or {} }
211 os.mkdir_all(os.join_path(tmp, 'root'))!
212 saved_root, saved_db, saved_user := root_path, db_path, download_user
213 defer {
214 reset_config()
215 root_path, db_path, download_user = saved_root, saved_db, saved_user
216 }
217 reset_config()
218 root_path = os.join_path(tmp, 'root') + '/'
219 db_path = os.join_path(tmp, 'db') + '/'
220 // the sandbox user need not exist where the tests run
221 download_user = ''
222 ui := new_trans_ui()
223 handle := init_handle(ui)!
224 defer { handle.release() or {} }
225 assert handle.option_get_gpgdir() == '/etc/pacman.d/gnupg/'
226}
227
228// the config's options are the flags a run starts with: on for every mode
229// that has the flag, untouched where it has none, and off again by --eval
230fn test_config_options_set_mode_flags() ! {
231 tmp := config_tmp('flags')!
232 defer {
233 os.rmdir_all(tmp) or {}
234 reset_config()
235 }
236 cfg := os.join_path(tmp, 'config.lip')
237 os.write_file(cfg, '(options :noconfirm true :needed true :nopkgbuildconfirm true :no-upgrade-deps true :color "never")\n')!
238 load_config(cfg, [])!
239 assert opt_color == 'never'
240 install_mode := parse_mode(['vpac', 'i', 'foo'])!
241 if install_mode is ConfigInstall {
242 assert install_mode.noconfirm && install_mode.needed
243 assert install_mode.no_pkgbuild_confirm && install_mode.no_upgrade_deps
244 assert install_mode.query == ['foo']
245 } else {
246 assert false
247 }
248 remove_mode := parse_mode(['vpac', 'r', 'foo'])!
249 if remove_mode is ConfigRemove {
250 assert remove_mode.noconfirm
251 } else {
252 assert false
253 }
254 load_config(cfg, ['(options :noconfirm false)'])!
255 again := parse_mode(['vpac', 'i', 'foo'])!
256 if again is ConfigInstall {
257 assert !again.noconfirm && again.needed
258 } else {
259 assert false
260 }
261}
262
263// the tunables the modes read are the config's: thresholds, sizes, the
264// defaults a flag would otherwise carry, and the theme. a flag on the
265// command line still wins, and the next load puts every one back
266fn test_config_tunables() ! {
267 tmp := config_tmp('tunables')!
268 old_color := color_flag
269 defer {
270 os.rmdir_all(tmp) or {}
271 color_flag = old_color
272 reset_config()
273 }
274 cfg := os.join_path(tmp, 'config.lip')
275 os.write_file(cfg, '(options :sync-age 0 :suggest 0.5 :fuzzy true)\n' +
276 '(search :fields ["name" "version"] :thold 0.7)\n' +
277 '(rank :top 3 :candidates 12 :retest 0 :completion 0.9 :max-delay 3600)\n' +
278 '(pkgbuild :timeout 30)\n(clean :keep 2)\n(tree :depth 4)\n' +
279 '(color :version "cyan" :warning ["bold" "magenta"] :name "none")\n')!
280 load_config(cfg, [])!
281 assert sync_max_age == 0
282 assert suggest_thold == 0.5
283 assert cfg_search_fields == ['name', 'version']
284 assert rank_speed_candidates == 12 && rank_retest == 0
285 assert rank_min_completion == 0.9 && rank_max_delay == 3600
286 assert pkgbuild_rpc_timeout == 30
287
288 search_mode := parse_mode(['vpac', 's', 'foo'])!
289 if search_mode is ConfigSearch {
290 assert search_mode.thold == 0.7 && search_mode.fuzzy
291 } else {
292 assert false
293 }
294 given := parse_mode(['vpac', 's', '--thold', '0.95', 'foo'])!
295 if given is ConfigSearch {
296 assert given.thold == 0.95
297 } else {
298 assert false
299 }
300 rank_mode := parse_mode(['vpac', 'k', 'arch'])!
301 if rank_mode is ConfigRank {
302 assert rank_mode.top == 3
303 } else {
304 assert false
305 }
306 rank_given := parse_mode(['vpac', 'k', '-n', '7', 'arch'])!
307 if rank_given is ConfigRank {
308 assert rank_given.top == 7
309 } else {
310 assert false
311 }
312 clean_mode := parse_mode(['vpac', 'c'])!
313 if clean_mode is ConfigClean {
314 assert clean_mode.keep == 2
315 } else {
316 assert false
317 }
318 tree_mode := parse_mode(['vpac', 't', 'foo'])!
319 if tree_mode is ConfigTree {
320 assert tree_mode.depth == 4
321 } else {
322 assert false
323 }
324
325 color_flag = 'always'
326 assert version_style('1') == '\x1b[36m1\x1b[39m'
327 assert name_style('n') == 'n'
328 assert warning('w').starts_with('\x1b[')
329
330 os.write_file(cfg, '(path :db "${tmp}/db/")\n')!
331 load_config(cfg, [])!
332 assert sync_max_age == 24 * 60 * 60
333 assert cfg_search_fields == search_default_fields
334 assert rank_top == 10 && cfg_clean_keep == 0 && cfg_tree_depth == 0
335 assert theme == default_theme()
336}
337
338// the live display and the ranker's oses are the config's too: a bar's
339// graphic and columns, a spinner's frames, and an os defined beside the
340// ranker's own, which a mirrorlist on the disk is enough for
341fn test_config_display_and_mirrorlists() ! {
342 tmp := config_tmp('display')!
343 defer {
344 os.rmdir_all(tmp) or {}
345 reset_config()
346 }
347 list := os.join_path(tmp, 'mirrorlist')
348 os.write_file(list, 'Server = https://one.example/\$repo/os/\$arch\n# a comment\nServer = https://two.example/\$repo/os/\$arch\n')!
349 cfg := os.join_path(tmp, 'config.lip')
350 os.write_file(cfg, '(progress :style "classic" :start "|" :end "|" :fill "#" :head "" :empty "." :length 12 :percent false :eta false :elapsed true :rate false :delay 100)\n' +
351 '(spinner :frames ["a" "b"] :interval 200 :done "ok" :elapsed true)\n' +
352 '(mirrorlist :name "myos" :list "${list}" :repo "main")\n' +
353 '(mirrorlist :name "arch" :status "https://example.org/status.json" :repo "core")\n' +
354 '(rank :dir "${tmp}/ranked/")\n')!
355 load_config(cfg, [])!
356
357 bar := bar_opts(text: 'x', show_rate: true)
358 assert bar.text == 'x' && bar.length == 12
359 assert !bar.show_percent && !bar.show_left && bar.show_elapsed && !bar.show_rate
360 style := bar.style
361 if style is progress.ClassicStyle {
362 assert style.start == '|' && style.end == '|'
363 assert style.fill == `#` && style.empty == `.` && style.head == 0
364 } else {
365 assert false, 'the bar is not the classic one'
366 }
367 spin := spinner_opts(text: 'working')
368 assert spin.text == 'working' && spin.frames == ['a', 'b']
369 assert spin.interval == 200 * time.millisecond
370 assert spin.done_frame == 'ok' && spin.show_elapsed
371
372 assert rank_os_name(['myos'])! == 'myos'
373 assert rank_oses['arch'].status_json == 'https://example.org/status.json'
374 assert 'artix' in rank_oses
375 mirrors := fetch_mirrorlist(rank_oses['myos'])!
376 assert mirrors.len == 2, mirrors.str()
377 assert rank_write_target('myos') == '${tmp}/ranked/myos.lip'
378
379 os.write_file(cfg, '(spinner :frames "line")\n(progress :style "block")\n')!
380 load_config(cfg, [])!
381 assert cfg_progress.frames == progress.spinner_line()
382 assert bar_opts(text: 'x').style is progress.BlockStyle
383 assert bar_opts(text: 'x').length == 30
384 assert 'myos' !in rank_oses
385 assert rank_write_target('arch') == '/etc/vpac.d/mirrors/arch.lip'
386}
387
388// an option the config turns off holds no entry: only what is on is kept,
389// and a false after a true takes the true back
390fn test_config_options_hold_only_what_is_on() ! {
391 tmp := config_tmp('optflags')!
392 defer {
393 os.rmdir_all(tmp) or {}
394 reset_config()
395 }
396 cfg := os.join_path(tmp, 'config.lip')
397 os.write_file(cfg, '(options :noconfirm false :needed true)\n')!
398 load_config(cfg, [])!
399 assert 'noconfirm' !in opt_flags
400 assert opt_flags == {
401 'needed': true
402 }
403 // a later form, as an --eval is, turns it off again and leaves nothing
404 load_config(cfg, ['(options :needed false)'])!
405 assert opt_flags.len == 0
406 mode := parse_mode(['vpac', 'i', 'foo'])! as ConfigInstall
407 assert !mode.needed && !mode.noconfirm
408 // the key of the flag nothing read is gone with the flag
409 for form in ['(options :upgrade-deps true)', '(options :verbose "yes")'] {
410 os.write_file(cfg, form + '\n')!
411 if _ := load_config(cfg, []) {
412 assert false, '${form} was accepted'
413 }
414 }
415}
416
417// options :verbose turns the verbose log on for every run, and a load that
418// does not name it turns it off again: it is the config's, reset with it
419fn test_config_verbose_is_reset_with_the_config() ! {
420 tmp := config_tmp('verbose')!
421 saved_flag := verbose_flag
422 defer {
423 verbose_flag = saved_flag
424 os.rmdir_all(tmp) or {}
425 reset_config()
426 }
427 verbose_flag = false
428 cfg := os.join_path(tmp, 'config.lip')
429 os.write_file(cfg, '(options :verbose true)\n')!
430 load_config(cfg, [])!
431 assert cfg_verbose && verbose_on()
432 // an --eval after the file takes it back for a run
433 load_config(cfg, ['(options :verbose false)'])!
434 assert !cfg_verbose && !verbose_on()
435 os.write_file(cfg, '(options :color "never")\n')!
436 load_config(cfg, [])!
437 assert !cfg_verbose
438 // the command line's flag is not the config's to reset
439 verbose_flag = true
440 load_config(cfg, [])!
441 assert verbose_on() && !cfg_verbose
442 // and it is no mode flag: no mode's struct grows a field from it
443 mode := parse_mode(['vpac', 'i', 'foo'])! as ConfigInstall
444 assert mode.query == ['foo']
445}
446