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_not_cmd(cmd): """Ensure the cd completer doesn't complete other commands""" assert not COMPLETERS[cmd](CompletionContext( CommandContext( args=(CommandArg(f"not-{cmd}"), ), arg_index=1, )))
def test_man_completion(monkeypatch, tmpdir, xonsh_builtins): tempdir = tmpdir.mkdir("test_man") monkeypatch.setitem( os.environ, "MANPATH", os.path.dirname(os.path.abspath(__file__)) ) xonsh_builtins.__xonsh__.env.update({"XONSH_DATA_DIR": str(tempdir)}) completions = complete_from_man(CompletionContext( CommandContext(args=(CommandArg("yes"),), arg_index=1, prefix="--") )) assert "--version" in completions assert "--help" in completions
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 complete_cmd(cmd, prefix, opening_quote="", closing_quote=""): result = COMPLETERS[cmd](CompletionContext( CommandContext( args=(CommandArg(cmd), ), arg_index=1, prefix=prefix, opening_quote=opening_quote, closing_quote=closing_quote, is_after_closing_quote=bool(closing_quote), ))) assert result and len(result) == 2 completions, lprefix = result assert lprefix == len(opening_quote) + len(prefix) + len( closing_quote) # should override the quotes return 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 test_skipper_arg(completion_context_parse, xession, monkeypatch): monkeypatch.setattr(xession.shell.shell, "completer", Completer(), raising=False) bash_completer_mock = Mock() monkeypatch.setattr(xession, "completers", {"bash": bash_completer_mock}) bash_completer_mock.return_value = {"--count "} assert "--count " in completions_from_result( complete_skipper(completion_context_parse("sudo grep --coun", 16))) call_args = bash_completer_mock.call_args[0] assert len(call_args) == 1 context = call_args[0] assert isinstance(context, CompletionContext) assert context.command == CommandContext(args=(CommandArg("grep"), ), arg_index=1, prefix="--coun")
a = A() res = complete_python( CompletionContext(python=PythonContext("a.w", 2, ctx=locals()))) assert res and len(res) == 2 comps, _ = res assert "a.wow(" in comps @pytest.mark.parametrize( "command, exp", ( ( CommandContext( args=(CommandArg("import"), ), arg_index=1, prefix="pathli"), {"pathlib"}, ), ( CommandContext( args=(CommandArg("from"), ), arg_index=1, prefix="pathli"), {"pathlib"}, ), ( CommandContext( args=(CommandArg("import"), ), arg_index=1, prefix="os.pa"), {"os.path"}, ), ( CommandContext(args=(CommandArg("import"), ), arg_index=1,
def test_options(): assert complete_completer(CompletionContext(CommandContext( args=(CommandArg("completer"),), arg_index=1, ))) == {"add", "remove", "list", "help"}
["/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, ), ( CommandContext(args=(CommandArg("ls"), ), arg_index=1, prefix="", opening_quote="'"), {"'testdir/'", "'spaced dir/'"}, 1,
def test_bash_completer_empty_prefix(): context = CompletionContext( CommandContext(args=(CommandArg("git"), ), arg_index=1, prefix="")) bash_completions, bash_lprefix = complete_from_bash(context) assert {"clean", "show"}.issubset(bash_completions)
def test_xontrib(): assert complete_xontrib( CompletionContext( CommandContext(args=(CommandArg("xontrib"), ), arg_index=1, prefix="l"))) == {"list", "load"}
["/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, ),
@pytest.fixture(autouse=True) def setup(monkeypatch, tmp_path, xonsh_builtins): if not xonsh_builtins.__xonsh__.env.get("BASH_COMPLETIONS"): monkeypatch.setitem(xonsh_builtins.__xonsh__.env, "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), (CommandContext( args=(CommandArg("ls"), ), arg_index=1, prefix="", opening_quote="'"), {"'testdir/'", "'spaced dir/'"}, 1), )) def test_bash_completer(command_context, completions, lprefix): bash_completions, bash_lprefix = complete_from_bash( CompletionContext(command_context)) assert bash_completions == completions and bash_lprefix == lprefix @skip_if_on_darwin @skip_if_on_windows
def test_xonfig(): assert complete_xonfig( CompletionContext( CommandContext(args=(CommandArg("xonfig"), ), arg_index=1, prefix="-"))) == {"-h"}
def test_complete_python_ctx(): class A: def wow(): pass a = A() res = complete_python(CompletionContext(python=PythonContext("a.w", 2, ctx=locals()))) assert res and len(res) == 2 comps, _ = res assert "a.wow(" in comps @pytest.mark.parametrize("command, exp", ( (CommandContext(args=(CommandArg("import"),), arg_index=1, prefix="pathli"), {"pathlib"}), (CommandContext(args=(CommandArg("from"),), arg_index=1, prefix="pathli"), {"pathlib "}), (CommandContext(args=(CommandArg("import"),), arg_index=1, prefix="os.pa"), {"os.path"}), (CommandContext(args=(CommandArg("from"), CommandArg("x"),), arg_index=2), {"import"}), (CommandContext(args=( CommandArg("import"), CommandArg("os,"), ), arg_index=2, prefix="pathli"), {"pathlib"}), (CommandContext(args=( CommandArg("from"), CommandArg("pathlib"), CommandArg("import"), ), arg_index=3, prefix="PurePa"), {"PurePath"}), )) def test_complete_import(command, exp): result = complete_import(CompletionContext(command, python=PythonContext("", 0) # `complete_import` needs this )) assert result == exp
"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}", CommandContext(args=(CommandArg("command"), CommandArg("a")), arg_index=2, prefix="b")), (f"command a b{X}",
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}", CommandContext( args=(CommandArg("command"), CommandArg("a")), arg_index=2, prefix="b" ),