BadTestBear   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 2 1
A __init__() 0 2 1
1
import multiprocessing
2
import unittest
3
from os.path import abspath
4
5
from coalib.bears.Bear import Bear
6
from coalib.results.Result import Result
7
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
8
from coalib.processes.communication.LogMessage import LogMessage
9
from coalib.settings.Section import Section
10
from coalib.settings.Setting import Setting
11
12
13
class BadTestBear(Bear):
0 ignored issues
show
Unused Code introduced by
This abstract class does not seem to be used anywhere.
Loading history...
14
15
    def __init__(self, section, queue):
16
        Bear.__init__(self, section, queue)
17
18
    def run(self):
19
        raise NotImplementedError
20
21
22
class TestBear(Bear):
23
24
    BEAR_DEPS = {BadTestBear}
25
26
    def __init__(self, section, queue):
27
        Bear.__init__(self, section, queue)
28
29
    def run(self):
30
        self.print("set", "up", delimiter="=")
31
        self.err("teardown")
32
        self.err()
33
34
35
class TypedTestBear(Bear):
36
37
    def __init__(self, section, queue):
38
        Bear.__init__(self, section, queue)
39
        self.was_executed = False
40
41
    def run(self, something: int):
42
        self.was_executed = True
43
        return []
44
45
46
class BearWithPrerequisites(Bear):
47
    prerequisites_fulfilled = True
48
49
    def __init__(self, section, queue, prerequisites_fulfilled):
50
        BearWithPrerequisites.prerequisites_fulfilled = prerequisites_fulfilled
51
        Bear.__init__(self, section, queue)
52
        self.was_executed = False
53
54
    def run(self):
55
        self.was_executed = True
56
        return []
57
58
    @classmethod
59
    def check_prerequisites(cls):
60
        return cls.prerequisites_fulfilled
61
62
63
class BearTest(unittest.TestCase):
64
65
    def setUp(self):
66
        self.queue = multiprocessing.Queue()
67
        self.settings = Section("test_settings")
68
        self.uut = TestBear(self.settings, self.queue)
69
70
    def test_simple_api(self):
71
        self.assertRaises(TypeError, TestBear, self.settings, 2)
72
        self.assertRaises(TypeError, TestBear, None, self.queue)
73
        self.assertRaises(NotImplementedError, self.uut.kind)
74
75
        base = Bear(self.settings, None)
76
        self.assertRaises(NotImplementedError, base.run)
77
        self.assertEqual(base.get_non_optional_settings(), {})
78
79
    def test_message_queue(self):
80
        self.uut.execute()
81
        self.check_message(LOG_LEVEL.DEBUG,
82
                           "Running bear TestBear...")
83
        self.check_message(LOG_LEVEL.DEBUG, "set=up")
84
        self.check_message(LOG_LEVEL.ERROR, "teardown")
85
86
    def test_bad_bear(self):
87
        self.uut = BadTestBear(self.settings, self.queue)
88
        self.uut.execute()
89
        self.check_message(LOG_LEVEL.DEBUG)
90
        self.check_message(LOG_LEVEL.WARNING,
91
                           "Bear BadTestBear failed to run. Take a look at "
92
                           "debug messages (`-L DEBUG`) for further "
93
                           "information.")
94
        # debug message contains custom content, dont test this here
95
        self.queue.get()
96
97
    def test_inconvertible(self):
98
        self.uut = TypedTestBear(self.settings, self.queue)
99
        self.settings.append(Setting("something", "5"))
100
        self.uut.execute()
101
        self.check_message(LOG_LEVEL.DEBUG)
102
        self.assertTrue(self.uut.was_executed)
103
104
        self.settings.append(Setting("something", "nonsense"))
105
        self.uut.was_executed = False
106
        self.uut.execute()
107
        self.check_message(LOG_LEVEL.DEBUG)
108
        self.check_message(LOG_LEVEL.WARNING)
109
        self.assertTrue(self.queue.empty())
110
        self.assertFalse(self.uut.was_executed)
111
112
    def check_message(self, log_level, message=None):
113
        msg = self.queue.get()
114
        self.assertIsInstance(msg, LogMessage)
115
        if message:
116
            self.assertEqual(msg.message, message)
117
118
        self.assertEqual(msg.log_level, log_level, msg)
119
120
    def test_no_queue(self):
121
        uut = TestBear(self.settings, None)
122
        uut.execute()  # No exceptions
123
124
    def test_dependencies(self):
125
        self.assertEqual(Bear.BEAR_DEPS, set())
126
        self.assertEqual(Bear.missing_dependencies([]), set())
127
        self.assertEqual(Bear.missing_dependencies([BadTestBear]), set())
128
129
        self.assertEqual(TestBear.missing_dependencies([]), {BadTestBear})
130
        self.assertEqual(TestBear.missing_dependencies([BadTestBear]), set())
131
        self.assertEqual(TestBear.missing_dependencies([TestBear]),
132
                         {BadTestBear})
133
        self.assertEqual(TestBear.missing_dependencies([TestBear,
134
                                                        BadTestBear]),
135
                         set())
136
137
    def test_check_prerequisites(self):
138
        uut = BearWithPrerequisites(self.settings, self.queue, True)
139
        uut.execute()
140
        self.check_message(LOG_LEVEL.DEBUG)
141
        self.assertTrue(self.queue.empty())
142
        self.assertTrue(uut.was_executed)
143
144
        self.assertRaisesRegex(RuntimeError,
145
                               "The bear BearWithPrerequisites does not "
146
                               "fulfill all requirements\\.",
147
                               BearWithPrerequisites,
148
                               self.settings,
149
                               self.queue,
150
                               False)
151
152
        self.check_message(LOG_LEVEL.WARNING,
153
                           "The bear BearWithPrerequisites does not fulfill "
154
                           "all requirements.")
155
        self.assertTrue(self.queue.empty())
156
157
        self.assertRaisesRegex(RuntimeError,
158
                               "The bear BearWithPrerequisites does not "
159
                               "fulfill all requirements\\. Just because "
160
                               "I want to\\.",
161
                               BearWithPrerequisites,
162
                               self.settings,
163
                               self.queue,
164
                               "Just because I want to.")
165
166
        self.check_message(LOG_LEVEL.WARNING,
167
                           "The bear BearWithPrerequisites does not fulfill "
168
                           "all requirements. Just because I want to.")
169
        self.assertTrue(self.queue.empty())
170
171
    def test_get_config_dir(self):
172
        section = Section("default")
173
        section.append(Setting("files", "**", "/path/to/dir/config"))
174
        uut = TestBear(section, None)
175
        self.assertEqual(uut.get_config_dir(), abspath("/path/to/dir"))
176
177
    def test_new_result(self):
178
        bear = Bear(self.settings, None)
179
        result = bear.new_result('test message', '/tmp/testy')
180
        expected = Result.from_values(bear, 'test message', '/tmp/testy')
181
        self.assertEqual(result, expected)
182