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

Commit f129001

Browse files
committed
feat: Support versions on random positions
1 parent e2c7303 commit f129001

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

commitizen/bump.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,31 @@ def update_version_in_files(
150150
"""
151151
# TODO: separate check step and write step
152152
for location in files:
153-
filepath, *regexes = location.split(":", maxsplit=1)
153+
filepath, *regexes = location.split(":")
154154
regex = regexes[0] if regexes else None
155+
after = regexes[1] if regexes[1:] else None
156+
after_found = False
155157

156158
# Read in the file
157159
file_content = []
158160
current_version_found = False
159161
with open(filepath, "r") as version_file:
160162
for line in version_file:
161-
if regex:
163+
if regex and (not after or after_found):
162164
match = re.search(regex, line)
163165
if not match:
164166
file_content.append(line)
165167
continue
168+
if after:
169+
match = re.search(after, line)
170+
if match:
171+
file_content.append(line)
172+
after_found = True
173+
continue
166174

167175
# Replace the target string
168-
if current_version in line:
176+
if current_version in line and (not after or after_found):
177+
after_found = False # Consume after otherwise next matches of regex will match
169178
current_version_found = True
170179
file_content.append(line.replace(current_version, new_version))
171180
else:

tests/test_bump_update_version_in_files.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import pytest
24

35
from commitizen import bump
@@ -33,6 +35,21 @@
3335
}
3436
"""
3537

38+
# The order cannot be guaranteed here
39+
CARGO_LOCK = """
40+
[[package]]
41+
name = "textwrap"
42+
version = "1.2.3"
43+
44+
[[package]]
45+
name = "there-i-fixed-it"
46+
version = "1.2.3"
47+
48+
[[package]]
49+
name = "other-project"
50+
version = "1.2.3"
51+
"""
52+
3653

3754
@pytest.fixture(scope="function")
3855
def commitizen_config_file(tmpdir):
@@ -55,6 +72,13 @@ def inconsistent_python_version_file(tmpdir):
5572
return str(tmp_file)
5673

5774

75+
@pytest.fixture(scope="function")
76+
def random_location_version_file(tmpdir):
77+
tmp_file = tmpdir.join("Cargo.lock")
78+
tmp_file.write(CARGO_LOCK)
79+
return str(tmp_file)
80+
81+
5882
@pytest.fixture(scope="function")
5983
def version_repeated_file(tmpdir):
6084
tmp_file = tmpdir.join("package.json")
@@ -90,6 +114,19 @@ def test_partial_update_of_file(version_repeated_file):
90114
assert old_version in data
91115

92116

117+
def test_random_location(random_location_version_file):
118+
old_version = "1.2.3"
119+
new_version = "2.0.0"
120+
regex = "version"
121+
location = f"{random_location_version_file}:{regex}:there-i-fixed-it"
122+
123+
bump.update_version_in_files(old_version, new_version, [location])
124+
with open(random_location_version_file, "r") as f:
125+
data = f.read()
126+
assert len(re.findall(old_version, data)) == 2
127+
assert len(re.findall(new_version, data)) == 1
128+
129+
93130
def test_file_version_inconsistent_error(
94131
commitizen_config_file, inconsistent_python_version_file, version_repeated_file
95132
):

0 commit comments

Comments
 (0)