forked from coala/coala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoalaDebugFlagTest.py
129 lines (110 loc) · 4.86 KB
/
coalaDebugFlagTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import sys
import unittest
from unittest.mock import MagicMock, patch
from coalib import coala
from coala_utils.ContextManagers import prepare_file
from tests.TestUtilities import execute_coala, bear_test_module
# patch gets strangely lost when only defined in method or with context where
# actually needed
@patch('coalib.coala_modes.mode_json')
class coalaDebugFlagTest(unittest.TestCase):
def setUp(self):
self.old_argv = sys.argv
def pipReqIsInstalledMock(self):
"""
Prepare a patch for ``PipRequirement.is_installed`` method that
always returns ``True``, used for faking an installed ipdb.
"""
return patch('dependency_management.requirements.PipRequirement.'
'PipRequirement.is_installed', lambda self: True)
def pipReqIsNotInstalledMock(self):
"""
Prepare a patch for ``PipRequirement.is_installed`` method that
always returns ``False``, used for faking a not installed ipdb.
"""
return patch('dependency_management.requirements.PipRequirement.'
'PipRequirement.is_installed', lambda self: False)
def ipdbMock(self):
"""
Prepare a mocked ``ipdb`` module with a mocked
``launch_ipdb_on_exception`` function, which is used in
``coala --debug`` mode to open and ``ipdb>`` prompt when unexpected
exceptions occur
"""
mock = MagicMock()
def __exit__(self, *exc_info):
"""
Make mocked ``ipdb.launch_ipdb_on_exception()`` context just
reraise the exception.
"""
raise
mock.launch_ipdb_on_exception.__enter__ = None
mock.launch_ipdb_on_exception.__exit__ = __exit__
return mock
def tearDown(self):
sys.argv = self.old_argv
def test_no_ipdb(self, mocked_mode_json):
mocked_mode_json.side_effect = None
with bear_test_module(), \
prepare_file(['#fixme '], None) as (lines, filename), \
self.pipReqIsNotInstalledMock():
# additionally use RaiseTestBear to verify independency from
# failing bears
status, stdout, stderr = execute_coala(
coala.main, 'coala', '--debug', '--json',
'-c', os.devnull,
'-f', filename,
'-b', 'RaiseTestBear')
assert status == 13
assert not stdout
assert '--debug flag requires ipdb.' in stderr
def test_bear__init__raises(self, mocked_mode_json):
mocked_mode_json.side_effect = None
mocked_ipdb = self.ipdbMock()
with bear_test_module(), \
prepare_file(['#fixme '], None) as (lines, filename), \
self.pipReqIsInstalledMock(), \
patch.dict('sys.modules', ipdb=mocked_ipdb), \
self.assertRaisesRegex(
RuntimeError,
r'^The bear ErrorTestBear does not fulfill all '
r"requirements\. 'I_do_not_exist' is not installed\.$"):
execute_coala(
coala.main, 'coala', '--debug',
'-c', os.devnull,
'-f', filename,
'-b', 'ErrorTestBear')
mocked_ipdb.launch_ipdb_on_exception.assert_called_once_with()
def test_bear_run_raises(self, mocked_mode_json):
mocked_mode_json.side_effect = None
mocked_ipdb = self.ipdbMock()
with bear_test_module(), \
prepare_file(['#fixme '], None) as (lines, filename), \
self.pipReqIsInstalledMock(), \
patch.dict('sys.modules', ipdb=mocked_ipdb), \
self.assertRaisesRegex(
RuntimeError, r"^That's all the RaiseTestBear can do\.$"):
execute_coala(
coala.main, 'coala', '--debug',
'-c', os.devnull,
'-f', filename,
'-b', 'RaiseTestBear')
mocked_ipdb.launch_ipdb_on_exception.assert_called_once_with()
def test_coala_main_mode_json_launches_ipdb(self, mocked_mode_json):
mocked_mode_json.side_effect = RuntimeError('Mocked mode_json fails.')
mocked_ipdb = self.ipdbMock()
with bear_test_module(), \
prepare_file(['#fixme '], None) as (lines, filename), \
self.pipReqIsInstalledMock(), \
patch.dict('sys.modules', ipdb=mocked_ipdb), \
self.assertRaisesRegex(RuntimeError,
r'^Mocked mode_json fails\.$'):
# additionally use RaiseTestBear to verify independency from
# failing bears
execute_coala(
coala.main, 'coala', '--debug', '--json',
'-c', os.devnull,
'-f', filename,
'-b', 'RaiseTestBear')
mocked_ipdb.launch_ipdb_on_exception.assert_called_once_with()