Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ repos:

**NOTE:** when using as a pre-commit hook, `input` is supplied automatically (with the current commit's message).

**NOTE:** pre-commit passes each item in `args` as a single argument, so an option and its value go in separate items,
like `[--scopes, api]` (or `[--scopes=api]`), not `[--scopes api]`.

## Development

`conventional-pre-commit` comes with a [VS Code devcontainer](https://code.visualstudio.com/learn/develop-cloud/containers)
Expand Down
8 changes: 8 additions & 0 deletions conventional_pre_commit/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def main(argv=[]):

try:
args = parser.parse_args(argv)
# pre-commit passes each item in `args` as one argument, so `--scopes api` arrives
# as a single string. argparse doesn't see an option in it and takes it as a type.
for commit_type in args.types:
if commit_type.startswith("-"):
parser.error(
f'"{commit_type}" looks like an option and its value in one argument. '
+ "Pass them separately, e.g. [--scopes, api] or [--scopes=api]."
)
except SystemExit:
return RESULT_FAIL

Expand Down
13 changes: 13 additions & 0 deletions tests/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ def test_main_success__conventional_with_scope(cmd, conventional_commit_with_sco
assert result == RESULT_SUCCESS


def test_main_fail__option_and_value_in_one_arg(conventional_commit_with_scope_path, capsys):
result = main(["--force-scope", "--scopes api", conventional_commit_with_scope_path])

assert result == RESULT_FAIL
assert '"--scopes api" looks like an option and its value in one argument' in capsys.readouterr().err


def test_main_success__option_and_value_with_equals(conventional_commit_with_scope_path):
result = main(["--force-scope", "--scopes=scope", conventional_commit_with_scope_path])

assert result == RESULT_SUCCESS


def test_main_success__fixup_commit(fixup_commit_path):
result = main([fixup_commit_path])

Expand Down