xonsh/tests/test_integrations.py

90 lines
2.1 KiB
Python
Raw Normal View History

2016-10-13 02:03:30 -04:00
import os
2016-10-13 02:41:53 -04:00
import sys
2016-10-13 02:45:37 -04:00
import shutil
2016-10-13 02:03:30 -04:00
import subprocess
import pytest
2016-10-13 02:41:53 -04:00
import xonsh
from xonsh.platform import ON_WINDOWS
XONSH_PREFIX = xonsh.__file__
if 'site-packages' in XONSH_PREFIX:
# must be installed version of xonsh
num_up = 5
else:
# must be in source dir
num_up = 2
for i in range(num_up):
XONSH_PREFIX = os.path.dirname(XONSH_PREFIX)
PATH = os.path.join(os.path.dirname(__file__), 'bin') + os.pathsep + \
os.path.join(XONSH_PREFIX, 'bin') + os.pathsep + \
os.path.join(XONSH_PREFIX, 'Scripts') + os.pathsep + \
os.path.join(XONSH_PREFIX, 'scripts') + os.pathsep + \
os.path.dirname(sys.executable) + os.pathsep + \
os.environ['PATH']
2016-10-13 02:03:30 -04:00
#
# The following list contains a (stdin, stdout, returncode) tuples
#
ALL_PLATFORMS = [
# test calling a function alias
("""
def _f():
print('hello')
aliases['f'] = _f
f
""", "hello\n", 0),
# test redirecting a function alias
("""
def _f():
print('Wow Mom!')
aliases['f'] = _f
f > tttt
with open('tttt') as tttt:
s = tttt.read().strip()
print('REDIRECTED OUTPUT: ' + s)
""", "REDIRECTED OUTPUT: Wow Mom!\n", 0),
2016-10-13 03:20:42 -04:00
# test system exit in function alias
("""
import sys
def _f():
sys.exit(42)
aliases['f'] = _f
print(![f].returncode)
""", "42\n", 0),
2016-10-13 02:03:30 -04:00
]
@pytest.mark.parametrize('case', ALL_PLATFORMS)
def test_script(case):
script, exp_out, exp_rtn = case
env = dict(os.environ)
2016-10-13 02:41:53 -04:00
env['PATH'] = PATH
2016-10-13 02:03:30 -04:00
env['XONSH_DEBUG'] = '1'
env['XONSH_SHOW_TRACEBACK'] = '1'
2016-10-13 03:20:42 -04:00
env['RAISE_SUBPROC_ERROR'] = '1'
2016-10-13 02:41:53 -04:00
xonsh = 'xonsh.bat' if ON_WINDOWS else 'xon.sh'
2016-10-13 02:45:37 -04:00
xonsh = shutil.which(xonsh, path=PATH)
2016-10-13 02:10:34 -04:00
p = subprocess.Popen([xonsh, '--no-rc'],
2016-10-13 02:03:30 -04:00
env=env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
try:
2016-10-13 02:48:00 -04:00
out, err = p.communicate(input=script, timeout=10)
2016-10-13 02:03:30 -04:00
except subprocess.TimeoutExpired:
p.kill()
raise
assert exp_out == out
2016-10-13 02:41:53 -04:00
assert exp_rtn == p.returncode
2016-10-13 02:03:30 -04:00