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