xonsh/tests/tools.py

99 lines
2.4 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
2015-09-24 19:27:19 -04:00
from nose.plugins.skip import SkipTest
2015-03-28 17:55:48 -05:00
from xonsh.built_ins import ensure_list_of_strs
2016-05-29 13:09:03 -04:00
builtins.__xonsh_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]
2015-10-20 17:53:12 -04:00
ON_MAC = (platform.system() == 'Darwin')
2015-09-24 19:27:19 -04: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
2015-02-11 02:01:35 -06:00
@contextmanager
def mock_xonsh_env(xenv):
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
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__
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
def skipper():
"""Raises SkipTest"""
raise SkipTest
def skip_if(cond):
"""Skips a test under a given condition."""
def dec(f):
if cond:
return skipper
else:
return f
return dec