def xonsh_builtins(xonsh_events): """Mock out most of the builtins xonsh attributes.""" old_builtins = set(dir(builtins)) builtins.__xonsh_env__ = DummyEnv() if ON_WINDOWS: builtins.__xonsh_env__["PATHEXT"] = [".EXE", ".BAT", ".CMD"] builtins.__xonsh_ctx__ = {} builtins.__xonsh_shell__ = DummyShell() builtins.__xonsh_help__ = lambda x: x builtins.__xonsh_glob__ = glob.glob builtins.__xonsh_exit__ = False builtins.__xonsh_superhelp__ = lambda x: x builtins.__xonsh_regexpath__ = lambda x: [] builtins.__xonsh_expand_path__ = lambda x: x builtins.__xonsh_subproc_captured__ = sp builtins.__xonsh_subproc_uncaptured__ = sp builtins.__xonsh_stdout_uncaptured__ = None builtins.__xonsh_stderr_uncaptured__ = None builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs builtins.__xonsh_commands_cache__ = DummyCommandsCache() builtins.__xonsh_all_jobs__ = {} builtins.__xonsh_history__ = DummyHistory() builtins.__xonsh_subproc_captured_hiddenobject__ = sp builtins.__xonsh_enter_macro__ = enter_macro builtins.evalx = eval builtins.execx = None builtins.compilex = None builtins.aliases = {} # Unlike all the other stuff, this has to refer to the "real" one because all modules that would # be firing events on the global instance. builtins.events = xonsh_events yield builtins for attr in set(dir(builtins)) - old_builtins: delattr(builtins, attr) tasks.clear() # must to this to enable resetting all_jobs
def test_current_branch_calls_locate_binary_for_empty_cmds_cache(xonsh_builtins): cache = xonsh_builtins.__xonsh__.commands_cache xonsh_builtins.__xonsh__.env = DummyEnv(VC_BRANCH_TIMEOUT=1) cache.is_empty = Mock(return_value=True) cache.locate_binary = Mock(return_value="") vc.current_branch() assert cache.locate_binary.called
def test_git_dirty_working_directory_includes_untracked( xonsh_builtins, test_repo, include_untracked): xonsh_builtins.__xonsh__.env = DummyEnv( VC_GIT_INCLUDE_UNTRACKED=include_untracked) if test_repo["vc"] != "git": return Path("untracked-test-file").touch() assert vc.git_dirty_working_directory() == include_untracked
def test_current_branch_does_not_call_locate_binary_for_non_empty_cmds_cache(xonsh_builtins): cache = xonsh_builtins.__xonsh_commands_cache__ xonsh_builtins.__xonsh_env__ = DummyEnv(VC_BRANCH_TIMEOUT=1) cache.is_empty = Mock(return_value=False) cache.locate_binary = Mock(return_value='') # make lazy locate return nothing to avoid running vc binaries cache.lazy_locate_binary = Mock(return_value='') vc.current_branch() assert not cache.locate_binary.called
def xonsh_builtins(): """Mock out most of the builtins xonsh attributes.""" builtins.__xonsh_env__ = DummyEnv() if ON_WINDOWS: builtins.__xonsh_env__['PATHEXT'] = ['.EXE', '.BAT', '.CMD'] builtins.__xonsh_ctx__ = {} builtins.__xonsh_shell__ = DummyShell() builtins.__xonsh_help__ = lambda x: x builtins.__xonsh_glob__ = glob.glob builtins.__xonsh_exit__ = False builtins.__xonsh_superhelp__ = lambda x: x builtins.__xonsh_regexpath__ = lambda x: [] builtins.__xonsh_expand_path__ = lambda x: x builtins.__xonsh_subproc_captured__ = sp builtins.__xonsh_subproc_uncaptured__ = sp builtins.__xonsh_stdout_uncaptured__ = None builtins.__xonsh_stderr_uncaptured__ = None builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs builtins.__xonsh_commands_cache__ = DummyCommandsCache() builtins.__xonsh_all_jobs__ = {} builtins.__xonsh_history__ = DummyHistory() builtins.XonshBlockError = XonshBlockError builtins.__xonsh_subproc_captured_hiddenobject__ = sp builtins.evalx = eval builtins.execx = None builtins.compilex = None builtins.aliases = {} # Unlike all the other stuff, this has to refer to the "real" one because all modules that would # be firing events on the global instance. builtins.events = events yield builtins del builtins.__xonsh_env__ del builtins.__xonsh_ctx__ del builtins.__xonsh_shell__ del builtins.__xonsh_help__ del builtins.__xonsh_glob__ del builtins.__xonsh_exit__ del builtins.__xonsh_superhelp__ del builtins.__xonsh_regexpath__ del builtins.__xonsh_expand_path__ del builtins.__xonsh_stdout_uncaptured__ del builtins.__xonsh_stderr_uncaptured__ del builtins.__xonsh_subproc_captured__ del builtins.__xonsh_subproc_uncaptured__ del builtins.__xonsh_ensure_list_of_strs__ del builtins.__xonsh_commands_cache__ del builtins.__xonsh_all_jobs__ del builtins.__xonsh_history__ del builtins.XonshBlockError del builtins.evalx del builtins.execx del builtins.compilex del builtins.aliases del builtins.events tasks.clear() # must to this to enable resetting all_jobs
def xonsh_builtins(monkeypatch, xonsh_events, session_vars): """Mock out most of the builtins xonsh attributes.""" old_builtins = dict(vars(builtins).items()) # type: ignore XSH.load(ctx={}, **session_vars) def locate_binary(self, name): return os.path.join(os.path.dirname(__file__), "bin", name) for attr, val in [ ("env", DummyEnv()), ("shell", DummyShell()), ("help", lambda x: x), ("aliases", Aliases()), ("exit", False), ("history", DummyHistory()), # ("subproc_captured", sp), ("subproc_uncaptured", sp), ("subproc_captured_stdout", sp), ("subproc_captured_inject", sp), ("subproc_captured_object", sp), ("subproc_captured_hiddenobject", sp), ]: monkeypatch.setattr(XSH, attr, val) if ON_WINDOWS: XSH.env["PATHEXT"] = [".EXE", ".BAT", ".CMD"] cc = XSH.commands_cache monkeypatch.setattr(cc, "locate_binary", types.MethodType(locate_binary, cc)) monkeypatch.setattr(cc, "_cmds_cache", {}) for attr, val in [ ("evalx", eval), ("execx", None), ("compilex", None), # Unlike all the other stuff, this has to refer to the "real" one because all modules that would # be firing events on the global instance. ("events", xonsh_events), ]: # attributes to builtins are dynamicProxy and should pickup the following monkeypatch.setattr(XSH.builtins, attr, val) # todo: remove using builtins for tests at all yield builtins XSH.unload() for attr in set(dir(builtins)) - set(old_builtins): if hasattr(builtins, attr): delattr(builtins, attr) for attr, old_value in old_builtins.items(): setattr(builtins, attr, old_value) tasks.clear() # must to this to enable resetting all_jobs
def xonsh_builtins(monkeypatch, xonsh_events): """Mock out most of the builtins xonsh attributes.""" old_builtins = set(dir(builtins)) execer = getattr(getattr(builtins, "__xonsh__", None), "execer", None) session = XonshSession(execer=execer, ctx={}) ensure_attached_session(monkeypatch, session) builtins.__xonsh__.env = DummyEnv() if ON_WINDOWS: builtins.__xonsh__.env["PATHEXT"] = [".EXE", ".BAT", ".CMD"] builtins.__xonsh__.shell = DummyShell() builtins.__xonsh__.help = lambda x: x builtins.__xonsh__.glob = glob.glob builtins.__xonsh__.exit = False builtins.__xonsh__.superhelp = lambda x: x builtins.__xonsh__.pathsearch = pathsearch builtins.__xonsh__.globsearch = globsearch builtins.__xonsh__.regexsearch = regexsearch builtins.__xonsh__.regexpath = lambda x: [] builtins.__xonsh__.expand_path = lambda x: x builtins.__xonsh__.subproc_captured = sp builtins.__xonsh__.subproc_uncaptured = sp builtins.__xonsh__.stdout_uncaptured = None builtins.__xonsh__.stderr_uncaptured = None builtins.__xonsh__.ensure_list_of_strs = ensure_list_of_strs builtins.__xonsh__.commands_cache = DummyCommandsCache() builtins.__xonsh__.all_jobs = {} builtins.__xonsh__.list_of_strs_or_callables = list_of_strs_or_callables builtins.__xonsh__.list_of_list_of_strs_outer_product = ( list_of_list_of_strs_outer_product ) builtins.__xonsh__.history = DummyHistory() builtins.__xonsh__.subproc_captured_stdout = sp builtins.__xonsh__.subproc_captured_inject = sp builtins.__xonsh__.subproc_captured_object = sp builtins.__xonsh__.subproc_captured_hiddenobject = sp builtins.__xonsh__.enter_macro = enter_macro builtins.__xonsh__.completers = None builtins.__xonsh__.call_macro = call_macro builtins.__xonsh__.enter_macro = enter_macro builtins.__xonsh__.path_literal = path_literal builtins.__xonsh__.builtins = _BuiltIns(execer=execer) builtins.evalx = eval builtins.execx = None builtins.compilex = None builtins.aliases = {} # Unlike all the other stuff, this has to refer to the "real" one because all modules that would # be firing events on the global instance. builtins.events = xonsh_events yield builtins monkeypatch.delattr(builtins, "__xonsh__", raising=False) for attr in set(dir(builtins)) - old_builtins: if hasattr(builtins, attr): delattr(builtins, attr) tasks.clear() # must to this to enable resetting all_jobs
def ctx(): """Context in which the ptk multiline functionality will be tested.""" builtins.__xonsh_env__ = DummyEnv() builtins.__xonsh_env__['INDENT'] = ' ' from xonsh.ptk2.key_bindings import carriage_return ptk_buffer = Buffer() ptk_buffer.accept_action = MagicMock(name='accept') cli = MagicMock(name='cli', spec=Application) yield Context(indent=' ', buffer=ptk_buffer, accept=ptk_buffer.accept_action, cli=cli, cr=carriage_return) del builtins.__xonsh_env__
def ctx(): """Context in which the ptk multiline functionality will be tested.""" builtins.__xonsh__ = XonshSession() builtins.__xonsh__.env = DummyEnv() builtins.__xonsh__.env["INDENT"] = " " from xonsh.ptk_shell.key_bindings import carriage_return ptk_buffer = Buffer() ptk_buffer.accept_action = MagicMock(name="accept") cli = MagicMock(name="cli", spec=Application) yield Context( indent=" ", buffer=ptk_buffer, accept=ptk_buffer.accept_action, cli=cli, cr=carriage_return, ) del builtins.__xonsh__.env del builtins.__xonsh__