Completed
Pull Request — master (#2337)
by
unknown
02:10
created

test_version_conflict_in_collecting_bears()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 7
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
    @unittest.mock.patch('coalib.parsing.DefaultArgParser.get_all_bears_names')
72
    @unittest.mock.patch('coalib.collecting.Collectors.icollect_bears')
73
    def test_version_conflict_in_collecting_bears(self, import_fn, _):
74
        with bear_test_module():
75
            import_fn.side_effect = VersionConflict("msg1", "msg2")
76
            retval, _ = execute_coala(coala_json.main, 'coala-json', '-B')
77
            self.assertEqual(retval, 13)
78
79
    def test_version(self):
80
        with self.assertRaises(SystemExit):
81
            execute_coala(coala_json.main, 'coala-json', '-v')
82
83
    def test_text_logs(self):
84
        retval, output = execute_coala(
85
            coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex')
86
        self.assertRegex(
87
            output,
88
            ".*\\[ERROR\\].*The requested coafile '.*' does not exist. .+\n")
89
90
    def test_output_file(self):
91
        with prepare_file(["#todo this is todo"], None) as (lines, filename):
92
            retval, output = execute_coala(coala_json.main, "coala-json",
93
                                           "-c", os.devnull,
94
                                           "-b", "LineCountTestBear",
95
                                           "-f", re.escape(filename))
96
            exp_retval, exp_output = execute_coala(coala_json.main,
97
                                                   "coala-json",
98
                                                   "-c", os.devnull,
99
                                                   "-b", "LineCountTestBear",
100
                                                   "-f", re.escape(filename),
101
                                                   "-o", "file.json")
102
103
        with open('file.json') as fp:
104
            data = json.load(fp)
105
106
        output = json.loads(output)
107
108
        self.assertEqual(data['logs'][0]['log_level'],
109
                         output['logs'][0]['log_level'])
110
        os.remove('file.json')
111