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