1
|
|
|
import queue |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from coalib.bears.LocalBear import LocalBear |
5
|
|
|
from coalib.misc.ContextManagers import prepare_file |
6
|
|
|
from coalib.results.Result import Result |
7
|
|
|
from coalib.settings.Section import Section |
8
|
|
|
from coalib.settings.Setting import Setting |
9
|
|
|
from bears.tests.BearTestHelper import generate_skip_decorator |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def execute_bear(bear, *args, **kwargs): |
13
|
|
|
try: |
14
|
|
|
bear_output_generator = bear.execute(*args, **kwargs) |
15
|
|
|
assert bear_output_generator is not None |
16
|
|
|
except AssertionError: |
17
|
|
|
msg = [] |
18
|
|
|
while not bear.message_queue.empty(): |
19
|
|
|
msg.append(bear.message_queue.get().message) |
20
|
|
|
raise AssertionError("Bear returned None on execution \n" + |
21
|
|
|
"\n".join(msg)) |
22
|
|
|
return list(bear_output_generator) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class LocalBearTestHelper(unittest.TestCase): # pragma: no cover |
26
|
|
|
""" |
27
|
|
|
This is a helper class for simplification of testing of local bears. |
28
|
|
|
|
29
|
|
|
Please note that all abstraction will prepare the lines so you don't need |
30
|
|
|
to do that if you use them. |
31
|
|
|
|
32
|
|
|
If you miss some methods, get in contact with us, we'll be happy to help! |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
def check_validity(self, |
36
|
|
|
local_bear, |
37
|
|
|
lines, |
38
|
|
|
filename=None, |
39
|
|
|
valid=True, |
40
|
|
|
force_linebreaks=True, |
41
|
|
|
create_tempfile=True, |
42
|
|
|
tempfile_kwargs={}): |
43
|
|
|
""" |
44
|
|
|
Asserts that a check of the given lines with the given local bear |
45
|
|
|
either yields or does not yield any results. |
46
|
|
|
|
47
|
|
|
:param local_bear: The local bear to check with. |
48
|
|
|
:param lines: The lines to check. (string if single line |
49
|
|
|
or List of strings) |
50
|
|
|
:param filename: The filename, if it matters. |
51
|
|
|
:param valid: Whether the lines are valid or not. |
52
|
|
|
:param force_linebreaks: Whether to append newlines at each line |
53
|
|
|
if needed. (Bears expect a \\n for every line) |
54
|
|
|
:param create_tempfile: Whether to save lines in tempfile if needed. |
55
|
|
|
:param tempfile_kwargs: Kwargs passed to tempfile.mkstemp(). |
56
|
|
|
""" |
57
|
|
|
if isinstance(lines, str): |
58
|
|
|
lines = [lines] |
59
|
|
|
|
60
|
|
|
assert isinstance(self, unittest.TestCase) |
61
|
|
|
self.assertIsInstance(local_bear, |
62
|
|
|
LocalBear, |
63
|
|
|
msg="The given bear is not a local bear.") |
64
|
|
|
self.assertIsInstance(lines, |
65
|
|
|
list, |
66
|
|
|
msg="The given lines are not a list.") |
67
|
|
|
|
68
|
|
|
with prepare_file(lines, |
69
|
|
|
filename, |
70
|
|
|
force_linebreaks=force_linebreaks, |
71
|
|
|
create_tempfile=create_tempfile, |
72
|
|
|
tempfile_kwargs=tempfile_kwargs) as (lines, filename): |
73
|
|
|
|
74
|
|
|
bear_output = execute_bear(local_bear, filename, lines) |
75
|
|
|
if valid: |
76
|
|
|
msg = ("The local bear '{}' yields a result although it " |
77
|
|
|
"shouldn't.".format(local_bear.__class__.__name__)) |
78
|
|
|
self.assertEqual(bear_output, [], msg=msg) |
79
|
|
|
else: |
80
|
|
|
msg = ("The local bear '{}' yields no result although it " |
81
|
|
|
"should.".format(local_bear.__class__.__name__)) |
82
|
|
|
self.assertNotEqual(len(bear_output), 0, msg=msg) |
83
|
|
|
|
84
|
|
|
def check_results(self, |
85
|
|
|
local_bear, |
86
|
|
|
lines, |
87
|
|
|
results, |
88
|
|
|
filename=None, |
89
|
|
|
check_order=False, |
90
|
|
|
force_linebreaks=True, |
91
|
|
|
create_tempfile=True, |
92
|
|
|
tempfile_kwargs={}): |
93
|
|
|
""" |
94
|
|
|
Asserts that a check of the given lines with the given local bear does |
95
|
|
|
yield exactly the given results. |
96
|
|
|
|
97
|
|
|
:param local_bear: The local bear to check with. |
98
|
|
|
:param lines: The lines to check. (string if single line |
99
|
|
|
or List of strings) |
100
|
|
|
:param results: The expected result or list of results. |
101
|
|
|
:param filename: The filename, if it matters. |
102
|
|
|
:param force_linebreaks: Whether to append newlines at each line |
103
|
|
|
if needed. (Bears expect a \\n for every line) |
104
|
|
|
:param create_tempfile: Whether to save lines in tempfile if needed. |
105
|
|
|
:param tempfile_kwargs: Kwargs passed to tempfile.mkstemp(). |
106
|
|
|
""" |
107
|
|
|
if isinstance(lines, str): |
108
|
|
|
lines = [lines] |
109
|
|
|
if isinstance(results, Result): |
110
|
|
|
results = [results] |
111
|
|
|
|
112
|
|
|
assert isinstance(self, unittest.TestCase) |
113
|
|
|
self.assertIsInstance(local_bear, |
114
|
|
|
LocalBear, |
115
|
|
|
msg="The given bear is not a local bear.") |
116
|
|
|
self.assertIsInstance(lines, |
117
|
|
|
list, |
118
|
|
|
msg="The given lines are not a list.") |
119
|
|
|
self.assertIsInstance(results, |
120
|
|
|
list, |
121
|
|
|
msg="The given results are not a list.") |
122
|
|
|
|
123
|
|
|
with prepare_file(lines, |
124
|
|
|
filename, |
125
|
|
|
force_linebreaks=force_linebreaks, |
126
|
|
|
create_tempfile=create_tempfile, |
127
|
|
|
tempfile_kwargs=tempfile_kwargs) as (lines, filename): |
128
|
|
|
|
129
|
|
|
bear_output = execute_bear(local_bear, filename, lines) |
130
|
|
|
msg = ("The local bear '{}' doesn't yield the right results. Or the" |
131
|
|
|
" order may be wrong.".format(local_bear.__class__.__name__)) |
132
|
|
|
if not check_order: |
133
|
|
|
self.assertEqual(sorted(bear_output), sorted(results), msg=msg) |
134
|
|
|
else: |
135
|
|
|
self.assertEqual(bear_output, results, msg=msg) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def verify_local_bear(bear, |
139
|
|
|
valid_files, |
140
|
|
|
invalid_files, |
141
|
|
|
filename=None, |
142
|
|
|
settings={}, |
143
|
|
|
force_linebreaks=True, |
144
|
|
|
create_tempfile=True, |
145
|
|
|
tempfile_kwargs={}): |
146
|
|
|
""" |
147
|
|
|
Generates a test for a local bear by checking the given valid and invalid |
148
|
|
|
file contents. Simply use it on your module level like: |
149
|
|
|
|
150
|
|
|
YourTestName = verify_local_bear(YourBear, (['valid line'],), |
151
|
|
|
(['invalid line'],)) |
152
|
|
|
|
153
|
|
|
:param bear: The Bear class to test. |
154
|
|
|
:param valid_files: An iterable of files as a string list that won't |
155
|
|
|
yield results. |
156
|
|
|
:param invalid_files: An iterable of files as a string list that must |
157
|
|
|
yield results. |
158
|
|
|
:param filename: The filename to use for valid and invalid files. |
159
|
|
|
:param settings: A dictionary of keys and values (both string) from |
160
|
|
|
which settings will be created that will be made |
161
|
|
|
available for the tested bear. |
162
|
|
|
:param force_linebreaks: Whether to append newlines at each line |
163
|
|
|
if needed. (Bears expect a \\n for every line) |
164
|
|
|
:param create_tempfile: Whether to save lines in tempfile if needed. |
165
|
|
|
:param tempfile_kwargs: Kwargs passed to tempfile.mkstemp() if tempfile |
166
|
|
|
needs to be created. |
167
|
|
|
:return: A unittest.TestCase object. |
168
|
|
|
""" |
169
|
|
|
@generate_skip_decorator(bear) |
170
|
|
|
class LocalBearTest(LocalBearTestHelper): |
171
|
|
|
|
172
|
|
|
def setUp(self): |
173
|
|
|
self.section = Section('name') |
174
|
|
|
self.uut = bear(self.section, |
175
|
|
|
queue.Queue()) |
176
|
|
|
for name, value in settings.items(): |
177
|
|
|
self.section.append(Setting(name, value)) |
178
|
|
|
|
179
|
|
|
def test_valid_files(self): |
180
|
|
|
for file in valid_files: |
181
|
|
|
self.check_validity(self.uut, |
182
|
|
|
file, |
183
|
|
|
filename, |
184
|
|
|
valid=True, |
185
|
|
|
force_linebreaks=force_linebreaks, |
186
|
|
|
create_tempfile=create_tempfile, |
187
|
|
|
tempfile_kwargs=tempfile_kwargs) |
188
|
|
|
|
189
|
|
|
def test_invalid_files(self): |
190
|
|
|
for file in invalid_files: |
191
|
|
|
self.check_validity(self.uut, |
192
|
|
|
file, |
193
|
|
|
filename, |
194
|
|
|
valid=False, |
195
|
|
|
force_linebreaks=force_linebreaks, |
196
|
|
|
create_tempfile=create_tempfile, |
197
|
|
|
tempfile_kwargs=tempfile_kwargs) |
198
|
|
|
|
199
|
|
|
return LocalBearTest |
200
|
|
|
|