Completed
Pull Request — master (#2063)
by Abdeali
02:50 queued 52s
created

coalaTest.test_unimportable_bear()   A

Complexity

Conditions 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
c 2
b 0
f 0
dl 0
loc 21
rs 9.0534
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):
16
17
    def setUp(self):
18
        self.old_argv = sys.argv
19
20
    def tearDown(self):
21
        sys.argv = self.old_argv
22
23
    def test_coala(self):
24
        with bear_test_module(), \
25
                prepare_file(["#fixme"], None) as (lines, filename):
26
            retval, output = execute_coala(
27
                             coala.main,
28
                            "coala", "-c", os.devnull,
29
                            "-f", re.escape(filename),
30
                            "-b", "LineCountTestBear")
31
            self.assertIn("This file has 1 lines.",
32
                          output,
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,
37
                                       "-S", "default.enabled=false")
38
        self.assertEqual(retval, 0)
39
        self.assertIn("No existent section was targeted or enabled", output)
40
41
    def test_show_bears(self):
42
        retval, output = execute_coala(coala.main, "coala", "-A")
43
        self.assertEqual(retval, 0)
44
        lines = output.splitlines()
45
        bear_missing_lines = sum(1 for line in lines if "WARNING" in line)
46
        self.assertEqual(bear_missing_lines, 0)
47
48
        with bear_test_module():
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)
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)
63
            self.assertEqual(retval, 0)
64
            self.assertIn(LineCountTestBear.run.__doc__.strip(), output)
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,
71
                                                    "msg1", "msg2"))
72
            retval, output = execute_coala(coala.main, "coala", "-A")
73
            self.assertEqual(retval, 13)
74
            self.assertIn(("There is a conflict in the version of a "
75
                           "dependency you have installed"), output)
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))
83
            retval, output = execute_coala(coala.main, "coala", "-A")
84
            self.assertEqual(retval, 0)
85
            self.assertIn("Unable to collect bears from", output)
86
87
            import_fn.side_effect = (
88
                lambda *args, **kwargs: raise_error(VersionConflict,
89
                                                    "msg1", "msg2"))
90
            retval, output = execute_coala(coala.main, "coala", "-A")
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)
94
            self.assertRegex(output,
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