module main import alpm import os // --eval takes its blip from the next argument or from =; an empty one is // refused like an empty --config or --color value is fn test_scan_globals_eval() ! { mut chosen := '' mut evals := []string{} mut rest := []string{} chosen, evals, rest = scan_globals(['vpac', '--eval', '(define x 1)', 's', 'zsh'])! assert chosen == '' assert evals == ['(define x 1)'] assert rest == ['vpac', 's', 'zsh'] chosen, evals, rest = scan_globals(['vpac', '--eval=(define x 2)', '--config=other.lip', 's'])! assert chosen == 'other.lip' assert evals == ['(define x 2)'] assert rest == ['vpac', 's'] for arg in ['--eval=', '--eval', '--config=', '--config'] { if _, _, _ := scan_globals(['vpac', arg]) { assert false, '"${arg}" was taken without its value' } else { assert err.msg().contains('needs'), '"${arg}": ${err.msg()}' } } } // the global flags parse wherever they stand, and none of them bleeds into // the rest the mode sees fn test_scan_globals_mixed() ! { saved_kind, saved_color := ui_kind, color_flag defer { ui_kind, color_flag = saved_kind, saved_color } _, _, rest := scan_globals(['vpac', 'i', '--color=never', 'zsh', '--ui', 'ipc'])! assert rest == ['vpac', 'i', 'zsh'] } fn test_scan_ui() { for args, want in { 'vpac s --ui ipc': 'ipc' 'vpac --ui=ipc s': 'ipc' 'vpac --ui tty s': 'tty' // the last valid one wins, as in scan_globals 'vpac --ui tty s --ui ipc': 'ipc' 'vpac --ui=ipc --ui=tty': 'tty' // another global's value is stepped over, not read as a flag 'vpac --eval --ui ipc': '' 'vpac --config --ui --ui ipc': 'ipc' // no valid one: none, and scan_globals says why on the terminal 'vpac s zsh': '' 'vpac --ui bogus s': '' 'vpac --ui': '' 'vpac --ui=': '' } { got := scan_ui(args.split(' ')) or { '' } assert got == want, args } } // an error in any global is told the frontend's way when --ui ipc is on the // command line, wherever it stands: the ui is settled before the error fn test_scan_globals_settles_the_ui_before_failing() { saved_kind := ui_kind defer { ui_kind = saved_kind } for args in ['vpac --color sometimes --ui ipc s', 'vpac --color sometimes s --ui=ipc', 'vpac --ui ipc --color sometimes s'] { ui_kind = 'tty' if _, _, _ := scan_globals(args.split(' ')) { assert false, '${args} was accepted' } assert ui_kind == 'ipc', args } // a bad --ui itself names no frontend: the terminal tells it ui_kind = 'tty' if _, _, _ := scan_globals('vpac --color sometimes --ui bogus s'.split(' ')) { assert false } assert ui_kind == 'tty' } // the doubled flags are counts, as pacman reads -ss, -cc, -gg, -tt and -uu fn test_doubled_flags_count() ! { r := parse_mode(['vpac', 'r', '-ss', '-u', 'foo'])! as ConfigRemove assert r.recursive_level == 2 && r.unneeded && r.query == ['foo'] assert recurse_flags(r.recursive, r.recursive_level) == alpm.TransFlag.recurse | .recurse_all one := parse_mode(['vpac', 'r', '--recursive', 'foo'])! as ConfigRemove assert recurse_flags(one.recursive, one.recursive_level) == alpm.TransFlag.recurse c := parse_mode(['vpac', 'c', '-cc'])! as ConfigClean assert c.level == 2 && !c.keep_current kc := parse_mode(['vpac', 'c', '--keep-current'])! as ConfigClean assert kc.keep_current && kc.level == 0 u := parse_mode(['vpac', 'u', '-uu'])! as ConfigUpgrade assert u.sysupgrade_level == 2 d := parse_mode(['vpac', 'd', '-tt'])! as ConfigOrphans assert d.level == 2 } fn test_search_group_and_unrequired_counts() ! { g := parse_mode(['vpac', 's', '-gg'])! as ConfigSearch assert g.groups && g.groups_level == 2 // groups are the sync dbs' unless -l asks for the installed ones assert g.remote && !g.local lg := parse_mode(['vpac', 's', '-l', '-g', 'base'])! as ConfigSearch assert lg.groups && lg.local && !lg.remote && lg.query == ['base'] long := parse_mode(['vpac', 's', '--groups'])! as ConfigSearch assert long.groups t := parse_mode(['vpac', 's', '--unrequired'])! as ConfigSearch assert t.unrequired && t.unrequired_level == 1 && t.local tt := parse_mode(['vpac', 's', '--unrequired', '-d', '--unrequired'])! as ConfigSearch assert tt.unrequired && tt.unrequired_level == 2 && tt.deps } fn test_new_query_flags_parse() ! { q := parse_mode(['vpac', 'q', '-c', '-p', 'a.pkg.tar', 'b.pkg.tar'])! as ConfigInfo assert q.changelog && q.file && q.query == ['a.pkg.tar', 'b.pkg.tar'] o := parse_mode(['vpac', 'o', '-q', '/usr/bin/ls'])! as ConfigOwner assert o.quiet f := parse_mode(['vpac', 'f', '-x', '--machinereadable', 'ls$'])! as ConfigFind assert f.regex && f.machinereadable && f.query == ['ls$'] } fn test_ranged_flags_are_checked() ! { // the flags are held to the ranges the config's forms are for bad in [['vpac', 's', '--thold', '2', 'x'], ['vpac', 's', '-t', '1.5', 'x'], ['vpac', 's', '--thold', '-1', 'x'], ['vpac', 'k', '--top', '0'], ['vpac', 'k', '--top', '-2']] { if _ := parse_mode(bad) { assert false, 'parsed ${bad}' } } s := parse_mode(['vpac', 's', '--thold', '0.5', 'x'])! as ConfigSearch assert s.thold == 0.5 k := parse_mode(['vpac', 'k', '--top', '3'])! as ConfigRank assert k.top == 3 } fn test_help_flag_is_the_parsers() ! { // bundled with other flags it still asks for help; as another flag's // value it does not s := parse_mode(['vpac', 's', '-lh'])! as ConfigSearch assert s.show_help && s.local q := parse_mode(['vpac', 'q', '--fields', '-h', 'x'])! as ConfigInfo assert !q.show_help && q.fields == '-h' } fn test_parse_target() { assert parse_target('bash') == Target{ name: 'bash' } assert parse_target('core/bash') == Target{'core', 'bash'} assert parse_target('local/bash>=5') == Target{'local', 'bash>=5'} // a leading slash is no db prefix assert parse_target('/bash').repo == '' assert local_target_name('local/bash') == 'bash' assert local_target_name('core/bash') == 'core/bash' assert has_constraint('bash>=5') && has_constraint('sh=1') && !has_constraint('bash') } fn test_flags_nothing_read_are_gone() ! { // install's -u, -s and -v and search's -v parsed into fields no code // read: a flag that does nothing is refused instead of taken for dead in [['vpac', 'i', '-u', 'x'], ['vpac', 'i', '--upgrade-deps', 'x'], ['vpac', 'i', '-s', 'x'], ['vpac', 'i', '--sync', 'x'], ['vpac', 'i', '-v', 'x'], ['vpac', 's', '-v', 'x']] { if _ := parse_mode(dead) { assert false, 'parsed ${dead}' } } // the ones that do something still parse, bundled too w := parse_mode(['vpac', 'i', '-w', 'x'])! as ConfigInstall assert w.download_only && w.query == ['x'] wp := parse_mode(['vpac', 'i', '-wp', '--no-upgrade-deps', 'x'])! as ConfigInstall assert wp.download_only && wp.print_only && wp.no_upgrade_deps s := parse_mode(['vpac', 's', '-s', 'x'])! as ConfigSearch assert s.sync_dbs } fn test_unknown_mode_flags_are_errors() ! { // a flag the mode has no field for is named, not dropped: long, short, // bundled with known ones, before or after the targets, and a mistyped // spelling of a real one for bad in [['vpac', 'q', '--bogus', 'bash'], ['vpac', 'q', '-Z', 'bash'], ['vpac', 'q', 'bash', '--bogus'], ['vpac', 'i', '--noconfrim', 'x'], ['vpac', 'i', '--asdep', 'x'], ['vpac', 'r', '--print', '--bogus', 'x'], ['vpac', 's', '-t', '-1', 'x']] { if c := parse_mode(bad) { assert false, 'parsed ${bad} as ${c}' } else { assert err.msg().contains('unknown option') || err.msg().contains('--thold'), err.msg() } } named := parse_mode(['vpac', 'q', '--bogus', '-Z', 'bash']) or { assert err.msg() == 'unknown option: "--bogus", "-Z"', err.msg() ModeConfig(ConfigInfo{}) } assert (named as ConfigInfo).query.len == 0 // what is known still parses in every position, and a mode with no // flags given at all is not an error q := parse_mode(['vpac', 'q', 'bash', '-c', 'zsh'])! as ConfigInfo assert q.changelog && q.query == ['bash', 'zsh'] bare := parse_mode(['vpac', 'd'])! as ConfigOrphans assert bare.level == 0 // targets that stand before a flag are targets all the same, in the // order given, with the ones after it mixed := parse_mode(['vpac', 'i', 'a', '--needed', 'b', '-w', 'c', 'd'])! as ConfigInstall assert mixed.query == ['a', 'b', 'c', 'd'] && mixed.needed && mixed.download_only lead := parse_mode(['vpac', 'r', 'a', 'b', '--print'])! as ConfigRemove assert lead.query == ['a', 'b'] && lead.print_only // a lone dash names neither a flag nor a target: the parser's error if d := parse_mode(['vpac', 'q', '-', '-c']) { assert false, 'a lone dash parsed: ${d}' } // a mode with no targets says so of one given before a flag if o := parse_mode(['vpac', 'd', 'stray', '-t']) { assert false, 'orphans took a target: ${o}' } else { assert err.msg().contains('takes no targets'), err.msg() } } fn test_needs_root_by_mode() ! { root := fn (args []string) !bool { return needs_root(parse_mode(args)!)! } // what changes the system runs as root for args in [['vpac', 'i', 'x'], ['vpac', 'r', 'x'], ['vpac', 'u'], ['vpac', 'c'], ['vpac', 'm', '-r', 'd', 'x'], ['vpac', 'a'], ['vpac', 'b', '/x'], ['vpac', 'f', '-y', '/x'], ['vpac', 'i', '-w', 'x'], ['vpac', 'u', '-w'], ['vpac', 'i', '--dbonly', 'x']] { assert root(args)!, '${args} runs as the user' } // a --print preview changes nothing and stays the user's, whatever // else is asked beside it for args in [['vpac', 'i', '-p', 'x'], ['vpac', 'r', '--print', 'x'], ['vpac', 'u', '-p'], ['vpac', 'c', '-p'], ['vpac', 'i', '-p', '--dbonly', '--noconfirm', 'x'], ['vpac', 'c', '-p', '-a']] { assert !root(args)!, '${args} asks for root' } // what only reads never asks for root for args in [['vpac', 's', 'x'], ['vpac', 'q', 'x'], ['vpac', 'w', 'x'], ['vpac', 'n', 'x'], ['vpac', 'l', 'x'], ['vpac', 'o', '/x'], ['vpac', 'd'], ['vpac', 't', 'x'], ['vpac', 'v'], ['vpac', 'f', '/x'], ['vpac', 'f', '-l', 'x'], ['vpac', 'k', 'arch']] { assert !root(args)!, '${args} asks for root' } } fn test_needs_root_for_rank_follows_the_file() ! { if os.geteuid() == 0 { // root may write anywhere: there is no 'not the user's' to tell return } tmp := os.join_path(os.vtmp_dir(), 'vpac_test_needs_root_${os.getpid()}') os.mkdir_all(os.join_path(tmp, 'ro'))! saved_dir := rank_dir defer { rank_dir = saved_dir os.chmod(os.join_path(tmp, 'ro'), 0o755) or {} os.rmdir_all(tmp) or {} } os.chmod(os.join_path(tmp, 'ro'), 0o555)! os_name := rank_oses.keys()[0] // printing a ranking writes nothing assert !needs_root(ConfigRank{ query: [os_name] })! // -o: a file the user may write, or may create, is the user's assert !needs_root(ConfigRank{ output: os.join_path(tmp, 'new.lip'), query: [os_name] })! os.write_file(os.join_path(tmp, 'there.lip'), '')! assert !needs_root(ConfigRank{ output: os.join_path(tmp, 'there.lip'), query: [os_name] })! // one it may not is root's: an existing read-only file, a new file in // a read-only dir, and a path under a dir that is not there os.chmod(os.join_path(tmp, 'there.lip'), 0o444)! assert needs_root(ConfigRank{ output: os.join_path(tmp, 'there.lip'), query: [os_name] })! assert needs_root(ConfigRank{ output: os.join_path(tmp, 'ro', 'new.lip'), query: [os_name] })! assert needs_root(ConfigRank{ output: os.join_path(tmp, 'gone', 'new.lip'), query: [os_name] })! // -w writes the config's mirror file: the rank dir decides, and -o, // when both are given, is the file that is written rank_dir = tmp assert !needs_root(ConfigRank{ write: true, query: [os_name] })! rank_dir = os.join_path(tmp, 'ro') assert needs_root(ConfigRank{ write: true, query: [os_name] })! assert !needs_root(ConfigRank{ write: true output: os.join_path(tmp, 'new.lip') query: [os_name] })! // an os the ranker does not know is an error before any escalation if _ := needs_root(ConfigRank{ write: true, query: ['no-such-os'] }) { assert false, 'an unknown os was ranked' } } fn test_verbose_is_a_global_flag() ! { saved := verbose_flag defer { verbose_flag = saved } // wherever it stands, it is taken off the mode's arguments and turns // the verbose log on; without it the log stays off for args in [['vpac', '--verbose', 'i', 'x'], ['vpac', 'i', '--verbose', 'x'], ['vpac', 'i', 'x', '--verbose'], ['vpac', '--verbose', '--verbose', 'i', 'x']] { verbose_flag = false _, _, rest := scan_globals(args)! assert rest == ['vpac', 'i', 'x'], '${args}' assert verbose_flag, '${args}' } verbose_flag = false _, _, plain := scan_globals(['vpac', 'i', 'x'])! assert plain == ['vpac', 'i', 'x'] && !verbose_flag // as the value of another global it is that flag's value, not the flag _, evals, rest := scan_globals(['vpac', '--eval', '--verbose', 's'])! assert evals == ['--verbose'] && rest == ['vpac', 's'] && !verbose_flag // the short letter is not it: -v stays the mode's to refuse or to take _, _, short := scan_globals(['vpac', 'i', '-v', 'x'])! assert short == ['vpac', 'i', '-v', 'x'] && !verbose_flag // and it takes no value _, _, eq := scan_globals(['vpac', 'i', '--verbose=yes', 'x'])! assert eq == ['vpac', 'i', '--verbose=yes', 'x'] && !verbose_flag if _ := parse_mode(eq) { assert false, '--verbose=yes parsed as a mode flag' } }