1
|
|
|
import json |
2
|
|
|
import os |
3
|
|
|
import re |
4
|
|
|
import sys |
5
|
|
|
import unittest |
6
|
|
|
|
7
|
|
|
from coalib import coala_json |
8
|
|
|
from coalib.misc.ContextManagers import prepare_file |
9
|
|
|
from coalib.tests.TestUtilities import bear_test_module, execute_coala |
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class coalaJSONTest(unittest.TestCase): |
13
|
|
|
|
14
|
|
|
def setUp(self): |
15
|
|
|
self.old_argv = sys.argv |
16
|
|
|
|
17
|
|
|
def tearDown(self): |
18
|
|
|
sys.argv = self.old_argv |
19
|
|
|
|
20
|
|
|
def test_nonexistent(self): |
21
|
|
|
retval, output = execute_coala( |
22
|
|
|
coala_json.main, "coala-json", "-c", 'nonex', "test") |
23
|
|
|
output = json.loads(output) |
24
|
|
|
self.assertRegex( |
25
|
|
|
output["logs"][0]["message"], |
26
|
|
|
"The requested coafile '.*' does not exist.") |
27
|
|
|
|
28
|
|
|
def test_find_issues(self): |
29
|
|
|
with prepare_file(["#todo this is todo"], None) as (lines, filename): |
30
|
|
|
bear = "KeywordBear" |
31
|
|
|
retval, output = execute_coala(coala_json.main, "coala-json", "-c", |
32
|
|
|
os.devnull, "-S", |
33
|
|
|
"ci_keywords=#TODO", |
34
|
|
|
"cs_keywords=#todo", |
35
|
|
|
"bears=" + bear, |
36
|
|
|
"-f", re.escape(filename)) |
37
|
|
|
output = json.loads(output) |
38
|
|
|
self.assertEqual(output["results"]["default"][0]["message"], |
39
|
|
|
"The line contains the keyword `#todo`.", |
40
|
|
|
"coala-json output should match the keyword #todo") |
41
|
|
|
self.assertNotEqual(retval, |
42
|
|
|
0, "coala-json must return nonzero when " |
43
|
|
|
"matching `#todo` keyword") |
44
|
|
|
|
45
|
|
|
def test_fail_acquire_settings(self): |
46
|
|
|
retval, output = execute_coala( |
47
|
|
|
coala_json.main, 'coala-json', '-b', |
48
|
|
|
'SpaceConsistencyBear', '-c', os.devnull) |
49
|
|
|
output = json.loads(output) |
50
|
|
|
found = False |
51
|
|
|
for msg in output["logs"]: |
52
|
|
|
if "During execution, we found that some" in msg["message"]: |
53
|
|
|
found = True |
54
|
|
|
self.assertTrue(found) |
55
|
|
|
|
56
|
|
|
def test_version(self): |
57
|
|
|
with self.assertRaises(SystemExit): |
58
|
|
|
execute_coala(coala_json.main, 'coala-json', '-v') |
59
|
|
|
|
60
|
|
|
def test_text_logs(self): |
61
|
|
|
retval, output = execute_coala( |
62
|
|
|
coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex') |
63
|
|
|
self.assertRegex( |
64
|
|
|
output, |
65
|
|
|
".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n") |
66
|
|
|
|
67
|
|
|
def test_output_file(self): |
68
|
|
|
with prepare_file(["#todo this is todo"], None) as (lines, filename): |
69
|
|
|
retval, output = execute_coala(coala_json.main, "coala-json", |
70
|
|
|
"-c", os.devnull, |
71
|
|
|
"-b", "LineCountTestBear", |
72
|
|
|
"-f", re.escape(filename)) |
73
|
|
|
expected_retval, expected_output = execute_coala(coala_json.main, "coala-json", |
|
|
|
|
74
|
|
|
"-c", os.devnull, |
75
|
|
|
"-b", "LineCountTestBear", |
76
|
|
|
"-f", re.escape(filename), |
77
|
|
|
"-o", "file.json") |
78
|
|
|
|
79
|
|
|
with open('file.json') as fp: |
80
|
|
|
data = json.load(fp) |
81
|
|
|
|
82
|
|
|
self.assertEqual(data, output) |
83
|
|
|
os.remove('file.json') |
84
|
|
|
|