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
|
|
|
|
161
|
|
|
def test_iter_entry_points(name): |
162
|
|
|
assert name == "hello" |
163
|
|
|
|
164
|
|
|
class EntryPoint1: |
165
|
|
|
|
166
|
|
|
@staticmethod |
167
|
|
|
def load(): |
168
|
|
|
class PseudoPlugin: |
169
|
|
|
__file__ = "/path1/file1" |
170
|
|
|
return PseudoPlugin() |
171
|
|
|
|
172
|
|
|
class EntryPoint2: |
173
|
|
|
|
174
|
|
|
@staticmethod |
175
|
|
|
def load(): |
176
|
|
|
raise pkg_resources.DistributionNotFound |
177
|
|
|
|
178
|
|
|
return iter([EntryPoint1(), EntryPoint2()]) |
179
|
|
|
|
180
|
|
|
pkg_resources.iter_entry_points = test_iter_entry_points |
181
|
|
|
|
182
|
|
|
output = sorted(collect_registered_bears_dirs("hello")) |
183
|
|
|
self.assertEqual(output, ["/path1"]) |
184
|
|
|
pkg_resources.iter_entry_points = old_iter |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
class CollectBearsTest(unittest.TestCase): |
188
|
|
|
|
189
|
|
|
def setUp(self): |
190
|
|
|
current_dir = os.path.split(__file__)[0] |
191
|
|
|
self.collectors_test_dir = os.path.join(current_dir, |
192
|
|
|
"collectors_test_dir") |
193
|
|
|
|
194
|
|
|
self.log_printer = LogPrinter(ConsolePrinter()) |
195
|
|
|
|
196
|
|
|
def test_bear_empty(self): |
197
|
|
|
self.assertRaises(TypeError, collect_bears) |
198
|
|
|
|
199
|
|
|
def test_bear_invalid(self): |
200
|
|
|
with retrieve_stdout() as sio: |
201
|
|
|
self.assertEqual(collect_bears(["invalid_paths"], |
202
|
|
|
["invalid_name"], |
203
|
|
|
["invalid kind"], |
204
|
|
|
self.log_printer), ([],)) |
205
|
|
|
self.assertRegex(sio.getvalue(), |
206
|
|
|
".*\\[WARNING\\].*No bears were found matching " |
207
|
|
|
"'invalid_name'.\n") |
208
|
|
|
|
209
|
|
|
self.assertEqual(collect_bears(["invalid_paths"], |
210
|
|
|
["invalid_name"], |
211
|
|
|
["invalid kind1", "invalid kind2"], |
212
|
|
|
self.log_printer), ([], [])) |
213
|
|
|
|
214
|
|
|
def test_simple_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_string_single(self): |
222
|
|
|
self.assertEqual(len(collect_bears( |
223
|
|
|
os.path.join(self.collectors_test_dir, "bears"), |
224
|
|
|
["bear1"], |
225
|
|
|
["kind"], |
226
|
|
|
self.log_printer)[0]), 1) |
227
|
|
|
|
228
|
|
|
def test_reference_single(self): |
229
|
|
|
self.assertEqual(len(collect_bears( |
230
|
|
|
[os.path.join(self.collectors_test_dir, "bears")], |
231
|
|
|
["metabear"], |
232
|
|
|
["kind"], |
233
|
|
|
self.log_printer)[0]), 1) |
234
|
|
|
|
235
|
|
|
def test_no_duplications(self): |
236
|
|
|
self.assertEqual(len(collect_bears( |
237
|
|
|
[os.path.join(self.collectors_test_dir, "bears", "**")], |
238
|
|
|
["*"], |
239
|
|
|
["kind"], |
240
|
|
|
self.log_printer)[0]), 2) |
241
|
|
|
|
242
|
|
|
def test_wrong_kind(self): |
243
|
|
|
self.assertEqual(len(collect_bears( |
244
|
|
|
[os.path.join(self.collectors_test_dir, "bears", "**")], |
245
|
|
|
["*"], |
246
|
|
|
["other_kind"], |
247
|
|
|
self.log_printer)[0]), 0) |
248
|
|
|
|
249
|
|
|
def test_all_bears_from_sections(self): |
250
|
|
|
test_section = Section("test_section") |
251
|
|
|
test_section.bear_dirs = lambda: os.path.join(self.collectors_test_dir, |
252
|
|
|
"bears_local_global", |
253
|
|
|
"**") |
254
|
|
|
local_bears, global_bears = collect_all_bears_from_sections( |
255
|
|
|
{'test_section': test_section}, |
256
|
|
|
self.log_printer) |
257
|
|
|
|
258
|
|
|
self.assertEqual(len(local_bears['test_section']), 2) |
259
|
|
|
self.assertEqual(len(global_bears['test_section']), 2) |
260
|
|
|
|