| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | import os |
| 5 | |
| 6 | // --eval takes its scheme from the next argument or from =; an empty one is |
| 7 | // refused like an empty --config or --color value is |
| 8 | fn test_scan_globals_eval() ! { |
| 9 | mut chosen := '' |
| 10 | mut evals := []string{} |
| 11 | mut rest := []string{} |
| 12 | chosen, evals, rest = scan_globals(['vpac', '--eval', '(define x 1)', 's', 'zsh'])! |
| 13 | assert chosen == '' |
| 14 | assert evals == ['(define x 1)'] |
| 15 | assert rest == ['vpac', 's', 'zsh'] |
| 16 | |
| 17 | chosen, evals, rest = scan_globals(['vpac', '--eval=(define x 2)', '--config=other.scm', 's'])! |
| 18 | assert chosen == 'other.scm' |
| 19 | assert evals == ['(define x 2)'] |
| 20 | assert rest == ['vpac', 's'] |
| 21 | |
| 22 | for arg in ['--eval=', '--eval', '--config=', '--config'] { |
| 23 | if _, _, _ := scan_globals(['vpac', arg]) { |
| 24 | assert false, '"${arg}" was taken without its value' |
| 25 | } else { |
| 26 | assert err.msg().contains('needs'), '"${arg}": ${err.msg()}' |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // the global flags parse wherever they stand, and none of them bleeds into |
| 32 | // the rest the mode sees |
| 33 | fn test_scan_globals_mixed() ! { |
| 34 | saved_kind, saved_color := ui_kind, color_flag |
| 35 | defer { |
| 36 | ui_kind, color_flag = saved_kind, saved_color |
| 37 | } |
| 38 | _, _, rest := scan_globals(['vpac', 'i', '--color=never', 'zsh', '--ui', 'ipc'])! |
| 39 | assert rest == ['vpac', 'i', 'zsh'] |
| 40 | } |
| 41 | |
| 42 | fn test_scan_ui() { |
| 43 | for args, want in { |
| 44 | 'vpac s --ui ipc': 'ipc' |
| 45 | 'vpac --ui=ipc s': 'ipc' |
| 46 | 'vpac --ui tty s': 'tty' |
| 47 | // the last valid one wins, as in scan_globals |
| 48 | 'vpac --ui tty s --ui ipc': 'ipc' |
| 49 | 'vpac --ui=ipc --ui=tty': 'tty' |
| 50 | // another global's value is stepped over, not read as a flag |
| 51 | 'vpac --eval --ui ipc': '' |
| 52 | 'vpac --config --ui --ui ipc': 'ipc' |
| 53 | // no valid one: none, and scan_globals says why on the terminal |
| 54 | 'vpac s zsh': '' |
| 55 | 'vpac --ui bogus s': '' |
| 56 | 'vpac --ui': '' |
| 57 | 'vpac --ui=': '' |
| 58 | } { |
| 59 | got := scan_ui(args.split(' ')) or { '' } |
| 60 | assert got == want, args |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // an error in any global is told the frontend's way when --ui ipc is on the |
| 65 | // command line, wherever it stands: the ui is settled before the error |
| 66 | fn test_scan_globals_settles_the_ui_before_failing() { |
| 67 | saved_kind := ui_kind |
| 68 | defer { ui_kind = saved_kind } |
| 69 | for args in ['vpac --color sometimes --ui ipc s', 'vpac --color sometimes s --ui=ipc', |
| 70 | 'vpac --ui ipc --color sometimes s'] { |
| 71 | ui_kind = 'tty' |
| 72 | if _, _, _ := scan_globals(args.split(' ')) { |
| 73 | assert false, '${args} was accepted' |
| 74 | } |
| 75 | assert ui_kind == 'ipc', args |
| 76 | } |
| 77 | // a bad --ui itself names no frontend: the terminal tells it |
| 78 | ui_kind = 'tty' |
| 79 | if _, _, _ := scan_globals('vpac --color sometimes --ui bogus s'.split(' ')) { |
| 80 | assert false |
| 81 | } |
| 82 | assert ui_kind == 'tty' |
| 83 | } |
| 84 | |
| 85 | // the doubled flags are counts, as pacman reads -ss, -cc, -gg, -tt and -uu |
| 86 | fn test_doubled_flags_count() ! { |
| 87 | r := parse_mode(['vpac', 'r', '-ss', '-u', 'foo'])! as ConfigRemove |
| 88 | assert r.recursive_level == 2 && r.unneeded && r.query == ['foo'] |
| 89 | assert recurse_flags(r.recursive, r.recursive_level) == nspkg.TransFlag.recurse | .recurse_all |
| 90 | one := parse_mode(['vpac', 'r', '--recursive', 'foo'])! as ConfigRemove |
| 91 | assert recurse_flags(one.recursive, one.recursive_level) == nspkg.TransFlag.recurse |
| 92 | |
| 93 | c := parse_mode(['vpac', 'c', '-cc'])! as ConfigClean |
| 94 | assert c.level == 2 && !c.keep_current |
| 95 | kc := parse_mode(['vpac', 'c', '--keep-current'])! as ConfigClean |
| 96 | assert kc.keep_current && kc.level == 0 |
| 97 | |
| 98 | u := parse_mode(['vpac', 'u', '-uu'])! as ConfigUpgrade |
| 99 | assert u.sysupgrade_level == 2 |
| 100 | d := parse_mode(['vpac', 'd', '-tt'])! as ConfigOrphans |
| 101 | assert d.level == 2 |
| 102 | } |
| 103 | |
| 104 | fn test_search_group_and_unrequired_counts() ! { |
| 105 | g := parse_mode(['vpac', 's', '-gg'])! as ConfigSearch |
| 106 | assert g.groups && g.groups_level == 2 |
| 107 | // groups are the sync dbs' unless -l asks for the installed ones |
| 108 | assert g.remote && !g.local |
| 109 | lg := parse_mode(['vpac', 's', '-l', '-g', 'base'])! as ConfigSearch |
| 110 | assert lg.groups && lg.local && !lg.remote && lg.query == ['base'] |
| 111 | long := parse_mode(['vpac', 's', '--groups'])! as ConfigSearch |
| 112 | assert long.groups |
| 113 | |
| 114 | t := parse_mode(['vpac', 's', '--unrequired'])! as ConfigSearch |
| 115 | assert t.unrequired && t.unrequired_level == 1 && t.local |
| 116 | tt := parse_mode(['vpac', 's', '--unrequired', '-d', '--unrequired'])! as ConfigSearch |
| 117 | assert tt.unrequired && tt.unrequired_level == 2 && tt.deps |
| 118 | } |
| 119 | |
| 120 | fn test_new_query_flags_parse() ! { |
| 121 | q := parse_mode(['vpac', 'q', '-c', '-p', 'a.pkg.tar', 'b.pkg.tar'])! as ConfigInfo |
| 122 | assert q.changelog && q.file && q.query == ['a.pkg.tar', 'b.pkg.tar'] |
| 123 | o := parse_mode(['vpac', 'o', '-q', '/usr/bin/ls'])! as ConfigOwner |
| 124 | assert o.quiet |
| 125 | f := parse_mode(['vpac', 'f', '-x', '--machinereadable', 'ls$'])! as ConfigFind |
| 126 | assert f.regex && f.machinereadable && f.query == ['ls$'] |
| 127 | } |
| 128 | |
| 129 | fn test_ranged_flags_are_checked() ! { |
| 130 | // the flags are held to the ranges the config's forms are |
| 131 | for bad in [['vpac', 's', '--thold', '2', 'x'], ['vpac', 's', '-t', '1.5', 'x'], |
| 132 | ['vpac', 's', '--thold', '-1', 'x'], ['vpac', 'k', '--top', '0'], ['vpac', 'k', '--top', |
| 133 | '-2']] { |
| 134 | if _ := parse_mode(bad) { |
| 135 | assert false, 'parsed ${bad}' |
| 136 | } |
| 137 | } |
| 138 | s := parse_mode(['vpac', 's', '--thold', '0.5', 'x'])! as ConfigSearch |
| 139 | assert s.thold == 0.5 |
| 140 | k := parse_mode(['vpac', 'k', '--top', '3'])! as ConfigRank |
| 141 | assert k.top == 3 |
| 142 | } |
| 143 | |
| 144 | fn test_help_flag_is_the_parsers() ! { |
| 145 | // bundled with other flags it still asks for help; as another flag's |
| 146 | // value it does not |
| 147 | s := parse_mode(['vpac', 's', '-lh'])! as ConfigSearch |
| 148 | assert s.show_help && s.local |
| 149 | q := parse_mode(['vpac', 'q', '--fields', '-h', 'x'])! as ConfigInfo |
| 150 | assert !q.show_help && q.fields == '-h' |
| 151 | } |
| 152 | |
| 153 | fn test_parse_target() { |
| 154 | assert parse_target('bash') == Target{ |
| 155 | name: 'bash' |
| 156 | } |
| 157 | assert parse_target('core/bash') == Target{'core', 'bash'} |
| 158 | assert parse_target('local/bash>=5') == Target{'local', 'bash>=5'} |
| 159 | // a leading slash is no db prefix |
| 160 | assert parse_target('/bash').repo == '' |
| 161 | assert local_target_name('local/bash') == 'bash' |
| 162 | assert local_target_name('core/bash') == 'core/bash' |
| 163 | assert has_constraint('bash>=5') && has_constraint('sh=1') && !has_constraint('bash') |
| 164 | } |
| 165 | |
| 166 | fn test_flags_nothing_read_are_gone() ! { |
| 167 | // install's -u, -s and -v and search's -v parsed into fields no code |
| 168 | // read: a flag that does nothing is refused instead of taken |
| 169 | for dead in [['vpac', 'i', '-u', 'x'], ['vpac', 'i', '--upgrade-deps', 'x'], |
| 170 | ['vpac', 'i', '-s', 'x'], ['vpac', 'i', '--sync', 'x'], ['vpac', 'i', '-v', 'x'], |
| 171 | ['vpac', 's', '-v', 'x']] { |
| 172 | if _ := parse_mode(dead) { |
| 173 | assert false, 'parsed ${dead}' |
| 174 | } |
| 175 | } |
| 176 | // the ones that do something still parse, bundled too |
| 177 | w := parse_mode(['vpac', 'i', '-w', 'x'])! as ConfigInstall |
| 178 | assert w.download_only && w.query == ['x'] |
| 179 | wp := parse_mode(['vpac', 'i', '-wp', '--no-upgrade-deps', 'x'])! as ConfigInstall |
| 180 | assert wp.download_only && wp.print_only && wp.no_upgrade_deps |
| 181 | s := parse_mode(['vpac', 's', '-s', 'x'])! as ConfigSearch |
| 182 | assert s.sync_dbs |
| 183 | } |
| 184 | |
| 185 | fn test_unknown_mode_flags_are_errors() ! { |
| 186 | // a flag the mode has no field for is named, not dropped: long, short, |
| 187 | // bundled with known ones, before or after the targets, and a mistyped |
| 188 | // spelling of a real one |
| 189 | for bad in [['vpac', 'q', '--bogus', 'bash'], ['vpac', 'q', '-Z', 'bash'], |
| 190 | ['vpac', 'q', 'bash', '--bogus'], ['vpac', 'i', '--noconfrim', 'x'], |
| 191 | ['vpac', 'i', '--asdep', 'x'], ['vpac', 'r', '--print', '--bogus', 'x'], |
| 192 | ['vpac', 's', '-t', '-1', 'x']] { |
| 193 | if c := parse_mode(bad) { |
| 194 | assert false, 'parsed ${bad} as ${c}' |
| 195 | } else { |
| 196 | assert err.msg().contains('unknown option') || err.msg().contains('--thold'), err.msg() |
| 197 | } |
| 198 | } |
| 199 | named := parse_mode(['vpac', 'q', '--bogus', '-Z', 'bash']) or { |
| 200 | assert err.msg() == 'unknown option: "--bogus", "-Z"', err.msg() |
| 201 | ModeConfig(ConfigInfo{}) |
| 202 | } |
| 203 | assert (named as ConfigInfo).query.len == 0 |
| 204 | // what is known still parses in every position, and a mode with no |
| 205 | // flags given at all is not an error |
| 206 | q := parse_mode(['vpac', 'q', 'bash', '-c', 'zsh'])! as ConfigInfo |
| 207 | assert q.changelog && q.query == ['bash', 'zsh'] |
| 208 | bare := parse_mode(['vpac', 'd'])! as ConfigOrphans |
| 209 | assert bare.level == 0 |
| 210 | // targets that stand before a flag are targets all the same, in the |
| 211 | // order given, with the ones after it |
| 212 | mixed := parse_mode(['vpac', 'i', 'a', '--needed', 'b', '-w', 'c', 'd'])! as ConfigInstall |
| 213 | assert mixed.query == ['a', 'b', 'c', 'd'] && mixed.needed && mixed.download_only |
| 214 | lead := parse_mode(['vpac', 'r', 'a', 'b', '--print'])! as ConfigRemove |
| 215 | assert lead.query == ['a', 'b'] && lead.print_only |
| 216 | // a lone dash names neither a flag nor a target: the parser's error |
| 217 | if d := parse_mode(['vpac', 'q', '-', '-c']) { |
| 218 | assert false, 'a lone dash parsed: ${d}' |
| 219 | } |
| 220 | // a mode with no targets says so of one given before a flag |
| 221 | if o := parse_mode(['vpac', 'd', 'stray', '-t']) { |
| 222 | assert false, 'orphans took a target: ${o}' |
| 223 | } else { |
| 224 | assert err.msg().contains('takes no targets'), err.msg() |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn test_needs_root_by_mode() ! { |
| 229 | root := fn (args []string) !bool { |
| 230 | return needs_root(parse_mode(args)!)! |
| 231 | } |
| 232 | // what changes the system runs as root |
| 233 | for args in [['vpac', 'i', 'x'], ['vpac', 'r', 'x'], ['vpac', 'u'], ['vpac', 'c'], |
| 234 | ['vpac', 'm', '-r', 'd', 'x'], ['vpac', 'a'], ['vpac', 'b', '/x'], ['vpac', 'f', '-y', |
| 235 | '/x'], ['vpac', 'i', '-w', 'x'], ['vpac', 'u', '-w'], ['vpac', 'i', '--dbonly', 'x']] { |
| 236 | assert root(args)!, '${args} runs as the user' |
| 237 | } |
| 238 | // a --print preview changes nothing and stays the user's, whatever |
| 239 | // else is asked beside it |
| 240 | for args in [['vpac', 'i', '-p', 'x'], ['vpac', 'r', '--print', 'x'], ['vpac', 'u', '-p'], |
| 241 | ['vpac', 'c', '-p'], ['vpac', 'i', '-p', '--dbonly', '--noconfirm', 'x'], |
| 242 | ['vpac', 'c', '-p', '-a']] { |
| 243 | assert !root(args)!, '${args} asks for root' |
| 244 | } |
| 245 | // what only reads never asks for root |
| 246 | for args in [['vpac', 's', 'x'], ['vpac', 'q', 'x'], ['vpac', 'w', 'x'], ['vpac', 'n', 'x'], |
| 247 | ['vpac', 'l', 'x'], ['vpac', 'o', '/x'], ['vpac', 'd'], ['vpac', 't', 'x'], ['vpac', 'v'], |
| 248 | ['vpac', 'f', '/x'], ['vpac', 'f', '-l', 'x'], ['vpac', 'k', 'arch']] { |
| 249 | assert !root(args)!, '${args} asks for root' |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | fn test_needs_root_for_rank_follows_the_file() ! { |
| 254 | if os.geteuid() == 0 { |
| 255 | // root may write anywhere: there is no 'not the user's' to tell |
| 256 | return |
| 257 | } |
| 258 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_needs_root_${os.getpid()}') |
| 259 | os.mkdir_all(os.join_path(tmp, 'ro'))! |
| 260 | saved_dir := rank_dir |
| 261 | defer { |
| 262 | rank_dir = saved_dir |
| 263 | os.chmod(os.join_path(tmp, 'ro'), 0o755) or {} |
| 264 | os.rmdir_all(tmp) or {} |
| 265 | } |
| 266 | os.chmod(os.join_path(tmp, 'ro'), 0o555)! |
| 267 | os_name := rank_oses.keys()[0] |
| 268 | // printing a ranking writes nothing |
| 269 | assert !needs_root(ConfigRank{ query: [os_name] })! |
| 270 | // -o: a file the user may write, or may create, is the user's |
| 271 | assert !needs_root(ConfigRank{ output: os.join_path(tmp, 'new.scm'), query: [os_name] })! |
| 272 | os.write_file(os.join_path(tmp, 'there.scm'), '')! |
| 273 | assert !needs_root(ConfigRank{ output: os.join_path(tmp, 'there.scm'), query: [os_name] })! |
| 274 | // one it may not is root's: an existing read-only file, a new file in |
| 275 | // a read-only dir, and a path under a dir that is not there |
| 276 | os.chmod(os.join_path(tmp, 'there.scm'), 0o444)! |
| 277 | assert needs_root(ConfigRank{ output: os.join_path(tmp, 'there.scm'), query: [os_name] })! |
| 278 | assert needs_root(ConfigRank{ output: os.join_path(tmp, 'ro', 'new.scm'), query: [os_name] })! |
| 279 | assert needs_root(ConfigRank{ output: os.join_path(tmp, 'gone', 'new.scm'), query: [os_name] })! |
| 280 | // -w writes the config's mirror file: the rank dir decides, and -o, |
| 281 | // when both are given, is the file that is written |
| 282 | rank_dir = tmp |
| 283 | assert !needs_root(ConfigRank{ write: true, query: [os_name] })! |
| 284 | rank_dir = os.join_path(tmp, 'ro') |
| 285 | assert needs_root(ConfigRank{ write: true, query: [os_name] })! |
| 286 | assert !needs_root(ConfigRank{ |
| 287 | write: true |
| 288 | output: os.join_path(tmp, 'new.scm') |
| 289 | query: [os_name] |
| 290 | })! |
| 291 | // an os the ranker does not know is an error before any escalation |
| 292 | if _ := needs_root(ConfigRank{ write: true, query: ['no-such-os'] }) { |
| 293 | assert false, 'an unknown os was ranked' |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | fn test_verbose_is_a_global_flag() ! { |
| 298 | saved := verbose_flag |
| 299 | defer { verbose_flag = saved } |
| 300 | // wherever it stands, it is taken off the mode's arguments and turns |
| 301 | // the verbose log on; without it the log stays off |
| 302 | for args in [['vpac', '--verbose', 'i', 'x'], ['vpac', 'i', '--verbose', 'x'], |
| 303 | ['vpac', 'i', 'x', '--verbose'], ['vpac', '--verbose', '--verbose', 'i', 'x']] { |
| 304 | verbose_flag = false |
| 305 | _, _, rest := scan_globals(args)! |
| 306 | assert rest == ['vpac', 'i', 'x'], '${args}' |
| 307 | assert verbose_flag, '${args}' |
| 308 | } |
| 309 | verbose_flag = false |
| 310 | _, _, plain := scan_globals(['vpac', 'i', 'x'])! |
| 311 | assert plain == ['vpac', 'i', 'x'] && !verbose_flag |
| 312 | // as the value of another global it is that flag's value, not the flag |
| 313 | _, evals, rest := scan_globals(['vpac', '--eval', '--verbose', 's'])! |
| 314 | assert evals == ['--verbose'] && rest == ['vpac', 's'] && !verbose_flag |
| 315 | // the short letter is not it: -v stays the mode's to refuse or to take |
| 316 | _, _, short := scan_globals(['vpac', 'i', '-v', 'x'])! |
| 317 | assert short == ['vpac', 'i', '-v', 'x'] && !verbose_flag |
| 318 | // and it takes no value |
| 319 | _, _, eq := scan_globals(['vpac', 'i', '--verbose=yes', 'x'])! |
| 320 | assert eq == ['vpac', 'i', '--verbose=yes', 'x'] && !verbose_flag |
| 321 | if _ := parse_mode(eq) { |
| 322 | assert false, '--verbose=yes parsed as a mode flag' |
| 323 | } |
| 324 | } |
| 325 | |