1
|
|
|
import os |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from coalib.bearlib.abstractions.Lint import Lint, escape_path_argument |
5
|
|
|
from coalib.misc.ContextManagers import prepare_file |
6
|
|
|
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY |
7
|
|
|
from coalib.results.SourceRange import SourceRange |
8
|
|
|
from coalib.settings.Section import Section |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class LintTest(unittest.TestCase): |
12
|
|
|
|
13
|
|
|
def setUp(self): |
14
|
|
|
section = Section("some_name") |
15
|
|
|
self.uut = Lint(section, None) |
16
|
|
|
|
17
|
|
|
def test_invalid_output(self): |
18
|
|
|
out = list(self.uut.process_output( |
19
|
|
|
["1.0|0: Info message\n", |
20
|
|
|
"2.2|1: Normal message\n", |
21
|
|
|
"3.4|2: Major message\n"], |
22
|
|
|
"a/file.py", |
23
|
|
|
['original_file_lines_placeholder'])) |
24
|
|
|
self.assertEqual(len(out), 3) |
25
|
|
|
self.assertEqual(out[0].origin, "Lint") |
26
|
|
|
|
27
|
|
|
self.assertEqual(out[0].affected_code[0], |
28
|
|
|
SourceRange.from_values("a/file.py", 1, 0)) |
29
|
|
|
self.assertEqual(out[0].severity, RESULT_SEVERITY.INFO) |
30
|
|
|
self.assertEqual(out[0].message, "Info message") |
31
|
|
|
|
32
|
|
|
self.assertEqual(out[1].affected_code[0], |
33
|
|
|
SourceRange.from_values("a/file.py", 2, 2)) |
34
|
|
|
self.assertEqual(out[1].severity, RESULT_SEVERITY.NORMAL) |
35
|
|
|
self.assertEqual(out[1].message, "Normal message") |
36
|
|
|
|
37
|
|
|
self.assertEqual(out[2].affected_code[0], |
38
|
|
|
SourceRange.from_values("a/file.py", 3, 4)) |
39
|
|
|
self.assertEqual(out[2].severity, RESULT_SEVERITY.MAJOR) |
40
|
|
|
self.assertEqual(out[2].message, "Major message") |
41
|
|
|
|
42
|
|
|
def test_custom_regex(self): |
43
|
|
|
self.uut.output_regex = (r'(?P<origin>\w+)\|' |
44
|
|
|
r'(?P<line>\d+)\.(?P<column>\d+)\|' |
45
|
|
|
r'(?P<end_line>\d+)\.(?P<end_column>\d+)\|' |
46
|
|
|
r'(?P<severity>\w+): (?P<message>.*)') |
47
|
|
|
self.uut.severity_map = {"I": RESULT_SEVERITY.INFO} |
48
|
|
|
out = list(self.uut.process_output( |
49
|
|
|
["info_msg|1.0|2.3|I: Info message\n"], |
50
|
|
|
'a/file.py', |
51
|
|
|
['original_file_lines_placeholder'])) |
52
|
|
|
self.assertEqual(len(out), 1) |
53
|
|
|
self.assertEqual(out[0].affected_code[0].start.line, 1) |
54
|
|
|
self.assertEqual(out[0].affected_code[0].start.column, 0) |
55
|
|
|
self.assertEqual(out[0].affected_code[0].end.line, 2) |
56
|
|
|
self.assertEqual(out[0].affected_code[0].end.column, 3) |
57
|
|
|
self.assertEqual(out[0].severity, RESULT_SEVERITY.INFO) |
58
|
|
|
self.assertEqual(out[0].origin, 'Lint (info_msg)') |
59
|
|
|
|
60
|
|
|
def test_valid_output(self): |
61
|
|
|
out = list(self.uut.process_output( |
62
|
|
|
["Random line that shouldn't be captured\n", |
63
|
|
|
"*************\n"], |
64
|
|
|
'a/file.py', |
65
|
|
|
['original_file_lines_placeholder'])) |
66
|
|
|
self.assertEqual(len(out), 0) |
67
|
|
|
|
68
|
|
|
def test_stdin_input(self): |
69
|
|
|
with prepare_file(["abcd", "efgh"], None) as (lines, filename): |
70
|
|
|
# Use more which is a command that can take stdin and show it. |
71
|
|
|
# This is available in windows and unix. |
72
|
|
|
self.uut.executable = "more" |
73
|
|
|
self.uut.use_stdin = True |
74
|
|
|
self.uut.use_stderr = False |
75
|
|
|
self.uut.process_output = lambda output, filename, file: output |
76
|
|
|
|
77
|
|
|
out = self.uut.lint(file=lines) |
78
|
|
|
# Some implementations of `more` add an extra newline at the end. |
79
|
|
|
self.assertTrue(("abcd\n", "efgh\n") == out or |
80
|
|
|
("abcd\n", "efgh\n", "\n") == out) |
81
|
|
|
|
82
|
|
|
def test_stderr_output(self): |
83
|
|
|
self.uut.executable = "echo" |
84
|
|
|
self.uut.arguments = "hello" |
85
|
|
|
self.uut.use_stdin = False |
86
|
|
|
self.uut.use_stderr = True |
87
|
|
|
self.uut.process_output = lambda output, filename, file: output |
88
|
|
|
out = self.uut.lint("unused_filename") |
89
|
|
|
self.assertEqual((), out) # stderr is used |
90
|
|
|
|
91
|
|
|
self.uut.use_stderr = False |
92
|
|
|
out = self.uut.lint("unused_filename") |
93
|
|
|
self.assertEqual(('hello\n',), out) # stdout is used |
94
|
|
|
|
95
|
|
|
def assert_warn(line): |
96
|
|
|
assert line == "hello" |
97
|
|
|
old_warn = self.uut.warn |
98
|
|
|
self.uut.warn = assert_warn |
99
|
|
|
self.uut._print_errors(["hello", "\n"]) |
100
|
|
|
self.uut.warn = old_warn |
101
|
|
|
|
102
|
|
|
def test_gives_corrected(self): |
103
|
|
|
self.uut.gives_corrected = True |
104
|
|
|
out = tuple(self.uut.process_output(["a", "b"], "filename", ["a", "b"])) |
105
|
|
|
self.assertEqual((), out) |
106
|
|
|
out = tuple(self.uut.process_output(["a", "b"], "filename", ["a"])) |
107
|
|
|
self.assertEqual(len(out), 1) |
108
|
|
|
|
109
|
|
|
def test_check_prerequisites(self): |
110
|
|
|
old_binary = Lint.executable |
111
|
|
|
invalid_binary = "invalid_binary_which_doesnt_exist" |
112
|
|
|
Lint.executable = invalid_binary |
113
|
|
|
|
114
|
|
|
self.assertEqual(Lint.check_prerequisites(), |
115
|
|
|
"'{}' is not installed.".format(invalid_binary)) |
116
|
|
|
|
117
|
|
|
# "echo" is existent on nearly all platforms. |
118
|
|
|
Lint.executable = "echo" |
119
|
|
|
self.assertTrue(Lint.check_prerequisites()) |
120
|
|
|
|
121
|
|
|
Lint.executable = old_binary |
122
|
|
|
|
123
|
|
|
old_command = Lint.prerequisite_command |
124
|
|
|
|
125
|
|
|
Lint.prerequisite_command = ["command_which_doesnt_exist"] |
126
|
|
|
self.assertEqual(Lint.check_prerequisites(), Lint.prerequisite_fail_msg) |
127
|
|
|
|
128
|
|
|
Lint.prerequisite_command = "command_which_isnt_a_list" |
129
|
|
|
self.assertRaises(TypeError, Lint.check_prerequisites) |
130
|
|
|
|
131
|
|
|
Lint.prerequisite_command = ["cd", |
132
|
|
|
os.path.join('non', 'existent', 'path')] |
133
|
|
|
self.assertEqual(Lint.check_prerequisites(), Lint.prerequisite_fail_msg) |
134
|
|
|
|
135
|
|
|
Lint.prerequisite_command = ["echo", "abc"] |
136
|
|
|
self.assertTrue(Lint.check_prerequisites()) |
137
|
|
|
|
138
|
|
|
Lint.prerequisite_command = old_command |
139
|
|
|
|
140
|
|
|
def test_config_file_generator(self): |
141
|
|
|
self.uut.executable = "echo" |
142
|
|
|
self.uut.arguments = "-c {config_file}" |
143
|
|
|
|
144
|
|
|
self.assertEqual( |
145
|
|
|
self.uut._create_command(config_file="configfile").strip(), |
146
|
|
|
"echo -c " + escape_path_argument("configfile")) |
147
|
|
|
|
148
|
|
|
def test_generate_config_file_generator(self): |
149
|
|
|
self.uut.executable = "echo" |
150
|
|
|
self.uut.config_file = lambda: ["config line1"] |
151
|
|
|
config_filename = self.uut.generate_config_file() |
152
|
|
|
self.assertTrue(os.path.isfile(config_filename)) |
153
|
|
|
os.remove(config_filename) |
154
|
|
|
|
155
|
|
|
# To complete coverage of closing the config file and check if any |
156
|
|
|
# errors are thrown there. |
157
|
|
|
self.uut.lint("filename") |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
class EscapePathArgumentTest(unittest.TestCase): |
161
|
|
|
|
162
|
|
|
def test_escape_path_argument_sh(self): |
163
|
|
|
_type = "sh" |
164
|
|
|
self.assertEqual( |
165
|
|
|
escape_path_argument("/home/usr/a-file", _type), |
166
|
|
|
"/home/usr/a-file") |
167
|
|
|
self.assertEqual( |
168
|
|
|
escape_path_argument("/home/usr/a-dir/", _type), |
169
|
|
|
"/home/usr/a-dir/") |
170
|
|
|
self.assertEqual( |
171
|
|
|
escape_path_argument("/home/us r/a-file with spaces.bla", |
172
|
|
|
_type), |
173
|
|
|
"'/home/us r/a-file with spaces.bla'") |
174
|
|
|
self.assertEqual( |
175
|
|
|
escape_path_argument("/home/us r/a-dir with spaces/x/", |
176
|
|
|
_type), |
177
|
|
|
"'/home/us r/a-dir with spaces/x/'") |
178
|
|
|
self.assertEqual( |
179
|
|
|
escape_path_argument( |
180
|
|
|
"relative something/with cherries and/pickles.delicious", |
181
|
|
|
_type), |
182
|
|
|
"'relative something/with cherries and/pickles.delicious'") |
183
|
|
|
|
184
|
|
|
def test_escape_path_argument_cmd(self): |
185
|
|
|
_type = "cmd" |
186
|
|
|
self.assertEqual( |
187
|
|
|
escape_path_argument("C:\\Windows\\has-a-weird-shell.txt", _type), |
188
|
|
|
"\"C:\\Windows\\has-a-weird-shell.txt\"") |
189
|
|
|
self.assertEqual( |
190
|
|
|
escape_path_argument("C:\\Windows\\lolrofl\\dirs\\", _type), |
191
|
|
|
"\"C:\\Windows\\lolrofl\\dirs\\\"") |
192
|
|
|
self.assertEqual( |
193
|
|
|
escape_path_argument("X:\\Users\\Maito Gai\\fi le.exe", _type), |
194
|
|
|
"\"X:\\Users\\Maito Gai\\fi le.exe\"") |
195
|
|
|
self.assertEqual( |
196
|
|
|
escape_path_argument("X:\\Users\\Mai to Gai\\director y\\", |
197
|
|
|
_type), |
198
|
|
|
"\"X:\\Users\\Mai to Gai\\director y\\\"") |
199
|
|
|
self.assertEqual( |
200
|
|
|
escape_path_argument("X:\\Users\\Maito Gai\\\"seven-gates\".y", |
201
|
|
|
_type), |
202
|
|
|
"\"X:\\Users\\Maito Gai\\^\"seven-gates^\".y\"") |
203
|
|
|
self.assertEqual( |
204
|
|
|
escape_path_argument("System32\\my-custom relative tool\\", |
205
|
|
|
_type), |
206
|
|
|
"\"System32\\my-custom relative tool\\\"") |
207
|
|
|
self.assertEqual( |
208
|
|
|
escape_path_argument("System32\\illegal\" name \"\".curd", _type), |
209
|
|
|
"\"System32\\illegal^\" name ^\"^\".curd\"") |
210
|
|
|
|
211
|
|
|
def test_escape_path_argument_unsupported(self): |
212
|
|
|
_type = "INVALID" |
213
|
|
|
self.assertEqual( |
214
|
|
|
escape_path_argument("/home/usr/a-file", _type), |
215
|
|
|
"/home/usr/a-file") |
216
|
|
|
self.assertEqual( |
217
|
|
|
escape_path_argument("/home/us r/a-file with spaces.bla", _type), |
218
|
|
|
"/home/us r/a-file with spaces.bla") |
219
|
|
|
self.assertEqual( |
220
|
|
|
escape_path_argument("|home|us r|a*dir with spaces|x|", _type), |
221
|
|
|
"|home|us r|a*dir with spaces|x|") |
222
|
|
|
self.assertEqual( |
223
|
|
|
escape_path_argument("system|a|b|c?d", _type), |
224
|
|
|
"system|a|b|c?d") |
225
|
|
|
|