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): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
13 | |||
14 | def setUp(self): |
||
15 | self.old_argv = sys.argv |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
16 | |||
17 | def tearDown(self): |
||
18 | sys.argv = self.old_argv |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
19 | |||
20 | def test_nonexistent(self): |
||
21 | retval, output = execute_coala( |
||
22 | coala_json.main, "coala-json", "-c", 'nonex', "test") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
23 | output = json.loads(output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
31 | retval, output = execute_coala(coala_json.main, "coala-json", |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
32 | "-c", os.devnull, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
33 | "-b", "LineCountTestBear", |
||
34 | "-f", re.escape(filename)) |
||
35 | output = json.loads(output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
36 | self.assertEqual(output["results"]["default"][0]["message"], |
||
37 | "This file has 1 lines.") |
||
38 | self.assertNotEqual(retval, 0, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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', |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
45 | '-c', os.devnull, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
46 | '-b', 'SpaceConsistencyTestBear') |
||
47 | output = json.loads(output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
53 | |||
54 | def test_version(self): |
||
55 | with self.assertRaises(SystemExit): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
56 | execute_coala(coala_json.main, 'coala-json', '-v') |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
57 | |||
58 | def test_text_logs(self): |
||
59 | retval, output = execute_coala( |
||
60 | coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex') |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
61 | self.assertRegex( |
||
62 | output, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
67 | retval, output = execute_coala(coala_json.main, "coala-json", |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
68 | "-c", os.devnull, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
79 | data = json.load(fp) |
||
80 | |||
81 | output = json.loads(output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
82 | |||
83 | self.assertEqual(data['logs'][0]['log_level'], |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
84 | output['logs'][0]['log_level']) |
||
85 | os.remove('file.json') |
||
86 |