Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:04
created

coalaJSONTest.test_find_issues()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 12
rs 9.4285
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
The variable unittest does not seem to be defined.
Loading history...
13
14
    def setUp(self):
15
        self.old_argv = sys.argv
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sys does not seem to be defined.
Loading history...
16
17
    def tearDown(self):
18
        sys.argv = self.old_argv
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
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
The variable coala_json does not seem to be defined.
Loading history...
23
        output = json.loads(output)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable output does not seem to be defined.
Loading history...
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
The variable filename does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable lines does not seem to be defined.
Loading history...
31
            retval, output = execute_coala(coala_json.main, "coala-json",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable coala_json does not seem to be defined.
Loading history...
32
                                           "-c", os.devnull,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable os does not seem to be defined.
Loading history...
33
                                           "-b", "LineCountTestBear",
34
                                           "-f", re.escape(filename))
35
            output = json.loads(output)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable output does not seem to be defined.
Loading history...
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
The variable retval does not seem to be defined.
Loading history...
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
The variable coala_json does not seem to be defined.
Loading history...
45
                                           '-c', os.devnull,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable os does not seem to be defined.
Loading history...
46
                                           '-b', 'SpaceConsistencyTestBear')
47
            output = json.loads(output)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable output does not seem to be defined.
Loading history...
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
The variable found does not seem to be defined.
Loading history...
53
54
    def test_version(self):
55
        with self.assertRaises(SystemExit):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SystemExit does not seem to be defined.
Loading history...
56
            execute_coala(coala_json.main, 'coala-json', '-v')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable coala_json does not seem to be defined.
Loading history...
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
The variable coala_json does not seem to be defined.
Loading history...
61
        self.assertRegex(
62
            output,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable output does not seem to be defined.
Loading history...
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
The variable filename does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable lines does not seem to be defined.
Loading history...
67
            retval, output = execute_coala(coala_json.main, "coala-json",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable coala_json does not seem to be defined.
Loading history...
68
                                           "-c", os.devnull,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable os does not seem to be defined.
Loading history...
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
The variable fp does not seem to be defined.
Loading history...
79
            data = json.load(fp)
80
81
        output = json.loads(output)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable output does not seem to be defined.
Loading history...
82
83
        self.assertEqual(data['logs'][0]['log_level'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable data does not seem to be defined.
Loading history...
84
                         output['logs'][0]['log_level'])
85
        os.remove('file.json')
86