feat(hooks): support interactive hook scripts by PhilipNelson5 · Pull Request #1925 · commitizen-tools/commitizen · GitHub
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
24 changes: 24 additions & 0 deletions commitizen/cmd.py
9 changes: 2 additions & 7 deletions commitizen/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ def run(hooks: str | list[str], _env_prefix: str = "CZ_", **env: object) -> None
for hook in hooks:
out.info(f"Running hook '{hook}'")

c = cmd.run(hook, env=_format_env(_env_prefix, env))
return_code = cmd.run_interactive(hook, env=_format_env(_env_prefix, env))

if c.out:
out.write(c.out)
if c.err:
out.error(c.err)

if c.return_code != 0:
if return_code != 0:
raise RunHookError(f"Running hook '{hook}' failed")


Expand Down
8 changes: 4 additions & 4 deletions tests/test_bump_hooks.py
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then how should we test the cmd.run? Is the test for that removed?

Copy link
Copy Markdown
Contributor Author

@PhilipNelson5 PhilipNelson5 Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of the project history. I don't know if it was ever tested.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I just misunderstood what the test test_run does here.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_run(mocker: MockFixture):
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"]

cmd_run_mock = mocker.Mock()
cmd_run_mock.return_value.return_code = 0
mocker.patch.object(cmd, "run", cmd_run_mock)
cmd_run_mock.return_value = 0
mocker.patch.object(cmd, "run_interactive", cmd_run_mock)

hooks.run(bump_hooks)

Expand All @@ -29,8 +29,8 @@ def test_run_error(mocker: MockFixture):
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"]

cmd_run_mock = mocker.Mock()
cmd_run_mock.return_value.return_code = 1
mocker.patch.object(cmd, "run", cmd_run_mock)
cmd_run_mock.return_value = 1
mocker.patch.object(cmd, "run_interactive", cmd_run_mock)

with pytest.raises(RunHookError):
hooks.run(bump_hooks)
Expand Down
75 changes: 75 additions & 0 deletions tests/test_cmd.py
Loading