Repository navigation
Expand file tree
/
Copy pathtest_importcompletion.py
More file actions
257 lines (221 loc) · 8.56 KB
/
Copy pathtest_importcompletion.py
File metadata and controls
257 lines (221 loc) · 8.56 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
import os
import tempfile
import unittest
from pathlib import Path
from bpython.importcompletion import ModuleGatherer
class TestSimpleComplete(unittest.TestCase):
def setUp(self):
self.module_gatherer = ModuleGatherer()
self.module_gatherer.modules = [
"zzabc",
"zzabd",
"zzefg",
"zzabc.e",
"zzabc.f",
"zzefg.a1",
"zzefg.a2",
]
def test_simple_completion(self):
self.assertSetEqual(
self.module_gatherer.complete(10, "import zza"), {"zzabc", "zzabd"}
)
self.assertSetEqual(
self.module_gatherer.complete(11, "import zza"), {"zzabc", "zzabd"}
)
def test_import_empty(self):
self.assertSetEqual(
self.module_gatherer.complete(13, "import zzabc."),
{"zzabc.e", "zzabc.f"},
)
self.assertSetEqual(
self.module_gatherer.complete(14, "import zzabc."),
{"zzabc.e", "zzabc.f"},
)
def test_import(self):
self.assertSetEqual(
self.module_gatherer.complete(14, "import zzefg.a"),
{"zzefg.a1", "zzefg.a2"},
)
self.assertSetEqual(
self.module_gatherer.complete(15, "import zzefg.a"),
{"zzefg.a1", "zzefg.a2"},
)
@unittest.expectedFailure
def test_import_blank(self):
self.assertSetEqual(
self.module_gatherer.complete(7, "import "),
{"zzabc", "zzabd", "zzefg"},
)
self.assertSetEqual(
self.module_gatherer.complete(8, "import "),
{"zzabc", "zzabd", "zzefg"},
)
@unittest.expectedFailure
def test_from_import_empty(self):
self.assertSetEqual(
self.module_gatherer.complete(5, "from "),
{"zzabc", "zzabd", "zzefg"},
)
self.assertSetEqual(
self.module_gatherer.complete(6, "from "),
{"zzabc", "zzabd", "zzefg"},
)
@unittest.expectedFailure
def test_from_module_import_empty(self):
self.assertSetEqual(
self.module_gatherer.complete(18, "from zzabc import "), {"e", "f"}
)
self.assertSetEqual(
self.module_gatherer.complete(19, "from zzabc import "), {"e", "f"}
)
self.assertSetEqual(
self.module_gatherer.complete(19, "from zzabc import "), {"e", "f"}
)
self.assertSetEqual(
self.module_gatherer.complete(19, "from zzabc import "), {"e", "f"}
)
def test_from_module_import(self):
self.assertSetEqual(
self.module_gatherer.complete(19, "from zzefg import a"),
{"a1", "a2"},
)
self.assertSetEqual(
self.module_gatherer.complete(20, "from zzefg import a"),
{"a1", "a2"},
)
self.assertSetEqual(
self.module_gatherer.complete(20, "from zzefg import a"),
{"a1", "a2"},
)
self.assertSetEqual(
self.module_gatherer.complete(20, "from zzefg import a"),
{"a1", "a2"},
)
class TestRealComplete(unittest.TestCase):
def setUp(self):
self.module_gatherer = ModuleGatherer()
while self.module_gatherer.find_coroutine():
pass
__import__("sys")
__import__("os")
def test_from_attribute(self):
self.assertSetEqual(
self.module_gatherer.complete(19, "from sys import arg"), {"argv"}
)
def test_from_attr_module(self):
self.assertSetEqual(
self.module_gatherer.complete(9, "from os.p"), {"os.path"}
)
def test_from_package(self):
self.assertSetEqual(
self.module_gatherer.complete(17, "from xml import d"), {"dom"}
)
class TestAvoidSymbolicLinks(unittest.TestCase):
def setUp(self):
with tempfile.TemporaryDirectory() as import_test_folder:
base_path = Path(import_test_folder)
(base_path / "Level0" / "Level1" / "Level2").mkdir(parents=True)
(base_path / "Left").mkdir(parents=True)
(base_path / "Right").mkdir(parents=True)
current_path = base_path / "Level0"
(current_path / "__init__.py").touch()
current_path = current_path / "Level1"
(current_path / "__init__.py").touch()
current_path = current_path / "Level2"
(current_path / "__init__.py").touch()
# Level0/Level1/Level2/Level3 -> Level0/Level1
(current_path / "Level3").symlink_to(
base_path / "Level0" / "Level1", target_is_directory=True
)
current_path = base_path / "Right"
(current_path / "__init__.py").touch()
# Right/toLeft -> Left
(current_path / "toLeft").symlink_to(
base_path / "Left", target_is_directory=True
)
current_path = base_path / "Left"
(current_path / "__init__.py").touch()
# Left/toRight -> Right
(current_path / "toRight").symlink_to(
base_path / "Right", target_is_directory=True
)
self.module_gatherer = ModuleGatherer((base_path.absolute(),))
while self.module_gatherer.find_coroutine():
pass
def test_simple_symbolic_link_loop(self):
filepaths = [
"Left.toRight.toLeft",
"Left.toRight",
"Left",
"Level0.Level1.Level2.Level3",
"Level0.Level1.Level2",
"Level0.Level1",
"Level0",
"Right",
"Right.toLeft",
"Right.toLeft.toRight",
]
for thing in self.module_gatherer.modules:
self.assertIn(thing, filepaths)
if thing == "Left.toRight.toLeft":
filepaths.remove("Right.toLeft")
filepaths.remove("Right.toLeft.toRight")
if thing == "Right.toLeft.toRight":
filepaths.remove("Left.toRight.toLeft")
filepaths.remove("Left.toRight")
filepaths.remove(thing)
self.assertFalse(filepaths)
class TestSkiplist(unittest.TestCase):
def test_absolute_paths(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory).resolve()
for name in ("first", "second"):
(root / name).mkdir()
(root / name / "__init__.py").touch()
(root / name / "inside.py").touch()
cases = (
([root], str(root), set()),
([root], str(root / "first"), {"second", "second.inside"}),
([root / "first"], str(root), set()),
)
for paths, skip, expected in cases:
with self.subTest(paths=paths, skip=skip):
gatherer = ModuleGatherer(paths, [skip])
while gatherer.find_coroutine():
pass
self.assertEqual(gatherer.modules, expected)
def test_relative_name_pattern_remains_supported(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory).resolve()
for name in ("first", "second"):
(root / name).mkdir()
(root / name / "__init__.py").touch()
(root / name / "inside.py").touch()
gatherer = ModuleGatherer([root], ["f*"])
while gatherer.find_coroutine():
pass
self.assertEqual(gatherer.modules, {"second", "second.inside"})
class TestBugReports(unittest.TestCase):
def test_issue_847(self):
with tempfile.TemporaryDirectory() as import_test_folder:
# ./xyzzy
# ./xyzzy/__init__.py
# ./xyzzy/plugh
# ./xyzzy/plugh/__init__.py
# ./xyzzy/plugh/bar.py
# ./xyzzy/plugh/foo.py
base_path = Path(import_test_folder)
(base_path / "xyzzy" / "plugh").mkdir(parents=True)
(base_path / "xyzzy" / "__init__.py").touch()
(base_path / "xyzzy" / "plugh" / "__init__.py").touch()
(base_path / "xyzzy" / "plugh" / "bar.py").touch()
(base_path / "xyzzy" / "plugh" / "foo.py").touch()
module_gatherer = ModuleGatherer((base_path.absolute(),))
while module_gatherer.find_coroutine():
pass
self.assertSetEqual(
module_gatherer.complete(17, "from xyzzy.plugh."),
{"xyzzy.plugh.bar", "xyzzy.plugh.foo"},
)
if __name__ == "__main__":
unittest.main()