def test_special_tokens(jedi_xontrib): assert jedi_xontrib.complete_jedi(CompletionContext(python=PythonContext("", 0))) \ .issuperset(jedi_xontrib.XONSH_SPECIAL_TOKENS) assert jedi_xontrib.complete_jedi( CompletionContext(python=PythonContext("@", 1))) == {"@", "@(", "@$("} assert jedi_xontrib.complete_jedi( CompletionContext(python=PythonContext("$", 1))) == {"$[", "${", "$("}
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 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 # all the args before the current argument for arg in command_context.args[: command_context.arg_index]: if arg.value not in SKIP_TOKENS: break skip_part_num += 1 if skip_part_num == 0: return None skipped_command_context = command_context._replace( args=command_context.args[skip_part_num:], arg_index=command_context.arg_index - skip_part_num, ) if skipped_command_context.arg_index == 0: # completing the command after a SKIP_TOKEN return complete_command(skipped_command_context) completer: Completer = XSH.shell.shell.completer # type: ignore return completer.complete_from_context(CompletionContext(skipped_command_context))
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 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_rich_completions(jedi_xontrib, jedi_mock, completion, rich_completion): comp_type, comp_name, comp_complete, sig, inf = completion comp_mock = MagicMock() comp_mock.type = comp_type comp_mock.name = comp_name comp_mock.complete = comp_complete if sig: sig_mock = MagicMock() sig_mock.to_string.return_value = sig comp_mock.get_signatures.return_value = [sig_mock] else: comp_mock.get_signatures.return_value = [] if inf: inf_type, inf_desc = inf inf_mock = MagicMock() inf_mock.type = inf_type inf_mock.description = inf_desc comp_mock.infer.return_value = [inf_mock] else: comp_mock.infer.return_value = [] jedi_xontrib.XONSH_SPECIAL_TOKENS = [] jedi_mock.Interpreter().complete.return_value = [comp_mock] completions = jedi_xontrib.complete_jedi( CompletionContext(python=PythonContext("", 0))) assert len(completions) == 1 (ret_completion, ) = completions assert isinstance(ret_completion, RichCompletion) assert ret_completion == rich_completion assert ret_completion.display == rich_completion.display assert ret_completion.description == rich_completion.description
def test_complete_python(code, exp): res = complete_python( CompletionContext(python=PythonContext(code, len(code), ctx={})) ) assert res and len(res) == 2 comps, _ = res assert exp in comps
def test_empty_subexpr(): completions = complete_base( CompletionContext(command=CommandContext((), 0, subcmd_opening="$("), python=None)) assert completions for exp in ["cool"]: assert exp in completions assert "abs" not in completions
def test_quote_handling(command_context, completions, lprefix): bash_completions, bash_lprefix = complete_from_bash( CompletionContext(command_context)) assert bash_completions == completions and bash_lprefix == lprefix assert all( isinstance(comp, RichCompletion) and not comp.append_closing_quote for comp in bash_completions ) # make sure the completer handles the closing quote by itself
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_equal_sign_arg(command_context, completions, lprefix, exp_append_space): bash_completions, bash_lprefix = complete_from_bash( CompletionContext(command_context)) assert bash_completions == completions and bash_lprefix == lprefix assert all( isinstance(comp, RichCompletion) and comp.append_space == exp_append_space for comp in bash_completions)
def test_empty_subexpr(): completions = complete_base( CompletionContext(command=CommandContext((), 0, subcmd_opening="$("), python=None)) completions = set(map(str, completions)) assert completions assert completions.issuperset({"cool"}) assert "abs" not in completions
def complete_rmdir(command: CommandContext): """ Completion for "rmdir", includes only valid directory names. """ opts = complete_from_man(CompletionContext(command)) comps, lp = complete_dir(command) if len(comps) == 0 and len(opts) == 0: raise StopIteration return comps | opts, lp
def test_complete_import(command, exp): result = complete_import( CompletionContext( command, python=PythonContext("", 0) # `complete_import` needs this )) if isinstance(result, tuple): result, _ = result result = set(result) assert result == exp
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_multiline(jedi_xontrib, jedi_mock, monkeypatch): complete_document = "xx = 1\n1 + x" jedi_xontrib.complete_jedi( CompletionContext( python=PythonContext(complete_document, len(complete_document)))) assert jedi_mock.Interpreter.call_args_list[0][0][0] == complete_document assert jedi_mock.Interpreter().complete.call_args_list == [ call(2, 5) # line (one-indexed), column (zero-indexed) ]
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
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_options(): assert complete_completer(CompletionContext(CommandContext( args=(CommandArg("completer"),), arg_index=1, ))) == {"add", "remove", "list", "help"}
def test_xontrib(): assert complete_xontrib( CompletionContext( CommandContext(args=(CommandArg("xontrib"), ), arg_index=1, prefix="l"))) == {"list", "load"}
def test_xonfig(): assert complete_xonfig( CompletionContext( CommandContext(args=(CommandArg("xonfig"), ), arg_index=1, prefix="-"))) == {"-h"}
spec = find_xontrib("jedi") yield importlib.import_module(spec.name) del sys.modules[spec.name] def test_completer_added(jedi_xontrib, xession): assert "xontrib.jedi" in sys.modules assert "python" not in xession.completers assert "python_mode" not in xession.completers assert "jedi_python" in xession.completers @pytest.mark.parametrize( "context", [ CompletionContext(python=PythonContext("10 + x", 6)), ], ) @pytest.mark.parametrize("version", ["new", "old"]) def test_jedi_api(jedi_xontrib, jedi_mock, version, context, xession): if version == "old": jedi_mock.__version__ = "0.15.0" jedi_mock.Interpreter().completions.return_value = [] jedi_mock.reset_mock() jedi_xontrib.complete_jedi(context) extra_namespace = {"__xonsh__": xession} try: extra_namespace["_"] = _ except NameError:
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
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_complete_import(command, exp): result = complete_import(CompletionContext(command, python=PythonContext("", 0) # `complete_import` needs this )) assert result == exp