Completed
Push — master ( 3a6d00...f9e46c )
by
unknown
01:59
created

coalaJSONTest.test_show_all_bears()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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