1
|
|
|
import sys |
2
|
|
|
import os |
3
|
|
|
import unittest |
4
|
|
|
import re |
5
|
|
|
import tempfile |
6
|
|
|
import subprocess |
7
|
|
|
|
8
|
|
|
if sys.version_info < (3, 4): |
9
|
|
|
import imp as importlib |
|
|
|
|
10
|
|
|
else: |
11
|
|
|
import importlib |
|
|
|
|
12
|
|
|
|
13
|
|
|
sys.path.insert(0, ".") |
14
|
|
|
from coalib.misc.ContextManagers import retrieve_stdout |
|
|
|
|
15
|
|
|
from coalib import coala_ci |
|
|
|
|
16
|
|
|
from coalib.settings import ConfigurationGathering |
|
|
|
|
17
|
|
|
from coalib.output.Tagging import get_tag_path |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def execute_coala_ci(args): |
21
|
|
|
""" |
22
|
|
|
Executes the coala-ci main function with the given argument string. |
23
|
|
|
|
24
|
|
|
:param args: A list of arguments to pass to coala. |
25
|
|
|
:return: A tuple holding the return value first and the stdout output |
26
|
|
|
as second element. |
27
|
|
|
""" |
28
|
|
|
stdout_file = tempfile.TemporaryFile() |
29
|
|
|
proc = subprocess.Popen([sys.executable, "-m", "coala.coala_ci"] + |
|
|
|
|
30
|
|
|
list(args), |
31
|
|
|
stdout=stdout_file, |
32
|
|
|
stderr=subprocess.STDOUT) |
33
|
|
|
retval = proc.wait() |
34
|
|
|
stdout_file.seek(0) |
35
|
|
|
output = stdout_file.read().decode(sys.stdout.encoding, |
36
|
|
|
errors="replace") |
37
|
|
|
return retval, output |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class coalaTest(unittest.TestCase): |
41
|
|
|
|
42
|
|
|
def setUp(self): |
43
|
|
|
self.old_argv = sys.argv |
44
|
|
|
self.unescaped_coafile = os.path.abspath("./.coafile") |
45
|
|
|
self.coafile = re.escape(self.unescaped_coafile) |
46
|
|
|
|
47
|
|
|
def tearDown(self): |
48
|
|
|
sys.argv = self.old_argv |
49
|
|
|
|
50
|
|
|
def test_nonexistent(self): |
51
|
|
|
retval, output = execute_coala_ci(("-c", "nonex", "test")) |
52
|
|
|
self.assertRegex( |
53
|
|
|
output, |
54
|
|
|
".*\\[ERROR\\].*The requested coafile '.*' does not exist.\n") |
55
|
|
|
|
56
|
|
|
def test_find_no_issues(self): |
57
|
|
|
retval, output = execute_coala_ci(('docs', '-c', self.coafile)) |
58
|
|
|
self.assertRegex(output, |
59
|
|
|
"(.*Unable to collect bears from.*PyLintBear.*)?", |
60
|
|
|
"coala-ci output should be empty when running " |
61
|
|
|
"over its own code.") |
62
|
|
|
self.assertEqual(retval, |
63
|
|
|
0, |
64
|
|
|
"coala-ci must return zero when running over its " |
65
|
|
|
"own code.") |
66
|
|
|
|
67
|
|
|
def test_find_issues(self): |
68
|
|
|
retval, output = execute_coala_ci(("todos", "-c", self.coafile)) |
69
|
|
|
self.assertRegex(output, |
70
|
|
|
"(.*Unable to collect bears from.*PyLintBear.*)?", |
71
|
|
|
"coala-ci output should be empty when running " |
72
|
|
|
"over its own code. (Target section: todos)") |
73
|
|
|
self.assertNotEqual(retval, |
74
|
|
|
0, |
75
|
|
|
"coala-ci must return nonzero when running over " |
76
|
|
|
"its own code. (Target section: todos)") |
77
|
|
|
|
78
|
|
|
def test_tagging(self): |
79
|
|
|
execute_coala_ci(('docs', "-S", "tag=test_tag", "-c", self.coafile)) |
80
|
|
|
tag_path = get_tag_path("test_tag", self.unescaped_coafile) |
81
|
|
|
self.assertTrue(os.path.exists(tag_path)) |
82
|
|
|
execute_coala_ci(('docs', "-S", "dtag=test_tag", "-c", self.coafile)) |
83
|
|
|
self.assertFalse(os.path.exists(tag_path)) |
84
|
|
|
|
85
|
|
|
def test_fail_acquire_settings(self): |
86
|
|
|
retval, output = execute_coala_ci(( |
87
|
|
|
"-b", 'SpaceConsistencyBear', '-c', os.devnull)) |
88
|
|
|
self.assertIn("During execution, we found that some", output) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
if __name__ == '__main__': |
92
|
|
|
unittest.main(verbosity=2) |
93
|
|
|
|