Completed
Pull Request — master (#2304)
by Lasse
02:02
created

coalaTest.test_show_all_bears()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
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 LineCountTestBear
0 ignored issues
show
Unused Code introduced by
Unused LineCountTestBear imported from tests.test_bears.LineCountTestBear
Loading history...
11
from tests.test_bears.internal_folder.SpaceConsistencyTestBear import (
0 ignored issues
show
Unused Code introduced by
Unused SpaceConsistencyTestBear imported from tests.test_bears.internal_folder.SpaceConsistencyTestBear
Loading history...
12
    SpaceConsistencyTestBear)
13
from tests.test_bears.JavaTestBear import JavaTestBear
0 ignored issues
show
Unused Code introduced by
Unused JavaTestBear imported from tests.test_bears.JavaTestBear
Loading history...
14
from tests.TestUtilities import execute_coala, bear_test_module
15
16
17
class coalaTest(unittest.TestCase):
18
19
    def setUp(self):
20
        self.old_argv = sys.argv
21
22
    def tearDown(self):
23
        sys.argv = self.old_argv
24
25
    def test_coala(self):
26
        with bear_test_module(), \
27
                prepare_file(["#fixme"], None) as (lines, filename):
28
            retval, output = execute_coala(
29
                             coala.main,
30
                            "coala", "-c", os.devnull,
31
                            "-f", re.escape(filename),
32
                            "-b", "LineCountTestBear")
33
            self.assertIn("This file has 1 lines.",
34
                          output,
35
                          "The output should report count as 1 lines")
36
37
    def test_did_nothing(self):
38
        retval, output = execute_coala(coala.main, "coala", "-c", os.devnull,
39
                                       "-S", "default.enabled=false")
40
        self.assertEqual(retval, 0)
41
        self.assertIn("No existent section was targeted or enabled", output)
42
43
    def test_show_all_bears(self):
44
        with bear_test_module():
45
            retval, output = execute_coala(coala.main, "coala", "-B")
46
            self.assertEqual(retval, 0)
47
            self.assertEqual(len(output.splitlines()), 4)
48
49
    def test_show_language_bears(self):
50
        with bear_test_module():
51
            retval, output = execute_coala(
52
                coala.main, "coala", "-B", "-l", "java")
53
            self.assertEqual(retval, 0)
54
            self.assertEqual(len(output.splitlines()), 2)
55
56
    @unittest.mock.patch('coalib.parsing.DefaultArgParser.get_all_bears_names')
57
    @unittest.mock.patch('coalib.collecting.Collectors.icollect_bears')
58
    def test_version_conflict_in_collecting_bears(self, import_fn, _):
59
        with bear_test_module():
60
            import_fn.side_effect = VersionConflict("msg1", "msg2")
61
            retval, output = execute_coala(coala.main, "coala", "-B")
62
            self.assertEqual(retval, 13)
63
            self.assertIn(("There is a conflict in the version of a "
64
                           "dependency you have installed"), output)
65
            self.assertIn("pip install msg2", output)  # Check recommendation
66
67
    @unittest.mock.patch('coalib.collecting.Collectors._import_bears')
68
    def test_unimportable_bear(self, import_fn):
69
        with bear_test_module():
70
            import_fn.side_effect = SyntaxError
71
            retval, output = execute_coala(coala.main, "coala", "-B")
72
            self.assertEqual(retval, 0)
73
            self.assertIn("Unable to collect bears from", output)
74
75
            import_fn.side_effect = VersionConflict("msg1", "msg2")
76
            retval, output = execute_coala(coala.main, "coala", "-B")
77
            # Note that bear version conflicts don't give exitcode=13,
78
            # they just give a warning with traceback in log_level debug.
79
            self.assertEqual(retval, 0)
80
            self.assertRegex(output,
81
                             "Unable to collect bears from .* because there "
82
                             "is a conflict with the version of a dependency "
83
                             "you have installed")
84
            self.assertIn("pip install msg2", output)  # Check recommendation
85