Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect missing arg for prepare-commit-msg and commit-msg #1341

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions pre_commit/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ def run(
f'`git add {config_file}` to fix this.',
)
return 1
if (
args.hook_stage in {'prepare-commit-msg', 'commit-msg'} and
not args.commit_msg_filename
):
logger.error(
f'`--commit-msg-filename` is required for '
f'`--hook-stage {args.hook_stage}`',
)
return 1

# Expose origin / source as environment variables for hooks to consume
if args.origin and args.source:
Expand Down
19 changes: 11 additions & 8 deletions tests/commands/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,7 @@ def test_stages(cap_out, store, repo_with_passing_hook):
'language': 'pygrep',
'stages': [stage],
}
for i, stage in enumerate(
(
'commit', 'push', 'manual', 'prepare-commit-msg',
'commit-msg',
), 1,
)
for i, stage in enumerate(('commit', 'push', 'manual'), 1)
],
}
add_config_to_repo(repo_with_passing_hook, config)
Expand All @@ -686,8 +681,6 @@ def _run_for_stage(stage):
assert _run_for_stage('commit').startswith(b'hook 1...')
assert _run_for_stage('push').startswith(b'hook 2...')
assert _run_for_stage('manual').startswith(b'hook 3...')
assert _run_for_stage('prepare-commit-msg').startswith(b'hook 4...')
assert _run_for_stage('commit-msg').startswith(b'hook 5...')


def test_commit_msg_hook(cap_out, store, commit_msg_repo):
Expand Down Expand Up @@ -819,6 +812,16 @@ def test_error_with_unstaged_config(cap_out, store, modified_config_repo):
assert ret == 1


def test_commit_msg_missing_filename(cap_out, store, repo_with_passing_hook):
args = run_opts(hook_stage='commit-msg')
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
assert ret == 1
assert printed == (
b'[ERROR] `--commit-msg-filename` is required for '
b'`--hook-stage commit-msg`\n'
)


@pytest.mark.parametrize(
'opts', (run_opts(all_files=True), run_opts(files=[C.CONFIG_FILE])),
)
Expand Down