-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_landmark.py
239 lines (190 loc) · 7.29 KB
/
test_landmark.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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
import os
from landmark import (
check_is_non_empty,
check_is_executable,
Landmark,
LandmarkClause,
parse,
segs,
Succeed,
)
@pytest.fixture(scope="function")
def home_and_here(request, tmpdir):
"""=> home, p under home, segs(p)"""
request.addfinalizer(lambda: tmpdir.remove(rec=1, ignore_errors=True))
home = tmpdir.join("home", "user0")
home.join(".bashrc").write_text(u"#", "ascii", ensure=True)
home.join(".bashrc").chmod(0o700)
p = home.join("foo", "bar").strpath
home.join("x").ensure_dir()
home.join("y/z").ensure_dir()
s = segs(p)
return home.strpath, p, s
@pytest.fixture(scope="function")
def non_empty_and_executable_dot_bashrc():
"""Make a hopeful landmark clause"""
where = LandmarkClause()
where.push_cond(check_is_non_empty, ".bashrc")
where.push_cond(check_is_executable, "{1}")
return where
def test_landmark(home_and_here, non_empty_and_executable_dot_bashrc):
home, p, s = home_and_here
hit = os.path.join(home, ".bashrc")
lm = Landmark(None, None, non_empty_and_executable_dot_bashrc, "ctx")
res = lm.match(p, s)
assert res == ([home, hit, hit], "ctx")
lm = Landmark(
os.path.dirname(home), "one", non_empty_and_executable_dot_bashrc, "ctx"
)
res = lm.match(p, s)
assert res == ([home, hit, hit], "ctx")
lm = Landmark(
os.path.dirname(os.path.dirname(home)),
"rec",
non_empty_and_executable_dot_bashrc,
"ctx",
)
res = lm.match(p, s)
assert res == ([home, hit, hit], "ctx")
lm = Landmark(home, None, non_empty_and_executable_dot_bashrc, "ctx")
res = lm.match(p, s)
assert res == ([home, hit, hit], "ctx")
lm = Landmark(home, None, None, "ctx")
res = lm.match(p, s)
assert res == ([home], "ctx")
lm = Landmark("/", None, None, "ctx")
res = lm.match(p, s)
assert res == (["/"], "ctx")
def test_landmark_backtrack(home_and_here):
home, p, s = home_and_here
where = LandmarkClause()
where.push_cond(os.path.isdir, "*")
where.push_cond(os.path.isdir, "{1}/z")
lm = Landmark(home, "rec", where, "ctx")
res = lm.match(p, s)
assert res == ([home, os.path.join(home, "y"), os.path.join(home, "y/z")], "ctx")
nowhere = LandmarkClause()
nowhere.push_cond(os.path.isdir, "*")
nowhere.push_cond(check_is_non_empty, "{1}/z")
lm = Landmark(home, "rec", nowhere, "ctx")
res = lm.match(p, s)
assert res == (None, None)
@pytest.fixture(scope="function")
def hopeless_where():
"""Make a hopeless landmark clause"""
where1 = LandmarkClause()
where1.push_cond(lambda p: True, ".non-existent-file")
return where1
def test_landmark_no_match(home_and_here, hopeless_where):
home, p, s = home_and_here
lm = Landmark(home, None, None, "ctx")
p1 = os.path.abspath(os.path.join(home, "..", "user1"))
res = lm.match(p1, segs(p1))
assert res == (None, None)
lm = Landmark(None, None, hopeless_where, "ctx")
res = lm.match(p, s)
assert res == (None, None)
lm = Landmark(os.path.dirname(home), "one", hopeless_where, "ctx")
res = lm.match(p, s)
assert res == (None, None)
lm = Landmark(home, "one", None, "ctx")
res = lm.match(home, segs(home))
assert res == (None, None)
def test_landmark_match_shortcut(home_and_here, non_empty_and_executable_dot_bashrc):
home, p, s = home_and_here
hit = os.path.join(home, ".bashrc")
lm = Landmark(
os.path.dirname(home), "one", non_empty_and_executable_dot_bashrc, "ctx"
)
res = lm.match_shortcut(os.path.basename(home), None)
assert res == ([home, hit, hit], "ctx")
lm = Landmark(os.path.dirname(home), "one", None, "ctx")
res = lm.match_shortcut(os.path.basename(home), None)
assert res == ([home], "ctx")
lm = Landmark(home, None, non_empty_and_executable_dot_bashrc, "ctx")
res = lm.match_shortcut(os.path.basename(home), None)
assert res == ([home, hit, hit], "ctx")
def test_landmark_match_shortcut_no_match(home_and_here, hopeless_where):
home, p, s = home_and_here
lm = Landmark(os.path.dirname(home), "one", None, "ctx")
res = lm.match_shortcut("user1", None)
assert res == (None, None)
lm = Landmark(home, None, None, "ctx")
res = lm.match_shortcut("user1", None)
assert res == (None, None)
lm = Landmark(os.path.dirname(home), "one", hopeless_where, "ctx")
res = lm.match_shortcut(os.path.basename(home), None)
assert res == (None, None)
def test_parse():
lm = parse(["#test", "", "where -s .bashrc := zzz"])[0]
assert lm.prefix_segs == []
assert lm.wildcard_descendant == "rec"
c = lm.where.conds[0]
assert c.check == check_is_non_empty
assert c.relative == ".bashrc"
assert lm.context == "zzz"
assert lm.src == "where -s .bashrc := zzz"
lm = parse(["#test", "", "/home/* where -e .bashrc := zzz"])[0]
assert lm.prefix_segs == ["home"]
assert lm.wildcard_descendant == "one"
c = lm.where.conds[0]
assert c.check == os.path.exists
assert c.relative == ".bashrc"
lm = parse(["#test", "", "/home/** where -e .bashrc := zzz"])[0]
assert lm.prefix_segs == ["home"]
assert lm.wildcard_descendant == "rec"
c = lm.where.conds[0]
assert c.check == os.path.exists
assert c.relative == ".bashrc"
lm = parse(["#test", "", "/home/pedronis where -f .bashrc := zzz"])[0]
assert lm.prefix_segs == ["home", "pedronis"]
assert lm.wildcard_descendant is None
c = lm.where.conds[0]
assert c.check == os.path.isfile
assert c.relative == ".bashrc"
lm = parse(["#test", "", '/home/pedronis where -f "this one" := zzz'])[0]
assert lm.prefix_segs == ["home", "pedronis"]
assert lm.wildcard_descendant is None
c = lm.where.conds[0]
assert c.check == os.path.isfile
assert c.relative == "this one"
lm = parse(["#test", "", "/home/pedronis := zzz"])[0]
assert lm.prefix_segs == ["home", "pedronis"]
assert lm.wildcard_descendant is None
assert isinstance(lm.where, Succeed)
lm = parse(["#test", "", "/ := zzz"])[0]
assert lm.prefix_segs == []
assert lm.wildcard_descendant is None
assert isinstance(lm.where, Succeed)
lm = parse(["#test", "", "/home/** := zzz"])
assert len(lm) == 0
def test_placeholder_error(capsys):
rule1 = "where -e {2}/x := ctx"
lm = parse([rule1])[0]
res = lm.match("/", segs("/"))
assert res == (None, None)
assert capsys.readouterr() == (
"",
"contextual: [rule: {}] {!r} has unbound/unknown placeholder\n".format(
rule1, "{2}/x"
),
)