|
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", |
|
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
|
|
|
|
|
65
|
|
|
def test_tagging(self): |
|
66
|
|
|
execute_coala(coala_ci.main, "coala-ci", 'docs', |
|
67
|
|
|
"-S", "tag=test_tag", "-c", self.coafile) |
|
68
|
|
|
tag_path = get_tag_path("test_tag", self.unescaped_coafile) |
|
69
|
|
|
self.assertTrue(os.path.exists(tag_path)) |
|
70
|
|
|
execute_coala(coala_ci.main, "coala-ci", 'docs', |
|
71
|
|
|
"-S", "dtag=test_tag", "-c", self.coafile) |
|
72
|
|
|
self.assertFalse(os.path.exists(tag_path)) |
|
73
|
|
|
|
|
74
|
|
|
def test_fail_acquire_settings(self): |
|
75
|
|
|
retval, output = execute_coala(coala_ci.main, |
|
76
|
|
|
"coala-ci", |
|
77
|
|
|
"-b", |
|
78
|
|
|
'SpaceConsistencyBear', |
|
79
|
|
|
'-c', |
|
80
|
|
|
os.devnull) |
|
81
|
|
|
self.assertIn("During execution, we found that some", output) |
|
82
|
|
|
|
|
83
|
|
|
def test_coala_delete_orig(self): |
|
84
|
|
|
with TemporaryDirectory() as tempdir,\ |
|
85
|
|
|
NamedTemporaryFile(suffix='.orig', |
|
86
|
|
|
dir=tempdir, |
|
87
|
|
|
delete=False) as orig_file,\ |
|
88
|
|
|
make_temp(suffix='.coafile', prefix='', dir=tempdir) as coafile,\ |
|
89
|
|
|
make_temp(dir=tempdir) as unrelated_file: |
|
90
|
|
|
orig_file.close() |
|
91
|
|
|
execute_coala(coala_ci.main, "coala-ci", |
|
92
|
|
|
"-c", re.escape(coafile)) |
|
93
|
|
|
self.assertFalse(os.path.isfile(orig_file.name)) |
|
94
|
|
|
self.assertTrue(os.path.isfile(unrelated_file)) |
|
95
|
|
|
|