5
5
import subprocess
6
6
from pathlib import Path
7
7
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
+ )
9
17
from tests .conftest import inside_dir , get_config_data
10
18
11
19
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
+
12
54
def test_scm_env_returns_correct_info (git_repo : Path ):
13
55
"""Should return information about the latest tag."""
14
56
readme = git_repo .joinpath ("readme.md" )
@@ -58,26 +100,18 @@ def test_includes_os_environ(self):
58
100
config , _ , _ = get_config_data ({"current_version" : "0.1.0" })
59
101
result_env = base_env (config )
60
102
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 )
64
104
65
105
def test_includes_scm_info (self ):
66
106
"""The output includes SCM information."""
67
107
config , _ , _ = get_config_data ({"current_version" : "0.1.0" })
68
108
result_env = base_env (config )
69
109
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 )
77
111
78
112
79
113
def test_current_version_env_includes_correct_info ():
80
- """pass """
114
+ """The version_env for a version should include all its parts """
81
115
config , _ , current_version = get_config_data (
82
116
{"current_version" : "0.1.0" , "parse" : r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)" }
83
117
)
@@ -86,3 +120,38 @@ def test_current_version_env_includes_correct_info():
86
120
assert result [f"{ PREFIX } CURRENT_MAJOR" ] == "0"
87
121
assert result [f"{ PREFIX } CURRENT_MINOR" ] == "1"
88
122
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