-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_traverse_code.py
227 lines (159 loc) · 7.08 KB
/
test_traverse_code.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
import unittest
from pathlib import Path
from abstra_internals.utils.file import traverse_code
from tests.fixtures import clear_dir, init_dir
class TestTraverseCode(unittest.TestCase):
def setUp(self):
self.root = init_dir()
def tearDown(self):
clear_dir(self.root)
def test_no_import(self):
entrypoint = Path("entrypoint.py")
entrypoint.touch()
self.assertEqual(list(traverse_code(entrypoint)), [entrypoint])
def test_invalid_import(self):
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import invalid")
self.assertEqual(list(traverse_code(entrypoint)), [entrypoint])
def test_import_module(self):
module = Path("module.py")
module.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import module")
self.assertSetEqual(set(traverse_code(entrypoint)), set([entrypoint, module]))
def test_import_package(self):
package = Path("package")
package.mkdir()
init = package.joinpath("__init__.py")
init.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import package")
self.assertSetEqual(set(traverse_code(entrypoint)), set([entrypoint, init]))
def test_import_from_submodule(self):
module = Path("module")
module.mkdir()
submodule = module.joinpath("submodule")
submodule.mkdir()
helper = submodule.joinpath("helper.py")
helper.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import module.submodule.helper")
self.assertEqual(set(traverse_code(entrypoint)), set([entrypoint, helper]))
def test_nested_relative_import(self):
module = Path("module")
module.mkdir()
helper = module.joinpath("helper.py")
helper.write_text("from .submodule import another_helper")
submodule = module.joinpath("submodule")
submodule.mkdir()
another_helper = submodule.joinpath("another_helper.py")
another_helper.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import module.helper")
self.assertSetEqual(
set(traverse_code(entrypoint)), set([entrypoint, helper, another_helper])
)
def test_nested_relative_import_from_submodule(self):
module = Path("module")
module.mkdir()
helper = module.joinpath("helper.py")
helper.touch()
submodule = module.joinpath("submodule")
submodule.mkdir()
another_helper = submodule.joinpath("another_helper.py")
another_helper.write_text("from .. import helper")
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import module.submodule.another_helper")
self.assertSetEqual(
set(traverse_code(entrypoint)), set([entrypoint, another_helper, helper])
)
def test_absolute_import_from_nested_module(self):
module = Path("module")
module.mkdir()
helper = module.joinpath("helper.py")
helper.write_text("from module.submodule import another_helper")
submodule = module.joinpath("submodule")
submodule.mkdir()
another_helper = submodule.joinpath("another_helper.py")
another_helper.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("import module.helper")
self.assertSetEqual(
set(traverse_code(entrypoint)), set([entrypoint, helper, another_helper])
)
def test_import_multiple_modules_from_package(self):
package = Path("package")
package.mkdir()
module_a = package.joinpath("module_a.py")
module_a.touch()
module_b = package.joinpath("module_b.py")
module_b.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from package import module_a, module_b")
self.assertSetEqual(
set(traverse_code(entrypoint)),
set([entrypoint, module_a, module_b]),
)
def test_import_from_submodule_with_module_name(self):
module = Path("module")
module.mkdir()
submodule = module.joinpath("submodule")
submodule.mkdir()
helper = submodule.joinpath("helper.py")
# module name is module.submodule
helper.write_text("from ...module.submodule import another_helper")
another_helper = submodule.joinpath("another_helper.py")
another_helper.touch()
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from module.submodule import helper")
self.assertSetEqual(
set(traverse_code(entrypoint)), set([entrypoint, helper, another_helper])
)
def test_raise_syntax_error(self):
entrypoint = Path("entrypoint.py")
entrypoint.write_text("invalid syntax")
with self.assertRaises(SyntaxError):
list(traverse_code(entrypoint, raise_on_syntax_errors=True))
def test_from_import_with_method(self):
module = Path("module.py")
module.write_text("def method(): pass")
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from module import method")
self.assertSetEqual(set(traverse_code(entrypoint)), set([entrypoint, module]))
def test_from_import_with_class(self):
module = Path("module.py")
module.write_text("class Class: pass")
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from module import Class")
self.assertSetEqual(set(traverse_code(entrypoint)), set([entrypoint, module]))
def test_from_import_with_multiple_methods(self):
module = Path("module.py")
module.write_text("def method_a(): pass\ndef method_b(): pass")
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from module import method_a, method_b")
self.assertSetEqual(set(traverse_code(entrypoint)), set([entrypoint, module]))
def test_from_import_with_nested_module_importing_methods(self):
module = Path("module")
module.mkdir()
submodule = module.joinpath("submodule.py")
submodule.write_text("def method(): pass")
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from module.submodule import method")
self.assertSetEqual(
set(traverse_code(entrypoint)), set([entrypoint, submodule])
)
def test_from_import_with_relative_import_importing_methods(self):
module = Path("module")
module.mkdir()
submodule = module.joinpath("submodule")
submodule.mkdir()
helper = submodule.joinpath("helper.py")
# module name is module.submodule
helper.write_text("from ...module.submodule.another_helper import some_method")
another_helper = submodule.joinpath("another_helper.py")
another_helper.write_text("def some_method(): pass")
entrypoint = Path("entrypoint.py")
entrypoint.write_text("from module.submodule import helper")
self.assertSetEqual(
set(traverse_code(entrypoint)), set([entrypoint, helper, another_helper])
)