2015-11-16 14:04:32 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-02-11 02:01:35 -06:00
|
|
|
"""Tests the xonsh lexer."""
|
|
|
|
from __future__ import unicode_literals, print_function
|
2016-09-09 00:08:13 -04:00
|
|
|
import os
|
2015-09-24 19:27:19 -04:00
|
|
|
import sys
|
2016-08-19 01:45:44 -04:00
|
|
|
import ast
|
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
|
2016-09-08 21:40:18 -04:00
|
|
|
from collections.abc import MutableMapping
|
2015-02-11 02:01:35 -06:00
|
|
|
|
2016-06-23 14:46:15 +02:00
|
|
|
import pytest
|
|
|
|
|
2016-06-22 11:33:03 -04:00
|
|
|
from xonsh.environ import Env
|
2016-02-18 00:45:01 -05:00
|
|
|
from xonsh.base_shell import BaseShell
|
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')
|
2016-07-26 10:09:09 -04:00
|
|
|
ON_CONDA = True in [conda in pytest.__file__ for conda
|
|
|
|
in ['anaconda', 'miniconda']]
|
2015-09-24 19:27:19 -04:00
|
|
|
|
2016-06-29 19:28:39 +03:00
|
|
|
# pytest skip decorators
|
|
|
|
skip_if_py34 = pytest.mark.skipif(VER_MAJOR_MINOR < VER_3_5, reason="Py3.5+ only test")
|
2016-06-23 14:46:15 +02:00
|
|
|
|
2016-07-23 17:27:27 -04:00
|
|
|
skip_if_on_conda = pytest.mark.skipif(ON_CONDA,
|
|
|
|
reason="Conda and virtualenv _really_ hate each other")
|
|
|
|
|
2016-06-24 23:51:55 +03:00
|
|
|
skip_if_on_windows = pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
|
|
|
|
|
2016-06-27 18:39:10 +03:00
|
|
|
skip_if_on_unix = pytest.mark.skipif(not ON_WINDOWS, reason='Windows stuff')
|
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
skip_if_on_darwin = pytest.mark.skipif(ON_DARWIN, reason='not Mac friendly')
|
|
|
|
|
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-06-26 22:09:16 +02:00
|
|
|
|
2016-02-20 14:53:00 -05:00
|
|
|
class DummyStyler():
|
|
|
|
styles = defaultdict(None.__class__)
|
|
|
|
|
2016-06-26 22:09:16 +02:00
|
|
|
|
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-09-08 21:40:18 -04:00
|
|
|
class DummyCommandsCache():
|
|
|
|
|
2016-09-09 00:08:13 -04:00
|
|
|
def locate_binary(self, name):
|
|
|
|
return os.path.join(os.path.dirname(__file__), 'bin', name)
|
2016-09-08 21:40:18 -04:00
|
|
|
|
|
|
|
class DummyEnv(MutableMapping):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._d = dict(*args, **kwargs)
|
|
|
|
|
|
|
|
def detype(self):
|
|
|
|
return self._d
|
|
|
|
|
|
|
|
def __getitem__(self, k):
|
|
|
|
return self._d[k]
|
|
|
|
|
|
|
|
def __setitem__(self, k, v):
|
|
|
|
self._d[k] = v
|
|
|
|
|
|
|
|
def __delitem__(self, k):
|
|
|
|
del self._d[k]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._d)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
yield from self._d
|
|
|
|
|
2016-06-05 16:25:43 -04:00
|
|
|
#
|
|
|
|
# Execer tools
|
|
|
|
#
|
|
|
|
|
2016-06-29 19:28:39 +03:00
|
|
|
def check_exec(input, **kwargs):
|
2016-06-25 13:38:43 +03:00
|
|
|
if not input.endswith('\n'):
|
|
|
|
input += '\n'
|
2016-07-01 15:43:16 +03:00
|
|
|
builtins.__xonsh_execer__.exec(input, **kwargs)
|
|
|
|
return True
|
2016-06-05 16:25:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def check_eval(input):
|
2016-07-01 15:43:16 +03:00
|
|
|
builtins.__xonsh_env__ = Env({'AUTO_CD': False, 'XONSH_ENCODING': 'utf-8',
|
|
|
|
'XONSH_ENCODING_ERRORS': 'strict', 'PATH': []})
|
2016-06-21 11:43:08 -04:00
|
|
|
if ON_WINDOWS:
|
2016-07-01 15:43:16 +03:00
|
|
|
builtins.__xonsh_env__['PATHEXT'] = ['.COM', '.EXE', '.BAT', '.CMD']
|
|
|
|
builtins.__xonsh_execer__.eval(input)
|
|
|
|
return True
|
2016-06-05 16:25:43 -04:00
|
|
|
|
2016-06-26 22:09:16 +02:00
|
|
|
|
2016-06-05 16:25:43 -04:00
|
|
|
def check_parse(input):
|
2016-07-01 15:43:16 +03:00
|
|
|
tree = builtins.__xonsh_execer__.parse(input, ctx=None)
|
2016-06-10 02:50:56 -04:00
|
|
|
return tree
|
2016-08-19 01:45:44 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Parser tools
|
|
|
|
#
|
|
|
|
|
2016-08-19 12:11:37 -04:00
|
|
|
|
2016-08-19 01:45:44 -04:00
|
|
|
def nodes_equal(x, y):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
assert type(x) == type(y)
|
|
|
|
if isinstance(x, (ast.Expr, ast.FunctionDef, ast.ClassDef)):
|
|
|
|
assert x.lineno == y.lineno
|
|
|
|
assert x.col_offset == y.col_offset
|
|
|
|
for (xname, xval), (yname, yval) in zip(ast.iter_fields(x),
|
|
|
|
ast.iter_fields(y)):
|
|
|
|
assert xname == yname
|
|
|
|
assert type(xval) == type(yval)
|
|
|
|
for xchild, ychild in zip(ast.iter_child_nodes(x),
|
|
|
|
ast.iter_child_nodes(y)):
|
|
|
|
assert nodes_equal(xchild, ychild)
|
|
|
|
return True
|