xonsh/tests/test_pipelines.py
Noorhteen Raja NJ 7c4e207abd
update test xsh usage (#4581)
* todo

* test: remove usage of DummyEnv and setting .env attribute on xession fixture

one step closer to making too much of tweaking to xession during tests

* test: fix tests vox and gitstatus-prompt

* docs: update test-fixture usage

* fix: import flake8 error

* test: remove direct access to XSH in tests

* test: remove usage of XSH in test files

* todo

* test: use tmp-dir to create stubs

* refactor: use fixture factory to mock out XonshSession

* refactor: remove direct access of XSH from functions

* refactor: remove direct access of XSH from functions

* fix: qa checks

* refactor: rename variables to match their values

* test: update failing tests because it had no PATH set previously

* fix: remove builtins usage from pyghooks.py

* style:

* refactor: update tests to use fixtures

* fix: env varialbe is setup per function

some tests accidentally update the env variables and that is leaking
into next tests

* fix: failing vox tests

* test: set commands_cache per test

* test: fix failing tests

* fix: failing tests on linux

ptk-highlight

* fix: failing tests on Windows

cwd-prompt

* test: copy env as to not affect original object

* fix: lazily evaluate cmds-cache in pyghooks

* test: fix failing tests

* fix: qa errors import

* test: set commands-cache per test

while caching path results

* test: speedup test_thread_local_swap

* fix: failing tests on windows

* refactor: Execer doesn't control session

* refactor: XSH.unload will take care of reversing builtins attrs set

* test: use env.update over monkeypatch

* Revert "test: use env.update over monkeypatch"

This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-07 17:33:22 -05:00

113 lines
3.3 KiB
Python

"""
Tests for command pipelines.
"""
import os
import pytest
from xonsh.platform import ON_WINDOWS
from xonsh.procs.pipelines import CommandPipeline
from tests.tools import skip_if_on_windows, skip_if_on_unix
@pytest.fixture(autouse=True)
def patched_events(monkeypatch, xonsh_events, xonsh_session):
from xonsh.jobs import tasks
tasks.clear()
# needed for ci tests
monkeypatch.setitem(
xonsh_session.env, "RAISE_SUBPROC_ERROR", False
) # for the failing `grep` commands
monkeypatch.setitem(
xonsh_session.env, "XONSH_CAPTURE_ALWAYS", True
) # capture output of ![]
if ON_WINDOWS:
monkeypatch.setattr(
xonsh_session,
"aliases",
{
"echo": "cmd /c echo".split(),
"grep": "cmd /c findstr".split(),
},
raising=False,
)
@pytest.mark.parametrize(
"cmdline, stdout, stderr",
(
("!(echo hi)", "hi\n", ""),
("!(echo hi o>e)", "", "hi\n"),
pytest.param(
"![echo hi]",
"hi\n",
"",
marks=pytest.mark.xfail(
ON_WINDOWS,
reason="ConsoleParallelReader doesn't work without a real console",
),
),
pytest.param(
"![echo hi o>e]",
"",
"hi\n",
marks=pytest.mark.xfail(
ON_WINDOWS, reason="stderr isn't captured in ![] on windows"
),
),
pytest.param(
r"!(echo 'hi\nho')", "hi\nho\n", "", marks=skip_if_on_windows
), # won't work with cmd
# for some reason cmd's echo adds an extra space:
pytest.param(
r"!(cmd /c 'echo hi && echo ho')", "hi \nho\n", "", marks=skip_if_on_unix
),
("!(echo hi | grep h)", "hi\n", ""),
("!(echo hi | grep x)", "", ""),
),
)
def test_command_pipeline_capture(cmdline, stdout, stderr, xonsh_execer):
pipeline: CommandPipeline = xonsh_execer.eval(cmdline)
assert pipeline.out == stdout
assert pipeline.err == (stderr or None)
assert pipeline.raw_out == stdout.replace("\n", os.linesep).encode()
assert pipeline.raw_err == stderr.replace("\n", os.linesep).encode()
@pytest.mark.parametrize(
"cmdline, output",
(
("echo hi", "hi\n"),
("echo hi | grep h", "hi\n"),
("echo hi | grep x", ""),
pytest.param("echo -n hi", "hi", marks=skip_if_on_windows),
),
)
def test_simple_capture(cmdline, output, xonsh_execer):
assert xonsh_execer.eval(f"$({cmdline})") == output
def test_raw_substitution(xonsh_execer):
assert xonsh_execer.eval("$(echo @(b'bytes!'))") == "bytes!\n"
@pytest.mark.parametrize(
"cmdline, result",
(
("bool(!(echo 1))", True),
("bool(!(nocommand))", False),
("int(!(echo 1))", 0),
("int(!(nocommand))", 1),
("hash(!(echo 1))", 0),
("hash(!(nocommand))", 1),
("str(!(echo 1))", "1\n"),
("str(!(nocommand))", ""),
("!(echo 1) == 0", True),
("!(nocommand) == 1", True),
pytest.param("!(echo -n str) == 'str'", True, marks=skip_if_on_windows),
("!(nocommand) == ''", True),
),
)
def test_casting(cmdline, result, xonsh_execer):
assert xonsh_execer.eval(f"{cmdline}") == result