Completed
Pull Request — master (#2443)
by
unknown
01:55
created

coalaTest.test_unimportable_bear()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 18
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
            # 4 bears plus 1 line holding the closing colour escape sequence
44
            self.assertEqual(len(output.strip().splitlines()), 5)
45
46
    def test_show_language_bears(self):
47
        with bear_test_module():
48
            retval, output = execute_coala(
49
                coala.main, "coala", "-B", "-l", "java")
50
            self.assertEqual(retval, 0)
51
            # 2 bears plus 1 line holding the closing colour escape sequence
52
            self.assertEqual(len(output.splitlines()), 3)
53
54
    def test_show_capabilities_with_supported_language(self):
55
        with bear_test_module():
56
            retval, output = execute_coala(
57
                coala.main, "coala", "-p", "R")
58
            self.assertEqual(retval, 0)
59
            self.assertEqual(len(output.splitlines()), 2)
60
61
    @unittest.mock.patch('coalib.parsing.DefaultArgParser.get_all_bears_names')
62
    @unittest.mock.patch('coalib.collecting.Collectors.icollect_bears')
63
    def test_version_conflict_in_collecting_bears(self, import_fn, _):
64
        with bear_test_module():
65
            import_fn.side_effect = VersionConflict("msg1", "msg2")
66
            retval, output = execute_coala(coala.main, "coala", "-B")
67
            self.assertEqual(retval, 13)
68
            self.assertIn(("There is a conflict in the version of a "
69
                           "dependency you have installed"), output)
70
            self.assertIn("pip install msg2", output)  # Check recommendation
71
72
    @unittest.mock.patch('coalib.collecting.Collectors._import_bears')
73
    def test_unimportable_bear(self, import_fn):
74
        with bear_test_module():
75
            import_fn.side_effect = SyntaxError
76
            retval, output = execute_coala(coala.main, "coala", "-B")
77
            self.assertEqual(retval, 0)
78
            self.assertIn("Unable to collect bears from", output)
79
80
            import_fn.side_effect = VersionConflict("msg1", "msg2")
81
            retval, output = execute_coala(coala.main, "coala", "-B")
82
            # Note that bear version conflicts don't give exitcode=13,
83
            # they just give a warning with traceback in log_level debug.
84
            self.assertEqual(retval, 0)
85
            self.assertRegex(output,
86
                             "Unable to collect bears from .* because there "
87
                             "is a conflict with the version of a dependency "
88
                             "you have installed")
89
            self.assertIn("pip install msg2", output)  # Check recommendation
90