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
141 lines (103 loc) · 3.51 KB
/
main_test.py
File metadata and controls
141 lines (103 loc) · 3.51 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
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import os.path
import mock
import pytest
from pre_commit import main
from testing.auto_namedtuple import auto_namedtuple
from testing.util import cwd
FNS = (
'autoupdate', 'clean', 'install', 'install_hooks', 'migrate_config', 'run',
'sample_config', 'uninstall',
)
CMDS = tuple(fn.replace('_', '-') for fn in FNS)
@pytest.fixture
def mock_commands():
mcks = {fn: mock.patch.object(main, fn).start() for fn in FNS}
ret = auto_namedtuple(**mcks)
yield ret
for mck in ret:
mck.stop()
class CalledExit(Exception):
pass
@pytest.fixture
def argparse_exit_mock():
with mock.patch.object(
argparse.ArgumentParser, 'exit', side_effect=CalledExit,
) as exit_mock:
yield exit_mock
@pytest.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']),
])
@pytest.mark.parametrize('command', CMDS)
def test_all_cmds(command, mock_commands, mock_store_dir):
main.main((command,))
assert getattr(mock_commands, command.replace('-', '_')).call_count == 1
assert_only_one_mock_called(mock_commands)
def test_try_repo(mock_store_dir):
with mock.patch.object(main, 'try_repo') as patch:
main.main(('try-repo', '.'))
assert patch.call_count == 1
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_store_dir,
):
with cwd(tempdir_factory.get()):
with pytest.raises(SystemExit):
main.main([])
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
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 {}\n'.format(log_file)
)
def test_warning_on_tags_only(mock_commands, cap_out, mock_store_dir):
main.main(('autoupdate', '--tags-only'))
assert '--tags-only is the default' in cap_out.get()