Failed Conditions
Pull Request — master (#1451)
by Abdeali
01:45
created

coalib.tests.coalaTest.test_find_no_issues()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

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