| 1 |
import os |
| 2 |
from unittest.mock import call |
| 3 |
|
| 4 |
import pytest |
| 5 |
from pytest_mock import MockFixture |
| 6 |
|
| 7 |
from commitizen import cmd, hooks |
| 8 |
from commitizen.exceptions import RunHookError |
| 9 |
|
| 10 |
|
| 11 |
def test_run(mocker: MockFixture): |
| 12 |
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"] |
| 13 |
|
| 14 |
cmd_run_mock = mocker.Mock() |
| 15 |
cmd_run_mock.return_value = 0 |
| 16 |
mocker.patch.object(cmd, "run_interactive", cmd_run_mock) |
| 17 |
|
| 18 |
hooks.run(bump_hooks) |
| 19 |
|
| 20 |
cmd_run_mock.assert_has_calls( |
| 21 |
[ |
| 22 |
call("pre_bump_hook", env=dict(os.environ)), |
| 23 |
call("pre_bump_hook_1", env=dict(os.environ)), |
| 24 |
] |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def test_run_error(mocker: MockFixture): |
| 29 |
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"] |
| 30 |
|
| 31 |
cmd_run_mock = mocker.Mock() |
| 32 |
cmd_run_mock.return_value = 1 |
| 33 |
mocker.patch.object(cmd, "run_interactive", cmd_run_mock) |
| 34 |
|
| 35 |
with pytest.raises(RunHookError): |
| 36 |
hooks.run(bump_hooks) |
| 37 |
|
| 38 |
|
| 39 |
def test_format_env(): |
| 40 |
result = hooks._format_env("TEST_", {"foo": "bar", "bar": "baz"}) |
| 41 |
assert result["TEST_FOO"] == "bar" |
| 42 |
assert result["TEST_BAR"] == "baz" |
| 43 |
|
| 44 |
|
| 45 |
def test_run_integration(): |
| 46 |
"""Integration test that actually executes hooks.run() without mocking.""" |
| 47 |
hooks.run("python -c \"print('hook ran')\"") |
| 48 |
|
| 49 |
|
| 50 |
def test_run_integration_error(): |
| 51 |
"""Integration test that a failing hook raises RunHookError.""" |
| 52 |
with pytest.raises(RunHookError): |
| 53 |
hooks.run('python -c "import sys; sys.exit(1)"') |