Skip to content

Commit 24a9bdc

Browse files
committed
Fixed missing new version info in some hook environments
Introduce the `new_version_env` function and update existing functions (`get_setup_hook_env` and `get_pre_commit_hook_env`) to include new version environment variables. Added new tests for verifying the inclusion of OS, SCM, current, and new version information in hook environments.
1 parent 6f5d56b commit 24a9bdc

File tree

3 files changed

+97
-16
lines changed

3 files changed

+97
-16
lines changed

bumpversion/hooks.py

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Dict, List, Optional
77

88
from bumpversion.config.models import Config
9+
from bumpversion.context import get_context
910
from bumpversion.ui import get_indented_logger
1011
from bumpversion.versioning.models import Version
1112

@@ -52,6 +53,15 @@ def version_env(version: Version, version_prefix: str) -> Dict[str, str]:
5253
return {f"{PREFIX}{version_prefix}{part.upper()}": version[part].value for part in version}
5354

5455

56+
def new_version_env(config: Config, current_version: Version, new_version: Version) -> Dict[str, str]:
57+
"""Provide the environment dictionary for new_version serialized and tag name."""
58+
ctx = get_context(config, current_version, new_version)
59+
new_version_string = config.version_config.serialize(new_version, ctx)
60+
ctx["new_version"] = new_version_string
61+
new_version_tag = config.tag_name.format(**ctx)
62+
return {f"{PREFIX}NEW_VERSION": new_version_string, f"{PREFIX}NEW_VERSION_TAG": new_version_tag}
63+
64+
5565
def get_setup_hook_env(config: Config, current_version: Version) -> Dict[str, str]:
5666
"""Provide the environment dictionary for `setup_hook`s."""
5767
return {**base_env(config), **scm_env(config), **version_env(current_version, "CURRENT_")}
@@ -64,6 +74,7 @@ def get_pre_commit_hook_env(config: Config, current_version: Version, new_versio
6474
**scm_env(config),
6575
**version_env(current_version, "CURRENT_"),
6676
**version_env(new_version, "NEW_"),
77+
**new_version_env(config, current_version, new_version),
6778
}
6879

6980

@@ -74,6 +85,7 @@ def get_post_commit_hook_env(config: Config, current_version: Version, new_versi
7485
**scm_env(config),
7586
**version_env(current_version, "CURRENT_"),
7687
**version_env(new_version, "NEW_"),
88+
**new_version_env(config, current_version, new_version),
7789
}
7890

7991

tests/conftest.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
from contextlib import contextmanager
55
from click.testing import CliRunner
66
from pathlib import Path
7-
from typing import Generator
7+
from typing import Generator, Tuple
88

99
import pytest
10-
10+
from bumpversion.config import Config
1111
from bumpversion.versioning.models import Version
12+
from bumpversion.versioning.version_config import VersionConfig
1213

1314

1415
@pytest.fixture
@@ -41,10 +42,9 @@ def inside_dir(dirpath: Path) -> Generator:
4142
os.chdir(old_path)
4243

4344

44-
def get_config_data(overrides: dict) -> tuple:
45+
def get_config_data(overrides: dict) -> Tuple[Config, VersionConfig, Version]:
4546
"""Get the configuration, version_config and version."""
4647
from bumpversion import config
47-
from bumpversion.versioning.version_config import VersionConfig
4848

4949
conf = config.get_configuration(config_file="missing", **overrides)
5050
version_config = VersionConfig(conf.parse, conf.serialize, conf.search, conf.replace, conf.parts)

tests/test_hooks/test_envs.py

+81-12
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,52 @@
55
import subprocess
66
from pathlib import Path
77

8-
from bumpversion.hooks import scm_env, PREFIX, base_env, version_env
8+
from bumpversion.hooks import (
9+
scm_env,
10+
PREFIX,
11+
base_env,
12+
version_env,
13+
new_version_env,
14+
get_setup_hook_env,
15+
get_pre_commit_hook_env,
16+
)
917
from tests.conftest import inside_dir, get_config_data
1018

1119

20+
def assert_os_environ_items_included(result_env: dict) -> None:
21+
"""Assert that the OS environment variables are in the result."""
22+
for var, value in os.environ.items():
23+
assert var in result_env
24+
assert result_env[var] == value
25+
26+
27+
def assert_scm_info_included(result_env: dict):
28+
"""Assert the SCM information is included in the result."""
29+
assert f"{PREFIX}COMMIT_SHA" in result_env
30+
assert f"{PREFIX}DISTANCE_TO_LATEST_TAG" in result_env
31+
assert f"{PREFIX}IS_DIRTY" in result_env
32+
assert f"{PREFIX}BRANCH_NAME" in result_env
33+
assert f"{PREFIX}SHORT_BRANCH_NAME" in result_env
34+
assert f"{PREFIX}CURRENT_VERSION" in result_env
35+
assert f"{PREFIX}CURRENT_TAG" in result_env
36+
37+
38+
def assert_current_version_info_included(result_env: dict):
39+
"""Assert the current version information is included in the result."""
40+
assert f"{PREFIX}CURRENT_MAJOR" in result_env
41+
assert f"{PREFIX}CURRENT_MINOR" in result_env
42+
assert f"{PREFIX}CURRENT_PATCH" in result_env
43+
44+
45+
def assert_new_version_info_included(result_env: dict):
46+
"""Assert the new version information is included in the result."""
47+
assert f"{PREFIX}NEW_MAJOR" in result_env
48+
assert f"{PREFIX}NEW_MINOR" in result_env
49+
assert f"{PREFIX}NEW_PATCH" in result_env
50+
assert f"{PREFIX}NEW_VERSION" in result_env
51+
assert f"{PREFIX}NEW_VERSION_TAG" in result_env
52+
53+
1254
def test_scm_env_returns_correct_info(git_repo: Path):
1355
"""Should return information about the latest tag."""
1456
readme = git_repo.joinpath("readme.md")
@@ -58,26 +100,18 @@ def test_includes_os_environ(self):
58100
config, _, _ = get_config_data({"current_version": "0.1.0"})
59101
result_env = base_env(config)
60102

61-
for var, value in os.environ.items():
62-
assert var in result_env
63-
assert result_env[var] == value
103+
assert_os_environ_items_included(result_env)
64104

65105
def test_includes_scm_info(self):
66106
"""The output includes SCM information."""
67107
config, _, _ = get_config_data({"current_version": "0.1.0"})
68108
result_env = base_env(config)
69109

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
110+
assert_scm_info_included(result_env)
77111

78112

79113
def test_current_version_env_includes_correct_info():
80-
"""pass"""
114+
"""The version_env for a version should include all its parts"""
81115
config, _, current_version = get_config_data(
82116
{"current_version": "0.1.0", "parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"}
83117
)
@@ -86,3 +120,38 @@ def test_current_version_env_includes_correct_info():
86120
assert result[f"{PREFIX}CURRENT_MAJOR"] == "0"
87121
assert result[f"{PREFIX}CURRENT_MINOR"] == "1"
88122
assert result[f"{PREFIX}CURRENT_PATCH"] == "0"
123+
124+
125+
def test_new_version_env_includes_correct_info():
126+
"""The new_version_env should return the serialized version and tag name."""
127+
128+
config, _, current_version = get_config_data(
129+
{"current_version": "0.1.0", "parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"}
130+
)
131+
new_version = current_version.bump("minor")
132+
result = new_version_env(config, current_version, new_version)
133+
134+
assert result[f"{PREFIX}NEW_VERSION"] == "0.2.0"
135+
assert result[f"{PREFIX}NEW_VERSION_TAG"] == "v0.2.0"
136+
137+
138+
def test_get_setup_hook_env_includes_correct_info():
139+
"""The setup hook environment should contain specific information."""
140+
config, _, current_version = get_config_data({"current_version": "0.1.0"})
141+
result_env = get_setup_hook_env(config, current_version)
142+
143+
assert_os_environ_items_included(result_env)
144+
assert_scm_info_included(result_env)
145+
assert_current_version_info_included(result_env)
146+
147+
148+
def test_get_pre_commit_hook_env_includes_correct_info():
149+
"""The pre-commit hook environment should contain specific information."""
150+
config, _, current_version = get_config_data({"current_version": "0.1.0"})
151+
new_version = current_version.bump("minor")
152+
result_env = get_pre_commit_hook_env(config, current_version, new_version)
153+
154+
assert_os_environ_items_included(result_env)
155+
assert_scm_info_included(result_env)
156+
assert_current_version_info_included(result_env)
157+
assert_new_version_info_included(result_env)

0 commit comments

Comments
 (0)