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

coalib.tests.coalaJSONTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %
Metric Value
dl 0
loc 54
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_text_logs() 0 6 1
A test_fail_acquire_settings() 0 10 3
A tearDown() 0 2 1
A setUp() 0 4 1
A test_version() 0 4 1
A test_nonexistent() 0 7 1
A test_find_issues() 0 12 1
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
from coalib.misc import Constants
10
11
12
class coalaJSONTest(unittest.TestCase):
13
14
    def setUp(self):
15
        self.old_argv = sys.argv
16
        self.unescaped_coafile = os.path.abspath("./.coafile")
17
        self.coafile = re.escape(self.unescaped_coafile)
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
        retval, output = execute_coala(
32
            coala_json.main, "coala-json", "todos", "-c",
33
            self.coafile)
34
        output = json.loads(output)
35
        self.assertRegex(output["results"]["todos"][0]["message"],
36
                         r'The line contains the keyword `# \w+`.',
37
                         "coala-json output should be empty when running "
38
                         "over its own code. (Target section: todos)")
39
        self.assertNotEqual(retval,
40
                            0,
41
                            "coala-json must return nonzero when running over "
42
                            "its own code. (Target section: todos)")
43
44
    def test_fail_acquire_settings(self):
45
        retval, output = execute_coala(
46
            coala_json.main, 'coala-json', '-b',
47
            'SpaceConsistencyBear', '-c', os.devnull)
48
        output = json.loads(output)
49
        found = False
50
        for msg in output["logs"]:
51
            if "During execution, we found that some" in msg["message"]:
52
                found = True
53
        self.assertTrue(found)
54
55
    def test_version(self):
56
        retval, output = execute_coala(coala_json.main, 'coala-json', '-v')
57
        self.assertEqual(retval, 0)
58
        self.assertEqual(output.strip(), Constants.VERSION)
59
60
    def test_text_logs(self):
61
        retval, output = execute_coala(
62
            coala_json.main, 'coala-json', '--text-logs', '-c', 'nonex')
63
        self.assertRegex(
64
            output,
65
            ".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n")
66