module main import os import time import x.progress fn config_tmp(name string) !string { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_config_${name}_${os.getpid()}') os.mkdir_all(os.join_path(tmp, 'db', 'local'))! os.write_file(os.join_path(tmp, 'db', 'local', 'ALPM_DB_VERSION'), '9\n')! return tmp } fn test_config_scheme_load_and_eval_share_bindings() ! { tmp := config_tmp('scheme_load')! defer { reset_config() os.rmdir_all(tmp) or { went_on('remove the test config', err) } } os.mkdir_all(os.join_path(tmp, 'parts'))! os.write_file(os.join_path(tmp, 'parts', 'network.scm'), '(define parallel 6)\n(network #:parallel parallel)')! cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(load "parts/network.scm")\n(pkg #:hold (list "glibc" "pacman"))')! load_config(cfg, ['(network #:parallel (+ parallel 1))'])! assert parallel_downloads == 7 assert cfg_hold == ['glibc', 'pacman'] } fn test_config_vile_assignments_and_repositories() ! { tmp := config_tmp('vile')! defer { reset_config() os.rmdir_all(tmp) or { went_on('remove the test config', err) } } cfg := os.join_path(tmp, 'config.vile') os.write_file(os.join_path(tmp, 'mirrors.vile'), 'servers := ["https://example.org/repo"]')! os.write_file(cfg, 'load("mirrors.vile") network-connections = 123 network-disable-timeout = true options-color = "never" rank-parallel = network-connections repositories = list(sync-repo("core", servers, ["search"]), pkgbuild-repo("aur", "https://aur.example")) ')! load_config(cfg, ['network-connections = network-connections + 1'])! assert parallel_downloads == 124 && rank_parallel == 123 assert cfg_disable_timeout && opt_color == 'never' assert cfg_repos.map(it.name) == ['core', 'aur'] assert cfg_repos[0].servers == ['https://example.org/repo'] assert cfg_repos[0].usage == .search && cfg_repos[1].kind == 'pkgbuild' assert rank_write_target('arch').ends_with('/arch.vile') load_config(cfg, ['(network #:parallel 5)', 'network-connections = network-connections + 2'])! assert parallel_downloads == 7 load_config(cfg, ['repositories = list(sync-repo("other", servers))'])! assert cfg_repos.map(it.name) == ['other'] load_config(cfg, ['(repo #:name "extra" #:src "file:///extra")', 'repositories = append(repositories, list(sync-repo("other", servers)))'])! assert cfg_repos.map(it.name) == ['core', 'aur', 'extra', 'other'] for bad in ['network-connections = 0', 'network-connetions = 8', 'repositories = list(sync-repo("local", servers))'] { load_config(cfg, [bad]) or { continue } assert false, 'accepted invalid setting: ${bad}' } } fn test_config_options() ! { tmp := config_tmp('options')! defer { os.rmdir_all(tmp) or {} } cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(path #:db "${tmp}/db/")\n' + '(pkg #:ignoregroup #("kde-applications") #:noupgrade #("etc/foo.conf") #:noextract #("usr/share/doc/*" "usr/share/man/*"))\n' + '(path #:syslog #t)\n' + '(network #:disable-timeout #t #:disable-sandbox #t)\n' + r'(repo #:name "core" #:src #("https://example.org/$repo") #:usage #("sync" "Search"))' + '\n' + r'(repo #:name "extra" #:src #("https://example.org/$repo"))' + '\n')! load_config(cfg, [])! assert cfg_ignore_groups == ['kde-applications'] assert cfg_noupgrade == ['etc/foo.conf'] assert cfg_noextract == ['usr/share/doc/*', 'usr/share/man/*'] assert cfg_disable_timeout assert cfg_disable_sandbox assert cfg_syslog core := cfg_repos.filter(it.name == 'core')[0] extra := cfg_repos.filter(it.name == 'extra')[0] // pacman's Usage bits: Sync 1, Search 2; every use when none is given assert int(core.usage) == 1 | 2 assert int(extra.usage) == 15 // and libalpm gets them from init_handle ui := new_trans_ui() mut handle := init_handle(ui)! defer { handle.release() or {} } mut groups := handle.option_get_ignore_groups() assert groups.strs() == ['kde-applications'] mut noupgrade := handle.option_get_noupgrades() assert noupgrade.strs() == ['etc/foo.conf'] mut noextract := handle.option_get_no_extracts() assert noextract.strs() == ['usr/share/doc/*', 'usr/share/man/*'] assert handle.option_match_no_extract('usr/share/doc/zsh/README') assert !handle.option_match_no_extract('usr/bin/zsh') assert handle.option_get_disable_sandbox_filesystem() assert handle.option_get_disable_sandbox_syscalls() } // the sandbox stays on unless the config turns it off: a config that does not // name it, or names it false, leaves libalpm's sandbox on for the handle fn test_config_disable_sandbox_defaults_on() ! { tmp := config_tmp('sandbox')! defer { os.rmdir_all(tmp) or {} reset_config() } cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(network #:parallel 2)\n')! load_config(cfg, [])! assert !cfg_disable_sandbox ui := new_trans_ui() mut handle := init_handle(ui)! defer { handle.release() or {} } assert !handle.option_get_disable_sandbox_filesystem() assert !handle.option_get_disable_sandbox_syscalls() // a false written out is the same as leaving it out os.write_file(cfg, '(network #:disable-sandbox #f)\n')! load_config(cfg, [])! assert !cfg_disable_sandbox } fn test_config_rejects_bad_values() ! { tmp := config_tmp('bad')! defer { os.rmdir_all(tmp) or {} } cfg := os.join_path(tmp, 'config.scm') for form, says in { '(repo #:name "x" #:usage #("sometimes"))': 'takes sync, search, install, upgrade and all' '(repo #:name "x" #:usage #())': 'needs at least one' '(network #:disable-timeout "yes")': 'must be #t or #f' '(network #:disable-sandbox "yes")': 'must be #t or #f' // an empty one verifies nothing, and a list of no protocols accepts // no transfers; a repo that names no servers has nothing to fetch '(network #:siglevel #())': 'needs at least one of' '(network #:protocols #())': 'needs at least one' '(network #:protocols #("ftp"))': 'takes https and http' '(network #:protocols #("https" "HTTPS"))': 'takes https and http' '(pkg #:arch "")': 'must be one plain word' '(pkg #:arch "x86 64")': 'must be one plain word' '(pkg #:arch " x86_64")': 'must be one plain word' '(pkg #:arch "a/b")': 'must be one plain word' '(repo #:name "local" #:src #("http://x"))': 'cannot be named local' '(repo #:name "x" #:src #("http://x")) (repo #:name "x" #:src #("http://y"))': 'is defined twice' '(repo #:name "x" #:src #("http://x")) (repo #:name "x" #:type "pkgbuild" #:src #("http://y"))': 'is defined twice' '(mirrorlist #:name "my os" #:repo "core" #:status "https://x")': 'one plain word' '(mirrorlist #:name "os\t" #:repo "core" #:status "https://x")': 'one plain word' '(repo #:name "x" #:src #())': 'needs at least one url' '(repo #:name "x" #:src "")': 'has an empty url in it' '(repo #:name "x" #:type "sync")': 'takes at least one #:src' '(options #:root " ")': 'must not be empty' '(options #:colour "always")': 'has no key #:colour' '(options #:color "sometimes")': 'takes auto, always or never' '(options #:noconfirm "yes")': 'must be #t or #f' '(options #:suggest 2)': 'between 0 and 1' '(options #:sync-age -1)': 'at least 0' '(search #:fields #("nope"))': 'unknown field' '(search #:thold "high")': 'must be a number' '(rank #:top 0)': 'at least 1' '(pkgbuild #:timeout 0)': 'at least 1' '(color #:nope "red")': 'has no role #:nope' '(progress #:style "round")': 'classic or block' '(progress #:fill "==")': 'must be one character' '(progress #:empty "")': 'must be one character' '(spinner #:frames "nope")': 'not "nope"' '(spinner #:frames #())': 'at least one frame' '(mirrorlist #:name "x" #:repo "core")': 'one of #:status and #:list' '(mirrorlist #:name "x" #:list "/m")': 'needs a #:repo' '(mirrorlist #:name "a/b" #:list "/m" #:repo "c")': 'one plain word' '(mirrorlist #:name "x" #:status "/m" #:repo "c")': 'http or https url' '(rank #:dir "mirrors")': 'absolute path' '(color #:name "pink")': 'not "pink"' } { os.write_file(cfg, form + '\n')! if _ := load_config(cfg, []) { assert false, '${form} was accepted' } else { assert err.msg().contains(says), '${form}: ${err.msg()}' } } // a list may be a single string, as everywhere in the config os.write_file(cfg, '(pkg #:noupgrade "etc/foo.conf")\n')! load_config(cfg, [])! assert cfg_noupgrade == ['etc/foo.conf'] } // a second load answers nothing of the first's: every global is back at its // default before the file is read, so nothing is kept that the new file does // not name, and no repo of the old load survives fn test_config_resets_between_loads() ! { tmp := config_tmp('reset')! defer { os.rmdir_all(tmp) or {} } cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(repo #:name "one" #:src #("https://one.example/$repo"))\n (network #:parallel 5 #:siglevel #("package" "database"))\n(pkg #:hold #("glibc") #:ignore #("flatpak"))\n(options #:root "doas -n")\n')! load_config(cfg, [])! assert root_provider == 'doas -n' assert cfg_repos.map(it.name) == ['one'] assert cfg_repos.len == 1 assert parallel_downloads == 5 assert cfg_hold == ['glibc'] assert cfg_ignore == ['flatpak'] assert cfg_disable_timeout == false os.write_file(cfg, '(repo #:name "two" #:src "https://two.example/$repo")\n')! load_config(cfg, [])! assert cfg_repos.map(it.name) == ['two'] assert cfg_repos.len == 1 assert parallel_downloads == 3 assert root_provider == 'sudo' assert opt_color == '' assert opt_flags.len == 0 assert cfg_hold == []string{} assert cfg_ignore == []string{} } // check_hold refuses a name on the hold list, the targets of a removal as // well as whatever a transaction pulls in beyond them fn test_check_hold() { mut old := cfg_hold.clone() defer { cfg_hold = old.clone() } cfg_hold = ['held', 'base'] check_hold(['other', 'more']) or { assert false, 'a name off the hold list was refused' } if _ := check_hold(['fine', 'held']) { assert false, 'a held package was let through' } else { assert err.msg().contains('held'), err.msg() } } // the keyring dir defaults to pacman's GPGDir, so a config that names no // #:gpg checks signatures against the system keyring, not root's ~/.gnupg; // a named one overrides it for its load only fn test_config_gpgdir_default() ! { tmp := config_tmp('gpgdir')! defer { os.rmdir_all(tmp) or {} } cfg := os.join_path(tmp, 'config.scm') saved := cfg_gpgdir defer { cfg_gpgdir = saved } os.write_file(cfg, '(network #:parallel 2)\n')! load_config(cfg, [])! assert cfg_gpgdir == '/etc/pacman.d/gnupg/' os.write_file(cfg, '(path #:gpg "${tmp}/keys")\n')! load_config(cfg, [])! assert cfg_gpgdir == '${tmp}/keys/' os.write_file(cfg, '(network #:parallel 2)\n')! load_config(cfg, [])! assert cfg_gpgdir == '/etc/pacman.d/gnupg/' } // the default reaches the handle every writing mode opens fn test_init_handle_sets_the_default_gpgdir() ! { tmp := config_tmp('gpghandle')! defer { os.rmdir_all(tmp) or {} } os.mkdir_all(os.join_path(tmp, 'root'))! saved_root, saved_db, saved_user := root_path, db_path, download_user defer { reset_config() root_path, db_path, download_user = saved_root, saved_db, saved_user } reset_config() root_path = os.join_path(tmp, 'root') + '/' db_path = os.join_path(tmp, 'db') + '/' // the sandbox user need not exist where the tests run download_user = '' ui := new_trans_ui() handle := init_handle(ui)! defer { handle.release() or {} } assert handle.option_get_gpgdir() == '/etc/pacman.d/gnupg/' } // the config's options are the flags a run starts with: on for every mode // that has the flag, untouched where it has none, and off again by --eval fn test_config_options_set_mode_flags() ! { tmp := config_tmp('flags')! defer { os.rmdir_all(tmp) or {} reset_config() } cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(options #:noconfirm #t #:needed #t #:nopkgbuildconfirm #t #:no-upgrade-deps #t #:color "never")\n')! load_config(cfg, [])! assert opt_color == 'never' install_mode := parse_mode(['vpac', 'i', 'foo'])! if install_mode is ConfigInstall { assert install_mode.noconfirm && install_mode.needed assert install_mode.no_pkgbuild_confirm && install_mode.no_upgrade_deps assert install_mode.query == ['foo'] } else { assert false } remove_mode := parse_mode(['vpac', 'r', 'foo'])! if remove_mode is ConfigRemove { assert remove_mode.noconfirm } else { assert false } load_config(cfg, ['(options #:noconfirm #f)'])! again := parse_mode(['vpac', 'i', 'foo'])! if again is ConfigInstall { assert !again.noconfirm && again.needed } else { assert false } } // the tunables the modes read are the config's: thresholds, sizes, the // defaults a flag would otherwise carry, and the theme. a flag on the // command line still wins, and the next load puts every one back fn test_config_tunables() ! { tmp := config_tmp('tunables')! old_color := color_flag defer { os.rmdir_all(tmp) or {} color_flag = old_color reset_config() } cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(options #:sync-age 0 #:suggest 0.5 #:fuzzy #t)\n' + '(search #:fields #("name" "version") #:thold 0.7)\n' + '(rank #:top 3 #:candidates 12 #:retest 0 #:completion 0.9 #:max-delay 3600)\n' + '(pkgbuild #:timeout 30)\n(clean #:keep 2)\n(tree #:depth 4)\n' + '(color #:version "cyan" #:warning #("bold" "magenta") #:name "none")\n')! load_config(cfg, [])! assert sync_max_age == 0 assert suggest_thold == 0.5 assert cfg_search_fields == ['name', 'version'] assert rank_speed_candidates == 12 && rank_retest == 0 assert rank_min_completion == 0.9 && rank_max_delay == 3600 assert pkgbuild_rpc_timeout == 30 search_mode := parse_mode(['vpac', 's', 'foo'])! if search_mode is ConfigSearch { assert search_mode.thold == 0.7 && search_mode.fuzzy } else { assert false } given := parse_mode(['vpac', 's', '--thold', '0.95', 'foo'])! if given is ConfigSearch { assert given.thold == 0.95 } else { assert false } rank_mode := parse_mode(['vpac', 'k', 'arch'])! if rank_mode is ConfigRank { assert rank_mode.top == 3 } else { assert false } rank_given := parse_mode(['vpac', 'k', '-n', '7', 'arch'])! if rank_given is ConfigRank { assert rank_given.top == 7 } else { assert false } clean_mode := parse_mode(['vpac', 'c'])! if clean_mode is ConfigClean { assert clean_mode.keep == 2 } else { assert false } tree_mode := parse_mode(['vpac', 't', 'foo'])! if tree_mode is ConfigTree { assert tree_mode.depth == 4 } else { assert false } color_flag = 'always' assert version_style('1') == '\x1b[36m1\x1b[39m' assert name_style('n') == 'n' assert warning('w').starts_with('\x1b[') os.write_file(cfg, '(path #:db "${tmp}/db/")\n')! load_config(cfg, [])! assert sync_max_age == 24 * 60 * 60 assert cfg_search_fields == search_default_fields assert rank_top == 10 && cfg_clean_keep == 0 && cfg_tree_depth == 0 assert theme == default_theme() } // the live display and the ranker's oses are the config's too: a bar's // graphic and columns, a spinner's frames, and an os defined beside the // ranker's own, which a mirrorlist on the disk is enough for fn test_config_display_and_mirrorlists() ! { tmp := config_tmp('display')! defer { os.rmdir_all(tmp) or {} reset_config() } list := os.join_path(tmp, 'mirrorlist') os.write_file(list, 'Server = https://one.example/\$repo/os/\$arch\n# a comment\nServer = https://two.example/\$repo/os/\$arch\n')! cfg := os.join_path(tmp, 'config.scm') 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' + '(spinner #:frames #("a" "b") #:interval 200 #:done "ok" #:elapsed #t)\n' + '(mirrorlist #:name "myos" #:list "${list}" #:repo "main")\n' + '(mirrorlist #:name "arch" #:status "https://example.org/status.json" #:repo "core")\n' + '(rank #:dir "${tmp}/ranked/")\n')! load_config(cfg, [])! bar := bar_opts(text: 'x', show_rate: true) assert bar.text == 'x' && bar.length == 12 assert !bar.show_percent && !bar.show_left && bar.show_elapsed && !bar.show_rate style := bar.style if style is progress.ClassicStyle { assert style.start == '|' && style.end == '|' assert style.fill == `#` && style.empty == `.` && style.head == 0 } else { assert false, 'the bar is not the classic one' } spin := spinner_opts(text: 'working') assert spin.text == 'working' && spin.frames == ['a', 'b'] assert spin.interval == 200 * time.millisecond assert spin.done_frame == 'ok' && spin.show_elapsed assert rank_os_name(['myos'])! == 'myos' assert rank_oses['arch'].status_json == 'https://example.org/status.json' assert 'artix' in rank_oses mirrors := fetch_mirrorlist(rank_oses['myos'])! assert mirrors.len == 2, mirrors.str() assert rank_write_target('myos') == '${tmp}/ranked/myos.scm' os.write_file(cfg, '(spinner #:frames "line")\n(progress #:style "block")\n')! load_config(cfg, [])! assert cfg_progress.frames == progress.spinner_line() assert bar_opts(text: 'x').style is progress.BlockStyle assert bar_opts(text: 'x').length == 30 assert 'myos' !in rank_oses assert rank_write_target('arch') == '/etc/vpac.d/mirrors/arch.scm' } // an option the config turns off holds no entry: only what is on is kept, // and a false after a true takes the true back fn test_config_options_hold_only_what_is_on() ! { tmp := config_tmp('optflags')! defer { os.rmdir_all(tmp) or {} reset_config() } cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(options #:noconfirm #f #:needed #t)\n')! load_config(cfg, [])! assert 'noconfirm' !in opt_flags assert opt_flags == { 'needed': true } // a later form, as an --eval is, turns it off again and leaves nothing load_config(cfg, ['(options #:needed #f)'])! assert opt_flags.len == 0 mode := parse_mode(['vpac', 'i', 'foo'])! as ConfigInstall assert !mode.needed && !mode.noconfirm // the key of the flag nothing read is gone with the flag for form in ['(options #:upgrade-deps #t)', '(options #:verbose "yes")'] { os.write_file(cfg, form + '\n')! if _ := load_config(cfg, []) { assert false, '${form} was accepted' } } } // options #:verbose turns the verbose log on for every run, and a load that // does not name it turns it off again: it is the config's, reset with it fn test_config_verbose_is_reset_with_the_config() ! { tmp := config_tmp('verbose')! saved_flag := verbose_flag defer { verbose_flag = saved_flag os.rmdir_all(tmp) or {} reset_config() } verbose_flag = false cfg := os.join_path(tmp, 'config.scm') os.write_file(cfg, '(options #:verbose #t)\n')! load_config(cfg, [])! assert cfg_verbose && verbose_on() // an --eval after the file takes it back for a run load_config(cfg, ['(options #:verbose #f)'])! assert !cfg_verbose && !verbose_on() os.write_file(cfg, '(options #:color "never")\n')! load_config(cfg, [])! assert !cfg_verbose // the command line's flag is not the config's to reset verbose_flag = true load_config(cfg, [])! assert verbose_on() && !cfg_verbose // and it is no mode flag: no mode's struct grows a field from it mode := parse_mode(['vpac', 'i', 'foo'])! as ConfigInstall assert mode.query == ['foo'] }