forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.py
More file actions
158 lines (116 loc) · 4.31 KB
/
main_test.py
File metadata and controls
158 lines (116 loc) · 4.31 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import mock
import pytest
from pre_commit import main
from pre_commit.error_handler import PreCommitSystemExit
from pre_commit.util import cwd
from testing.auto_namedtuple import auto_namedtuple
@pytest.yield_fixture
def mock_commands():
with mock.patch.object(main, 'autoupdate') as autoupdate_mock:
with mock.patch.object(main, 'clean') as clean_mock:
with mock.patch.object(main, 'install') as install_mock:
with mock.patch.object(main, 'uninstall') as uninstall_mock:
with mock.patch.object(main, 'run') as run_mock:
yield auto_namedtuple(
autoupdate_mock=autoupdate_mock,
clean_mock=clean_mock,
install_mock=install_mock,
uninstall_mock=uninstall_mock,
run_mock=run_mock,
)
class CalledExit(Exception):
pass
@pytest.yield_fixture
def argparse_exit_mock():
with mock.patch.object(
argparse.ArgumentParser, 'exit', side_effect=CalledExit,
) as exit_mock:
yield exit_mock
@pytest.yield_fixture
def argparse_parse_args_spy():
parse_args_mock = mock.Mock()
original_parse_args = argparse.ArgumentParser.parse_args
def fake_parse_args(self, args):
# call our spy object
parse_args_mock(args)
return original_parse_args(self, args)
with mock.patch.object(
argparse.ArgumentParser, 'parse_args', fake_parse_args,
):
yield parse_args_mock
def assert_only_one_mock_called(mock_objs):
total_call_count = sum(mock_obj.call_count for mock_obj in mock_objs)
assert total_call_count == 1
def test_overall_help(mock_commands, argparse_exit_mock):
with pytest.raises(CalledExit):
main.main(['--help'])
def test_help_command(
mock_commands, argparse_exit_mock, argparse_parse_args_spy,
):
with pytest.raises(CalledExit):
main.main(['help'])
argparse_parse_args_spy.assert_has_calls([
mock.call(['help']),
mock.call(['--help']),
])
def test_help_other_command(
mock_commands, argparse_exit_mock, argparse_parse_args_spy,
):
with pytest.raises(CalledExit):
main.main(['help', 'run'])
argparse_parse_args_spy.assert_has_calls([
mock.call(['help', 'run']),
mock.call(['run', '--help']),
])
def test_install_command(mock_commands):
main.main(['install'])
assert mock_commands.install_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_uninstall_command(mock_commands):
main.main(['uninstall'])
assert mock_commands.uninstall_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_clean_command(mock_commands):
main.main(['clean'])
assert mock_commands.clean_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_autoupdate_command(mock_commands):
main.main(['autoupdate'])
assert mock_commands.autoupdate_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_run_command(mock_commands):
main.main(['run'])
assert mock_commands.run_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_no_commands_run_command(mock_commands):
main.main([])
assert mock_commands.run_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_help_cmd_in_empty_directory(
mock_commands,
tempdir_factory,
argparse_exit_mock,
argparse_parse_args_spy,
):
path = tempdir_factory.get()
with cwd(path):
with pytest.raises(CalledExit):
main.main(['help', 'run'])
argparse_parse_args_spy.assert_has_calls([
mock.call(['help', 'run']),
mock.call(['run', '--help']),
])
def test_expected_fatal_error_no_git_repo(
tempdir_factory, cap_out, mock_out_store_directory,
):
with cwd(tempdir_factory.get()):
with pytest.raises(PreCommitSystemExit):
main.main([])
assert cap_out.get() == (
'An error has occurred: FatalError: git failed. '
'Is it installed, and are you in a Git repository directory?\n'
'Check the log at ~/.pre-commit/pre-commit.log\n'
)