1import builtins
2import glob
3import os
4
5import pytest
6
7from xonsh.built_ins import ensure_list_of_strs, enter_macro
8from xonsh.execer import Execer
9from xonsh.jobs import tasks
10from xonsh.events import events
11from xonsh.platform import ON_WINDOWS
12
13from tools import DummyShell, sp, DummyCommandsCache, DummyEnv, DummyHistory
14
15
16@pytest.fixture
17def source_path():
18    """Get the xonsh source path."""
19    pwd = os.path.dirname(__file__)
20    return os.path.dirname(pwd)
21
22
23@pytest.fixture
24def xonsh_execer(monkeypatch):
25    """Initiate the Execer with a mocked nop `load_builtins`"""
26    monkeypatch.setattr(
27        "xonsh.built_ins.load_builtins.__code__",
28        (lambda *args, **kwargs: None).__code__,
29    )
30    execer = Execer(unload=False)
31    builtins.__xonsh_execer__ = execer
32    return execer
33
34
35@pytest.yield_fixture
36def xonsh_events():
37    yield events
38    for name, oldevent in vars(events).items():
39        # Heavily based on transmogrification
40        species = oldevent.species
41        newevent = events._mkevent(name, species, species.__doc__)
42        setattr(events, name, newevent)
43
44
45@pytest.yield_fixture
46def xonsh_builtins(xonsh_events):
47    """Mock out most of the builtins xonsh attributes."""
48    old_builtins = set(dir(builtins))
49    builtins.__xonsh_env__ = DummyEnv()
50    if ON_WINDOWS:
51        builtins.__xonsh_env__["PATHEXT"] = [".EXE", ".BAT", ".CMD"]
52    builtins.__xonsh_ctx__ = {}
53    builtins.__xonsh_shell__ = DummyShell()
54    builtins.__xonsh_help__ = lambda x: x
55    builtins.__xonsh_glob__ = glob.glob
56    builtins.__xonsh_exit__ = False
57    builtins.__xonsh_superhelp__ = lambda x: x
58    builtins.__xonsh_regexpath__ = lambda x: []
59    builtins.__xonsh_expand_path__ = lambda x: x
60    builtins.__xonsh_subproc_captured__ = sp
61    builtins.__xonsh_subproc_uncaptured__ = sp
62    builtins.__xonsh_stdout_uncaptured__ = None
63    builtins.__xonsh_stderr_uncaptured__ = None
64    builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs
65    builtins.__xonsh_commands_cache__ = DummyCommandsCache()
66    builtins.__xonsh_all_jobs__ = {}
67    builtins.__xonsh_history__ = DummyHistory()
68    builtins.__xonsh_subproc_captured_hiddenobject__ = sp
69    builtins.__xonsh_enter_macro__ = enter_macro
70    builtins.evalx = eval
71    builtins.execx = None
72    builtins.compilex = None
73    builtins.aliases = {}
74    # Unlike all the other stuff, this has to refer to the "real" one because all modules that would
75    # be firing events on the global instance.
76    builtins.events = xonsh_events
77    yield builtins
78    for attr in set(dir(builtins)) - old_builtins:
79        delattr(builtins, attr)
80    tasks.clear()  # must to this to enable resetting all_jobs
81
82
83if ON_WINDOWS:
84    try:
85        import win_unicode_console
86    except ImportError:
87        pass
88    else:
89
90        @pytest.fixture(autouse=True)
91        def disable_win_unicode_console(monkeypatch):
92            """ Disable win_unicode_console if it is present since it collides with
93            pytests ouptput capture"""
94            monkeypatch.setattr(win_unicode_console, "enable", lambda: None)
95