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