xonsh/tests/test_main.py

91 lines
2.6 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 os.path
2016-07-01 13:35:16 +03:00
import sys
2016-07-01 13:35:16 +03:00
import xonsh.main
import pytest
def Shell(*args, **kwargs):
pass
2016-07-01 13:35:16 +03:00
@pytest.fixture
def shell(xonsh_builtins, monkeypatch):
2016-07-03 12:00:24 +03:00
"""Xonsh Shell Mock"""
2016-07-01 13:35:16 +03:00
monkeypatch.setattr(xonsh.main, 'Shell', Shell)
def test_premain_no_arg(shell, monkeypatch):
monkeypatch.setattr(sys.stdin, 'isatty', lambda: True)
xonsh.main.premain([])
assert builtins.__xonsh_env__.get('XONSH_LOGIN')
2016-03-20 19:43:37 -04:00
2016-07-01 13:35:16 +03:00
def test_premain_interactive(shell):
xonsh.main.premain(['-i'])
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):
xonsh.main.premain(['-l', '-c', 'echo "hi"'])
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):
xonsh.main.premain(['-l'])
assert (builtins.__xonsh_env__.get('XONSH_LOGIN'))
2016-07-01 13:35:16 +03:00
def test_premain_D(shell):
xonsh.main.premain(['-DTEST1=1616', '-DTEST2=LOL'])
assert (builtins.__xonsh_env__.get('TEST1') == '1616')
assert (builtins.__xonsh_env__.get('TEST2') == 'LOL')
@pytest.mark.parametrize(
'arg', ['', '-i', '-vERSION', '-hAALP', 'TTTT', '-TT', '--TTT'])
2016-07-01 13:35:16 +03:00
def test_premain_with_file_argument(arg, shell):
xonsh.main.premain(['tests/sample.xsh', arg])
assert not (builtins.__xonsh_env__.get('XONSH_INTERACTIVE'))
2016-07-01 13:35:16 +03:00
def test_premain_interactive__with_file_argument(shell):
xonsh.main.premain(['-i', 'tests/sample.xsh'])
assert (builtins.__xonsh_env__.get('XONSH_INTERACTIVE'))
2016-07-01 13:35:16 +03:00
@pytest.mark.parametrize('case', ['----', '--hep', '-TT', '--TTTT'])
def test_premain_invalid_arguments(case, shell, capsys):
with pytest.raises(SystemExit):
xonsh.main.premain([case])
assert 'unrecognized argument' in capsys.readouterr()[1]
def test_main_failback(shell, monkeypatch):
failback_checker = []
monkeypatch.setattr(sys, 'stderr', open(os.devnull, 'w'))
def mocked_main(*args):
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])
monkeypatch.setattr(os, 'execlp', mocked_execlp)
monkeypatch.setattr(os.path, 'exists', lambda x: True)
@contextmanager
def mocked_open(*args):
2016-12-17 18:22:34 +08:00
yield ['/usr/local/bin/xonsh', '/bin/xshell']
monkeypatch.setattr(builtins, 'open', mocked_open)
xonsh.main.main()
assert failback_checker == ['/bin/xshell', '/bin/xshell']