Skip to content

Commit 8446567

Browse files
committed
Added tests for hooks
1 parent 049b470 commit 8446567

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

tests/test_hooks/__init__.py

Whitespace-only changes.

tests/test_hooks/test_envs.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Tests for environment generation for hooks."""
2+
3+
import datetime
4+
import os
5+
import subprocess
6+
from pathlib import Path
7+
8+
from bumpversion.hooks import scm_env, PREFIX, base_env, current_version_env
9+
from tests.conftest import inside_dir, get_config_data
10+
11+
12+
def test_scm_env_returns_correct_info(git_repo: Path):
13+
"""Should return information about the latest tag."""
14+
readme = git_repo.joinpath("readme.md")
15+
readme.touch()
16+
tag_prefix = "v"
17+
overrides = {"current_version": "0.1.0", "commit": True, "tag": True, "tag_name": f"{tag_prefix}{{new_version}}"}
18+
19+
with inside_dir(git_repo):
20+
# Add a file and tag
21+
subprocess.run(["git", "add", "readme.md"])
22+
subprocess.run(["git", "commit", "-m", "first"])
23+
subprocess.run(["git", "tag", f"{tag_prefix}0.1.0"])
24+
conf, _, _ = get_config_data(overrides)
25+
26+
result = scm_env(conf)
27+
assert result[f"{PREFIX}BRANCH_NAME"] == "master"
28+
assert len(result[f"{PREFIX}COMMIT_SHA"]) == 40
29+
assert result[f"{PREFIX}CURRENT_TAG"] == "v0.1.0"
30+
assert result[f"{PREFIX}CURRENT_VERSION"] == "0.1.0"
31+
assert result[f"{PREFIX}DISTANCE_TO_LATEST_TAG"] == "0"
32+
assert result[f"{PREFIX}IS_DIRTY"] == "False"
33+
assert result[f"{PREFIX}SHORT_BRANCH_NAME"] == "master"
34+
35+
36+
class MockDatetime(datetime.datetime):
37+
@classmethod
38+
def now(cls, tz=None):
39+
return cls(2022, 2, 1, 17) if tz else cls(2022, 2, 1, 12)
40+
41+
42+
class TestBaseEnv:
43+
"""Tests for base_env function."""
44+
45+
def test_includes_now_and_utcnow(self, mocker):
46+
"""The output includes NOW and UTCNOW."""
47+
mocker.patch("datetime.datetime", new=MockDatetime)
48+
config, _, _ = get_config_data({"current_version": "0.1.0"})
49+
result_env = base_env(config)
50+
51+
assert f"{PREFIX}NOW" in result_env
52+
assert f"{PREFIX}UTCNOW" in result_env
53+
assert result_env[f"{PREFIX}NOW"] == "2022-02-01T12:00:00"
54+
assert result_env[f"{PREFIX}UTCNOW"] == "2022-02-01T17:00:00"
55+
56+
def test_includes_os_environ(self):
57+
"""The output includes the current process' environment."""
58+
config, _, _ = get_config_data({"current_version": "0.1.0"})
59+
result_env = base_env(config)
60+
61+
for var, value in os.environ.items():
62+
assert var in result_env
63+
assert result_env[var] == value
64+
65+
def test_includes_scm_info(self):
66+
"""The output includes SCM information."""
67+
config, _, _ = get_config_data({"current_version": "0.1.0"})
68+
result_env = base_env(config)
69+
70+
assert f"{PREFIX}COMMIT_SHA" in result_env
71+
assert f"{PREFIX}DISTANCE_TO_LATEST_TAG" in result_env
72+
assert f"{PREFIX}IS_DIRTY" in result_env
73+
assert f"{PREFIX}BRANCH_NAME" in result_env
74+
assert f"{PREFIX}SHORT_BRANCH_NAME" in result_env
75+
assert f"{PREFIX}CURRENT_VERSION" in result_env
76+
assert f"{PREFIX}CURRENT_TAG" in result_env
77+
78+
79+
def test_current_version_env_includes_correct_info():
80+
"""pass"""
81+
config, _, _ = get_config_data(
82+
{"current_version": "0.1.0", "parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"}
83+
)
84+
result = current_version_env(config)
85+
86+
assert result[f"{PREFIX}CURRENT_MAJOR"] == "0"
87+
assert result[f"{PREFIX}CURRENT_MINOR"] == "1"
88+
assert result[f"{PREFIX}CURRENT_PATCH"] == "0"

tests/test_hooks/test_run_command.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import subprocess
2+
3+
import pytest
4+
5+
from bumpversion.hooks import run_command
6+
7+
8+
class TestRunCommand:
9+
"""Test the run_command function."""
10+
11+
def test_runs_a_str_command(self):
12+
"""Runs the command formatted as a string."""
13+
result = run_command("echo Hello")
14+
assert isinstance(result, subprocess.CompletedProcess)
15+
assert result.stdout == "Hello\n"
16+
17+
def test_can_access_env(self):
18+
"""The command can access custom environment variables."""
19+
result = run_command("echo $TEST_ENV", environment={"TEST_ENV": "Hello"})
20+
assert isinstance(result, subprocess.CompletedProcess)
21+
assert result.stdout == "Hello\n"
22+
23+
def test_non_zero_exit(self):
24+
"""The result shows a non-zero result code."""
25+
result = run_command("exit 1")
26+
assert result.returncode == 1
27+
28+
@pytest.mark.parametrize(
29+
"invalid_script",
30+
[(123,), (None,), (["exit", "1"])],
31+
)
32+
def test_an_invalid_script_raises_type_error(self, invalid_script):
33+
with pytest.raises(TypeError):
34+
run_command(invalid_script)
35+
36+
@pytest.mark.parametrize(
37+
"invalid_env",
38+
[("string",), (123,), (None,)],
39+
)
40+
def test_an_invalid_env_raises_type_error(self, invalid_env):
41+
with pytest.raises(TypeError):
42+
run_command("echo Hello", environment=invalid_env)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import subprocess
2+
3+
from bumpversion.config import Config
4+
from bumpversion.hooks import run_setup_hooks
5+
from tests.conftest import get_config_data
6+
7+
8+
def setup_hook_env(config: Config) -> dict:
9+
"""Mocked function for the environment setup"""
10+
return {}
11+
12+
13+
def run_command(script: str, env: dict) -> subprocess.CompletedProcess:
14+
"""Mocked function for command execution"""
15+
return subprocess.CompletedProcess(args=script, returncode=0)
16+
17+
18+
def test_run_setup_hooks_calls_each_hook(mocker):
19+
"""The run_setup_hooks function runs each hook."""
20+
# Assemble
21+
setup_hook_env_mock = mocker.patch("bumpversion.hooks.setup_hook_env", side_effect=setup_hook_env)
22+
run_command_mock = mocker.patch("bumpversion.hooks.run_command", side_effect=run_command)
23+
24+
config, _, _ = get_config_data({"current_version": "0.1.0", "setup_hooks": ["script1", "script2"]})
25+
26+
# Act
27+
result = run_setup_hooks(config)
28+
29+
# Asserts for function's behavior
30+
setup_hook_env_mock.assert_called_once_with(config)
31+
assert run_command_mock.call_count == len(config.setup_hooks)
32+
run_command_mock.assert_any_call("script1", {})
33+
run_command_mock.assert_any_call("script2", {})

0 commit comments

Comments
 (0)