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
|
2021-05-20 15:44:26 +05:30
|
|
|
import types
|
|
|
|
import typing as tp
|
|
|
|
from unittest.mock import MagicMock
|
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
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
from xonsh.built_ins import XonshSession, XSH
|
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
|
2021-05-11 13:10:58 +03:00
|
|
|
from xonsh.parsers.completion_context import CompletionContextParser
|
2016-09-09 00:08:13 -04:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
from xonsh import commands_cache
|
|
|
|
from tools import DummyShell, sp, 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)
|
|
|
|
|
|
|
|
|
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`"""
|
2017-02-13 00:25:38 -05:00
|
|
|
execer = Execer(unload=False)
|
2021-05-20 15:44:26 +05:30
|
|
|
monkeypatch.setattr(XSH, "execer", execer)
|
2018-11-19 21:56:58 -05:00
|
|
|
yield execer
|
2021-05-20 15:44:26 +05:30
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def patch_commands_cache_bins(xession, tmp_path, monkeypatch):
|
|
|
|
def _factory(binaries: tp.List[str]):
|
|
|
|
if not xession.env.get("PATH"):
|
|
|
|
xession.env["PATH"] = [tmp_path]
|
|
|
|
exec_mock = MagicMock(return_value=binaries)
|
|
|
|
monkeypatch.setattr(commands_cache, "executables_in", exec_mock)
|
|
|
|
cc = commands_cache.CommandsCache()
|
|
|
|
xession.commands_cache = cc
|
|
|
|
return cc
|
|
|
|
|
|
|
|
return _factory
|
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))
|
2021-05-20 15:44:26 +05:30
|
|
|
XSH.load(
|
|
|
|
execer=Execer(unload=False),
|
|
|
|
ctx={},
|
2018-10-01 15:31:37 -04:00
|
|
|
)
|
2021-05-20 15:44:26 +05:30
|
|
|
if ON_WINDOWS:
|
|
|
|
XSH.env["PATHEXT"] = [".EXE", ".BAT", ".CMD"]
|
|
|
|
|
|
|
|
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", {}),
|
|
|
|
("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)
|
|
|
|
|
|
|
|
cc = XSH.commands_cache
|
|
|
|
monkeypatch.setattr(cc, "locate_binary", types.MethodType(locate_binary, cc))
|
|
|
|
|
|
|
|
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
|
2016-06-25 21:07:25 +03:00
|
|
|
yield builtins
|
2021-05-20 15:44:26 +05:30
|
|
|
XSH.unload()
|
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
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def xession(xonsh_builtins) -> XonshSession:
|
|
|
|
return XSH
|
|
|
|
|
|
|
|
|
2021-05-11 13:10:58 +03:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def completion_context_parse():
|
|
|
|
return CompletionContextParser().parse
|
|
|
|
|
|
|
|
|
2021-06-07 23:10:40 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def ptk_shell(xonsh_execer):
|
|
|
|
from prompt_toolkit.input import create_pipe_input
|
|
|
|
from prompt_toolkit.output import DummyOutput
|
|
|
|
from xonsh.ptk_shell.shell import PromptToolkitShell
|
|
|
|
|
|
|
|
inp = create_pipe_input()
|
|
|
|
out = DummyOutput()
|
|
|
|
shell = PromptToolkitShell(
|
|
|
|
execer=xonsh_execer, ctx={}, ptk_args={"input": inp, "output": out}
|
|
|
|
)
|
|
|
|
yield inp, out, shell
|
|
|
|
inp.close()
|
|
|
|
|
|
|
|
|
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"""
|
2020-08-10 16:25:58 +02:00
|
|
|
if config.getoption("--flake8", ""):
|
2020-05-06 21:23:57 -04:00
|
|
|
pytest.exit("pytest-flake8 no longer supported, use flake8 instead.")
|