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

coalib.tests.coalaTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %
Metric Value
wmc 11
dl 0
loc 84
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A test_coala_delete_orig() 0 12 2
A test_fail_acquire_settings() 0 8 1
A tearDown() 0 2 1
A test_tagging() 0 8 1
A test_find_issues() 0 10 1
A test_find_no_issues() 0 10 1
A test_fix_patchable_issues() 0 11 2
A setUp() 0 4 1
A test_nonexistent() 0 6 1
1
import sys
2
import os
3
import unittest
4
import re
5
from tempfile import TemporaryDirectory, NamedTemporaryFile
6
7
from bears.c_languages.IndentBear import IndentBear
8
from bears.tests.BearTestHelper import generate_skip_decorator
9
from coalib.misc.ContextManagers import make_temp, prepare_file
10
from coalib import coala_ci
11
from coalib.output.Tagging import get_tag_path
12
from coalib.tests.TestUtilities import execute_coala
13
14
15
class coalaTest(unittest.TestCase):
16
17
    def setUp(self):
18
        self.old_argv = sys.argv
19
        self.unescaped_coafile = os.path.abspath("./.coafile")
20
        self.coafile = re.escape(self.unescaped_coafile)
21
22
    def tearDown(self):
23
        sys.argv = self.old_argv
24
25
    def test_nonexistent(self):
26
        retval, output = execute_coala(
27
            coala_ci.main, "coala-ci", "-c", 'nonex', "test")
28
        self.assertRegex(
29
            output,
30
            ".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n")
31
32
    def test_find_no_issues(self):
33
        retval, output = execute_coala(
34
            coala_ci.main, "coala-ci", 'docs', '-c', self.coafile)
35
        self.assertRegex(output,
36
                         "(.*Unable to collect bears from.*PyLintBear.*)?",
37
                         "coala-ci output should be empty when running "
38
                         "over its own code.")
39
        self.assertEqual(retval,
40
                         0,
41
                         "coala-ci must return zero when running over its "
42
                         "own code.")
43
44
    def test_find_issues(self):
45
        retval, output = execute_coala(
46
            coala_ci.main, "coala-ci", "todos", "-c", self.coafile)
47
        self.assertRegex(output,
48
                         r'The line contains the keyword `# \w+`.',
49
                         "coala-ci output should be empty when running "
50
                         "over its own code. (Target section: todos)")
51
        self.assertNotEqual(retval,
52
                            0,
53
                            "coala-ci must return nonzero when running over "
54
                            "its own code. (Target section: todos)")
55
56
    @generate_skip_decorator(IndentBear)
57
    def test_fix_patchable_issues(self):
58
        with prepare_file(["    #include <a>"], None) as (lines, filename):
59
            bear = "IndentBear"
60
            retval, output = execute_coala(
61
                coala_ci.main, "coala-ci", "-c", os.devnull, "--settings",
62
                "files=" + filename, "bears=" + bear, "autoapply=true",
63
                "default_actions=" + bear + ":ApplyPatchAction")
64
            self.assertEqual(retval,
65
                             5,
66
                             "coala-ci must return exitcode 5 when it "
67
                             "autofixes the code.")
68
69
    def test_tagging(self):
70
        execute_coala(coala_ci.main, "coala-ci", 'docs',
71
                      "-S", "tag=test_tag", "-c", self.coafile)
72
        tag_path = get_tag_path("test_tag", self.unescaped_coafile)
73
        self.assertTrue(os.path.exists(tag_path))
74
        execute_coala(coala_ci.main, "coala-ci", 'docs',
75
                      "-S", "dtag=test_tag", "-c", self.coafile)
76
        self.assertFalse(os.path.exists(tag_path))
77
78
    def test_fail_acquire_settings(self):
79
        retval, output = execute_coala(coala_ci.main,
80
                                       "coala-ci",
81
                                       "-b",
82
                                       'SpaceConsistencyBear',
83
                                       '-c',
84
                                       os.devnull)
85
        self.assertIn("During execution, we found that some", output)
86
87
    def test_coala_delete_orig(self):
88
        with TemporaryDirectory() as tempdir,\
89
             NamedTemporaryFile(suffix='.orig',
90
                                dir=tempdir,
91
                                delete=False) as orig_file,\
92
             make_temp(suffix='.coafile', prefix='', dir=tempdir) as coafile,\
93
             make_temp(dir=tempdir) as unrelated_file:
94
            orig_file.close()
95
            execute_coala(coala_ci.main, "coala-ci",
96
                          "-c", re.escape(coafile))
97
            self.assertFalse(os.path.isfile(orig_file.name))
98
            self.assertTrue(os.path.isfile(unrelated_file))
99