Skip to content

Commit f205e6d

Browse files
author
Jacob Scott
committed
Incoroporate PR feedback
* Make config_file a required argument to Runner * Update main.py * Update tests to make them all green New test to test alternate config functionality coming in next commit
1 parent f1c00ee commit f205e6d

File tree

7 files changed

+63
-59
lines changed

7 files changed

+63
-59
lines changed

pre_commit/main.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ def _add_color_option(parser):
3535

3636

3737
def _add_config_option(parser):
38-
parser.add_argument('-c', '--config', help='Path to alternate config file')
38+
parser.add_argument(
39+
'-c', '--config', default='.pre-commit-config.yaml',
40+
help='Path to alternate config file'
41+
)
3942

4043

4144
def main(argv=None):
4245
argv = argv if argv is not None else sys.argv[1:]
4346
argv = [five.to_text(arg) for arg in argv]
4447
parser = argparse.ArgumentParser()
4548

49+
parser.set_defaults(config='.pre-commit-config.yaml')
4650
# http://stackoverflow.com/a/8521644/812183
4751
parser.add_argument(
4852
'-V', '--version',
@@ -58,6 +62,7 @@ def main(argv=None):
5862
'install', help='Install the pre-commit script.',
5963
)
6064
_add_color_option(install_parser)
65+
_add_config_option(install_parser)
6166
install_parser.add_argument(
6267
'-f', '--overwrite', action='store_true',
6368
help='Overwrite existing hooks / remove migration mode.',
@@ -78,6 +83,7 @@ def main(argv=None):
7883
'uninstall', help='Uninstall the pre-commit script.',
7984
)
8085
_add_color_option(uninstall_parser)
86+
_add_config_option(uninstall_parser)
8187
uninstall_parser.add_argument(
8288
'-t', '--hook-type', choices=('pre-commit', 'pre-push'),
8389
default='pre-commit',
@@ -87,7 +93,7 @@ def main(argv=None):
8793
'clean', help='Clean out pre-commit files.',
8894
)
8995
_add_color_option(clean_parser)
90-
96+
_add_config_option(clean_parser)
9197
autoupdate_parser = subparsers.add_parser(
9298
'autoupdate',
9399
help="Auto-update pre-commit config to the latest repos' versions.",
@@ -97,6 +103,7 @@ def main(argv=None):
97103

98104
run_parser = subparsers.add_parser('run', help='Run hooks.')
99105
_add_color_option(run_parser)
106+
_add_config_option(run_parser)
100107
run_parser.add_argument('hook', nargs='?', help='A single hook-id to run')
101108
run_parser.add_argument(
102109
'--no-stash', default=False, action='store_true',
@@ -124,7 +131,6 @@ def main(argv=None):
124131
'--hook-stage', choices=('commit', 'push'), default='commit',
125132
help='The stage during which the hook is fired e.g. commit or push.',
126133
)
127-
_add_config_option(run_parser)
128134
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
129135
run_mutex_group.add_argument(
130136
'--all-files', '-a', action='store_true', default=False,
@@ -158,10 +164,7 @@ def main(argv=None):
158164

159165
with error_handler():
160166
add_logging_handler(args.color)
161-
runner_kwargs = {}
162-
if hasattr(args, 'config_file'):
163-
runner_kwargs['config_file'] = args.config_file
164-
runner = Runner.create(**runner_kwargs)
167+
runner = Runner.create(args.config)
165168
git.check_for_cygwin_mismatch()
166169

167170
if args.command == 'install':

pre_commit/runner.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from cached_property import cached_property
66

7-
import pre_commit.constants as C
87
from pre_commit import git
98
from pre_commit.clientlib.validate_config import load_config
109
from pre_commit.repository import Repository
@@ -16,19 +15,19 @@ class Runner(object):
1615
repository under test.
1716
"""
1817

19-
def __init__(self, git_root, config_file=None):
18+
def __init__(self, git_root, config_file):
2019
self.git_root = git_root
21-
self.config_file = config_file or C.CONFIG_FILE
20+
self.config_file = config_file
2221

2322
@classmethod
24-
def create(cls, config_file=None):
23+
def create(cls, config_file):
2524
"""Creates a PreCommitRunner by doing the following:
2625
- Finds the root of the current git repository
2726
- chdir to that directory
2827
"""
2928
root = git.get_root()
3029
os.chdir(root)
31-
return cls(root, config_file=config_file)
30+
return cls(root, config_file)
3231

3332
@cached_property
3433
def git_dir(self):

tests/commands/autoupdate_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_autoupdate_up_to_date_repo(
4545

4646
before = open(C.CONFIG_FILE).read()
4747
assert '^$' not in before
48-
runner = Runner('.')
48+
runner = Runner('.', C.CONFIG_FILE)
4949
ret = autoupdate(runner)
5050
after = open(C.CONFIG_FILE).read()
5151
assert ret == 0
@@ -86,7 +86,7 @@ def test_autoupdate_out_of_date_repo(
8686
write_config('.', config)
8787

8888
before = open(C.CONFIG_FILE).read()
89-
runner = Runner('.')
89+
runner = Runner('.', C.CONFIG_FILE)
9090
ret = autoupdate(runner)
9191
after = open(C.CONFIG_FILE).read()
9292
assert ret == 0
@@ -111,7 +111,7 @@ def test_autoupdate_tagged_repo(
111111
)
112112
write_config('.', config)
113113

114-
ret = autoupdate(Runner('.'))
114+
ret = autoupdate(Runner('.', C.CONFIG_FILE))
115115
assert ret == 0
116116
assert 'v1.2.3' in open(C.CONFIG_FILE).read()
117117

@@ -156,7 +156,7 @@ def test_autoupdate_hook_disappearing_repo(
156156
write_config('.', config)
157157

158158
before = open(C.CONFIG_FILE).read()
159-
runner = Runner('.')
159+
runner = Runner('.', C.CONFIG_FILE)
160160
ret = autoupdate(runner)
161161
after = open(C.CONFIG_FILE).read()
162162
assert ret == 1
@@ -167,7 +167,7 @@ def test_autoupdate_local_hooks(tempdir_factory):
167167
git_path = git_dir(tempdir_factory)
168168
config = config_with_local_hooks()
169169
path = add_config_to_repo(git_path, config)
170-
runner = Runner(path)
170+
runner = Runner(path, C.CONFIG_FILE)
171171
assert autoupdate(runner) == 0
172172
new_config_writen = load_config(runner.config_file_path)
173173
assert len(new_config_writen) == 1
@@ -183,7 +183,7 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
183183
local_config = config_with_local_hooks()
184184
config = [local_config, stale_config]
185185
write_config('.', config)
186-
runner = Runner('.')
186+
runner = Runner('.', C.CONFIG_FILE)
187187
assert autoupdate(runner) == 0
188188
new_config_writen = load_config(runner.config_file_path)
189189
assert len(new_config_writen) == 2

tests/commands/install_uninstall_test.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import mock
1212

13+
import pre_commit.constants as C
1314
from pre_commit.commands.install_uninstall import IDENTIFYING_HASH
1415
from pre_commit.commands.install_uninstall import install
1516
from pre_commit.commands.install_uninstall import is_our_pre_commit
@@ -53,7 +54,7 @@ def test_is_previous_pre_commit(in_tmpdir):
5354

5455
def test_install_pre_commit(tempdir_factory):
5556
path = git_dir(tempdir_factory)
56-
runner = Runner(path)
57+
runner = Runner(path, C.CONFIG_FILE)
5758
ret = install(runner)
5859
assert ret == 0
5960
assert os.path.exists(runner.pre_commit_path)
@@ -87,7 +88,7 @@ def test_install_hooks_directory_not_present(tempdir_factory):
8788
hooks = os.path.join(path, '.git', 'hooks')
8889
if os.path.exists(hooks): # pragma: no cover (latest git)
8990
shutil.rmtree(hooks)
90-
runner = Runner(path)
91+
runner = Runner(path, C.CONFIG_FILE)
9192
install(runner)
9293
assert os.path.exists(runner.pre_commit_path)
9394

@@ -97,7 +98,7 @@ def test_install_hooks_dead_symlink(
9798
tempdir_factory,
9899
): # pragma: no cover (non-windows)
99100
path = git_dir(tempdir_factory)
100-
runner = Runner(path)
101+
runner = Runner(path, C.CONFIG_FILE)
101102
mkdirp(os.path.dirname(runner.pre_commit_path))
102103
os.symlink('/fake/baz', os.path.join(path, '.git', 'hooks', 'pre-commit'))
103104
install(runner)
@@ -106,14 +107,14 @@ def test_install_hooks_dead_symlink(
106107

107108
def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory):
108109
path = git_dir(tempdir_factory)
109-
runner = Runner(path)
110+
runner = Runner(path, C.CONFIG_FILE)
110111
ret = uninstall(runner)
111112
assert ret == 0
112113

113114

114115
def test_uninstall(tempdir_factory):
115116
path = git_dir(tempdir_factory)
116-
runner = Runner(path)
117+
runner = Runner(path, C.CONFIG_FILE)
117118
assert not os.path.exists(runner.pre_commit_path)
118119
install(runner)
119120
assert os.path.exists(runner.pre_commit_path)
@@ -156,7 +157,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
156157
def test_install_pre_commit_and_run(tempdir_factory):
157158
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
158159
with cwd(path):
159-
assert install(Runner(path)) == 0
160+
assert install(Runner(path, C.CONFIG_FILE)) == 0
160161

161162
ret, output = _get_commit_output(tempdir_factory)
162163
assert ret == 0
@@ -172,7 +173,7 @@ def test_install_in_submodule_and_run(tempdir_factory):
172173

173174
sub_pth = os.path.join(parent_path, 'sub')
174175
with cwd(sub_pth):
175-
assert install(Runner(sub_pth)) == 0
176+
assert install(Runner(sub_pth, C.CONFIG_FILE)) == 0
176177
ret, output = _get_commit_output(tempdir_factory)
177178
assert ret == 0
178179
assert NORMAL_PRE_COMMIT_RUN.match(output)
@@ -189,7 +190,7 @@ def test_commit_am(tempdir_factory):
189190
with io.open('unstaged', 'w') as foo_file:
190191
foo_file.write('Oh hai')
191192

192-
assert install(Runner(path)) == 0
193+
assert install(Runner(path, C.CONFIG_FILE)) == 0
193194

194195
ret, output = _get_commit_output(tempdir_factory)
195196
assert ret == 0
@@ -198,8 +199,8 @@ def test_commit_am(tempdir_factory):
198199
def test_install_idempotent(tempdir_factory):
199200
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
200201
with cwd(path):
201-
assert install(Runner(path)) == 0
202-
assert install(Runner(path)) == 0
202+
assert install(Runner(path, C.CONFIG_FILE)) == 0
203+
assert install(Runner(path, C.CONFIG_FILE)) == 0
203204

204205
ret, output = _get_commit_output(tempdir_factory)
205206
assert ret == 0
@@ -219,7 +220,7 @@ def test_environment_not_sourced(tempdir_factory):
219220
with cwd(path):
220221
# Patch the executable to simulate rming virtualenv
221222
with mock.patch.object(sys, 'executable', '/bin/false'):
222-
assert install(Runner(path)) == 0
223+
assert install(Runner(path, C.CONFIG_FILE)) == 0
223224

224225
# Use a specific homedir to ignore --user installs
225226
homedir = tempdir_factory.get()
@@ -257,7 +258,7 @@ def test_environment_not_sourced(tempdir_factory):
257258
def test_failing_hooks_returns_nonzero(tempdir_factory):
258259
path = make_consuming_repo(tempdir_factory, 'failing_hook_repo')
259260
with cwd(path):
260-
assert install(Runner(path)) == 0
261+
assert install(Runner(path, C.CONFIG_FILE)) == 0
261262

262263
ret, output = _get_commit_output(tempdir_factory)
263264
assert ret == 1
@@ -275,7 +276,7 @@ def test_failing_hooks_returns_nonzero(tempdir_factory):
275276
def test_install_existing_hooks_no_overwrite(tempdir_factory):
276277
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
277278
with cwd(path):
278-
runner = Runner(path)
279+
runner = Runner(path, C.CONFIG_FILE)
279280

280281
# Write out an "old" hook
281282
mkdirp(os.path.dirname(runner.pre_commit_path))
@@ -301,7 +302,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory):
301302
def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory):
302303
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
303304
with cwd(path):
304-
runner = Runner(path)
305+
runner = Runner(path, C.CONFIG_FILE)
305306

306307
# Write out an "old" hook
307308
mkdirp(os.path.dirname(runner.pre_commit_path))
@@ -330,7 +331,7 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory):
330331
def test_failing_existing_hook_returns_1(tempdir_factory):
331332
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
332333
with cwd(path):
333-
runner = Runner(path)
334+
runner = Runner(path, C.CONFIG_FILE)
334335

335336
# Write out a failing "old" hook
336337
mkdirp(os.path.dirname(runner.pre_commit_path))
@@ -349,7 +350,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory):
349350
def test_install_overwrite_no_existing_hooks(tempdir_factory):
350351
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
351352
with cwd(path):
352-
assert install(Runner(path), overwrite=True) == 0
353+
assert install(Runner(path, C.CONFIG_FILE), overwrite=True) == 0
353354

354355
ret, output = _get_commit_output(tempdir_factory)
355356
assert ret == 0
@@ -359,7 +360,7 @@ def test_install_overwrite_no_existing_hooks(tempdir_factory):
359360
def test_install_overwrite(tempdir_factory):
360361
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
361362
with cwd(path):
362-
runner = Runner(path)
363+
runner = Runner(path, C.CONFIG_FILE)
363364

364365
# Write out the "old" hook
365366
mkdirp(os.path.dirname(runner.pre_commit_path))
@@ -377,7 +378,7 @@ def test_install_overwrite(tempdir_factory):
377378
def test_uninstall_restores_legacy_hooks(tempdir_factory):
378379
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
379380
with cwd(path):
380-
runner = Runner(path)
381+
runner = Runner(path, C.CONFIG_FILE)
381382

382383
# Write out an "old" hook
383384
mkdirp(os.path.dirname(runner.pre_commit_path))
@@ -398,7 +399,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory):
398399
def test_replace_old_commit_script(tempdir_factory):
399400
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
400401
with cwd(path):
401-
runner = Runner(path)
402+
runner = Runner(path, C.CONFIG_FILE)
402403

403404
# Install a script that looks like our old script
404405
pre_commit_contents = io.open(
@@ -424,7 +425,7 @@ def test_replace_old_commit_script(tempdir_factory):
424425
def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
425426
path = git_dir(tempdir_factory)
426427
with cwd(path):
427-
runner = Runner(path)
428+
runner = Runner(path, C.CONFIG_FILE)
428429
mkdirp(os.path.dirname(runner.pre_commit_path))
429430
with io.open(runner.pre_commit_path, 'w') as pre_commit_file:
430431
pre_commit_file.write('#!/usr/bin/env bash\necho 1\n')
@@ -449,7 +450,7 @@ def test_installs_hooks_with_hooks_True(
449450
):
450451
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
451452
with cwd(path):
452-
install(Runner(path), hooks=True)
453+
install(Runner(path, C.CONFIG_FILE), hooks=True)
453454
ret, output = _get_commit_output(
454455
tempdir_factory, pre_commit_home=mock_out_store_directory,
455456
)
@@ -461,7 +462,7 @@ def test_installs_hooks_with_hooks_True(
461462
def test_installed_from_venv(tempdir_factory):
462463
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
463464
with cwd(path):
464-
install(Runner(path))
465+
install(Runner(path, C.CONFIG_FILE))
465466
# No environment so pre-commit is not on the path when running!
466467
# Should still pick up the python from when we installed
467468
ret, output = _get_commit_output(
@@ -495,7 +496,7 @@ def test_pre_push_integration_failing(tempdir_factory):
495496
path = tempdir_factory.get()
496497
cmd_output('git', 'clone', upstream, path)
497498
with cwd(path):
498-
install(Runner(path), hook_type='pre-push')
499+
install(Runner(path, C.CONFIG_FILE), hook_type='pre-push')
499500
# commit succeeds because pre-commit is only installed for pre-push
500501
assert _get_commit_output(tempdir_factory)[0] == 0
501502

@@ -511,7 +512,7 @@ def test_pre_push_integration_accepted(tempdir_factory):
511512
path = tempdir_factory.get()
512513
cmd_output('git', 'clone', upstream, path)
513514
with cwd(path):
514-
install(Runner(path), hook_type='pre-push')
515+
install(Runner(path, C.CONFIG_FILE), hook_type='pre-push')
515516
assert _get_commit_output(tempdir_factory)[0] == 0
516517

517518
retc, output = _get_push_output(tempdir_factory)
@@ -525,7 +526,7 @@ def test_pre_push_integration_empty_push(tempdir_factory):
525526
path = tempdir_factory.get()
526527
cmd_output('git', 'clone', upstream, path)
527528
with cwd(path):
528-
install(Runner(path), hook_type='pre-push')
529+
install(Runner(path, C.CONFIG_FILE), hook_type='pre-push')
529530
_get_push_output(tempdir_factory)
530531
retc, output = _get_push_output(tempdir_factory)
531532
assert output == 'Everything up-to-date\n'

0 commit comments

Comments
 (0)