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
|
import stat
import unittest
import yaml
from collections import namedtuple
from unittest.mock import patch
from parameterized import parameterized
import __res as res
NAMESPACE_PREFIX = b"py/namespace/"
TEST_SOURCE_ROOT = "/home/arcadia"
class ImporterMocks:
def __init__(self, mock_fs, mock_resources):
self._mock_fs = mock_fs
self._mock_resources = mock_resources
self._patchers = [
patch("__res.iter_keys", wraps=self._iter_keys),
patch("__res.__resource.find", wraps=self._resource_find),
patch("__res._path_isfile", wraps=self._path_isfile),
patch("__res._os.listdir", wraps=self._os_listdir),
patch("__res._os.lstat", wraps=self._os_lstat),
]
for patcher in self._patchers:
patcher.start()
def stop(self):
for patcher in self._patchers:
patcher.stop()
def _iter_keys(self, prefix):
assert prefix == NAMESPACE_PREFIX
for k in self._mock_resources.keys():
yield k, k.removeprefix(prefix)
def _resource_find(self, key):
return self._mock_resources.get(key)
def _lookup_mock_fs(self, filename):
path = filename.lstrip("/").split("/")
curdir = self._mock_fs
for item in path:
if item in curdir:
curdir = curdir[item]
else:
return None
return curdir
def _path_isfile(self, filename):
f = self._lookup_mock_fs(filename)
return isinstance(f, str)
def _os_lstat(self, filename):
f = self._lookup_mock_fs(filename)
mode = stat.S_IFDIR if isinstance(f, dict) else stat.S_IFREG
return namedtuple("fake_stat_type", "st_mode")(st_mode=mode)
def _os_listdir(self, dirname):
f = self._lookup_mock_fs(dirname)
if isinstance(f, dict):
return f.keys()
else:
return []
class ArcadiaSourceFinderTestCase(unittest.TestCase):
def setUp(self):
self.import_mock = ImporterMocks(yaml.safe_load(self._get_mock_fs()), self._get_mock_resources())
self.arcadia_source_finder = res.ArcadiaSourceFinder(TEST_SOURCE_ROOT)
def tearDown(self):
self.import_mock.stop()
def _get_mock_fs(self):
raise NotImplementedError()
def _get_mock_resources(self):
raise NotImplementedError()
class TestLibraryWithoutNamespace(ArcadiaSourceFinderTestCase):
def _get_mock_fs(self):
return """
home:
arcadia:
project:
lib:
mod1.py: ""
package1:
mod2.py: ""
"""
def _get_mock_resources(self):
return {
b"py/namespace/unique_prefix1/project/lib": b"project.lib.",
}
@parameterized.expand(
[
("project.lib.mod1", b"project/lib/mod1.py"),
("project.lib.package1.mod2", b"project/lib/package1/mod2.py"),
("project.lib.unknown_module", None),
("project.lib", None), # package
]
)
def test_get_module_path(self, module, path):
assert path == self.arcadia_source_finder.get_module_path(module)
@parameterized.expand(
[
("project.lib.mod1", False),
("project.lib.package1.mod2", False),
("project", True),
("project.lib", True),
("project.lib.package1", True),
]
)
def test_is_packages(self, module, is_package):
assert is_package == self.arcadia_source_finder.is_package(module)
def test_is_package_for_unknown_module(self):
self.assertRaises(
ImportError,
lambda: self.arcadia_source_finder.is_package("project.lib.package2"),
)
@parameterized.expand(
[
(
"",
{
("PFX.project", True),
},
),
(
"project.",
{
("PFX.lib", True),
},
),
(
"project.lib.",
{
("PFX.mod1", False),
("PFX.package1", True),
},
),
(
"project.lib.package1.",
{
("PFX.mod2", False),
},
),
]
)
def test_iter_modules(self, package_prefix, expected):
got = self.arcadia_source_finder.iter_modules(package_prefix, "PFX.")
assert expected == set(got)
# Check iter_modules() don't crash and return correct result after not existing module was requested
def test_iter_modules_after_unknown_module_import(self):
self.arcadia_source_finder.get_module_path("project.unknown_module")
assert {("lib", True)} == set(self.arcadia_source_finder.iter_modules("project.", ""))
class TestLibraryExtendedFromAnotherLibrary(ArcadiaSourceFinderTestCase):
def _get_mock_fs(self):
return """
home:
arcadia:
project:
lib:
mod1.py: ''
lib_extension:
mod2.py: ''
"""
def _get_mock_resources(self):
return {
b"py/namespace/unique_prefix1/project/lib": b"project.lib.",
b"py/namespace/unique_prefix2/project/lib_extension": b"project.lib.",
}
@parameterized.expand(
[
("project.lib.mod1", b"project/lib/mod1.py"),
("project.lib.mod2", b"project/lib_extension/mod2.py"),
]
)
def test_get_module_path(self, module, path):
assert path == self.arcadia_source_finder.get_module_path(module)
@parameterized.expand(
[
(
"project.lib.",
{
("PFX.mod1", False),
("PFX.mod2", False),
},
),
]
)
def test_iter_modules(self, package_prefix, expected):
got = self.arcadia_source_finder.iter_modules(package_prefix, "PFX.")
assert expected == set(got)
class TestNamespaceAndTopLevelLibraries(ArcadiaSourceFinderTestCase):
def _get_mock_fs(self):
return """
home:
arcadia:
project:
ns_lib:
mod1.py: ''
top_level_lib:
mod2.py: ''
"""
def _get_mock_resources(self):
return {
b"py/namespace/unique_prefix1/project/ns_lib": b"ns.",
b"py/namespace/unique_prefix2/project/top_level_lib": b".",
}
@parameterized.expand(
[
("ns.mod1", b"project/ns_lib/mod1.py"),
("mod2", b"project/top_level_lib/mod2.py"),
]
)
def test_get_module_path(self, module, path):
assert path == self.arcadia_source_finder.get_module_path(module)
@parameterized.expand(
[
("ns", True),
("ns.mod1", False),
("mod2", False),
]
)
def test_is_packages(self, module, is_package):
assert is_package == self.arcadia_source_finder.is_package(module)
@parameterized.expand(
[
"project",
"project.ns_lib",
"project.top_level_lib",
]
)
def test_is_package_for_unknown_modules(self, module):
self.assertRaises(ImportError, lambda: self.arcadia_source_finder.is_package(module))
@parameterized.expand(
[
(
"",
{
("PFX.ns", True),
("PFX.mod2", False),
},
),
(
"ns.",
{
("PFX.mod1", False),
},
),
]
)
def test_iter_modules(self, package_prefix, expected):
got = self.arcadia_source_finder.iter_modules(package_prefix, "PFX.")
assert expected == set(got)
class TestIgnoreDirectoriesWithYaMakeFile(ArcadiaSourceFinderTestCase):
"""Packages and modules from tests should not be part of pylib namespace"""
def _get_mock_fs(self):
return """
home:
arcadia:
contrib:
python:
pylib:
mod1.py: ""
tests:
conftest.py: ""
ya.make: ""
"""
def _get_mock_resources(self):
return {
b"py/namespace/unique_prefix1/contrib/python/pylib": b"pylib.",
}
def test_get_module_path_for_lib(self):
assert b"contrib/python/pylib/mod1.py" == self.arcadia_source_finder.get_module_path("pylib.mod1")
def test_get_module_for_tests(self):
assert self.arcadia_source_finder.get_module_path("pylib.tests.conftest") is None
def test_is_package_for_tests(self):
self.assertRaises(ImportError, lambda: self.arcadia_source_finder.is_package("pylib.tests"))
class TestMergingNamespaceAndDirectoryPackages(ArcadiaSourceFinderTestCase):
"""Merge parent package (top level in this test) dirs with namespace dirs (DEVTOOLS-8979)"""
def _get_mock_fs(self):
return """
home:
arcadia:
contrib:
python:
pylint:
ya.make: ""
pylint:
__init__.py: ""
patcher:
patch.py: ""
ya.make: ""
"""
def _get_mock_resources(self):
return {
b"py/namespace/unique_prefix1/contrib/python/pylint": b".",
b"py/namespace/unique_prefix1/contrib/python/pylint/patcher": b"pylint.",
}
@parameterized.expand(
[
("pylint.__init__", b"contrib/python/pylint/pylint/__init__.py"),
("pylint.patch", b"contrib/python/pylint/patcher/patch.py"),
]
)
def test_get_module_path(self, module, path):
assert path == self.arcadia_source_finder.get_module_path(module)
class TestEmptyResources(ArcadiaSourceFinderTestCase):
def _get_mock_fs(self):
return """
home:
arcadia:
project:
lib:
mod1.py: ''
"""
def _get_mock_resources(self):
return {}
def test_get_module_path(self):
assert self.arcadia_source_finder.get_module_path("project.lib.mod1") is None
def test_is_package(self):
self.assertRaises(ImportError, lambda: self.arcadia_source_finder.is_package("project"))
def test_iter_modules(self):
assert [] == list(self.arcadia_source_finder.iter_modules("", "PFX."))
class TestDictionaryChangedSizeDuringIteration(ArcadiaSourceFinderTestCase):
def _get_mock_fs(self):
return """
home:
arcadia:
project:
lib1:
mod1.py: ''
lib2:
mod2.py: ''
"""
def _get_mock_resources(self):
return {
b"py/namespace/unique_prefix1/project/lib1": b"project.lib1.",
b"py/namespace/unique_prefix1/project/lib2": b"project.lib2.",
}
def test_no_crash_on_recursive_iter_modules(self):
for package in self.arcadia_source_finder.iter_modules("project.", ""):
for _ in self.arcadia_source_finder.iter_modules(package[0], ""):
pass
|