-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_cmd.py
321 lines (252 loc) · 7.22 KB
/
test_cmd.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import pytest
from pytest_diff_selector.main import run
from conftest import append_file, write_file
def test_simple_scan(test_repo):
append_file(
test_repo,
"test_a.py",
"""
# comment
def test_func2():
assert True
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert ret == set()
def test_find_change_in_test_function(test_repo):
write_file(
test_repo,
"test_a.py",
"""
def test_func1():
call_something()
call_something_else()
assert True
# comment
def test_func2():
assert True
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::test_func1" in ret
def test_find_change_in_test_method(test_repo):
write_file(
test_repo,
"test_a.py",
"""
class TestSomething:
def test_method():
call_something()
call_something_else()
assert 0/1
def test_func1():
call_something()
assert False
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::TestSomething::test_method" in ret
def test_find_change_not_in_test_file(test_repo):
write_file(
test_repo,
"helper.py",
"""
def call_something():
print('doing some changes')
""",
)
test_repo.run("git add *.py")
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::test_func1" in ret
assert "test_a.py::TestSomething::test_method" in ret
def test_find_change_not_in_test_file_nested(test_repo):
write_file(
test_repo,
"test_a.py",
"""
from utils.helper import call_something
class TestSomething:
def test_method():
global_var = global_var + 1
call_something()
assert 0/1
def test_func1():
call_something()
assert False
""",
)
write_file(test_repo, "utils/__init__.py", """# just comment""")
write_file(
test_repo,
"utils/helper.py",
"""
def call_something():
print('doing')
""",
)
test_repo.run("git add utils/*.py")
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::test_func1" in ret
assert "test_a.py::TestSomething::test_method" in ret
def test_find_change_in_test_module_scope(test_repo):
write_file(
test_repo,
"test_a.py",
"""
global_var = 20
class TestSomething:
def test_method():
global_var = global_var + 1
call_something()
assert 0/1
def test_func1():
call_something()
assert False
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::TestSomething::test_method" in ret
def test_find_change_in_method_decorator(test_repo):
write_file(
test_repo,
"test_a.py",
"""
from helpers import call_something
import pytest
class TestSomething:
@pytest.mark.skip
def test_method():
global_var = global_var + 1
call_something()
assert 0/1
def test_func1():
call_something()
assert False
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::TestSomething::test_method" in ret
def test_find_change_with_no_python_file(test_repo):
write_file(
test_repo,
"README.md",
"""
# Just updating the docs
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert ret == []
def test_find_change_nested_calls(test_repo):
write_file(
test_repo,
"helpers.py",
"""
def call_something():
print('doing')
func1()
def func1():
print('doing B')
""",
)
test_repo.run("git add *.py")
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert ret == set()
@pytest.mark.skip(
reason="Not sure there's a way to detect this one, not without scanning before change took place"
)
def test_find_change_removal_from_end_of_func(test_repo): # pragma: no cover
write_file(
test_repo,
"helper.py",
"""
def call_something():
print('doing')
""",
)
test_repo.run("git add *.py")
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert "test_a.py::test_func1" in ret
assert "test_a.py::TestSomething::test_method" in ret
def test_find_change_in_inherited_class(test_repo):
write_file(
test_repo,
"test_a.py",
"""
from helpers import call_something
class FatherClass:
def father_method(self):
call_something()
class TestSomething(FatherClass):
def test_method(self):
global_var = global_var + 1
call_something()
self.father_method()
assert 0/1
def test_func1():
call_something()
assert False
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert {"test_a.py::TestSomething::test_method"} == ret
def test_find_change_in_new_file(test_repo):
write_file(
test_repo,
"test_b.py",
"""
def test_func1():
call_something()
assert False
""",
)
test_repo.run("git add *.py")
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert {"test_b.py::test_func1"} == ret
def test_find_change_in_inherited_class_tests(test_repo):
"""
* test the removal of test methods from class with __test__=False
* test the copy of test methods based on inheritance
"""
write_file(
test_repo,
"test_a.py",
"""
from helpers import call_something
class FatherClass:
__test__ = False
def father_method(self):
call_something()
def test_method_C(self):
do_something_important()
pass
class TestSomething(FatherClass):
def test_method(self):
global_var = global_var + 1
call_something()
self.father_method()
assert 0/1
def test_func1():
call_something()
assert False
""",
)
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert {
"test_a.py::TestSomething::test_method_C",
"test_a.py::TestSomething::test_method",
} == ret
def test_usage_of_list_comprehension(test_repo):
write_file(
test_repo,
"test_b.py",
"""
def test_func1():
l = [ i for i in range(10) ]
s = { i for i in range(10) }
call_something()
assert False
""",
)
test_repo.run("git add *.py")
ret = run(git_diff="HEAD", root_path=test_repo.workspace)
assert {"test_b.py::test_func1"} == ret