2015-11-21 12:30:43 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Tests the xonsh main function."""
|
|
|
|
from __future__ import unicode_literals, print_function
|
2016-12-17 18:10:29 +08:00
|
|
|
from contextlib import contextmanager
|
2015-11-21 12:30:43 -05:00
|
|
|
|
|
|
|
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
|
2015-11-21 12:30:43 -05:00
|
|
|
|
2016-07-01 13:35:16 +03:00
|
|
|
import xonsh.main
|
2017-09-27 19:15:28 -04:00
|
|
|
from xonsh.main import XonshMode
|
2017-04-21 14:29:55 -04:00
|
|
|
from xonsh.environ import Env
|
2016-07-01 13:35:16 +03:00
|
|
|
import pytest
|
2019-07-13 12:49:00 -04:00
|
|
|
from tools import TEST_DIR, skip_if_on_windows
|
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
|
2018-09-13 15:19:56 -04:00
|
|
|
def shell(xonsh_builtins, monkeypatch):
|
2016-07-03 12:00:24 +03:00
|
|
|
"""Xonsh Shell Mock"""
|
2019-04-19 22:52:10 +02:00
|
|
|
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-06-22 11:35:24 +03: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")
|
2015-11-21 12:30:43 -05:00
|
|
|
|
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")
|
2015-11-21 12:30:43 -05:00
|
|
|
|
2016-06-22 11:35:24 +03:00
|
|
|
|
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"
|
2016-06-18 16:29:30 +03:00
|
|
|
|
2016-06-18 20:14:05 +03:00
|
|
|
|
2017-09-27 19:15:28 -04:00
|
|
|
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])
|
2017-09-27 19:15:28 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
def test_force_interactive_rc_with_script(shell, tmpdir):
|
2018-08-30 09:18:49 -05:00
|
|
|
args = xonsh.main.premain(["-i", "tests/sample.xsh"])
|
2018-09-13 14:03:35 -04:00
|
|
|
assert builtins.__xonsh__.env.get("XONSH_INTERACTIVE")
|
2017-09-27 19:15:28 -04:00
|
|
|
|
|
|
|
|
2018-11-13 21:59:48 +01:00
|
|
|
def test_force_interactive_custom_rc_with_script(shell, tmpdir, monkeypatch):
|
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
|
2018-09-13 14:03:35 -04:00
|
|
|
assert f.strpath in builtins.__xonsh__.env.get("XONSHRC")
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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"]
|
|
|
|
)
|
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-06-18 20:14:05 +03:00
|
|
|
|
2016-06-20 19:27:53 +03:00
|
|
|
|
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")
|
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
|
2018-11-09 10:08:03 +01:00
|
|
|
def test_xonsh_failback(shell, 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)
|
|
|
|
monkeypatch.setattr(sys, "argv", ["xonsh", "-i"])
|
2016-12-17 18:10:29 +08:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def mocked_open(*args):
|
2018-08-30 09:18:49 -05:00
|
|
|
yield ["/usr/bin/xonsh", "/usr/bin/screen", "bash", "/bin/xshell"]
|
|
|
|
|
|
|
|
monkeypatch.setattr(builtins, "open", mocked_open)
|
2016-12-17 18:10:29 +08:00
|
|
|
|
|
|
|
xonsh.main.main()
|
2018-08-30 09:18:49 -05:00
|
|
|
assert failback_checker == ["/bin/xshell", "/bin/xshell"]
|
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])
|
2017-01-11 21:59:42 +08:00
|
|
|
with pytest.raises(Exception):
|
|
|
|
xonsh.main.main()
|
|
|
|
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()
|