Completed
Pull Request — master (#2469)
by
unknown
01:50
created

coalaJSONTest.test_show_bears_attributes()   A

Complexity

Conditions 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
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
            retval, output = execute_coala(
74
                coala_json.main, 'coala-json', '-p', 'java')
75
            self.assertEqual(retval, 0)
76
            output = json.loads(output)
77
            self.assertEqual(len(output["capabilities Can detect/Can fix"]), 1)
78
79
    def test_show_bears_attributes(self):
80
        with bear_test_module():
81
            retval, output = execute_coala(coala_json.main, 'coala-json', '-B')
82
            self.assertEqual(retval, 0)
83
            output = json.loads(output)
84
            # Get JavaTestBear
85
            bear = ([bear for bear in output["bears"]
86
                     if bear["name"] == "JavaTestBear"][0])
87
            self.assertTrue(bear, "JavaTestBear was not found.")
88
            self.assertEqual(bear["LANGUAGES"], ["java"])
89
            self.assertEqual(bear["LICENSE"], "AGPL-3.0")
90
            self.assertEqual(bear["metadata"]["desc"],
91
                             "Bear to test that collecting of languages works."
92
                             )
93
            self.assertTrue(bear["metadata"]["optional_params"])
94
            self.assertFalse(bear["metadata"]["non_optional_params"])
95
96
    @unittest.mock.patch('coalib.parsing.DefaultArgParser.get_all_bears_names')
97
    @unittest.mock.patch('coalib.collecting.Collectors.icollect_bears')
98
    def test_version_conflict_in_collecting_bears(self, import_fn, _):
99
        with bear_test_module():
100
            import_fn.side_effect = VersionConflict("msg1", "msg2")
101
            retval, _ = execute_coala(coala_json.main, 'coala-json', '-B')
102
            self.assertEqual(retval, 13)
103
104
    def test_version(self):
105
        with self.assertRaises(SystemExit):
106
            execute_coala(coala_json.main, 'coala-json', '-v')
107
108
    def test_text_logs(self):
109
        retval, output = execute_coala(
110
            coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex')
111
        self.assertRegex(
112
            output,
113
            ".*\\[ERROR\\].*The requested coafile '.*' does not exist. .+\n")
114
115
    def test_output_file(self):
116
        with prepare_file(["#todo this is todo"], None) as (lines, filename):
117
            retval, output = execute_coala(coala_json.main, "coala-json",
118
                                           "-c", os.devnull,
119
                                           "-b", "LineCountTestBear",
120
                                           "-f", re.escape(filename))
121
            exp_retval, exp_output = execute_coala(coala_json.main,
122
                                                   "coala-json",
123
                                                   "-c", os.devnull,
124
                                                   "-b", "LineCountTestBear",
125
                                                   "-f", re.escape(filename),
126
                                                   "-o", "file.json")
127
128
        with open('file.json') as fp:
129
            data = json.load(fp)
130
131
        output = json.loads(output)
132
133
        self.assertEqual(data['logs'][0]['log_level'],
134
                         output['logs'][0]['log_level'])
135
        os.remove('file.json')
136