|
1
|
|
|
import os |
|
2
|
|
|
import pkg_resources |
|
3
|
|
|
from pyprint.ConsolePrinter import ConsolePrinter |
|
4
|
|
|
import unittest |
|
5
|
|
|
|
|
6
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
|
7
|
|
|
from coalib.collecting.Collectors import (collect_files, |
|
8
|
|
|
collect_dirs, |
|
9
|
|
|
collect_bears, |
|
10
|
|
|
collect_all_bears_from_sections, |
|
11
|
|
|
collect_registered_bears_dirs) |
|
12
|
|
|
from coalib.misc.ContextManagers import retrieve_stdout |
|
13
|
|
|
from coalib.settings.Section import Section |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class CollectFilesTest(unittest.TestCase): |
|
17
|
|
|
|
|
18
|
|
|
def setUp(self): |
|
19
|
|
|
current_dir = os.path.split(__file__)[0] |
|
20
|
|
|
self.collectors_test_dir = os.path.join(current_dir, |
|
21
|
|
|
"collectors_test_dir") |
|
22
|
|
|
|
|
23
|
|
|
def test_file_empty(self): |
|
24
|
|
|
self.assertRaises(TypeError, collect_files) |
|
25
|
|
|
|
|
26
|
|
|
def test_file_invalid(self): |
|
27
|
|
|
self.assertEqual(collect_files(["invalid_path"]), []) |
|
28
|
|
|
|
|
29
|
|
|
def test_file_collection(self): |
|
30
|
|
|
self.assertEqual(collect_files([os.path.join(self.collectors_test_dir, |
|
31
|
|
|
"others", |
|
32
|
|
|
"*", |
|
33
|
|
|
"*2.py")]), |
|
34
|
|
|
[os.path.normcase(os.path.join( |
|
35
|
|
|
self.collectors_test_dir, |
|
36
|
|
|
"others", |
|
37
|
|
|
"py_files", |
|
38
|
|
|
"file2.py"))]) |
|
39
|
|
|
|
|
40
|
|
|
def test_file_string_collection(self): |
|
41
|
|
|
self.assertEqual(collect_files(os.path.join(self.collectors_test_dir, |
|
42
|
|
|
"others", |
|
43
|
|
|
"*", |
|
44
|
|
|
"*2.py")), |
|
45
|
|
|
[os.path.normcase(os.path.join( |
|
46
|
|
|
self.collectors_test_dir, |
|
47
|
|
|
"others", |
|
48
|
|
|
"py_files", |
|
49
|
|
|
"file2.py"))]) |
|
50
|
|
|
|
|
51
|
|
|
def test_ignored(self): |
|
52
|
|
|
self.assertEqual(collect_files([os.path.join(self.collectors_test_dir, |
|
53
|
|
|
"others", |
|
54
|
|
|
"*", |
|
55
|
|
|
"*2.py"), |
|
56
|
|
|
os.path.join(self.collectors_test_dir, |
|
57
|
|
|
"others", |
|
58
|
|
|
"*", |
|
59
|
|
|
"*2.py")], |
|
60
|
|
|
[os.path.join(self.collectors_test_dir, |
|
61
|
|
|
"others", |
|
62
|
|
|
"py_files", |
|
63
|
|
|
"file2.py")]), |
|
64
|
|
|
[]) |
|
65
|
|
|
|
|
66
|
|
|
def test_limited(self): |
|
67
|
|
|
self.assertEqual( |
|
68
|
|
|
collect_files([os.path.join(self.collectors_test_dir, |
|
69
|
|
|
"others", |
|
70
|
|
|
"*", |
|
71
|
|
|
"*py")], |
|
72
|
|
|
limit_file_paths=[os.path.join( |
|
73
|
|
|
self.collectors_test_dir, |
|
74
|
|
|
"others", |
|
75
|
|
|
"*", |
|
76
|
|
|
"*2.py")]), |
|
77
|
|
|
[os.path.normcase(os.path.join(self.collectors_test_dir, |
|
78
|
|
|
"others", |
|
79
|
|
|
"py_files", |
|
80
|
|
|
"file2.py"))]) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
class CollectDirsTest(unittest.TestCase): |
|
84
|
|
|
|
|
85
|
|
|
def setUp(self): |
|
86
|
|
|
current_dir = os.path.split(__file__)[0] |
|
87
|
|
|
self.collectors_test_dir = os.path.join(current_dir, |
|
88
|
|
|
"collectors_test_dir") |
|
89
|
|
|
|
|
90
|
|
|
def test_dir_empty(self): |
|
91
|
|
|
self.assertRaises(TypeError, collect_dirs) |
|
92
|
|
|
|
|
93
|
|
|
def test_dir_invalid(self): |
|
94
|
|
|
self.assertEqual(collect_dirs(["invalid_path"]), []) |
|
95
|
|
|
|
|
96
|
|
|
def test_dir_collection(self): |
|
97
|
|
|
self.assertEqual( |
|
98
|
|
|
sorted(i for i in |
|
99
|
|
|
collect_dirs([os.path.join(self.collectors_test_dir, |
|
100
|
|
|
"**")]) |
|
101
|
|
|
if "__pycache__" not in i), |
|
102
|
|
|
sorted([os.path.normcase(os.path.join( |
|
103
|
|
|
self.collectors_test_dir, "bears")), |
|
104
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
105
|
|
|
"bears_local_global")), |
|
106
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
107
|
|
|
"others")), |
|
108
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
109
|
|
|
"others", |
|
110
|
|
|
"c_files")), |
|
111
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
112
|
|
|
"others", |
|
113
|
|
|
"py_files")), |
|
114
|
|
|
os.path.normcase(self.collectors_test_dir+os.sep)])) |
|
115
|
|
|
|
|
116
|
|
|
def test_dir_string_collection(self): |
|
117
|
|
|
self.assertEqual( |
|
118
|
|
|
sorted(i for i in |
|
119
|
|
|
collect_dirs(os.path.join(self.collectors_test_dir, |
|
120
|
|
|
"**")) |
|
121
|
|
|
if "__pycache__" not in i), |
|
122
|
|
|
sorted([os.path.normcase(os.path.join( |
|
123
|
|
|
self.collectors_test_dir, "bears")), |
|
124
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
125
|
|
|
"bears_local_global")), |
|
126
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
127
|
|
|
"others")), |
|
128
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
129
|
|
|
"others", |
|
130
|
|
|
"c_files")), |
|
131
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
132
|
|
|
"others", |
|
133
|
|
|
"py_files")), |
|
134
|
|
|
os.path.normcase(self.collectors_test_dir+os.sep)])) |
|
135
|
|
|
|
|
136
|
|
|
def test_ignored(self): |
|
137
|
|
|
self.assertEqual( |
|
138
|
|
|
sorted(i for i in |
|
139
|
|
|
collect_dirs([os.path.join(self.collectors_test_dir, |
|
140
|
|
|
"**")], |
|
141
|
|
|
[os.path.normcase(os.path.join( |
|
142
|
|
|
self.collectors_test_dir, |
|
143
|
|
|
"others", |
|
144
|
|
|
"py_files"))]) |
|
145
|
|
|
if "__pycache__" not in i), |
|
146
|
|
|
|
|
147
|
|
|
sorted([os.path.normcase(os.path.join( |
|
148
|
|
|
self.collectors_test_dir, "bears")), |
|
149
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
150
|
|
|
"bears_local_global")), |
|
151
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
152
|
|
|
"others")), |
|
153
|
|
|
os.path.normcase(os.path.join(self.collectors_test_dir, |
|
154
|
|
|
"others", |
|
155
|
|
|
"c_files")), |
|
156
|
|
|
os.path.normcase(self.collectors_test_dir+os.sep)])) |
|
157
|
|
|
|
|
158
|
|
|
def test_collect_registered_bears_dirs(self): |
|
159
|
|
|
old_iter = pkg_resources.iter_entry_points |
|
160
|
|
|
def my_iter(name): |
|
161
|
|
|
assert name == "hello" |
|
162
|
|
|
class EntryPoint1: |
|
163
|
|
|
def load(self): |
|
|
|
|
|
|
164
|
|
|
class PseudoPlugin: |
|
165
|
|
|
__file__ = "/path1/file1" |
|
166
|
|
|
return PseudoPlugin() |
|
167
|
|
|
class EntryPoint2: |
|
168
|
|
|
def load(self): |
|
|
|
|
|
|
169
|
|
|
class PseudoPlugin: |
|
170
|
|
|
__file__ = "/path2/file2" |
|
171
|
|
|
raise pkg_resources.DistributionNotFound |
|
172
|
|
|
return PseudoPlugin() |
|
|
|
|
|
|
173
|
|
|
return iter([EntryPoint1(), EntryPoint2()]) |
|
174
|
|
|
pkg_resources.iter_entry_points = my_iter |
|
175
|
|
|
|
|
176
|
|
|
output = sorted(collect_registered_bears_dirs("hello")) |
|
177
|
|
|
self.assertEqual(output, ["/path1"]) |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
class CollectBearsTest(unittest.TestCase): |
|
181
|
|
|
|
|
182
|
|
|
def setUp(self): |
|
183
|
|
|
current_dir = os.path.split(__file__)[0] |
|
184
|
|
|
self.collectors_test_dir = os.path.join(current_dir, |
|
185
|
|
|
"collectors_test_dir") |
|
186
|
|
|
|
|
187
|
|
|
self.log_printer = LogPrinter(ConsolePrinter()) |
|
188
|
|
|
|
|
189
|
|
|
def test_bear_empty(self): |
|
190
|
|
|
self.assertRaises(TypeError, collect_bears) |
|
191
|
|
|
|
|
192
|
|
|
def test_bear_invalid(self): |
|
193
|
|
|
with retrieve_stdout() as sio: |
|
194
|
|
|
self.assertEqual(collect_bears(["invalid_paths"], |
|
195
|
|
|
["invalid_name"], |
|
196
|
|
|
["invalid kind"], |
|
197
|
|
|
self.log_printer), ([],)) |
|
198
|
|
|
self.assertRegex(sio.getvalue(), |
|
199
|
|
|
".*\\[WARNING\\].*No bears were found matching " |
|
200
|
|
|
"'invalid_name'.\n") |
|
201
|
|
|
|
|
202
|
|
|
self.assertEqual(collect_bears(["invalid_paths"], |
|
203
|
|
|
["invalid_name"], |
|
204
|
|
|
["invalid kind1", "invalid kind2"], |
|
205
|
|
|
self.log_printer), ([], [])) |
|
206
|
|
|
|
|
207
|
|
|
def test_simple_single(self): |
|
208
|
|
|
self.assertEqual(len(collect_bears( |
|
209
|
|
|
[os.path.join(self.collectors_test_dir, "bears")], |
|
210
|
|
|
["bear1"], |
|
211
|
|
|
["kind"], |
|
212
|
|
|
self.log_printer)[0]), 1) |
|
213
|
|
|
|
|
214
|
|
|
def test_string_single(self): |
|
215
|
|
|
self.assertEqual(len(collect_bears( |
|
216
|
|
|
os.path.join(self.collectors_test_dir, "bears"), |
|
217
|
|
|
["bear1"], |
|
218
|
|
|
["kind"], |
|
219
|
|
|
self.log_printer)[0]), 1) |
|
220
|
|
|
|
|
221
|
|
|
def test_reference_single(self): |
|
222
|
|
|
self.assertEqual(len(collect_bears( |
|
223
|
|
|
[os.path.join(self.collectors_test_dir, "bears")], |
|
224
|
|
|
["metabear"], |
|
225
|
|
|
["kind"], |
|
226
|
|
|
self.log_printer)[0]), 1) |
|
227
|
|
|
|
|
228
|
|
|
def test_no_duplications(self): |
|
229
|
|
|
self.assertEqual(len(collect_bears( |
|
230
|
|
|
[os.path.join(self.collectors_test_dir, "bears", "**")], |
|
231
|
|
|
["*"], |
|
232
|
|
|
["kind"], |
|
233
|
|
|
self.log_printer)[0]), 2) |
|
234
|
|
|
|
|
235
|
|
|
def test_wrong_kind(self): |
|
236
|
|
|
self.assertEqual(len(collect_bears( |
|
237
|
|
|
[os.path.join(self.collectors_test_dir, "bears", "**")], |
|
238
|
|
|
["*"], |
|
239
|
|
|
["other_kind"], |
|
240
|
|
|
self.log_printer)[0]), 0) |
|
241
|
|
|
|
|
242
|
|
|
def test_all_bears_from_sections(self): |
|
243
|
|
|
test_section = Section("test_section") |
|
244
|
|
|
test_section.bear_dirs = lambda: os.path.join(self.collectors_test_dir, |
|
245
|
|
|
"bears_local_global", |
|
246
|
|
|
"**") |
|
247
|
|
|
local_bears, global_bears = collect_all_bears_from_sections( |
|
248
|
|
|
{'test_section': test_section}, |
|
249
|
|
|
self.log_printer) |
|
250
|
|
|
|
|
251
|
|
|
self.assertEqual(len(local_bears['test_section']), 2) |
|
252
|
|
|
self.assertEqual(len(global_bears['test_section']), 2) |
|
253
|
|
|
|
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example
could be written as