xonsh/tests/test_main.py

234 lines
6.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""Tests the xonsh main function."""
from __future__ import unicode_literals, print_function
from contextlib import contextmanager
import builtins
import gc
import os
import os.path
2016-07-01 13:35:16 +03:00
import sys
2016-07-01 13:35:16 +03:00
import xonsh.main
from xonsh.main import XonshMode
2016-07-01 13:35:16 +03:00
import pytest
from tools import TEST_DIR, skip_if_on_windows
def Shell(*args, **kwargs):
pass
2016-07-01 13:35:16 +03:00
@pytest.fixture
2018-09-13 15:19:56 -04:00
def shell(xonsh_builtins, monkeypatch):
2016-07-03 12:00:24 +03:00
"""Xonsh Shell Mock"""
if hasattr(builtins, "__xonsh__"):
builtins.__xonsh__.unlink_builtins()
del builtins.__xonsh__
for xarg in dir(builtins):
if "__xonsh_" in xarg:
delattr(builtins, xarg)
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
def test_premain_no_arg(shell, monkeypatch):
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([])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("XONSH_LOGIN")
2016-07-01 13:35:16 +03:00
2016-03-20 19:43:37 -04:00
2016-07-01 13:35:16 +03:00
def test_premain_interactive(shell):
2018-08-30 09:18:49 -05:00
xonsh.main.premain(["-i"])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("XONSH_INTERACTIVE")
2016-06-22 18:23:36 -04:00
2016-07-01 13:35:16 +03:00
def test_premain_login_command(shell):
2018-08-30 09:18:49 -05:00
xonsh.main.premain(["-l", "-c", 'echo "hi"'])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("XONSH_LOGIN")
2016-03-20 21:31:14 -04:00
2016-07-01 13:35:16 +03:00
def test_premain_login(shell):
2018-08-30 09:18:49 -05:00
xonsh.main.premain(["-l"])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("XONSH_LOGIN")
2016-07-01 13:35:16 +03:00
def test_premain_D(shell):
2018-08-30 09:18:49 -05:00
xonsh.main.premain(["-DTEST1=1616", "-DTEST2=LOL"])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("TEST1") == "1616"
assert builtins.__xonsh__.env.get("TEST2") == "LOL"
def test_premain_custom_rc(shell, tmpdir, monkeypatch):
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])
assert args.mode == XonshMode.interactive
2018-09-13 14:03:35 -04:00
assert f.strpath in builtins.__xonsh__.env.get("XONSHRC")
2017-04-21 14:29:55 -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"])
assert not (args.mode == XonshMode.interactive)
def test_force_interactive_rc_with_script(shell, tmpdir):
xonsh.main.premain(["-i", "tests/sample.xsh"])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("XONSH_INTERACTIVE")
def test_force_interactive_custom_rc_with_script(shell, tmpdir, monkeypatch):
"""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")
f.write("print('hi')")
2018-08-30 09:18:49 -05:00
args = xonsh.main.premain(["-i", "--rc", f.strpath, "tests/sample.xsh"])
assert args.mode == XonshMode.interactive
2018-09-13 14:03:35 -04:00
assert f.strpath in builtins.__xonsh__.env.get("XONSHRC")
def test_custom_rc_with_script(shell, tmpdir):
"""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")
f.write("print('hi')")
2018-08-30 09:18:49 -05:00
args = xonsh.main.premain(["--rc", f.strpath, "tests/sample.xsh"])
assert not (args.mode == XonshMode.interactive)
2017-04-21 14:29:55 -04:00
def test_premain_no_rc(shell, tmpdir):
2018-09-13 16:49:09 -04:00
xonsh.main.premain(["--no-rc", "-i"])
2018-09-13 14:03:35 -04:00
assert not builtins.__xonsh__.env.get("XONSHRC")
2017-04-21 14:29:55 -04:00
@pytest.mark.parametrize(
2018-08-30 09:18:49 -05:00
"arg", ["", "-i", "-vERSION", "-hAALP", "TTTT", "-TT", "--TTT"]
)
2016-07-01 13:35:16 +03:00
def test_premain_with_file_argument(arg, shell):
2018-08-30 09:18:49 -05:00
xonsh.main.premain(["tests/sample.xsh", arg])
2018-09-13 14:03:35 -04:00
assert not (builtins.__xonsh__.env.get("XONSH_INTERACTIVE"))
2016-07-01 13:35:16 +03:00
def test_premain_interactive__with_file_argument(shell):
2018-08-30 09:18:49 -05:00
xonsh.main.premain(["-i", "tests/sample.xsh"])
2018-09-13 14:03:35 -04:00
assert builtins.__xonsh__.env.get("XONSH_INTERACTIVE")
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]
2019-02-13 18:49:39 -05:00
def test_premain_timings_arg(shell):
2019-02-13 18:49:39 -05:00
xonsh.main.premain(["--timings"])
@skip_if_on_windows
@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,
xonsh_builtins,
monkeypatch,
monkeypatch_stderr,
):
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)
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)
monkeypatch.setattr(sys, "argv", ["/bin/xonsh", "-i"]) # has to look like real path
@contextmanager
def mocked_open(*args):
yield rc_shells
2018-08-30 09:18:49 -05:00
monkeypatch.setattr(builtins, "open", mocked_open)
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
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
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])
2017-01-11 21:59:42 +08:00
with pytest.raises(Exception):
xonsh.main.main()
assert len(checker) == 0
def test_xonsh_no_file_returncode(shell, monkeypatch, monkeypatch_stderr):
monkeypatch.setattr(sys, "argv", ["xonsh", "foobazbarzzznotafileatall.xsh"])
with pytest.raises(SystemExit):
xonsh.main.main()