xonsh/tests/tools.py

100 lines
2.2 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")
2016-06-24 23:51:55 +03:00
skip_if_on_windows = pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2016-06-23 14:46:15 +02:00
skip_if_on_unix = pytest.mark.skipif(not ON_WINDOWS, reason='Windows stuff')
2016-06-23 14:46:15 +02:00
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-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)
2016-06-25 13:38:43 +03:00
def check_exec(input, xonsh_env, **kwargs):
# with mock_xonsh_env(None):
if not input.endswith('\n'):
input += '\n'
EXECER.debug_level = DEBUG_LEVEL
EXECER.exec(input, **kwargs)
2016-06-05 16:25:43 -04:00
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