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