mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-05 17:00:58 +01:00
Merge pull request #1386 from xonsh/test_xsh
Collecting `test*.xsh` files when testing
This commit is contained in:
commit
0b27499490
6 changed files with 106 additions and 4 deletions
13
news/test_xsh.rst
Normal file
13
news/test_xsh.rst
Normal file
|
@ -0,0 +1,13 @@
|
|||
**Added:**
|
||||
|
||||
* Added a py.test plugin to collect `test_*.xsh` files and run `test_*()` functions.
|
||||
|
||||
**Changed:** None
|
||||
|
||||
**Deprecated:** None
|
||||
|
||||
**Removed:** None
|
||||
|
||||
**Fixed:** None
|
||||
|
||||
**Security:** None
|
3
setup.py
3
setup.py
|
@ -310,7 +310,8 @@ def main():
|
|||
skw['entry_points'] = {
|
||||
'pygments.lexers': ['xonsh = xonsh.pyghooks:XonshLexer',
|
||||
'xonshcon = xonsh.pyghooks:XonshConsoleLexer'],
|
||||
}
|
||||
'pytest11': ['xonsh = xonsh.pytest_plugin']
|
||||
}
|
||||
skw['cmdclass']['develop'] = xdevelop
|
||||
setup(**skw)
|
||||
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
import glob
|
||||
import builtins
|
||||
|
||||
import pytest
|
||||
from tools import DummyShell, sp
|
||||
|
||||
import xonsh.built_ins
|
||||
|
||||
from xonsh.built_ins import ensure_list_of_strs
|
||||
from xonsh.execer import Execer
|
||||
from xonsh.tools import XonshBlockError
|
||||
from xonsh.events import events
|
||||
import glob
|
||||
|
||||
from tools import DummyShell, sp
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
18
tests/test_xonsh.xsh
Normal file
18
tests/test_xonsh.xsh
Normal file
|
@ -0,0 +1,18 @@
|
|||
|
||||
|
||||
def test_simple():
|
||||
assert 1 + 1 == 2
|
||||
|
||||
|
||||
def test_envionment():
|
||||
$USER = 'snail'
|
||||
x = 'USER'
|
||||
assert x in ${...}
|
||||
assert ${'U' + 'SER'} == 'snail'
|
||||
|
||||
|
||||
def test_xonsh_party():
|
||||
x = 'xonsh'
|
||||
y = 'party'
|
||||
out = $(echo @(x + ' ' + y))
|
||||
assert out == 'xonsh party\n'
|
|
@ -1,7 +1,7 @@
|
|||
__version__ = '0.4.5'
|
||||
|
||||
# amalgamate exclude jupyter_kernel parser_table parser_test_table pyghooks
|
||||
# amalgamate exclude winutils wizard
|
||||
# amalgamate exclude winutils wizard pytest_plugin
|
||||
import os as _os
|
||||
if _os.getenv('XONSH_DEBUG', ''):
|
||||
pass
|
||||
|
|
66
xonsh/pytest_plugin.py
Normal file
66
xonsh/pytest_plugin.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""Pytest plugin for testing xsh files."""
|
||||
import sys
|
||||
import importlib
|
||||
from traceback import format_list, extract_tb
|
||||
|
||||
import pytest
|
||||
from xonsh.imphooks import install_hook
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
install_hook()
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(items):
|
||||
items.sort(key=lambda x: 0 if isinstance(x, XshFunction) else 1)
|
||||
|
||||
|
||||
def _limited_traceback(excinfo):
|
||||
""" Return a formatted traceback with all the stack
|
||||
from this frame (i.e __file__) up removed
|
||||
"""
|
||||
tb = extract_tb(excinfo.tb)
|
||||
try:
|
||||
idx = [__file__ in e for e in tb].index(True)
|
||||
return format_list(tb[idx+1:])
|
||||
except ValueError:
|
||||
return format_list(tb)
|
||||
|
||||
|
||||
def pytest_collect_file(parent, path):
|
||||
if path.ext.lower() == ".xsh" and path.basename.startswith("test_"):
|
||||
return XshFile(path, parent)
|
||||
|
||||
|
||||
class XshFile(pytest.File):
|
||||
def collect(self):
|
||||
sys.path.append(self.fspath.dirname)
|
||||
mod = importlib.import_module(self.fspath.purebasename)
|
||||
sys.path.pop(0)
|
||||
tests = [t for t in dir(mod) if t.startswith('test_')]
|
||||
for test_name in tests:
|
||||
obj = getattr(mod, test_name)
|
||||
if hasattr(obj, '__call__'):
|
||||
yield XshFunction(name=test_name, parent=self,
|
||||
test_func=obj, test_module=mod)
|
||||
|
||||
|
||||
class XshFunction(pytest.Item):
|
||||
def __init__(self, name, parent, test_func, test_module):
|
||||
super().__init__(name, parent)
|
||||
self._test_func = test_func
|
||||
self._test_module = test_module
|
||||
|
||||
def runtest(self):
|
||||
self._test_func()
|
||||
|
||||
def repr_failure(self, excinfo):
|
||||
""" called when self.runtest() raises an exception. """
|
||||
formatted_tb = _limited_traceback(excinfo)
|
||||
formatted_tb.insert(0, "xonsh execution failed\n")
|
||||
formatted_tb.append('{}: {}'.format(excinfo.type.__name__, excinfo.value))
|
||||
return "".join(formatted_tb)
|
||||
|
||||
def reportinfo(self):
|
||||
return self.fspath, 0, "xonsh test: {}".format(self.name)
|
Loading…
Add table
Reference in a new issue