Completed
Pull Request — master (#2469)
by
unknown
02:04
created

coalaJSONTest   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 129
rs 10
wmc 28

13 Methods

Rating   Name   Duplication   Size   Complexity  
A test_find_issues() 0 12 2
A setUp() 0 2 1
A test_nonexistent() 0 7 1
A tearDown() 0 2 1
A test_fail_acquire_settings() 0 11 4
A test_show_language_bears() 0 7 2
A test_show_all_bears() 0 6 2
A test_version() 0 3 2
A test_output_file() 0 21 3
A test_version_conflict_in_collecting_bears() 0 7 2
A test_show_bears_attributes() 0 16 4
A test_text_logs() 0 6 1
A test_show_capabilities() 0 14 3
1
import json
2
import os
3
import re
4
import sys
5
import unittest
6
import unittest.mock
7
from pkg_resources import VersionConflict
8
9
from coalib import coala_json
10
from coalib.misc.ContextManagers import prepare_file
11
from tests.TestUtilities import bear_test_module, execute_coala
12
13
14
class coalaJSONTest(unittest.TestCase):
15
16
    def setUp(self):
17
        self.old_argv = sys.argv
18
19
    def tearDown(self):
20
        sys.argv = self.old_argv
21
22
    def test_nonexistent(self):
23
        retval, output = execute_coala(
24
            coala_json.main, "coala-json", "-c", 'nonex', "test")
25
        output = json.loads(output)
26
        self.assertRegex(
27
            output["logs"][0]["message"],
28
            "The requested coafile '.*' does not exist. .+")
29
30
    def test_find_issues(self):
31
        with bear_test_module(), \
32
                prepare_file(["#fixme"], None) as (lines, filename):
33
            retval, output = execute_coala(coala_json.main, "coala-json",
34
                                           "-c", os.devnull,
35
                                           "-b", "LineCountTestBear",
36
                                           "-f", re.escape(filename))
37
            output = json.loads(output)
38
            self.assertEqual(output["results"]["default"][0]["message"],
39
                             "This file has 1 lines.")
40
            self.assertNotEqual(retval, 0,
41
                                "coala-json must return nonzero when "
42
                                "results found")
43
44
    def test_fail_acquire_settings(self):
45
        with bear_test_module():
46
            retval, output = execute_coala(coala_json.main, 'coala-json',
47
                                           '-c', os.devnull,
48
                                           '-b', 'SpaceConsistencyTestBear')
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, "Missing settings not logged")
55
56
    def test_show_all_bears(self):
57
        with bear_test_module():
58
            retval, output = execute_coala(coala_json.main, 'coala-json', '-B')
59
            self.assertEqual(retval, 0)
60
            output = json.loads(output)
61
            self.assertEqual(len(output["bears"]), 4)
62
63
    def test_show_language_bears(self):
64
        with bear_test_module():
65
            retval, output = execute_coala(
66
                coala_json.main, 'coala-json', '-B', '-l', 'java')
67
            self.assertEqual(retval, 0)
68
            output = json.loads(output)
69
            self.assertEqual(len(output["bears"]), 2)
70
71
    def test_show_capabilities(self):
72
        with bear_test_module():
73
            exp_retval, exp_output = execute_coala(
74
                coala_json.main, 'coala-json', '-p', 'java', '-o', 'file.json')
75
            retval, output = execute_coala(
76
                coala_json.main, 'coala-json', '-p', 'java')
77
            self.assertEqual(retval, 0)
78
            self.assertEqual(exp_retval, 0)
79
            with open('file.json') as fp:
80
                data = json.load(fp)
81
            output = json.loads(output)
82
            self.assertEqual(output, data)
83
            self.assertEqual(len(output["capabilities Can detect/Can fix"]), 1)
84
            os.remove('file.json')
85
86
    def test_show_bears_attributes(self):
87
        with bear_test_module():
88
            retval, output = execute_coala(coala_json.main, 'coala-json', '-B')
89
            self.assertEqual(retval, 0)
90
            output = json.loads(output)
91
            # Get JavaTestBear
92
            bear = ([bear for bear in output["bears"]
93
                     if bear["name"] == "JavaTestBear"][0])
94
            self.assertTrue(bear, "JavaTestBear was not found.")
95
            self.assertEqual(bear["LANGUAGES"], ["java"])
96
            self.assertEqual(bear["LICENSE"], "AGPL-3.0")
97
            self.assertEqual(bear["metadata"]["desc"],
98
                             "Bear to test that collecting of languages works."
99
                             )
100
            self.assertTrue(bear["metadata"]["optional_params"])
101
            self.assertFalse(bear["metadata"]["non_optional_params"])
102
103
    @unittest.mock.patch('coalib.parsing.DefaultArgParser.get_all_bears_names')
104
    @unittest.mock.patch('coalib.collecting.Collectors.icollect_bears')
105
    def test_version_conflict_in_collecting_bears(self, import_fn, _):
106
        with bear_test_module():
107
            import_fn.side_effect = VersionConflict("msg1", "msg2")
108
            retval, _ = execute_coala(coala_json.main, 'coala-json', '-B')
109
            self.assertEqual(retval, 13)
110
111
    def test_version(self):
112
        with self.assertRaises(SystemExit):
113
            execute_coala(coala_json.main, 'coala-json', '-v')
114
115
    def test_text_logs(self):
116
        retval, output = execute_coala(
117
            coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex')
118
        self.assertRegex(
119
            output,
120
            ".*\\[ERROR\\].*The requested coafile '.*' does not exist. .+\n")
121
122
    def test_output_file(self):
123
        with prepare_file(["#todo this is todo"], None) as (lines, filename):
124
            retval, output = execute_coala(coala_json.main, "coala-json",
125
                                           "-c", os.devnull,
126
                                           "-b", "LineCountTestBear",
127
                                           "-f", re.escape(filename))
128
            exp_retval, exp_output = execute_coala(coala_json.main,
129
                                                   "coala-json",
130
                                                   "-c", os.devnull,
131
                                                   "-b", "LineCountTestBear",
132
                                                   "-f", re.escape(filename),
133
                                                   "-o", "file.json")
134
135
        with open('file.json') as fp:
136
            data = json.load(fp)
137
138
        output = json.loads(output)
139
140
        self.assertEqual(data['logs'][0]['log_level'],
141
                         output['logs'][0]['log_level'])
142
        os.remove('file.json')
143