xonsh/tests/test_xontribs.py

118 lines
2.3 KiB
Python
Raw Normal View History

2016-05-24 01:54:18 -04:00
"""xontrib tests, such as they are"""
2016-08-03 11:57:20 -04:00
import sys
2022-01-31 21:26:34 +05:30
2016-08-03 00:33:39 -04:00
import pytest
2022-01-31 21:26:34 +05:30
2022-05-05 00:32:20 +05:30
from xonsh.xontribs import (
xontrib_context,
xontribs_load,
xontribs_loaded,
xontribs_main,
)
2016-08-03 00:33:39 -04:00
2018-08-30 09:18:49 -05:00
2018-11-19 20:22:18 -05:00
@pytest.fixture
2016-08-03 00:33:39 -04:00
def tmpmod(tmpdir):
"""
Same as tmpdir but also adds/removes it to the front of sys.path.
Also cleans out any modules loaded as part of the test.
2016-08-03 00:33:39 -04:00
"""
sys.path.insert(0, str(tmpdir))
loadedmods = set(sys.modules.keys())
2016-08-03 00:33:39 -04:00
try:
yield tmpdir
finally:
del sys.path[0]
newmods = set(sys.modules.keys()) - loadedmods
for m in newmods:
del sys.modules[m]
2016-08-03 00:33:39 -04:00
2018-08-30 09:18:49 -05:00
2016-08-03 00:33:39 -04:00
def test_noall(tmpmod):
"""
Tests what get's exported from a module without __all__
"""
2018-08-30 09:18:49 -05:00
with tmpmod.mkdir("xontrib").join("spameggs.py").open("w") as x:
x.write(
"""
2016-08-03 00:33:39 -04:00
spam = 1
eggs = 2
_foobar = 3
2018-08-30 09:18:49 -05:00
"""
)
ctx = xontrib_context("spameggs")
assert ctx == {"spam": 1, "eggs": 2}
2016-08-03 00:33:39 -04:00
def test_withall(tmpmod):
"""
Tests what get's exported from a module with __all__
"""
2018-08-30 09:18:49 -05:00
with tmpmod.mkdir("xontrib").join("spameggs.py").open("w") as x:
x.write(
"""
2016-08-03 00:33:39 -04:00
__all__ = 'spam', '_foobar'
spam = 1
eggs = 2
_foobar = 3
2018-08-30 09:18:49 -05:00
"""
)
ctx = xontrib_context("spameggs")
assert ctx == {"spam": 1, "_foobar": 3}
2016-08-03 00:33:39 -04:00
2016-08-06 12:26:02 -04:00
def test_xshxontrib(tmpmod):
"""
Test that .xsh xontribs are loadable
"""
2018-08-30 09:18:49 -05:00
with tmpmod.mkdir("xontrib").join("script.xsh").open("w") as x:
x.write(
"""
2016-08-06 12:26:02 -04:00
hello = 'world'
2018-08-30 09:18:49 -05:00
"""
)
2016-08-06 12:26:02 -04:00
2018-08-30 09:18:49 -05:00
ctx = xontrib_context("script")
assert ctx == {"hello": "world"}
def test_xontrib_load(tmpmod):
"""
Test that .xsh xontribs are loadable
"""
with tmpmod.mkdir("xontrib").join("script.xsh").open("w") as x:
x.write(
"""
hello = 'world'
"""
)
xontribs_load(["script"])
assert "script" in xontribs_loaded()
def test_xontrib_load_dashed(tmpmod):
"""
Test that .xsh xontribs are loadable
"""
with tmpmod.mkdir("xontrib").join("scri-pt.xsh").open("w") as x:
x.write(
"""
hello = 'world'
"""
)
xontribs_load(["scri-pt"])
assert "scri-pt" in xontribs_loaded()
2022-05-05 00:32:20 +05:30
def test_xontrib_list(xession, capsys):
xontribs_main(["list"])
out, err = capsys.readouterr()
assert "abbrevs" in out