1 | import os |
||
2 | import re |
||
3 | import sys |
||
4 | import unittest |
||
5 | import unittest.mock |
||
6 | from pkg_resources import VersionConflict |
||
7 | |||
8 | from coalib import coala |
||
9 | from coalib.misc.ContextManagers import prepare_file |
||
10 | from tests.test_bears.LineCountTestBear import ( |
||
11 | LineCountTestBear) |
||
12 | from tests.TestUtilities import execute_coala, bear_test_module, raise_error |
||
13 | |||
14 | |||
15 | class coalaTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
16 | |||
17 | def setUp(self): |
||
18 | self.old_argv = sys.argv |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
19 | |||
20 | def tearDown(self): |
||
21 | sys.argv = self.old_argv |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
22 | |||
23 | def test_coala(self): |
||
24 | with bear_test_module(), \ |
||
25 | prepare_file(["#fixme"], None) as (lines, filename): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
26 | retval, output = execute_coala( |
||
27 | coala.main, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
28 | "coala", "-c", os.devnull, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
29 | "-f", re.escape(filename), |
||
30 | "-b", "LineCountTestBear") |
||
31 | self.assertIn("This file has 1 lines.", |
||
32 | output, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
33 | "The output should report count as 1 lines") |
||
34 | |||
35 | def test_did_nothing(self): |
||
36 | retval, output = execute_coala(coala.main, "coala", "-c", os.devnull, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
37 | "-S", "default.enabled=false") |
||
38 | self.assertEqual(retval, 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
39 | self.assertIn("No existent section was targeted or enabled", output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
40 | |||
41 | def test_show_bears(self): |
||
42 | with bear_test_module(): |
||
43 | retval, output = execute_coala(coala.main, "coala", "-A") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
44 | self.assertEqual(retval, 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
45 | lines = output.splitlines() |
||
46 | bear_missing_lines = sum(1 for line in lines if "WARNING" in line) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
47 | self.assertEqual(bear_missing_lines, 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
48 | |||
49 | retval, output = execute_coala(coala.main, "coala", "-A") |
||
50 | self.assertEqual(retval, 0) |
||
51 | |||
52 | lines = output.splitlines() |
||
53 | bear_lines = sum(1 for line in lines if line.startswith(" * ")) |
||
54 | self.assertEqual(bear_lines, 3) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
55 | |||
56 | for line in lines: |
||
57 | self.assertNotIn("WARNING", line) |
||
58 | |||
59 | retval, output = execute_coala( |
||
60 | coala.main, "coala", "-B", |
||
61 | "-b", "LineCountTestBear, SpaceConsistencyTestBear", |
||
62 | "-c", os.devnull) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
63 | self.assertEqual(retval, 0) |
||
64 | self.assertIn(LineCountTestBear.run.__doc__.strip(), output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
65 | |||
66 | @unittest.mock.patch('coalib.collecting.Collectors.icollect_bears') |
||
67 | def test_version_conflict_in_collecting_bears(self, import_fn): |
||
68 | with bear_test_module(): |
||
69 | import_fn.side_effect = ( |
||
70 | lambda *args, **kwargs: raise_error(VersionConflict, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
71 | "msg1", "msg2")) |
||
72 | retval, output = execute_coala(coala.main, "coala", "-A") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
73 | self.assertEqual(retval, 13) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
74 | self.assertIn(("There is a conflict in the version of a " |
||
75 | "dependency you have installed"), output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
76 | self.assertIn("pip install msg2", output) # Check recommendation |
||
77 | |||
78 | @unittest.mock.patch('coalib.collecting.Collectors._import_bears') |
||
79 | def test_unimportable_bear(self, import_fn): |
||
80 | with bear_test_module(): |
||
81 | import_fn.side_effect = ( |
||
82 | lambda *args, **kwargs: raise_error(SyntaxError)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
83 | retval, output = execute_coala(coala.main, "coala", "-A") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
84 | self.assertEqual(retval, 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
85 | self.assertIn("Unable to collect bears from", output) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
86 | |||
87 | import_fn.side_effect = ( |
||
88 | lambda *args, **kwargs: raise_error(VersionConflict, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
89 | "msg1", "msg2")) |
||
90 | retval, output = execute_coala(coala.main, "coala", "-A") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
91 | # Note that bear version conflicts don't give exitcode=13, |
||
92 | # they just give a warning with traceback in log_level debug. |
||
93 | self.assertEqual(retval, 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
94 | self.assertRegex(output, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
95 | "Unable to collect bears from .* because there " |
||
96 | "is a conflict with the version of a dependency " |
||
97 | "you have installed") |
||
98 | self.assertIn("pip install msg2", output) # Check recommendation |
||
99 |