xonsh/tests/tools.py

141 lines
3.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-02-11 02:01:35 -06:00
"""Tests the xonsh lexer."""
from __future__ import unicode_literals, print_function
2015-09-24 19:27:19 -04:00
import sys
import glob
2015-02-11 02:01:35 -06:00
import builtins
2015-10-20 17:53:12 -04:00
import platform
2015-02-11 02:01:35 -06:00
import subprocess
2016-02-20 14:53:00 -05:00
from collections import defaultdict
2015-02-11 02:01:35 -06:00
from contextlib import contextmanager
2016-06-23 14:46:15 +02:00
import pytest
2015-03-28 17:55:48 -05:00
from xonsh.built_ins import ensure_list_of_strs
2016-06-22 11:33:03 -04:00
from xonsh.environ import Env
builtins.__xonsh_env__ = Env()
2016-02-18 00:45:01 -05:00
from xonsh.base_shell import BaseShell
2016-06-05 16:25:43 -04:00
from xonsh.execer import Execer
from xonsh.tools import XonshBlockError
2015-03-28 17:55:48 -05:00
2015-09-24 19:27:19 -04:00
VER_3_4 = (3, 4)
VER_3_5 = (3, 5)
VER_MAJOR_MINOR = sys.version_info[:2]
2016-02-03 01:29:06 -05:00
VER_FULL = sys.version_info[:3]
2016-06-23 12:28:31 +03:00
ON_DARWIN = (platform.system() == 'Darwin')
ON_WINDOWS = (platform.system() == 'Windows')
2015-09-24 19:27:19 -04:00
2016-06-23 14:46:15 +02:00
skip_if_py34 = pytest.mark.skipif(VER_MAJOR_MINOR < VER_3_5,
reason="Py3.5+ only test")
skip_if_py35plus = pytest.mark.skipif(VER_MAJOR_MINOR < VER_3_5,
reason="Py3.5+ only test")
2015-02-24 21:58:37 -06:00
def sp(cmd):
return subprocess.check_output(cmd, universal_newlines=True)
2016-02-20 14:53:00 -05:00
class DummyStyler():
styles = defaultdict(None.__class__)
2016-02-18 00:45:01 -05:00
class DummyBaseShell(BaseShell):
def __init__(self):
2016-02-20 14:53:00 -05:00
self.styler = DummyStyler()
2016-02-18 00:45:01 -05:00
2015-12-05 15:32:38 -05:00
class DummyShell:
2016-03-02 22:01:44 -05:00
def settitle(self):
2015-12-05 15:32:38 -05:00
pass
2016-02-18 00:45:01 -05:00
_shell = None
@property
def shell(self):
if self._shell is None:
self._shell = DummyBaseShell()
return self._shell
2016-06-24 18:16:16 +03:00
@pytest.fixture
def xenv():
return {}
@pytest.yield_fixture
def xonsh_env(xenv):
2015-02-11 02:01:35 -06:00
builtins.__xonsh_env__ = xenv
2015-12-19 21:17:03 -05:00
builtins.__xonsh_ctx__ = {}
2015-12-05 15:32:38 -05:00
builtins.__xonsh_shell__ = DummyShell()
2015-02-11 02:01:35 -06:00
builtins.__xonsh_help__ = lambda x: x
2015-02-25 00:46:11 -06:00
builtins.__xonsh_glob__ = glob.glob
2015-03-01 21:23:01 -06:00
builtins.__xonsh_exit__ = False
2015-02-11 02:01:35 -06:00
builtins.__xonsh_superhelp__ = lambda x: x
builtins.__xonsh_regexpath__ = lambda x: []
2016-04-10 12:13:36 -04:00
builtins.__xonsh_expand_path__ = lambda x: x
2015-02-24 21:58:37 -06:00
builtins.__xonsh_subproc_captured__ = sp
builtins.__xonsh_subproc_uncaptured__ = sp
2015-03-28 17:55:48 -05:00
builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs
2016-06-05 16:25:43 -04:00
builtins.XonshBlockError = XonshBlockError
2015-12-19 21:17:03 -05:00
builtins.evalx = eval
2015-02-28 16:10:34 -06:00
builtins.execx = None
builtins.compilex = None
builtins.aliases = {}
2015-02-11 02:01:35 -06:00
yield
del builtins.__xonsh_env__
2015-12-19 21:17:03 -05:00
del builtins.__xonsh_ctx__
2015-12-05 15:32:38 -05:00
del builtins.__xonsh_shell__
2015-02-11 02:01:35 -06:00
del builtins.__xonsh_help__
2015-02-25 00:46:11 -06:00
del builtins.__xonsh_glob__
2015-03-01 21:23:01 -06:00
del builtins.__xonsh_exit__
2015-02-11 02:01:35 -06:00
del builtins.__xonsh_superhelp__
del builtins.__xonsh_regexpath__
2016-04-10 12:13:36 -04:00
del builtins.__xonsh_expand_path__
2015-07-29 23:58:25 +02:00
del builtins.__xonsh_subproc_captured__
del builtins.__xonsh_subproc_uncaptured__
2015-03-28 17:55:48 -05:00
del builtins.__xonsh_ensure_list_of_strs__
2016-06-05 16:25:43 -04:00
del builtins.XonshBlockError
2015-02-28 16:10:34 -06:00
del builtins.evalx
del builtins.execx
del builtins.compilex
del builtins.aliases
2015-02-11 02:01:35 -06:00
2015-09-24 19:27:19 -04:00
2016-06-05 16:25:43 -04:00
#
# Execer tools
#
DEBUG_LEVEL = 0
EXECER = None
def execer_setup():
# only setup one parser
global EXECER
if EXECER is None:
EXECER = Execer(debug_level=DEBUG_LEVEL, login=False)
def check_exec(input, **kwargs):
with mock_xonsh_env(None):
if not input.endswith('\n'):
input += '\n'
EXECER.debug_level = DEBUG_LEVEL
EXECER.exec(input, **kwargs)
def check_eval(input):
2016-06-21 11:43:08 -04:00
env = {'AUTO_CD': False, 'XONSH_ENCODING' :'utf-8',
'XONSH_ENCODING_ERRORS': 'strict', 'PATH': []}
if ON_WINDOWS:
env['PATHEXT'] = ['.COM', '.EXE', '.BAT', '.CMD']
with mock_xonsh_env(env):
2016-06-05 16:25:43 -04:00
EXECER.debug_level = DEBUG_LEVEL
EXECER.eval(input)
def check_parse(input):
with mock_xonsh_env(None):
EXECER.debug_level = DEBUG_LEVEL
tree = EXECER.parse(input, ctx=None)
return tree
2016-06-05 16:25:43 -04:00