Completed
Pull Request — master (#1504)
by Abdeali
01:41
created

coalib.tests.coalaCITest.test_coala_delete_orig()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

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