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 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 bear_test_module(), \ |
30
|
|
|
prepare_file(["#fixme"], None) as (lines, filename): |
|
|
|
|
31
|
|
|
retval, output = execute_coala(coala_json.main, "coala-json", |
|
|
|
|
32
|
|
|
"-c", os.devnull, |
|
|
|
|
33
|
|
|
"-b", "LineCountTestBear", |
34
|
|
|
"-f", re.escape(filename)) |
35
|
|
|
output = json.loads(output) |
|
|
|
|
36
|
|
|
self.assertEqual(output["results"]["default"][0]["message"], |
37
|
|
|
"This file has 1 lines.") |
38
|
|
|
self.assertNotEqual(retval, 0, |
|
|
|
|
39
|
|
|
"coala-json must return nonzero when " |
40
|
|
|
"results found") |
41
|
|
|
|
42
|
|
|
def test_fail_acquire_settings(self): |
43
|
|
|
with bear_test_module(): |
44
|
|
|
retval, output = execute_coala(coala_json.main, 'coala-json', |
|
|
|
|
45
|
|
|
'-c', os.devnull, |
|
|
|
|
46
|
|
|
'-b', 'SpaceConsistencyTestBear') |
47
|
|
|
output = json.loads(output) |
|
|
|
|
48
|
|
|
found = False |
49
|
|
|
for msg in output["logs"]: |
50
|
|
|
if "During execution, we found that some" in msg["message"]: |
51
|
|
|
found = True |
52
|
|
|
self.assertTrue(found, "Missing settings not logged") |
|
|
|
|
53
|
|
|
|
54
|
|
|
def test_version(self): |
55
|
|
|
with self.assertRaises(SystemExit): |
|
|
|
|
56
|
|
|
execute_coala(coala_json.main, 'coala-json', '-v') |
|
|
|
|
57
|
|
|
|
58
|
|
|
def test_text_logs(self): |
59
|
|
|
retval, output = execute_coala( |
60
|
|
|
coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex') |
|
|
|
|
61
|
|
|
self.assertRegex( |
62
|
|
|
output, |
|
|
|
|
63
|
|
|
".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n") |
64
|
|
|
|
65
|
|
|
def test_output_file(self): |
66
|
|
|
with prepare_file(["#todo this is todo"], None) as (lines, filename): |
|
|
|
|
67
|
|
|
retval, output = execute_coala(coala_json.main, "coala-json", |
|
|
|
|
68
|
|
|
"-c", os.devnull, |
|
|
|
|
69
|
|
|
"-b", "LineCountTestBear", |
70
|
|
|
"-f", re.escape(filename)) |
71
|
|
|
exp_retval, exp_output = execute_coala(coala_json.main, |
72
|
|
|
"coala-json", |
73
|
|
|
"-c", os.devnull, |
74
|
|
|
"-b", "LineCountTestBear", |
75
|
|
|
"-f", re.escape(filename), |
76
|
|
|
"-o", "file.json") |
77
|
|
|
|
78
|
|
|
with open('file.json') as fp: |
|
|
|
|
79
|
|
|
data = json.load(fp) |
80
|
|
|
|
81
|
|
|
output = json.loads(output) |
|
|
|
|
82
|
|
|
|
83
|
|
|
self.assertEqual(data['logs'][0]['log_level'], |
|
|
|
|
84
|
|
|
output['logs'][0]['log_level']) |
85
|
|
|
os.remove('file.json') |
86
|
|
|
|