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

fix(wip): add test for current breaking change #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,17 @@ def update_version_in_files(
with open(filepath, "r") as f:
version_file = f.read()

match = regex and re.search(regex, version_file, re.MULTILINE)
if match:
left = version_file[: match.end()]
right = version_file[match.end() :]
line_break = _get_line_break_position(right)
middle = right[:line_break]
current_version_found = current_version in middle
right = right[line_break:]
version_file = left + middle.replace(current_version, new_version) + right
if regex:
for match in re.finditer(regex, version_file, re.MULTILINE):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there, sorry to hear that my change broke the tool.
I think this approach could have a problem if the version would have a decimal change like from 9 to 10 as IIRC finditer still holds a reference to an old string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reproduce the error you could use this:

diff --git a/tests/test_bump_update_version_in_files.py b/tests/test_bump_update_version_in_files.py
index aebd879..3d289e2 100644
--- a/tests/test_bump_update_version_in_files.py
+++ b/tests/test_bump_update_version_in_files.py
@@ -39,15 +39,39 @@ REPEATED_VERSION_NUMBER = """
 CARGO_LOCK = """
 [[package]]
 name = "textwrap"
-version = "1.2.3"
+version = "1.2.9"
 
 [[package]]
 name = "there-i-fixed-it"
-version = "1.2.3"
+version = "1.2.9"
 
 [[package]]
 name = "other-project"
-version = "1.2.3"
+version = "1.2.9"
+
+[[package]]
+name = "other-project"
+version = "1.2.9"
+
+[[package]]
+name = "other-project"
+version = "1.2.9"
+
+[[package]]
+name = "other-project"
+version = "1.2.9"
+
+[[package]]
+name = "other-project"
+version = "1.2.9"
+
+[[package]]
+name = "other-project"
+version = "1.2.9"
+
+[[package]]
+name = "other-project"
+version = "1.2.9"
 """
 
 
@@ -115,8 +139,8 @@ def test_partial_update_of_file(version_repeated_file):
 
 
 def test_random_location(random_location_version_file):
-    old_version = "1.2.3"
-    new_version = "2.0.0"
+    old_version = "1.2.9"
+    new_version = "1.2.10"
     location = f"{random_location_version_file}:there-i-fixed-it.+\nversion"
 
     bump.update_version_in_files(old_version, new_version, [location])
@@ -127,8 +151,8 @@ def test_random_location(random_location_version_file):
 
 
 def test_duplicates_are_change_with_no_regex(random_location_version_file):
-    old_version = "1.2.3"
-    new_version = "2.0.0"
+    old_version = "1.2.9"
+    new_version = "1.2.10"
     location = f"{random_location_version_file}:version"
 
     bump.update_version_in_files(old_version, new_version, [location])

i think that a way (hacky though) to fix this would keep and offset variable that gets the string difference from the old version to the new (it can be positive and negative) so you take accountable this kind of problem

left = version_file[: match.end()]
right = version_file[match.end() :]
line_break = _get_line_break_position(right)
middle = right[:line_break]
current_version_found = current_version in middle
right = right[line_break:]
version_file = (
left + middle.replace(current_version, new_version) + right
)

if not regex:
current_version_regex = _version_to_regex(current_version)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_bump_update_version_in_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ def test_random_location(random_location_version_file):
assert len(re.findall(new_version, data)) == 1


def test_duplicates_are_change_with_no_regex(random_location_version_file):
old_version = "1.2.3"
new_version = "2.0.0"
location = f"{random_location_version_file}:version"

bump.update_version_in_files(old_version, new_version, [location])
with open(random_location_version_file, "r") as f:
data = f.read()
assert len(re.findall(old_version, data)) == 0
assert len(re.findall(new_version, data)) == 3


def test_file_version_inconsistent_error(
commitizen_config_file, inconsistent_python_version_file, version_repeated_file
):
Expand Down