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