forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_deps.py
More file actions
394 lines (313 loc) · 12.9 KB
/
test_deps.py
File metadata and controls
394 lines (313 loc) · 12.9 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""Tests for deps.py - dependency resolution."""
import pathlib
import tempfile
import unittest
from update_lib.deps import (
get_lib_paths,
get_soft_deps,
get_test_dependencies,
get_test_paths,
parse_lib_imports,
parse_test_imports,
)
class TestParseTestImports(unittest.TestCase):
"""Tests for parse_test_imports function."""
def test_from_test_import(self):
"""Test parsing 'from test import foo'."""
code = """
from test import string_tests
from test import lock_tests, other_tests
"""
imports = parse_test_imports(code)
self.assertEqual(imports, {"string_tests", "lock_tests", "other_tests"})
def test_from_test_dot_module(self):
"""Test parsing 'from test.foo import bar'."""
code = """
from test.string_tests import CommonTest
from test.support import verbose
"""
imports = parse_test_imports(code)
self.assertEqual(imports, {"string_tests"}) # support is excluded
def test_excludes_support(self):
"""Test that 'support' is excluded."""
code = """
from test import support
from test.support import verbose
"""
imports = parse_test_imports(code)
self.assertEqual(imports, set())
def test_regular_imports_ignored(self):
"""Test that regular imports are ignored."""
code = """
import os
from collections import defaultdict
from . import helper
"""
imports = parse_test_imports(code)
self.assertEqual(imports, set())
def test_syntax_error_returns_empty(self):
"""Test that syntax errors return empty set."""
code = "this is not valid python {"
imports = parse_test_imports(code)
self.assertEqual(imports, set())
class TestGetLibPaths(unittest.TestCase):
"""Tests for get_lib_paths function."""
def test_auto_detect_py_module(self):
"""Test auto-detection of _py{module}.py pattern."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
lib_dir = tmpdir / "Lib"
lib_dir.mkdir()
(lib_dir / "mymodule.py").write_text("# mymodule")
(lib_dir / "_pymymodule.py").write_text("# _pymymodule")
paths = get_lib_paths("mymodule", str(tmpdir))
self.assertEqual(len(paths), 2)
self.assertIn(tmpdir / "Lib" / "mymodule.py", paths)
self.assertIn(tmpdir / "Lib" / "_pymymodule.py", paths)
def test_default_file(self):
"""Test default to .py file."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
lib_dir = tmpdir / "Lib"
lib_dir.mkdir()
(lib_dir / "foo.py").write_text("# foo")
paths = get_lib_paths("foo", str(tmpdir))
self.assertEqual(paths, (tmpdir / "Lib" / "foo.py",))
def test_default_directory(self):
"""Test default to directory when file doesn't exist."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
lib_dir = tmpdir / "Lib"
lib_dir.mkdir()
(lib_dir / "foo").mkdir()
paths = get_lib_paths("foo", str(tmpdir))
self.assertEqual(paths, (tmpdir / "Lib" / "foo",))
class TestGetTestPaths(unittest.TestCase):
"""Tests for get_test_paths function."""
def test_known_dependency(self):
"""Test test with known path override."""
paths = get_test_paths("regrtest", "cpython")
self.assertEqual(len(paths), 1)
self.assertEqual(paths[0], pathlib.Path("cpython/Lib/test/test_regrtest"))
def test_default_directory(self):
"""Test default to test_name/ directory."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
test_dir = tmpdir / "Lib" / "test"
test_dir.mkdir(parents=True)
(test_dir / "test_foo").mkdir()
paths = get_test_paths("foo", str(tmpdir))
self.assertEqual(paths, (tmpdir / "Lib" / "test" / "test_foo",))
def test_default_file(self):
"""Test fallback to test_name.py file."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
test_dir = tmpdir / "Lib" / "test"
test_dir.mkdir(parents=True)
(test_dir / "test_foo.py").write_text("# test")
paths = get_test_paths("foo", str(tmpdir))
self.assertEqual(paths, (tmpdir / "Lib" / "test" / "test_foo.py",))
class TestGetTestDependencies(unittest.TestCase):
"""Tests for get_test_dependencies function."""
def test_parse_file_imports(self):
"""Test parsing imports from test file."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
test_dir = tmpdir / "test"
test_dir.mkdir()
# Create test file with import
test_file = test_dir / "test_foo.py"
test_file.write_text("""
from test import string_tests
class TestFoo:
pass
""")
# Create the dependency file
(test_dir / "string_tests.py").write_text("# string tests")
result = get_test_dependencies(test_file)
self.assertEqual(len(result["hard_deps"]), 1)
self.assertEqual(result["hard_deps"][0], test_dir / "string_tests.py")
self.assertEqual(result["data"], [])
def test_nonexistent_path(self):
"""Test nonexistent path returns empty."""
result = get_test_dependencies(pathlib.Path("/nonexistent/path"))
self.assertEqual(result, {"hard_deps": [], "data": []})
def test_transitive_data_dependency(self):
"""Test that data deps are resolved transitively.
Chain: test_codecencodings_kr -> multibytecodec_support -> cjkencodings
"""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
test_dir = tmpdir / "test"
test_dir.mkdir()
# Create test_codecencodings_kr.py that imports multibytecodec_support
test_file = test_dir / "test_codecencodings_kr.py"
test_file.write_text("""
from test import multibytecodec_support
class TestKR:
pass
""")
# Create multibytecodec_support.py (the intermediate dependency)
(test_dir / "multibytecodec_support.py").write_text("# support module")
# Create cjkencodings directory (the data dependency of multibytecodec_support)
(test_dir / "cjkencodings").mkdir()
result = get_test_dependencies(test_file)
# Should find multibytecodec_support.py as a hard_dep
self.assertEqual(len(result["hard_deps"]), 1)
self.assertEqual(
result["hard_deps"][0], test_dir / "multibytecodec_support.py"
)
# Should find cjkencodings as data (from multibytecodec_support's TEST_DEPENDENCIES)
self.assertEqual(len(result["data"]), 1)
self.assertEqual(result["data"][0], test_dir / "cjkencodings")
class TestParseLibImports(unittest.TestCase):
"""Tests for parse_lib_imports function."""
def test_import_statement(self):
"""Test parsing 'import foo'."""
code = """
import os
import sys
import collections.abc
"""
imports = parse_lib_imports(code)
self.assertEqual(imports, {"os", "sys", "collections.abc"})
def test_from_import(self):
"""Test parsing 'from foo import bar'."""
code = """
from os import path
from collections.abc import Mapping
from typing import Optional
"""
imports = parse_lib_imports(code)
self.assertEqual(imports, {"os", "collections.abc", "typing"})
def test_mixed_imports(self):
"""Test mixed import styles."""
code = """
import sys
from os import path
from collections import defaultdict
import functools
"""
imports = parse_lib_imports(code)
self.assertEqual(imports, {"sys", "os", "collections", "functools"})
def test_syntax_error_returns_empty(self):
"""Test that syntax errors return empty set."""
code = "this is not valid python {"
imports = parse_lib_imports(code)
self.assertEqual(imports, set())
def test_relative_import_skipped(self):
"""Test that relative imports (no module) are skipped."""
code = """
from . import foo
from .. import bar
"""
imports = parse_lib_imports(code)
self.assertEqual(imports, set())
class TestGetSoftDeps(unittest.TestCase):
"""Tests for get_soft_deps function."""
def test_with_temp_files(self):
"""Test soft deps detection with temp files."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
lib_dir = tmpdir / "Lib"
lib_dir.mkdir()
# Create a module that imports another module
(lib_dir / "foo.py").write_text("""
import bar
from baz import something
""")
# Create the imported modules
(lib_dir / "bar.py").write_text("# bar module")
(lib_dir / "baz.py").write_text("# baz module")
soft_deps = get_soft_deps("foo", str(tmpdir))
self.assertEqual(soft_deps, {"bar", "baz"})
def test_skips_self(self):
"""Test that module doesn't include itself in soft_deps."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
lib_dir = tmpdir / "Lib"
lib_dir.mkdir()
# Create a module that imports itself (circular)
(lib_dir / "foo.py").write_text("""
import foo
import bar
""")
(lib_dir / "bar.py").write_text("# bar module")
soft_deps = get_soft_deps("foo", str(tmpdir))
self.assertNotIn("foo", soft_deps)
self.assertIn("bar", soft_deps)
def test_filters_nonexistent(self):
"""Test that nonexistent modules are filtered out."""
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
lib_dir = tmpdir / "Lib"
lib_dir.mkdir()
# Create a module that imports nonexistent module
(lib_dir / "foo.py").write_text("""
import bar
import nonexistent
""")
(lib_dir / "bar.py").write_text("# bar module")
# nonexistent.py is NOT created
soft_deps = get_soft_deps("foo", str(tmpdir))
self.assertEqual(soft_deps, {"bar"})
class TestDircmpIsSame(unittest.TestCase):
"""Tests for _dircmp_is_same function."""
def test_identical_directories(self):
"""Test that identical directories return True."""
import filecmp
from update_lib.deps import _dircmp_is_same
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
dir1 = tmpdir / "dir1"
dir2 = tmpdir / "dir2"
dir1.mkdir()
dir2.mkdir()
(dir1 / "file.py").write_text("content")
(dir2 / "file.py").write_text("content")
dcmp = filecmp.dircmp(dir1, dir2)
self.assertTrue(_dircmp_is_same(dcmp))
def test_different_files(self):
"""Test that directories with different files return False."""
import filecmp
from update_lib.deps import _dircmp_is_same
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
dir1 = tmpdir / "dir1"
dir2 = tmpdir / "dir2"
dir1.mkdir()
dir2.mkdir()
(dir1 / "file.py").write_text("content1")
(dir2 / "file.py").write_text("content2")
dcmp = filecmp.dircmp(dir1, dir2)
self.assertFalse(_dircmp_is_same(dcmp))
def test_nested_identical(self):
"""Test that nested identical directories return True."""
import filecmp
from update_lib.deps import _dircmp_is_same
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
dir1 = tmpdir / "dir1"
dir2 = tmpdir / "dir2"
(dir1 / "sub").mkdir(parents=True)
(dir2 / "sub").mkdir(parents=True)
(dir1 / "sub" / "file.py").write_text("content")
(dir2 / "sub" / "file.py").write_text("content")
dcmp = filecmp.dircmp(dir1, dir2)
self.assertTrue(_dircmp_is_same(dcmp))
def test_nested_different(self):
"""Test that nested directories with differences return False."""
import filecmp
from update_lib.deps import _dircmp_is_same
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
dir1 = tmpdir / "dir1"
dir2 = tmpdir / "dir2"
(dir1 / "sub").mkdir(parents=True)
(dir2 / "sub").mkdir(parents=True)
(dir1 / "sub" / "file.py").write_text("content1")
(dir2 / "sub" / "file.py").write_text("content2")
dcmp = filecmp.dircmp(dir1, dir2)
self.assertFalse(_dircmp_is_same(dcmp))
if __name__ == "__main__":
unittest.main()