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

coalib.tests.coalaTest.test_fix_patchable_issues()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 11
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(["pass"], None) as (lines, filename):
56
            retval, output = execute_coala(
57
                coala_ci.main, "coala-ci", "-L", "DEBUG",
58
                "--files", filename, "--bears", "PEP8Bear",
59
                "-S", "autoapply=true", "max_line_length=2")
60
            self.assertEqual(retval,
61
                             5,
62
                             "coala-ci must return exitcode 5 when it "
63
                             "autofixes the code.")
64
            print(output)
65
66
    def test_tagging(self):
67
        execute_coala(coala_ci.main, "coala-ci", 'docs',
68
                      "-S", "tag=test_tag", "-c", self.coafile)
69
        tag_path = get_tag_path("test_tag", self.unescaped_coafile)
70
        self.assertTrue(os.path.exists(tag_path))
71
        execute_coala(coala_ci.main, "coala-ci", 'docs',
72
                      "-S", "dtag=test_tag", "-c", self.coafile)
73
        self.assertFalse(os.path.exists(tag_path))
74
75
    def test_fail_acquire_settings(self):
76
        retval, output = execute_coala(coala_ci.main,
77
                                       "coala-ci",
78
                                       "-b",
79
                                       'SpaceConsistencyBear',
80
                                       '-c',
81
                                       os.devnull)
82
        self.assertIn("During execution, we found that some", output)
83
84
    def test_coala_delete_orig(self):
85
        with TemporaryDirectory() as tempdir,\
86
             NamedTemporaryFile(suffix='.orig',
87
                                dir=tempdir,
88
                                delete=False) as orig_file,\
89
             make_temp(suffix='.coafile', prefix='', dir=tempdir) as coafile,\
90
             make_temp(dir=tempdir) as unrelated_file:
91
            orig_file.close()
92
            execute_coala(coala_ci.main, "coala-ci",
93
                          "-c", re.escape(coafile))
94
            self.assertFalse(os.path.isfile(orig_file.name))
95
            self.assertTrue(os.path.isfile(unrelated_file))
96