2016-06-25 01:15:48 +03:00
|
|
|
import builtins
|
2016-11-14 21:09:25 +02:00
|
|
|
import glob
|
|
|
|
import os
|
2018-11-09 10:08:03 +01:00
|
|
|
import sys
|
2016-07-05 09:10:22 +02:00
|
|
|
|
2016-06-25 01:15:48 +03:00
|
|
|
import pytest
|
2016-08-18 21:49:18 +02:00
|
|
|
|
2018-10-01 15:31:37 -04:00
|
|
|
from xonsh.built_ins import (
|
|
|
|
ensure_list_of_strs,
|
|
|
|
XonshSession,
|
|
|
|
pathsearch,
|
|
|
|
globsearch,
|
|
|
|
regexsearch,
|
|
|
|
list_of_strs_or_callables,
|
|
|
|
list_of_list_of_strs_outer_product,
|
|
|
|
call_macro,
|
|
|
|
enter_macro,
|
|
|
|
path_literal,
|
|
|
|
_BuiltIns,
|
|
|
|
)
|
2016-07-01 15:43:16 +03:00
|
|
|
from xonsh.execer import Execer
|
2016-09-24 16:09:01 -04:00
|
|
|
from xonsh.jobs import tasks
|
2016-08-27 12:21:15 -04:00
|
|
|
from xonsh.events import events
|
2016-09-01 11:54:45 +02:00
|
|
|
from xonsh.platform import ON_WINDOWS
|
2016-09-09 00:08:13 -04:00
|
|
|
|
2016-09-09 00:58:32 -04:00
|
|
|
from tools import DummyShell, sp, DummyCommandsCache, DummyEnv, DummyHistory
|
2016-06-25 01:15:48 +03:00
|
|
|
|
2016-07-05 09:10:22 +02:00
|
|
|
|
2016-11-14 21:09:25 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def source_path():
|
|
|
|
"""Get the xonsh source path."""
|
|
|
|
pwd = os.path.dirname(__file__)
|
|
|
|
return os.path.dirname(pwd)
|
|
|
|
|
|
|
|
|
2018-11-19 21:56:58 -05:00
|
|
|
def ensure_attached_session(monkeypatch, session):
|
2018-10-11 12:16:31 -04:00
|
|
|
for i in range(1, 11):
|
2018-11-19 21:56:58 -05:00
|
|
|
|
|
|
|
# next try to monkey patch with raising.
|
|
|
|
try:
|
|
|
|
monkeypatch.setattr(builtins, "__xonsh__", session, raising=True)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if hasattr(builtins, "__xonsh__"):
|
|
|
|
break
|
|
|
|
# first try to monkey patch without raising.
|
|
|
|
try:
|
|
|
|
monkeypatch.setattr(builtins, "__xonsh__", session, raising=False)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if hasattr(builtins, "__xonsh__"):
|
|
|
|
break
|
|
|
|
# now just try to apply it
|
2018-10-11 12:16:31 -04:00
|
|
|
builtins.__xonsh__ = session
|
|
|
|
if hasattr(builtins, "__xonsh__"):
|
|
|
|
break
|
2018-10-11 12:04:48 -04:00
|
|
|
# I have no idea why pytest fails to assign into the builtins module
|
|
|
|
# sometimes, but the following globals trick seems to work -scopatz
|
2018-10-11 12:16:31 -04:00
|
|
|
globals()["__builtins__"]["__xonsh__"] = session
|
|
|
|
if hasattr(builtins, "__xonsh__"):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Could not attach xonsh session to builtins " "after many tries!"
|
|
|
|
)
|
2018-10-11 12:04:48 -04:00
|
|
|
|
|
|
|
|
2016-07-01 15:43:16 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def xonsh_execer(monkeypatch):
|
2016-07-03 12:00:24 +03:00
|
|
|
"""Initiate the Execer with a mocked nop `load_builtins`"""
|
2018-08-30 09:18:49 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
"xonsh.built_ins.load_builtins.__code__",
|
|
|
|
(lambda *args, **kwargs: None).__code__,
|
|
|
|
)
|
2018-11-19 21:56:58 -05:00
|
|
|
added_session = False
|
2018-09-13 14:03:35 -04:00
|
|
|
if not hasattr(builtins, "__xonsh__"):
|
2018-11-19 21:56:58 -05:00
|
|
|
added_session = True
|
|
|
|
ensure_attached_session(monkeypatch, XonshSession())
|
2017-02-13 00:25:38 -05:00
|
|
|
execer = Execer(unload=False)
|
2018-09-13 14:03:35 -04:00
|
|
|
builtins.__xonsh__.execer = execer
|
2018-11-19 21:56:58 -05:00
|
|
|
yield execer
|
|
|
|
if added_session:
|
|
|
|
monkeypatch.delattr(builtins, "__xonsh__", raising=False)
|
2016-07-01 15:43:16 +03:00
|
|
|
|
2016-06-25 01:15:48 +03:00
|
|
|
|
2018-11-09 10:08:03 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def monkeypatch_stderr(monkeypatch):
|
|
|
|
"""Monkeypath sys.stderr with no ResourceWarning."""
|
|
|
|
with open(os.devnull, "w") as fd:
|
|
|
|
monkeypatch.setattr(sys, "stderr", fd)
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2018-11-19 20:22:18 -05:00
|
|
|
@pytest.fixture
|
2017-01-08 20:31:39 -05:00
|
|
|
def xonsh_events():
|
|
|
|
yield events
|
|
|
|
for name, oldevent in vars(events).items():
|
|
|
|
# Heavily based on transmogrification
|
2017-01-12 13:29:15 -05:00
|
|
|
species = oldevent.species
|
|
|
|
newevent = events._mkevent(name, species, species.__doc__)
|
2017-01-08 20:31:39 -05:00
|
|
|
setattr(events, name, newevent)
|
|
|
|
|
|
|
|
|
2018-11-19 20:22:18 -05:00
|
|
|
@pytest.fixture
|
2018-11-19 21:56:58 -05:00
|
|
|
def xonsh_builtins(monkeypatch, xonsh_events):
|
2016-07-03 12:00:24 +03:00
|
|
|
"""Mock out most of the builtins xonsh attributes."""
|
2017-02-27 21:44:58 +02:00
|
|
|
old_builtins = set(dir(builtins))
|
2018-09-13 14:03:35 -04:00
|
|
|
execer = getattr(getattr(builtins, "__xonsh__", None), "execer", None)
|
2018-10-11 11:22:57 -04:00
|
|
|
session = XonshSession(execer=execer, ctx={})
|
2018-11-19 21:56:58 -05:00
|
|
|
ensure_attached_session(monkeypatch, session)
|
2018-09-13 14:03:35 -04:00
|
|
|
builtins.__xonsh__.env = DummyEnv()
|
2016-09-09 09:19:05 +02:00
|
|
|
if ON_WINDOWS:
|
2018-09-13 14:03:35 -04:00
|
|
|
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
|
2018-10-01 15:31:37 -04:00
|
|
|
builtins.__xonsh__.pathsearch = pathsearch
|
|
|
|
builtins.__xonsh__.globsearch = globsearch
|
|
|
|
builtins.__xonsh__.regexsearch = regexsearch
|
2018-09-13 14:03:35 -04:00
|
|
|
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 = {}
|
2018-10-01 15:31:37 -04:00
|
|
|
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
|
|
|
|
)
|
2018-09-13 14:03:35 -04:00
|
|
|
builtins.__xonsh__.history = DummyHistory()
|
2018-10-01 15:31:37 -04:00
|
|
|
builtins.__xonsh__.subproc_captured_stdout = sp
|
|
|
|
builtins.__xonsh__.subproc_captured_inject = sp
|
|
|
|
builtins.__xonsh__.subproc_captured_object = sp
|
2018-09-13 14:03:35 -04:00
|
|
|
builtins.__xonsh__.subproc_captured_hiddenobject = sp
|
|
|
|
builtins.__xonsh__.enter_macro = enter_macro
|
2018-10-01 15:31:37 -04:00
|
|
|
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)
|
2016-06-25 01:15:48 +03:00
|
|
|
builtins.evalx = eval
|
|
|
|
builtins.execx = None
|
|
|
|
builtins.compilex = None
|
|
|
|
builtins.aliases = {}
|
2016-08-27 21:24:27 -04:00
|
|
|
# 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.
|
2017-01-08 20:31:39 -05:00
|
|
|
builtins.events = xonsh_events
|
2016-06-25 21:07:25 +03:00
|
|
|
yield builtins
|
2018-11-19 21:56:58 -05:00
|
|
|
monkeypatch.delattr(builtins, "__xonsh__", raising=False)
|
2017-02-27 21:44:58 +02:00
|
|
|
for attr in set(dir(builtins)) - old_builtins:
|
2018-10-01 15:48:47 -04:00
|
|
|
if hasattr(builtins, attr):
|
|
|
|
delattr(builtins, attr)
|
2016-09-24 16:13:14 -04:00
|
|
|
tasks.clear() # must to this to enable resetting all_jobs
|
2020-05-06 21:23:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
"""Abort test run if --flake8 requested, since it would hang on parser_test.py"""
|
|
|
|
if config.getoption('--flake8', ''):
|
|
|
|
pytest.exit("pytest-flake8 no longer supported, use flake8 instead.")
|