From e8841ec30abaffc5a4597eb010d2520e36131c11 Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Sat, 20 Mar 2021 22:39:33 +0100 Subject: [PATCH 1/2] feat: Support versions on random positions --- commitizen/bump.py | 47 +++++++++++++--------- tests/test_bump_update_version_in_files.py | 36 +++++++++++++++++ 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index ccb1257e7d..2d0c7aee83 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -150,26 +150,25 @@ 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) + + version_file = open(filepath, "r").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 = current_version_regex.search(version_file) and True + version_file = current_version_regex.sub(new_version, version_file) if check_consistency and not current_version_found: raise CurrentVersionNotFoundError( @@ -180,7 +179,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): diff --git a/tests/test_bump_update_version_in_files.py b/tests/test_bump_update_version_in_files.py index 26a6ad677a..cadf4af8d2 100644 --- a/tests/test_bump_update_version_in_files.py +++ b/tests/test_bump_update_version_in_files.py @@ -1,3 +1,5 @@ +import re + import pytest from commitizen import bump @@ -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): @@ -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") @@ -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 ): From b7bf4519f116c13217597983ef23fbf6f221c0b1 Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Wed, 31 Mar 2021 17:45:14 +0200 Subject: [PATCH 2/2] chore: Fix CR questions --- commitizen/bump.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index 2d0c7aee83..b91639289f 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -154,7 +154,9 @@ def update_version_in_files( regex = regexes[0] if regexes else None current_version_found = False - version_file = open(filepath, "r").read() + 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()] @@ -167,7 +169,7 @@ def update_version_in_files( if not regex: current_version_regex = _version_to_regex(current_version) - current_version_found = current_version_regex.search(version_file) and True + 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: