Completed
Pull Request — master (#1451)
by Abdeali
01:28
created

coalib.tests.coalaJSONTest.setUp()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 4
rs 10
1
import sys
2
import os
3
import unittest
4
import re
5
import json
6
7
from coalib import coala_json
8
from coalib.tests.TestUtilities import execute_coala
9
10
11
class coalaJSONTest(unittest.TestCase):
12
13
    def setUp(self):
14
        self.old_argv = sys.argv
15
        self.unescaped_coafile = os.path.abspath("./.coafile")
16
        self.coafile = re.escape(self.unescaped_coafile)
17
18
    def tearDown(self):
19
        sys.argv = self.old_argv
20
21
    def test_nonexistent(self):
22
        retval, output = execute_coala(
23
            coala_json.main, "coala-json", "-c", 'nonex', "test")
24
        output = json.loads(output)
25
        self.assertRegex(
26
            output["logs"][0]["message"],
27
            "The requested coafile '.*' does not exist.")
28
29
    def test_find_issues(self):
30
        retval, output = execute_coala(
31
            coala_json.main, "coala-json", "todos", "-c",
32
            self.coafile)
33
        output = json.loads(output)
34
        self.assertRegex(output["results"]["todos"][0]["message"],
35
                         r'The line contains the keyword `# \w+`.',
36
                         "coala-json output should be empty when running "
37
                         "over its own code. (Target section: todos)")
38
        self.assertNotEqual(retval,
39
                            0,
40
                            "coala-json must return nonzero when running over "
41
                            "its own code. (Target section: todos)")
42
43
    def test_fail_acquire_settings(self):
44
        retval, output = execute_coala(
45
            coala_json.main, 'coala-json', '-b',
46
            'SpaceConsistencyBear', '-c', os.devnull)
47
        output = json.loads(output)
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)
53
54
    def test_version(self):
55
        retval, output = execute_coala(coala_json.main, 'coala-json', '-v')
56
        self.assertEquals(retval, 0)
57
        self.assertNotIn("{", output)
58
59
    def test_text_logs(self):
60
        retval, output = execute_coala(
61
            coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex')
62
        self.assertRegex(
63
            output,
64
            ".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n")
65