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

Commit 6632b45

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

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import pytest
23

34
from commitizen import bump
@@ -33,6 +34,21 @@
3334
}
3435
"""
3536

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

3753
@pytest.fixture(scope="function")
3854
def commitizen_config_file(tmpdir):
@@ -55,6 +71,13 @@ def inconsistent_python_version_file(tmpdir):
5571
return str(tmp_file)
5672

5773

74+
@pytest.fixture(scope="function")
75+
def random_location_version_file(tmpdir):
76+
tmp_file = tmpdir.join("Cargo.lock")
77+
tmp_file.write(CARGO_LOCK)
78+
return str(tmp_file)
79+
80+
5881
@pytest.fixture(scope="function")
5982
def version_repeated_file(tmpdir):
6083
tmp_file = tmpdir.join("package.json")
@@ -90,6 +113,19 @@ def test_partial_update_of_file(version_repeated_file):
90113
assert old_version in data
91114

92115

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

0 commit comments

Comments
 (0)