1
|
|
|
import os |
2
|
|
|
import re |
3
|
|
|
import sys |
4
|
|
|
import unittest |
5
|
|
|
from tempfile import NamedTemporaryFile, TemporaryDirectory |
6
|
|
|
|
7
|
|
|
from pyprint.NullPrinter import NullPrinter |
8
|
|
|
|
9
|
|
|
from coalib import coala_ci |
10
|
|
|
from coalib.misc.ContextManagers import make_temp, prepare_file |
11
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
12
|
|
|
from coalib.output.Tagging import get_tag_path |
13
|
|
|
from tests.TestUtilities import bear_test_module, execute_coala |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class coalaCITest(unittest.TestCase): |
|
|
|
|
17
|
|
|
|
18
|
|
|
def setUp(self): |
19
|
|
|
self.old_argv = sys.argv |
|
|
|
|
20
|
|
|
self.unescaped_coafile = os.path.abspath("./.coafile") |
21
|
|
|
self.coafile = re.escape(self.unescaped_coafile) |
|
|
|
|
22
|
|
|
|
23
|
|
|
def tearDown(self): |
24
|
|
|
sys.argv = self.old_argv |
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_nonexistent(self): |
27
|
|
|
retval, output = execute_coala( |
28
|
|
|
coala_ci.main, "coala-ci", "-c", 'nonex', "test") |
|
|
|
|
29
|
|
|
self.assertRegex( |
30
|
|
|
output, |
|
|
|
|
31
|
|
|
".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n") |
32
|
|
|
|
33
|
|
|
def test_find_no_issues(self): |
34
|
|
|
with bear_test_module(), \ |
35
|
|
|
prepare_file(["#include <a>"], None) as (lines, filename): |
|
|
|
|
36
|
|
|
retval, output = execute_coala(coala_ci.main, "coala-ci", |
|
|
|
|
37
|
|
|
'-c', os.devnull, |
|
|
|
|
38
|
|
|
'-f', re.escape(filename), |
39
|
|
|
'-b', 'SpaceConsistencyTestBear', |
40
|
|
|
"--settings", "use_spaces=True") |
41
|
|
|
self.assertIn("Executing section Default", output) |
|
|
|
|
42
|
|
|
self.assertEqual(retval, 0, |
|
|
|
|
43
|
|
|
"coala-ci must return zero when successful") |
44
|
|
|
|
45
|
|
|
def test_find_issues(self): |
46
|
|
|
with bear_test_module(), \ |
47
|
|
|
prepare_file(["#fixme"], None) as (lines, filename): |
|
|
|
|
48
|
|
|
retval, output = execute_coala(coala_ci.main, "coala-ci", |
|
|
|
|
49
|
|
|
"-c", os.devnull, |
|
|
|
|
50
|
|
|
"-b", "LineCountTestBear", |
51
|
|
|
"-f", re.escape(filename)) |
52
|
|
|
self.assertIn("This file has 1 lines.", |
53
|
|
|
output, |
|
|
|
|
54
|
|
|
"The output should report count as 1 lines") |
55
|
|
|
self.assertNotEqual(retval, 0, |
|
|
|
|
56
|
|
|
"coala-ci was expected to return non-zero") |
57
|
|
|
|
58
|
|
|
def test_fix_patchable_issues(self): |
59
|
|
|
with bear_test_module(), \ |
60
|
|
|
prepare_file(["\t#include <a>"], None) as (lines, filename): |
|
|
|
|
61
|
|
|
retval, output = execute_coala( |
62
|
|
|
coala_ci.main, "coala-ci", |
|
|
|
|
63
|
|
|
"-c", os.devnull, |
|
|
|
|
64
|
|
|
"-f", re.escape(filename), |
65
|
|
|
"-b", "SpaceConsistencyTestBear", |
66
|
|
|
"--settings", "autoapply=true", "use_spaces=True", |
67
|
|
|
"default_actions=SpaceConsistencyTestBear:ApplyPatchAction") |
68
|
|
|
self.assertIn("Applied 'ApplyPatchAction'", output) |
|
|
|
|
69
|
|
|
self.assertEqual(retval, 5, |
|
|
|
|
70
|
|
|
"coala-ci must return exitcode 5 when it " |
71
|
|
|
"autofixes the code.") |
72
|
|
|
|
73
|
|
|
def test_tagging(self): |
74
|
|
|
with bear_test_module(), \ |
75
|
|
|
prepare_file(["\t#include <a>"], None) as (lines, filename): |
|
|
|
|
76
|
|
|
log_printer = LogPrinter(NullPrinter()) |
77
|
|
|
execute_coala(coala_ci.main, "coala-ci", "default", |
|
|
|
|
78
|
|
|
"-c", self.coafile, |
|
|
|
|
79
|
|
|
"-f", re.escape(filename), |
80
|
|
|
"-b", "SpaceConsistencyTestBear", |
81
|
|
|
"-S", "tag=test_tag") |
82
|
|
|
tag_path = get_tag_path("test_tag", |
83
|
|
|
self.unescaped_coafile, |
84
|
|
|
log_printer) |
|
|
|
|
85
|
|
|
self.assertTrue(os.path.exists(tag_path)) |
|
|
|
|
86
|
|
|
execute_coala(coala_ci.main, "coala-ci", "default", |
87
|
|
|
"-c", self.coafile, |
88
|
|
|
"-f", re.escape(filename), |
89
|
|
|
"-b", "SpaceConsistencyTestBear", |
90
|
|
|
"-S", "dtag=test_tag") |
91
|
|
|
self.assertFalse(os.path.exists(tag_path)) |
92
|
|
|
|
93
|
|
|
def test_fail_acquire_settings(self): |
94
|
|
|
with bear_test_module(): |
95
|
|
|
retval, output = execute_coala(coala_ci.main, "coala-ci", |
|
|
|
|
96
|
|
|
"-b", 'SpaceConsistencyTestBear', |
97
|
|
|
'-c', os.devnull) |
|
|
|
|
98
|
|
|
self.assertIn("During execution, we found that some", output) |
|
|
|
|
99
|
|
|
|
100
|
|
|
def test_coala_delete_orig(self): |
101
|
|
|
with TemporaryDirectory() as tempdir,\ |
|
|
|
|
102
|
|
|
NamedTemporaryFile(suffix='.orig', |
103
|
|
|
dir=tempdir, |
104
|
|
|
delete=False) as orig_file,\ |
|
|
|
|
105
|
|
|
make_temp(suffix='.coafile', prefix='', dir=tempdir) as coafile,\ |
|
|
|
|
106
|
|
|
make_temp(dir=tempdir) as unrelated_file: |
|
|
|
|
107
|
|
|
orig_file.close() |
108
|
|
|
|
109
|
|
|
execute_coala(coala_ci.main, "coala-ci", "-S", |
|
|
|
|
110
|
|
|
"project_dir=" + os.path.dirname(coafile)) |
111
|
|
|
self.assertFalse(os.path.isfile(orig_file.name)) |
112
|
|
|
self.assertTrue(os.path.isfile(unrelated_file)) |
113
|
|
|
|