|
1
|
|
|
import os |
|
2
|
|
|
import unittest |
|
3
|
|
|
|
|
4
|
|
|
from bears.c_languages.IndentBear import IndentBear |
|
5
|
|
|
from bears.tests.BearTestHelper import generate_skip_decorator |
|
6
|
|
|
from coalib.bearlib.abstractions.Lint import Lint |
|
7
|
|
|
from coalib.misc.Shell import escape_path_argument |
|
8
|
|
|
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY |
|
9
|
|
|
from coalib.results.SourceRange import SourceRange |
|
10
|
|
|
from coalib.settings.Section import Section |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class LintTest(unittest.TestCase): |
|
14
|
|
|
|
|
15
|
|
|
def setUp(self): |
|
16
|
|
|
section = Section("some_name") |
|
17
|
|
|
self.uut = Lint(section, None) |
|
18
|
|
|
|
|
19
|
|
|
def test_invalid_output(self): |
|
20
|
|
|
out = list(self.uut.process_output( |
|
21
|
|
|
["1.0|0: Info message\n", |
|
22
|
|
|
"2.2|1: Normal message\n", |
|
23
|
|
|
"3.4|2: Major message\n"], |
|
24
|
|
|
"a/file.py", |
|
25
|
|
|
['original_file_lines_placeholder'])) |
|
26
|
|
|
self.assertEqual(len(out), 3) |
|
27
|
|
|
self.assertEqual(out[0].origin, "Lint") |
|
28
|
|
|
|
|
29
|
|
|
self.assertEqual(out[0].affected_code[0], |
|
30
|
|
|
SourceRange.from_values("a/file.py", 1, 0)) |
|
31
|
|
|
self.assertEqual(out[0].severity, RESULT_SEVERITY.INFO) |
|
32
|
|
|
self.assertEqual(out[0].message, "Info message") |
|
33
|
|
|
|
|
34
|
|
|
self.assertEqual(out[1].affected_code[0], |
|
35
|
|
|
SourceRange.from_values("a/file.py", 2, 2)) |
|
36
|
|
|
self.assertEqual(out[1].severity, RESULT_SEVERITY.NORMAL) |
|
37
|
|
|
self.assertEqual(out[1].message, "Normal message") |
|
38
|
|
|
|
|
39
|
|
|
self.assertEqual(out[2].affected_code[0], |
|
40
|
|
|
SourceRange.from_values("a/file.py", 3, 4)) |
|
41
|
|
|
self.assertEqual(out[2].severity, RESULT_SEVERITY.MAJOR) |
|
42
|
|
|
self.assertEqual(out[2].message, "Major message") |
|
43
|
|
|
|
|
44
|
|
|
def test_custom_regex(self): |
|
45
|
|
|
self.uut.output_regex = (r'(?P<origin>\w+)\|' |
|
46
|
|
|
r'(?P<line>\d+)\.(?P<column>\d+)\|' |
|
47
|
|
|
r'(?P<end_line>\d+)\.(?P<end_column>\d+)\|' |
|
48
|
|
|
r'(?P<severity>\d+): (?P<message>.*)') |
|
49
|
|
|
self.uut.severity_map = {"I": RESULT_SEVERITY.INFO} |
|
50
|
|
|
out = list(self.uut.process_output( |
|
51
|
|
|
["info_msg|1.0|2.3|0: Info message\n"], |
|
52
|
|
|
'a/file.py', |
|
53
|
|
|
['original_file_lines_placeholder'])) |
|
54
|
|
|
self.assertEqual(len(out), 1) |
|
55
|
|
|
self.assertEqual(out[0].affected_code[0].start.line, 1) |
|
56
|
|
|
self.assertEqual(out[0].affected_code[0].start.column, 0) |
|
57
|
|
|
self.assertEqual(out[0].affected_code[0].end.line, 2) |
|
58
|
|
|
self.assertEqual(out[0].affected_code[0].end.column, 3) |
|
59
|
|
|
self.assertEqual(out[0].severity, RESULT_SEVERITY.INFO) |
|
60
|
|
|
self.assertEqual(out[0].origin, 'Lint (info_msg)') |
|
61
|
|
|
|
|
62
|
|
|
def test_valid_output(self): |
|
63
|
|
|
out = list(self.uut.process_output( |
|
64
|
|
|
["Random line that shouldn't be captured\n", |
|
65
|
|
|
"*************\n"], |
|
66
|
|
|
'a/file.py', |
|
67
|
|
|
['original_file_lines_placeholder'])) |
|
68
|
|
|
self.assertEqual(len(out), 0) |
|
69
|
|
|
|
|
70
|
|
|
@generate_skip_decorator(IndentBear) |
|
71
|
|
|
def test_stdin_input(self): |
|
72
|
|
|
self.uut.executable = IndentBear.executable |
|
73
|
|
|
self.uut.use_stdin = True |
|
74
|
|
|
self.uut.use_stderr = False |
|
75
|
|
|
self.uut.process_output = lambda output, filename, file: output |
|
76
|
|
|
|
|
77
|
|
|
input_file = ["int main(){return 0;}"] |
|
78
|
|
|
out = self.uut.lint(file=input_file) |
|
79
|
|
|
self.assertEqual(out, |
|
80
|
|
|
('int\n', 'main ()\n', '{\n', ' return 0;\n', '}\n')) |
|
81
|
|
|
|
|
82
|
|
|
def test_missing_binary(self): |
|
83
|
|
|
old_binary = Lint.executable |
|
84
|
|
|
invalid_binary = "invalid_binary_which_doesnt_exist" |
|
85
|
|
|
Lint.executable = invalid_binary |
|
86
|
|
|
|
|
87
|
|
|
self.assertEqual(Lint.check_prerequisites(), |
|
88
|
|
|
"'{}' is not installed.".format(invalid_binary)) |
|
89
|
|
|
|
|
90
|
|
|
# "echo" is existent on nearly all platforms. |
|
91
|
|
|
Lint.executable = "echo" |
|
92
|
|
|
self.assertTrue(Lint.check_prerequisites()) |
|
93
|
|
|
|
|
94
|
|
|
del Lint.executable |
|
95
|
|
|
self.assertTrue(Lint.check_prerequisites()) |
|
96
|
|
|
|
|
97
|
|
|
Lint.executable = old_binary |
|
98
|
|
|
|
|
99
|
|
|
def test_config_file_generator(self): |
|
100
|
|
|
self.uut.executable = "echo" |
|
101
|
|
|
self.uut.arguments = "-c {config_file}" |
|
102
|
|
|
|
|
103
|
|
|
self.assertEqual( |
|
104
|
|
|
self.uut._create_command("filename", config_file="configfile"), |
|
105
|
|
|
"echo -c configfile " + escape_path_argument("filename")) |
|
106
|
|
|
|
|
107
|
|
|
self.uut.config_file = lambda: ["config line1"] |
|
108
|
|
|
config_filename = self.uut.generate_config_file() |
|
109
|
|
|
self.assertTrue(os.path.isfile(config_filename)) |
|
110
|
|
|
os.remove(config_filename) |
|
111
|
|
|
|
|
112
|
|
|
# To complete coverage of closing the config file. |
|
113
|
|
|
self.uut.lint("filename") |
|
114
|
|
|
|