Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa06e72

Browse files
committed
Add a pre-commit sample-config command
1 parent 9d747fb commit fa06e72

4 files changed

Lines changed: 71 additions & 53 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
from __future__ import unicode_literals
4+
5+
6+
# TODO: maybe `git ls-remote git://github.com/pre-commit/pre-commit-hooks` to
7+
# determine the latest revision? This adds ~200ms from my tests (and is
8+
# significantly faster than https:// or http://). For now, periodically
9+
# manually updating the revision is fine.
10+
SAMPLE_CONFIG = '''\
11+
# See http://pre-commit.com for more information
12+
# See http://pre-commit.com/hooks.html for more hooks
13+
- repo: https://github.com/pre-commit/pre-commit-hooks
14+
sha: v0.7.1
15+
hooks:
16+
- id: trailing-whitespace
17+
- id: end-of-file-fixer
18+
- id: check-yaml
19+
- id: check-added-large-files
20+
'''
21+
22+
23+
def sample_config():
24+
print(SAMPLE_CONFIG, end='')
25+
return 0

pre_commit/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pre_commit.commands.install_uninstall import install_hooks
1515
from pre_commit.commands.install_uninstall import uninstall
1616
from pre_commit.commands.run import run
17+
from pre_commit.commands.sample_config import sample_config
1718
from pre_commit.error_handler import error_handler
1819
from pre_commit.logging_handler import add_logging_handler
1920
from pre_commit.runner import Runner
@@ -163,6 +164,12 @@ def main(argv=None):
163164
help='Specific filenames to run hooks on.',
164165
)
165166

167+
sample_config_parser = subparsers.add_parser(
168+
'sample-config', help='Produce a sample {} file'.format(C.CONFIG_FILE),
169+
)
170+
_add_color_option(sample_config_parser)
171+
_add_config_option(sample_config_parser)
172+
166173
help = subparsers.add_parser(
167174
'help', help='Show help for a specific command.',
168175
)
@@ -205,6 +212,8 @@ def main(argv=None):
205212
return autoupdate(runner, args.tags_only)
206213
elif args.command == 'run':
207214
return run(runner, args)
215+
elif args.command == 'sample-config':
216+
return sample_config()
208217
else:
209218
raise NotImplementedError(
210219
'Command {} not implemented.'.format(args.command)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
from pre_commit.commands.sample_config import sample_config
5+
6+
7+
def test_sample_config(capsys):
8+
ret = sample_config()
9+
assert ret == 0
10+
out, _ = capsys.readouterr()
11+
assert out == '''\
12+
# See http://pre-commit.com for more information
13+
# See http://pre-commit.com/hooks.html for more hooks
14+
- repo: https://github.com/pre-commit/pre-commit-hooks
15+
sha: v0.7.1
16+
hooks:
17+
- id: trailing-whitespace
18+
- id: end-of-file-fixer
19+
- id: check-yaml
20+
- id: check-added-large-files
21+
'''

tests/main_test.py

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
from testing.auto_namedtuple import auto_namedtuple
1313

1414

15+
FNS = (
16+
'autoupdate', 'clean', 'install', 'install_hooks', 'run', 'sample_config',
17+
'uninstall',
18+
)
19+
CMDS = tuple(fn.replace('_', '-') for fn in FNS)
20+
21+
1522
@pytest.yield_fixture
1623
def mock_commands():
17-
with mock.patch.object(main, 'autoupdate') as autoupdate_mock:
18-
with mock.patch.object(main, 'install_hooks') as install_hooks_mock:
19-
with mock.patch.object(main, 'install') as install_mock:
20-
with mock.patch.object(main, 'uninstall') as uninstall_mock:
21-
with mock.patch.object(main, 'run') as run_mock:
22-
with mock.patch.object(main, 'clean') as clean_mock:
23-
yield auto_namedtuple(
24-
autoupdate_mock=autoupdate_mock,
25-
clean_mock=clean_mock,
26-
install_mock=install_mock,
27-
install_hooks_mock=install_hooks_mock,
28-
uninstall_mock=uninstall_mock,
29-
run_mock=run_mock,
30-
)
24+
mcks = {fn: mock.patch.object(main, fn).start() for fn in FNS}
25+
ret = auto_namedtuple(**mcks)
26+
yield ret
27+
for mck in ret:
28+
mck.stop()
3129

3230

3331
class CalledExit(Exception):
@@ -93,45 +91,10 @@ def test_help_other_command(
9391
])
9492

9593

96-
def test_install_command(mock_commands):
97-
main.main(['install'])
98-
assert mock_commands.install_mock.call_count == 1
99-
assert_only_one_mock_called(mock_commands)
100-
101-
102-
def test_uninstall_command(mock_commands):
103-
main.main(['uninstall'])
104-
assert mock_commands.uninstall_mock.call_count == 1
105-
assert_only_one_mock_called(mock_commands)
106-
107-
108-
def test_clean_command(mock_commands):
109-
main.main(['clean'])
110-
assert mock_commands.clean_mock.call_count == 1
111-
assert_only_one_mock_called(mock_commands)
112-
113-
114-
def test_autoupdate_command(mock_commands):
115-
main.main(['autoupdate'])
116-
assert mock_commands.autoupdate_mock.call_count == 1
117-
assert_only_one_mock_called(mock_commands)
118-
119-
120-
def test_run_command(mock_commands):
121-
main.main(['run'])
122-
assert mock_commands.run_mock.call_count == 1
123-
assert_only_one_mock_called(mock_commands)
124-
125-
126-
def test_install_hooks_command(mock_commands):
127-
main.main(('install-hooks',))
128-
assert mock_commands.install_hooks_mock.call_count == 1
129-
assert_only_one_mock_called(mock_commands)
130-
131-
132-
def test_no_commands_run_command(mock_commands):
133-
main.main([])
134-
assert mock_commands.run_mock.call_count == 1
94+
@pytest.mark.parametrize('command', CMDS)
95+
def test_install_command(command, mock_commands):
96+
main.main((command,))
97+
assert getattr(mock_commands, command.replace('-', '_')).call_count == 1
13598
assert_only_one_mock_called(mock_commands)
13699

137100

0 commit comments

Comments
 (0)