File: test_xontribs.py

package info (click to toggle)
xonsh 0.13.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,024 kB
  • sloc: python: 46,350; makefile: 136; sh: 41; xml: 17
file content (164 lines) | stat: -rw-r--r-- 3,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""xontrib tests, such as they are"""
import sys

import pytest

from xonsh.xontribs import (
    xontrib_context,
    xontribs_load,
    xontribs_loaded,
    xontribs_main,
    xontribs_reload,
    xontribs_unload,
)


@pytest.fixture
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.
    """
    sys.path.insert(0, str(tmpdir))
    loadedmods = set(sys.modules.keys())
    try:
        yield tmpdir
    finally:
        del sys.path[0]
        newmods = set(sys.modules.keys()) - loadedmods
        for m in newmods:
            del sys.modules[m]


def test_noall(tmpmod):
    """
    Tests what get's exported from a module without __all__
    """

    with tmpmod.mkdir("xontrib").join("spameggs.py").open("w") as x:
        x.write(
            """
spam = 1
eggs = 2
_foobar = 3
"""
        )

    ctx = xontrib_context("spameggs")
    assert ctx == {"spam": 1, "eggs": 2}


def test_withall(tmpmod):
    """
    Tests what get's exported from a module with __all__
    """

    with tmpmod.mkdir("xontrib").join("spameggs.py").open("w") as x:
        x.write(
            """
__all__ = 'spam', '_foobar'
spam = 1
eggs = 2
_foobar = 3
"""
        )

    ctx = xontrib_context("spameggs")
    assert ctx == {"spam": 1, "_foobar": 3}


def test_xshxontrib(tmpmod):
    """
    Test that .xsh xontribs are loadable
    """
    with tmpmod.mkdir("xontrib").join("script.xsh").open("w") as x:
        x.write(
            """
hello = 'world'
"""
        )

    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_unload(tmpmod, xession):
    with tmpmod.mkdir("xontrib").join("script.py").open("w") as x:
        x.write(
            """
hello = 'world'

def _unload_xontrib_(xsh): del xsh.ctx['hello']
"""
        )

    xontribs_load(["script"])
    assert "script" in xontribs_loaded()
    assert "hello" in xession.ctx
    xontribs_unload(["script"])
    assert "script" not in xontribs_loaded()
    assert "hello" not in xession.ctx


def test_xontrib_reload(tmpmod, xession):
    with tmpmod.mkdir("xontrib").join("script.py").open("w") as x:
        x.write(
            """
hello = 'world'

def _unload_xontrib_(xsh): del xsh.ctx['hello']
"""
        )

    xontribs_load(["script"])
    assert "script" in xontribs_loaded()
    assert xession.ctx["hello"] == "world"

    with tmpmod.join("xontrib").join("script.py").open("w") as x:
        x.write(
            """
hello = 'world1'

def _unload_xontrib_(xsh): del xsh.ctx['hello']
"""
        )
    xontribs_reload(["script"])
    assert "script" in xontribs_loaded()
    assert xession.ctx["hello"] == "world1"


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()


def test_xontrib_list(xession, capsys):
    xontribs_main(["list"])
    out, err = capsys.readouterr()
    assert "abbrevs" in out