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