def test_empty_line(): completions = complete_base( CompletionContext(command=CommandContext((), 0), python=PythonContext("", 0))) assert completions for exp in ["cool", "abs"]: assert exp in completions
def test_combined_subcommand_arg(): command = f"echo file=$(pwd{X})/x" # index inside the subproc assert_match( command, CommandContext((), arg_index=0, prefix="pwd", subcmd_opening="$("), python_context=None, ) # index at the end of the command assert_match( command.replace(X, ""), CommandContext((CommandArg("echo"),), arg_index=1, prefix="file=$(pwd)/x"), is_main_command=True, )
def complete_skipper(command_context: CommandContext): """ Skip over several tokens (e.g., sudo) and complete based on the rest of the command. Contextual completers don't need us to skip tokens since they get the correct completion context - meaning we only need to skip commands like ``sudo``. """ skip_part_num = 0 for skip_part_num, arg in enumerate( command_context.args[:command_context.arg_index]): # all the args before the current argument if arg.value not in SKIP_TOKENS: break if skip_part_num == 0: return None skipped_context = CompletionContext(command=command_context._replace( args=command_context.args[skip_part_num:], arg_index=command_context.arg_index - skip_part_num, )) completers = builtins.__xonsh__.completers.values() # type: ignore for completer in completers: if is_contextual_completer(completer): results = completer(skipped_context) if results: return results return None
def test_other_subcommand_arg(): command = "echo $(pwd) " assert_match( command, CommandContext((CommandArg("echo"), CommandArg("$(pwd)")), arg_index=2), is_main_command=True, )
def test_xonfig_colors(monkeypatch): monkeypatch.setattr("xonsh.tools.color_style_names", lambda: ["blue", "brown", "other"]) assert complete_xonfig( CompletionContext( CommandContext(args=(CommandArg("xonfig"), CommandArg("colors")), arg_index=2, prefix="b"))) == {"blue", "brown"}
def test_package_list(): comps = complete_pip( CompletionContext( CommandContext( args=(CommandArg("pip3"), CommandArg("show")), arg_index=2, ))) assert "Package" not in comps assert "-----------------------------" not in comps assert "pytest" in comps
def test_commands(): comps = complete_pip( CompletionContext( CommandContext( args=(CommandArg("pip3"), ), arg_index=1, prefix="c", ))) assert comps.intersection({"cache", "check", "config"}) for comp in comps: assert isinstance(comp, RichCompletion) assert comp.append_space
def test_multiple_nested_commands(): assert_match( f"echo hi; echo $(ls{X})", CommandContext((), 0, prefix="ls", subcmd_opening="$("), python_context=None, )
def test_multiple_empty_commands(commandline): assert_match(commandline, CommandContext((), 0), is_main_command=True)
index = len(commandline) context = parse(commandline, index) if context is None: raise SyntaxError( "Failed to parse the commandline - set DEBUG = True in this file to see the error" ) if is_main_command and python_context is MISSING: python_context = PythonContext(commandline, index) if command_context is not MISSING: assert context.command == command_context if python_context is not MISSING: assert context.python == python_context COMMAND_EXAMPLES = ( (f"comm{X}", CommandContext(args=(), arg_index=0, prefix="comm")), (f" comm{X}", CommandContext(args=(), arg_index=0, prefix="comm")), (f"comm{X}and", CommandContext(args=(), arg_index=0, prefix="comm", suffix="and")), (f"command {X}", CommandContext(args=(CommandArg("command"),), arg_index=1)), (f"{X} command", CommandContext(args=(CommandArg("command"),), arg_index=0)), (f" command {X}", CommandContext(args=(CommandArg("command"),), arg_index=1)), ( f"command --{X}", CommandContext(args=(CommandArg("command"),), arg_index=1, prefix="--"), ), ( f"command a {X}", CommandContext(args=(CommandArg("command"), CommandArg("a")), arg_index=2), ), ( f"command a b{X}",
def test_xonfig(): assert complete_xonfig( CompletionContext( CommandContext(args=(CommandArg("xonfig"), ), arg_index=1, prefix="-"))) == {"-h"}
def test_xontrib(): assert complete_xontrib( CompletionContext( CommandContext(args=(CommandArg("xontrib"), ), arg_index=1, prefix="l"))) == {"list", "load"}
"BASH_COMPLETIONS", ["/usr/share/bash-completion/bash_completion"], ) (tmp_path / "testdir").mkdir() (tmp_path / "spaced dir").mkdir() monkeypatch.chdir(str(tmp_path)) @skip_if_on_darwin @skip_if_on_windows @pytest.mark.parametrize( "command_context, completions, lprefix", ( ( CommandContext( args=(CommandArg("bash"), ), arg_index=1, prefix="--deb"), {"--debug", "--debugger"}, 5, ), ( CommandContext(args=(CommandArg("ls"), ), arg_index=1, prefix=""), {"'testdir/'", "'spaced dir/'"}, 0, ), # tar replaces "~/" with "/home/user/", the change should be rolledback by us. ( CommandContext( args=(CommandArg("tar"), ), arg_index=1, prefix="~/"), {"~/c", "~/u", "~/t", "~/d", "~/A", "~/r", "~/x"}, 2, ),
def test_options(): assert complete_completer(CompletionContext(CommandContext( args=(CommandArg("completer"),), arg_index=1, ))) == {"add", "remove", "list", "help"}