Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 127d1b0

Browse files
jaysonsantosLee-W
authored andcommitted
fix: fix multiple versions bumps when version changes the string size
1 parent b94418b commit 127d1b0

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

commitizen/bump.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,15 @@ def update_version_in_files(
152152
for location in files:
153153
filepath, *regexes = location.split(":")
154154
regex = regexes[0] if regexes else None
155-
current_version_found = False
156155

157156
with open(filepath, "r") as f:
158157
version_file = f.read()
159158

160159
if regex:
161-
for match in re.finditer(regex, version_file, re.MULTILINE):
162-
left = version_file[: match.end()]
163-
right = version_file[match.end() :]
164-
line_break = _get_line_break_position(right)
165-
middle = right[:line_break]
166-
current_version_found = current_version in middle
167-
right = right[line_break:]
168-
version_file = (
169-
left + middle.replace(current_version, new_version) + right
170-
)
171-
172-
if not regex:
160+
current_version_found, version_file = _bump_with_regex(
161+
version_file, current_version, new_version, regex
162+
)
163+
else:
173164
current_version_regex = _version_to_regex(current_version)
174165
current_version_found = bool(current_version_regex.search(version_file))
175166
version_file = current_version_regex.sub(new_version, version_file)
@@ -186,6 +177,27 @@ def update_version_in_files(
186177
file.write("".join(version_file))
187178

188179

180+
def _bump_with_regex(version_file_contents, current_version, new_version, regex):
181+
current_version_found = False
182+
# Bumping versions that change the string length move the offset on the file contents as finditer keeps a
183+
# reference to the initial string that was used and calling search many times would lead in infinite loops
184+
# e.g.: 1.1.9 -> 1.1.20
185+
offset = 0
186+
for match in re.finditer(regex, version_file_contents, re.MULTILINE):
187+
left = version_file_contents[: match.end() + offset]
188+
right = version_file_contents[match.end() + offset :]
189+
line_break = _get_line_break_position(right)
190+
middle = right[:line_break]
191+
current_version_found_in_block = current_version in middle
192+
offset += len(new_version) - len(current_version)
193+
current_version_found |= current_version_found_in_block
194+
right = right[line_break:]
195+
version_file_contents = (
196+
left + middle.replace(current_version, new_version) + right
197+
)
198+
return current_version_found, version_file_contents
199+
200+
189201
def _get_line_break_position(text: str) -> int:
190202
position = text.find("\n")
191203
return max(position, 0)

tests/test_bump_update_version_in_files.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
version = "1.2.3"
5151
"""
5252

53+
MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n' * 30
54+
MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n' * 30
55+
5356

5457
@pytest.fixture(scope="function")
5558
def commitizen_config_file(tmpdir):
@@ -86,6 +89,20 @@ def version_repeated_file(tmpdir):
8689
return str(tmp_file)
8790

8891

92+
@pytest.fixture(scope="function")
93+
def multiple_versions_increase_string(tmpdir):
94+
tmp_file = tmpdir.join("anyfile")
95+
tmp_file.write(MULTIPLE_VERSIONS_INCREASE_STRING)
96+
return str(tmp_file)
97+
98+
99+
@pytest.fixture(scope="function")
100+
def multiple_versions_reduce_string(tmpdir):
101+
tmp_file = tmpdir.join("anyfile")
102+
tmp_file.write(MULTIPLE_VERSIONS_REDUCE_STRING)
103+
return str(tmp_file)
104+
105+
89106
@pytest.fixture(scope="function")
90107
def version_files(commitizen_config_file, python_version_file, version_repeated_file):
91108
return [commitizen_config_file, python_version_file, version_repeated_file]
@@ -138,6 +155,32 @@ def test_duplicates_are_change_with_no_regex(random_location_version_file):
138155
assert len(re.findall(new_version, data)) == 3
139156

140157

158+
def test_version_bump_increase_string_length(multiple_versions_increase_string):
159+
old_version = "1.2.9"
160+
new_version = "1.2.10"
161+
version_bump_change_string_size(
162+
multiple_versions_increase_string, old_version, new_version
163+
)
164+
165+
166+
def test_version_bump_reduce_string_length(multiple_versions_reduce_string):
167+
old_version = "1.2.10"
168+
new_version = "2.0.0"
169+
version_bump_change_string_size(
170+
multiple_versions_reduce_string, old_version, new_version
171+
)
172+
173+
174+
def version_bump_change_string_size(filename, old_version, new_version):
175+
location = f"{filename}:version"
176+
177+
bump.update_version_in_files(old_version, new_version, [location])
178+
with open(filename, "r") as f:
179+
data = f.read()
180+
assert len(re.findall(old_version, data)) == 0
181+
assert len(re.findall(new_version, data)) == 30
182+
183+
141184
def test_file_version_inconsistent_error(
142185
commitizen_config_file, inconsistent_python_version_file, version_repeated_file
143186
):

0 commit comments

Comments
 (0)