-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
186 lines (167 loc) · 5.53 KB
/
test_main.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# contextual: providing context for shell command invocations
# Copyright 2008-2015 Samuele Pedroni
#
# This file is part of contextual.
#
# contextual is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# contextual is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with contextual. If not, see <http://www.gnu.org/licenses/>.
#
import pytest
from _contextual import main
@pytest.fixture(scope="function")
def home_and_projs(request, tmpdir):
"""=> home, proj1/a, proj2/b, proj2/p1 -> proj1"""
request.addfinalizer(lambda: tmpdir.remove(rec=1, ignore_errors=True))
home = tmpdir.join("home", "user0")
a = home.join("proj1", "a").ensure_dir()
b = home.join("proj2", "b").ensure_dir()
p2p1 = home.join("proj2", "p1")
p2p1.mksymlinkto(a.dirpath())
return home, a, b, p2p1
def test_match(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ=1
""".format(
a.dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(a.strpath)
monkeypatch.setenv("PWD", a.strpath)
main([confp.strpath, "cmd"])
out, err = capsys.readouterr()
assert out == "PROJ=1\n"
def test_no_match(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ=1
""".format(
a.dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(b.strpath)
monkeypatch.setenv("PWD", b.strpath)
with pytest.raises(SystemExit) as exit_info:
main([confp.strpath, "cmd"])
assert exit_info.value.code == 1
out, err = capsys.readouterr()
assert out == "exit 1\n"
assert err.startswith("contextual: failed to infer context:")
def test_void_context(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ=1
/ :=
""".format(
a.dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(b.strpath)
monkeypatch.setenv("PWD", b.strpath)
main([confp.strpath, "cmd"])
out, err = capsys.readouterr()
assert out == "\n"
assert err == ""
def test_rules_used_once(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ={{ctx_dir}}
""".format(
a.dirpath().dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(p2p1.strpath)
monkeypatch.setenv("PWD", p2p1.strpath)
main([confp.strpath, "cmd"])
out, err = capsys.readouterr()
assert out == "PROJ={}\n".format(home)
def test_diverged_PWD(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ=1
{} := PROJ=2
""".format(
a.dirpath().strpath, b.dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(p2p1.strpath)
monkeypatch.setenv("PWD", p2p1.strpath)
main([confp.strpath, "cmd"])
out, err = capsys.readouterr()
assert out == "PROJ=1;PROJ=2\n"
def test_consider_command_path(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ=1
{} := PROJ=2
""".format(
a.dirpath().strpath, b.dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(a.strpath)
monkeypatch.setenv("PWD", a.strpath)
main([confp.strpath, b.join("cmd").strpath])
out, err = capsys.readouterr()
assert out == "PROJ=1;PROJ=2\n"
def test_broken_placeholder(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
conf = u"""
{} := PROJ=1
{} := PROJ={{2}}
""".format(
a.dirpath().strpath, b.dirpath().strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(p2p1.strpath)
monkeypatch.setenv("PWD", p2p1.strpath)
main([confp.strpath, "cmd"])
out, err = capsys.readouterr()
assert out == "PROJ=1\n"
assert err == "contextual: 'PROJ={2}' has unbound/unknown placeholder\n"
def test_trace(home_and_projs, monkeypatch, capsys):
home, a, b, p2p1 = home_and_projs
p1 = a.dirpath()
p2 = b.dirpath()
conf = u"""
{} := PROJ=1
{} := PROJ=2
/ :=
""".format(
p1.strpath, p2.strpath
)
confp = home.join("ctx.conf")
confp.write_text(conf, encoding="ascii")
monkeypatch.chdir(a.strpath)
monkeypatch.setenv("PWD", a.strpath)
with pytest.raises(SystemExit) as exit_info:
main([confp.strpath, "cmd", ":trace"])
assert exit_info.value.code == 0
out, err = capsys.readouterr()
assert out == "exit 0\n"
trace_lines = err.splitlines()
assert trace_lines == [
u"start-dir[PWD]: {}".format(a.strpath),
u" ~~ {} := PROJ=1 => {!r}".format(p1.strpath, [p1.strpath]),
u" ~~ {} := PROJ=2 => no".format(p2.strpath),
u" ~~ / := => void_context",
u"start-dir[getcwd]: {}".format(a.strpath),
u" ~~ {} := PROJ=2 => no".format(p2.strpath),
u"CONTEXT => PROJ=1",
]