2015-11-21 12:30:43 -05:00
|
|
|
"""Tests the xonsh main function."""
|
|
|
|
import builtins
|
2019-04-19 22:52:10 +02:00
|
|
|
import gc
|
2018-11-13 21:59:48 +01:00
|
|
|
import os
|
2016-12-17 18:10:29 +08:00
|
|
|
import os.path
|
2016-07-01 13:35:16 +03:00
|
|
|
import sys
|
2022-01-31 21:26:34 +05:30
|
|
|
from contextlib import contextmanager
|
2015-11-21 12:30:43 -05:00
|
|
|
|
2016-07-01 13:35:16 +03:00
|
|
|
import pytest
|
2022-01-31 21:26:34 +05:30
|
|
|
|
|
|
|
import xonsh.main
|
2022-03-24 00:09:28 +05:30
|
|
|
from xonsh.pytest.tools import ON_WINDOWS, TEST_DIR, skip_if_on_windows
|
2022-01-31 21:26:34 +05:30
|
|
|
from xonsh.main import XonshMode
|
2015-11-21 12:30:43 -05:00
|
|
|
|
|
|
|
|
2016-06-18 16:29:30 +03:00
|
|
|
def Shell(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-07-01 13:35:16 +03:00
|
|
|
@pytest.fixture
|
2021-05-20 15:44:26 +05:30
|
|
|
def shell(xession, monkeypatch):
|
2016-07-03 12:00:24 +03:00
|
|
|
"""Xonsh Shell Mock"""
|
2019-04-19 22:52:10 +02:00
|
|
|
gc.collect()
|
2018-08-30 09:18:49 -05:00
|
|
|
Shell.shell_type_aliases = {"rl": "readline"}
|
|
|
|
monkeypatch.setattr(xonsh.main, "Shell", Shell)
|
2016-07-01 13:35:16 +03:00
|
|
|
|
|
|
|
|
2021-09-15 22:14:28 +03:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def empty_xonshrc(monkeypatch):
|
|
|
|
# Don't use the local machine's xonshrc
|
|
|
|
empty_file_path = "NUL" if ON_WINDOWS else "/dev/null"
|
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC", empty_file_path)
|
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_no_arg(shell, monkeypatch, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
2016-07-01 13:35:16 +03:00
|
|
|
xonsh.main.premain([])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("XONSH_LOGIN")
|
2016-07-01 13:35:16 +03:00
|
|
|
|
2016-03-20 19:43:37 -04:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_interactive(shell, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
xonsh.main.premain(["-i"])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("XONSH_INTERACTIVE")
|
2016-06-22 18:23:36 -04:00
|
|
|
|
2016-06-22 11:35:24 +03:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_login_command(shell, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
xonsh.main.premain(["-l", "-c", 'echo "hi"'])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("XONSH_LOGIN")
|
2015-11-21 12:30:43 -05:00
|
|
|
|
2016-03-20 21:31:14 -04:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_login(shell, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
xonsh.main.premain(["-l"])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("XONSH_LOGIN")
|
2015-11-21 12:30:43 -05:00
|
|
|
|
2016-06-22 11:35:24 +03:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_D(shell, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
xonsh.main.premain(["-DTEST1=1616", "-DTEST2=LOL"])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("TEST1") == "1616"
|
|
|
|
assert xession.env.get("TEST2") == "LOL"
|
2016-06-18 16:29:30 +03:00
|
|
|
|
2016-06-18 20:14:05 +03:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_custom_rc(shell, tmpdir, monkeypatch, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
2019-02-13 18:49:39 -05:00
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
2018-08-30 09:18:49 -05:00
|
|
|
f = tmpdir.join("wakkawakka")
|
2017-04-21 14:29:55 -04:00
|
|
|
f.write("print('hi')")
|
2018-08-30 09:18:49 -05:00
|
|
|
args = xonsh.main.premain(["--rc", f.strpath])
|
2017-09-27 19:15:28 -04:00
|
|
|
assert args.mode == XonshMode.interactive
|
2021-08-17 17:01:41 +02:00
|
|
|
assert f.strpath in xession.rc_files
|
2021-05-20 15:44:26 +05:30
|
|
|
|
2017-04-21 14:29:55 -04:00
|
|
|
|
2021-04-07 21:37:21 +03:00
|
|
|
@pytest.mark.skipif(
|
2021-05-20 15:44:26 +05:30
|
|
|
ON_WINDOWS and sys.version[:3] == "3.8",
|
|
|
|
reason="weird failure on py38+windows",
|
|
|
|
)
|
|
|
|
def test_rc_with_modules(shell, tmpdir, monkeypatch, capsys, xession):
|
2021-01-04 17:31:05 +00:00
|
|
|
"""Test that an RC file can load modules inside the same folder it is located in."""
|
|
|
|
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
|
|
|
|
|
|
|
tmpdir.join("my_python_module.py").write("print('Hello,')")
|
|
|
|
tmpdir.join("my_xonsh_module.xsh").write("print('World!')")
|
|
|
|
rc = tmpdir.join("rc.xsh")
|
|
|
|
rc.write("from my_python_module import *\nfrom my_xonsh_module import *")
|
|
|
|
xonsh.main.premain(["--rc", rc.strpath])
|
|
|
|
|
2021-08-17 17:01:41 +02:00
|
|
|
assert rc.strpath in xession.rc_files
|
2021-01-04 17:31:05 +00:00
|
|
|
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert "Hello,\nWorld!" in stdout
|
|
|
|
assert len(stderr) == 0
|
|
|
|
|
|
|
|
# Check that the temporary rc's folder is not left behind on the path
|
|
|
|
assert tmpdir.strpath not in sys.path
|
|
|
|
|
|
|
|
|
2021-08-19 22:10:22 +05:30
|
|
|
def test_python_rc(shell, tmpdir, monkeypatch, capsys, xession, mocker):
|
|
|
|
"""Test that python based control files are executed using Python's parser"""
|
|
|
|
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
|
|
|
|
|
|
|
# spy on xonsh's compile method
|
|
|
|
spy = mocker.spy(xession.execer, "compile")
|
|
|
|
|
|
|
|
rc = tmpdir.join("rc.py")
|
|
|
|
rc.write("print('Hello World!')")
|
|
|
|
xonsh.main.premain(["--rc", rc.strpath])
|
|
|
|
|
|
|
|
assert rc.strpath in xession.rc_files
|
|
|
|
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert "Hello World!" in stdout
|
|
|
|
assert len(stderr) == 0
|
|
|
|
|
|
|
|
# Check that the temporary rc's folder is not left behind on the path
|
|
|
|
assert tmpdir.strpath not in sys.path
|
2021-09-26 21:02:24 +05:30
|
|
|
assert not spy.called
|
2021-08-19 22:10:22 +05:30
|
|
|
|
|
|
|
|
2021-05-11 22:42:37 +02:00
|
|
|
def test_rcdir(shell, tmpdir, monkeypatch, capsys):
|
|
|
|
"""
|
|
|
|
Test that files are loaded from an rcdir, after a normal rc file,
|
|
|
|
and in lexographic order.
|
|
|
|
"""
|
|
|
|
|
|
|
|
rcdir = tmpdir.join("rc.d")
|
|
|
|
rcdir.mkdir()
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC_DIR", str(rcdir))
|
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC", str(tmpdir.join("rc.xsh")))
|
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
|
|
|
|
|
|
|
rcdir.join("2.xsh").write("print('2.xsh')")
|
|
|
|
rcdir.join("0.xsh").write("print('0.xsh')")
|
|
|
|
rcdir.join("1.xsh").write("print('1.xsh')")
|
|
|
|
tmpdir.join("rc.xsh").write("print('rc.xsh')")
|
|
|
|
|
|
|
|
xonsh.main.premain([])
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
|
|
|
|
assert "rc.xsh\n0.xsh\n1.xsh\n2.xsh" in stdout
|
|
|
|
assert len(stderr) == 0
|
|
|
|
|
|
|
|
|
2021-08-19 18:14:20 +02:00
|
|
|
def test_rcdir_cli(shell, tmpdir, xession, monkeypatch):
|
|
|
|
"""Test that --rc DIR works"""
|
|
|
|
rcdir = tmpdir.join("rcdir")
|
|
|
|
rcdir.mkdir()
|
|
|
|
rc = rcdir.join("test.xsh")
|
|
|
|
rc.write("print('test.xsh')")
|
|
|
|
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
xargs = xonsh.main.premain(["--rc", rcdir.strpath])
|
|
|
|
assert len(xargs.rc) == 1 and xargs.rc[0] == rcdir.strpath
|
|
|
|
assert rc.strpath in xession.rc_files
|
|
|
|
|
|
|
|
|
2021-05-11 22:42:37 +02:00
|
|
|
def test_rcdir_empty(shell, tmpdir, monkeypatch, capsys):
|
|
|
|
"""Test that an empty XONSHRC_DIR is not an error"""
|
|
|
|
|
|
|
|
rcdir = tmpdir.join("rc.d")
|
|
|
|
rcdir.mkdir()
|
2021-05-20 15:44:26 +05:30
|
|
|
rc = tmpdir.join("rc.xsh")
|
|
|
|
rc.write_binary(b"")
|
2021-05-11 22:42:37 +02:00
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
2021-05-20 15:44:26 +05:30
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC", str(rc))
|
2021-05-11 22:42:37 +02:00
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC_DIR", str(rcdir))
|
|
|
|
|
|
|
|
xonsh.main.premain([])
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert len(stderr) == 0
|
|
|
|
|
|
|
|
|
2021-05-14 15:12:34 +02:00
|
|
|
# the parameterisation is a list of xonsh args, followed by the list
|
|
|
|
# of RC files (see function body) expected to be loaded in order
|
|
|
|
# note that a tty is not faked, so this will default to non-interactive
|
|
|
|
# (skipped on windows because XONSHRC="/path/to/f1.xsh:/path/to/f2.xsh"
|
|
|
|
# doesn't appear to work, while it does on other platforms)
|
|
|
|
@pytest.mark.skipif(ON_WINDOWS, reason="Issue with multi-file XONSHRC")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["args", "expected"],
|
|
|
|
[
|
|
|
|
# non-interactive, nothing loading
|
|
|
|
[[], []],
|
|
|
|
# interactive, normal XONSHRC and XONSHRC_DIR
|
|
|
|
[["-i"], ["F0", "F1", "D0", "D1"]],
|
|
|
|
# --no-rc wins over -i
|
|
|
|
[["--no-rc", "-i"], []],
|
|
|
|
# --no-rc wins over -i --rc
|
|
|
|
[["--no-rc", "-i", "--rc", "<R0>"], []],
|
|
|
|
# --rc does nothing in non-interactive mode
|
|
|
|
[["--rc", "<R0>"], []],
|
|
|
|
# but is respected in interactive
|
|
|
|
[["-i", "--rc", "<R0>"], ["R0"]],
|
|
|
|
# multiple invocations of --rc only use the last
|
|
|
|
[["-i", "--rc", "<R0>", "--rc", "<R1>"], ["R1"]],
|
|
|
|
# but multiple RC files can be specified after --rc
|
|
|
|
[["-i", "--rc", "<R0>", "<R1>"], ["R0", "R1"]],
|
|
|
|
# including the same file twice
|
|
|
|
[["-i", "--rc", "<R0>", "<R0>"], ["R0", "R0"]],
|
|
|
|
# scripts are non-interactive
|
|
|
|
[["<SC>"], ["SC"]],
|
|
|
|
# but -i will load the normal environment with a script
|
|
|
|
[["-i", "<SC>"], ["F0", "F1", "D0", "D1", "SC"]],
|
|
|
|
# no-rc has no effect on a script
|
|
|
|
[["--no-rc", "<SC>"], ["SC"]],
|
|
|
|
# but does prevent RC loading in -i mode
|
|
|
|
[["--no-rc", "-i", "<SC>"], ["SC"]],
|
|
|
|
# --rc doesn't work here because a script is non-interactive
|
|
|
|
[["--rc", "<R0>", "--", "<SC>"], ["SC"]],
|
|
|
|
# unless forced with -i
|
|
|
|
[["-i", "--rc", "<R0>", "--", "<SC>"], ["R0", "SC"]],
|
|
|
|
# --no-rc also wins here
|
|
|
|
[["-i", "--rc", "<R0>", "--no-rc", "--", "<SC>"], ["SC"]],
|
|
|
|
# single commands are non-interactive
|
|
|
|
[["-c", "pass"], []],
|
|
|
|
# but load RCs with -i
|
|
|
|
[["-i", "-c", "pass"], ["F0", "F1", "D0", "D1"]],
|
|
|
|
# --rc ignores without -i
|
|
|
|
[["--rc", "<R0>", "-c", "pass"], []],
|
|
|
|
# but used with -i
|
|
|
|
[["-i", "--rc", "<R0>", "-c", "pass"], ["R0"]],
|
|
|
|
],
|
|
|
|
ids=lambda ae: " ".join(ae),
|
|
|
|
)
|
|
|
|
def test_script_startup(shell, tmpdir, monkeypatch, capsys, args, expected):
|
|
|
|
"""
|
|
|
|
Test the correct scripts are loaded, in the correct order, for
|
|
|
|
different combinations of CLI arguments. See
|
|
|
|
https://github.com/xonsh/xonsh/issues/4096
|
|
|
|
|
|
|
|
This sets up a standard set of RC files which will be loaded,
|
|
|
|
and tests whether they print their payloads at all, or in the right
|
|
|
|
order, depending on the CLI arguments chosen.
|
|
|
|
"""
|
|
|
|
rcdir = tmpdir.join("rc.d")
|
|
|
|
rcdir.mkdir()
|
|
|
|
# XONSHRC_DIR files, in order
|
|
|
|
rcdir.join("d0.xsh").write("print('D0')")
|
|
|
|
rcdir.join("d1.xsh").write("print('D1')")
|
|
|
|
# XONSHRC files, in order
|
|
|
|
f0 = tmpdir.join("f0.xsh")
|
|
|
|
f0.write("print('F0')")
|
|
|
|
f1 = tmpdir.join("f1.xsh")
|
|
|
|
f1.write("print('F1')")
|
|
|
|
# RC files which can be explicitly loaded with --rc
|
|
|
|
r0 = tmpdir.join("r0.xsh")
|
|
|
|
r0.write("print('R0')")
|
|
|
|
r1 = tmpdir.join("r1.xsh")
|
|
|
|
r1.write("print('R1')")
|
|
|
|
# a (non-RC) script which can be loaded
|
|
|
|
sc = tmpdir.join("sc.xsh")
|
|
|
|
sc.write("print('SC')")
|
|
|
|
|
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC", f"{f0}:{f1}")
|
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC_DIR", str(rcdir))
|
|
|
|
|
|
|
|
# replace <RC> with the path to rc.xsh and <SC> with sc.xsh
|
|
|
|
args = [
|
|
|
|
a.replace("<R0>", str(r0)).replace("<R1>", str(r1)).replace("<SC>", str(sc))
|
|
|
|
for a in args
|
|
|
|
]
|
|
|
|
|
|
|
|
# since we only test xonsh.premain here, a script file (SC)
|
|
|
|
# won't actually get run here, so won't appear in the stdout,
|
|
|
|
# so don't check for it (but check for a .file in the parsed args)
|
|
|
|
check_for_script = "SC" in expected
|
|
|
|
expected = [e for e in expected if e != "SC"]
|
|
|
|
|
|
|
|
xargs = xonsh.main.premain(args)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert "\n".join(expected) in stdout
|
|
|
|
if check_for_script:
|
|
|
|
assert xargs.file is not None
|
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_rcdir_ignored_with_rc(shell, tmpdir, monkeypatch, capsys, xession):
|
2021-05-11 22:42:37 +02:00
|
|
|
"""Test that --rc suppresses loading XONSHRC_DIRs"""
|
|
|
|
|
|
|
|
rcdir = tmpdir.join("rc.d")
|
|
|
|
rcdir.mkdir()
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
monkeypatch.setitem(os.environ, "XONSHRC_DIR", str(rcdir))
|
|
|
|
rcdir.join("rcd.xsh").write("print('RCDIR')")
|
|
|
|
tmpdir.join("rc.xsh").write("print('RCFILE')")
|
|
|
|
|
|
|
|
xonsh.main.premain(["--rc", str(tmpdir.join("rc.xsh"))])
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert "RCDIR" not in stdout
|
|
|
|
assert "RCFILE" in stdout
|
2021-08-17 17:01:41 +02:00
|
|
|
assert str(rcdir.join("rcd.xsh")) not in xession.rc_files
|
2021-05-11 22:42:37 +02:00
|
|
|
|
|
|
|
|
2021-02-17 23:01:33 +05:30
|
|
|
@pytest.mark.skipif(ON_WINDOWS, reason="See https://github.com/xonsh/xonsh/issues/3936")
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_rc_with_modified_path(shell, tmpdir, monkeypatch, capsys, xession):
|
2021-01-04 17:31:05 +00:00
|
|
|
"""Test that an RC file can edit the sys.path variable without losing those values."""
|
|
|
|
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
|
|
|
|
|
|
|
rc = tmpdir.join("rc.xsh")
|
|
|
|
rc.write(f"import sys\nsys.path.append('{tmpdir.strpath}')\nprint('Hello, World!')")
|
|
|
|
xonsh.main.premain(["--rc", rc.strpath])
|
|
|
|
|
2021-08-17 17:01:41 +02:00
|
|
|
assert rc.strpath in xession.rc_files
|
2021-01-04 17:31:05 +00:00
|
|
|
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert "Hello, World!" in stdout
|
|
|
|
assert len(stderr) == 0
|
|
|
|
|
|
|
|
# Check that the path that was explicitly added is not accidentally deleted
|
|
|
|
assert tmpdir.strpath in sys.path
|
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_rc_with_failing_module(shell, tmpdir, monkeypatch, capsys, xession):
|
2021-01-04 17:31:05 +00:00
|
|
|
"""Test that an RC file which imports a module that throws an exception ."""
|
|
|
|
|
|
|
|
monkeypatch.setattr(sys.stdin, "isatty", lambda: True)
|
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
|
|
|
|
|
|
|
tmpdir.join("my_failing_module.py").write("raise RuntimeError('Unexpected error')")
|
|
|
|
rc = tmpdir.join("rc.xsh")
|
|
|
|
rc.write("from my_failing_module import *")
|
|
|
|
xonsh.main.premain(["--rc", rc.strpath])
|
|
|
|
|
2021-08-17 17:01:41 +02:00
|
|
|
assert rc.strpath not in xession.rc_files
|
2021-01-04 17:31:05 +00:00
|
|
|
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert len(stdout) == 0
|
|
|
|
assert "RuntimeError: Unexpected error" in stderr
|
|
|
|
|
|
|
|
# Check that the temporary rc's folder is not left behind on the path
|
|
|
|
assert tmpdir.strpath not in sys.path
|
|
|
|
|
|
|
|
|
2017-07-15 12:09:05 -04:00
|
|
|
def test_no_rc_with_script(shell, tmpdir):
|
2018-08-30 09:18:49 -05:00
|
|
|
args = xonsh.main.premain(["tests/sample.xsh"])
|
2017-09-27 19:15:28 -04:00
|
|
|
assert not (args.mode == XonshMode.interactive)
|
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_force_interactive_rc_with_script(shell, tmpdir, xession):
|
2020-05-05 06:42:28 -04:00
|
|
|
xonsh.main.premain(["-i", "tests/sample.xsh"])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("XONSH_INTERACTIVE")
|
2017-09-27 19:15:28 -04:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_force_interactive_custom_rc_with_script(shell, tmpdir, monkeypatch, xession):
|
2017-09-27 19:15:28 -04:00
|
|
|
"""Calling a custom RC file on a script-call with the interactive flag
|
|
|
|
should run interactively
|
|
|
|
"""
|
2019-02-13 18:49:39 -05:00
|
|
|
monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False")
|
2018-08-30 09:18:49 -05:00
|
|
|
f = tmpdir.join("wakkawakka")
|
2017-09-27 19:15:28 -04:00
|
|
|
f.write("print('hi')")
|
2018-08-30 09:18:49 -05:00
|
|
|
args = xonsh.main.premain(["-i", "--rc", f.strpath, "tests/sample.xsh"])
|
2017-09-27 19:15:28 -04:00
|
|
|
assert args.mode == XonshMode.interactive
|
2021-08-17 17:01:41 +02:00
|
|
|
assert f.strpath in xession.rc_files
|
2017-07-23 12:02:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_custom_rc_with_script(shell, tmpdir):
|
2017-09-27 19:15:28 -04:00
|
|
|
"""Calling a custom RC file on a script-call without the interactive flag
|
|
|
|
should not run interactively
|
|
|
|
"""
|
2018-08-30 09:18:49 -05:00
|
|
|
f = tmpdir.join("wakkawakka")
|
2017-07-15 12:09:05 -04:00
|
|
|
f.write("print('hi')")
|
2018-08-30 09:18:49 -05:00
|
|
|
args = xonsh.main.premain(["--rc", f.strpath, "tests/sample.xsh"])
|
2017-09-27 19:15:28 -04:00
|
|
|
assert not (args.mode == XonshMode.interactive)
|
2017-07-15 12:09:05 -04:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_no_rc(shell, tmpdir, xession):
|
2018-09-13 16:49:09 -04:00
|
|
|
xonsh.main.premain(["--no-rc", "-i"])
|
2021-08-17 17:01:41 +02:00
|
|
|
assert len(xession.rc_files) == 0
|
2017-04-21 14:29:55 -04:00
|
|
|
|
|
|
|
|
2016-12-17 18:10:29 +08:00
|
|
|
@pytest.mark.parametrize(
|
2018-08-30 09:18:49 -05:00
|
|
|
"arg", ["", "-i", "-vERSION", "-hAALP", "TTTT", "-TT", "--TTT"]
|
|
|
|
)
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_with_file_argument(arg, shell, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
xonsh.main.premain(["tests/sample.xsh", arg])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert not (xession.env.get("XONSH_INTERACTIVE"))
|
2016-06-18 20:14:05 +03:00
|
|
|
|
2016-06-20 19:27:53 +03:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_premain_interactive__with_file_argument(shell, xession):
|
2018-08-30 09:18:49 -05:00
|
|
|
xonsh.main.premain(["-i", "tests/sample.xsh"])
|
2021-05-20 15:44:26 +05:30
|
|
|
assert xession.env.get("XONSH_INTERACTIVE")
|
2016-06-20 19:27:53 +03:00
|
|
|
|
2016-06-18 20:14:05 +03:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
@pytest.mark.parametrize("case", ["----", "--hep", "-TT", "--TTTT"])
|
2017-02-13 00:25:38 -05:00
|
|
|
def test_premain_invalid_arguments(shell, case, capsys):
|
2016-07-01 13:35:16 +03:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
xonsh.main.premain([case])
|
2018-08-30 09:18:49 -05:00
|
|
|
assert "unrecognized argument" in capsys.readouterr()[1]
|
2016-12-17 18:10:29 +08:00
|
|
|
|
2019-02-13 18:49:39 -05:00
|
|
|
|
2018-11-13 22:12:25 +01:00
|
|
|
def test_premain_timings_arg(shell):
|
2019-02-13 18:49:39 -05:00
|
|
|
xonsh.main.premain(["--timings"])
|
|
|
|
|
2016-12-17 18:10:29 +08:00
|
|
|
|
2019-07-13 12:49:00 -04:00
|
|
|
@skip_if_on_windows
|
2020-06-25 19:49:04 -04:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("env_shell", "rc_shells", "exp_shell"),
|
|
|
|
[
|
|
|
|
("", [], ""),
|
|
|
|
("/argle/bash", [], "/argle/bash"),
|
|
|
|
("/bin/xonsh", [], ""),
|
|
|
|
(
|
|
|
|
"/argle/bash",
|
|
|
|
["/argle/xonsh", "/argle/dash", "/argle/sh", "/argle/bargle"],
|
|
|
|
"/argle/bash",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"",
|
|
|
|
["/argle/xonsh", "/argle/dash", "/argle/sh", "/argle/bargle"],
|
|
|
|
"/argle/dash",
|
|
|
|
),
|
|
|
|
("", ["/argle/xonsh", "/argle/screen", "/argle/sh"], "/argle/sh"),
|
|
|
|
("", ["/argle/xonsh", "/argle/screen"], ""),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@skip_if_on_windows
|
|
|
|
def test_xonsh_failback(
|
|
|
|
env_shell,
|
|
|
|
rc_shells,
|
|
|
|
exp_shell,
|
|
|
|
shell,
|
2022-01-08 04:03:22 +05:30
|
|
|
xession,
|
2020-06-25 19:49:04 -04:00
|
|
|
monkeypatch,
|
|
|
|
monkeypatch_stderr,
|
|
|
|
):
|
2016-12-17 18:10:29 +08:00
|
|
|
failback_checker = []
|
|
|
|
|
|
|
|
def mocked_main(*args):
|
2018-08-30 09:18:49 -05:00
|
|
|
raise Exception("A fake failure")
|
|
|
|
|
|
|
|
monkeypatch.setattr(xonsh.main, "main_xonsh", mocked_main)
|
2016-12-17 18:10:29 +08:00
|
|
|
|
|
|
|
def mocked_execlp(f, *args):
|
|
|
|
failback_checker.append(f)
|
|
|
|
failback_checker.append(args[0])
|
2018-08-30 09:18:49 -05:00
|
|
|
|
|
|
|
monkeypatch.setattr(os, "execlp", mocked_execlp)
|
|
|
|
monkeypatch.setattr(os.path, "exists", lambda x: True)
|
2020-06-25 19:49:04 -04:00
|
|
|
monkeypatch.setattr(sys, "argv", ["/bin/xonsh", "-i"]) # has to look like real path
|
2016-12-17 18:10:29 +08:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def mocked_open(*args):
|
2020-06-25 19:49:04 -04:00
|
|
|
yield rc_shells
|
2018-08-30 09:18:49 -05:00
|
|
|
|
|
|
|
monkeypatch.setattr(builtins, "open", mocked_open)
|
2016-12-17 18:10:29 +08:00
|
|
|
|
2020-06-25 19:49:04 -04:00
|
|
|
monkeypatch.setenv("SHELL", env_shell)
|
|
|
|
|
|
|
|
try:
|
|
|
|
xonsh.main.main() # if main doesn't raise, it did try to invoke a shell
|
|
|
|
assert failback_checker[0] == exp_shell
|
|
|
|
assert failback_checker[1] == failback_checker[0]
|
|
|
|
except Exception as e:
|
|
|
|
if len(e.args) and "A fake failure" in str(
|
|
|
|
e.args[0]
|
|
|
|
): # if it did raise expected exception
|
|
|
|
assert len(failback_checker) == 0 # then it didn't invoke a shell
|
|
|
|
else:
|
|
|
|
raise e # it raised something other than the test exception,
|
2016-12-18 02:32:08 +08:00
|
|
|
|
|
|
|
|
2018-11-09 10:08:03 +01:00
|
|
|
def test_xonsh_failback_single(shell, monkeypatch, monkeypatch_stderr):
|
2016-12-20 12:29:46 +08:00
|
|
|
class FakeFailureError(Exception):
|
|
|
|
pass
|
2016-12-18 02:32:08 +08:00
|
|
|
|
|
|
|
def mocked_main(*args):
|
2016-12-20 12:29:46 +08:00
|
|
|
raise FakeFailureError()
|
2018-08-30 09:18:49 -05:00
|
|
|
|
|
|
|
monkeypatch.setattr(xonsh.main, "main_xonsh", mocked_main)
|
|
|
|
monkeypatch.setattr(sys, "argv", ["xonsh", "-c", "echo", "foo"])
|
2016-12-18 02:32:08 +08:00
|
|
|
|
2016-12-20 12:29:46 +08:00
|
|
|
with pytest.raises(FakeFailureError):
|
|
|
|
xonsh.main.main()
|
2017-01-11 21:59:42 +08:00
|
|
|
|
|
|
|
|
2018-11-09 10:08:03 +01:00
|
|
|
def test_xonsh_failback_script_from_file(shell, monkeypatch, monkeypatch_stderr):
|
2017-01-11 21:59:42 +08:00
|
|
|
checker = []
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2017-01-11 21:59:42 +08:00
|
|
|
def mocked_execlp(f, *args):
|
|
|
|
checker.append(f)
|
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
monkeypatch.setattr(os, "execlp", mocked_execlp)
|
|
|
|
|
|
|
|
script = os.path.join(TEST_DIR, "scripts", "raise.xsh")
|
|
|
|
monkeypatch.setattr(sys, "argv", ["xonsh", script])
|
2022-02-04 04:46:44 +01:00
|
|
|
|
|
|
|
# changed in #4662: User-Code exceptions are now caught in main and handled there
|
|
|
|
# => we expect that no exception is thrown
|
|
|
|
|
2017-01-11 21:59:42 +08:00
|
|
|
assert len(checker) == 0
|
2019-09-09 14:39:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_xonsh_no_file_returncode(shell, monkeypatch, monkeypatch_stderr):
|
|
|
|
monkeypatch.setattr(sys, "argv", ["xonsh", "foobazbarzzznotafileatall.xsh"])
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
xonsh.main.main()
|