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

feat: Support versions on random positions #361

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
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
49 changes: 30 additions & 19 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,27 @@ def update_version_in_files(
"""
# TODO: separate check step and write step
for location in files:
filepath, *regexes = location.split(":", maxsplit=1)
filepath, *regexes = location.split(":")
regex = regexes[0] if regexes else None

# Read in the file
file_content = []
current_version_found = False
with open(filepath, "r") as version_file:
for line in version_file:
if regex:
match = re.search(regex, line)
if not match:
file_content.append(line)
continue

# Replace the target string
if current_version in line:
current_version_found = True
file_content.append(line.replace(current_version, new_version))
else:
file_content.append(line)

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 not regex:
current_version_regex = _version_to_regex(current_version)
current_version_found = bool(current_version_regex.search(version_file))
version_file = current_version_regex.sub(new_version, version_file)

if check_consistency and not current_version_found:
raise CurrentVersionNotFoundError(
Expand All @@ -180,7 +181,17 @@ def update_version_in_files(

# Write the file out again
with open(filepath, "w") as file:
file.write("".join(file_content))
file.write("".join(version_file))


def _get_line_break_position(text: str) -> int:
position = text.find("\n")
return max(position, 0)


def _version_to_regex(version: str):
clean_regex = version.replace(".", r"\.").replace("+", r"\+")
return re.compile(f"\\b{clean_regex}\\b")


def create_tag(version: Union[Version, str], tag_format: Optional[str] = None):
Expand Down
36 changes: 36 additions & 0 deletions tests/test_bump_update_version_in_files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pytest

from commitizen import bump
Expand Down Expand Up @@ -33,6 +35,21 @@
}
"""

# The order cannot be guaranteed here
CARGO_LOCK = """
[[package]]
name = "textwrap"
version = "1.2.3"

[[package]]
name = "there-i-fixed-it"
version = "1.2.3"

[[package]]
name = "other-project"
version = "1.2.3"
"""


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


@pytest.fixture(scope="function")
def random_location_version_file(tmpdir):
tmp_file = tmpdir.join("Cargo.lock")
tmp_file.write(CARGO_LOCK)
return str(tmp_file)


@pytest.fixture(scope="function")
def version_repeated_file(tmpdir):
tmp_file = tmpdir.join("package.json")
Expand Down Expand Up @@ -90,6 +114,18 @@ def test_partial_update_of_file(version_repeated_file):
assert old_version in data


def test_random_location(random_location_version_file):
old_version = "1.2.3"
new_version = "2.0.0"
location = f"{random_location_version_file}:there-i-fixed-it.+\nversion"

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)) == 2
assert len(re.findall(new_version, data)) == 1


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