Skip to content

Commit

Permalink
Merge pull request #42 from callowayproject/14-commit-and-tag-message…
Browse files Browse the repository at this point in the history
…s-fix

Fix incorrect versions reported in commit and tag messages
  • Loading branch information
coordt authored Aug 7, 2023
2 parents 0e53625 + 130bbe0 commit 2319c0c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 65 deletions.
3 changes: 2 additions & 1 deletion bumpversion/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ def do_bump(

configured_files = resolve_file_config(config.files, config.version_config)
modify_files(configured_files, version, next_version, ctx, dry_run)

update_config_file(config_file, config.current_version, next_version_str, dry_run)

ctx = get_context(config, version, next_version)
ctx["new_version"] = next_version_str
commit_and_tag(config, config_file, configured_files, ctx, dry_run)


Expand Down
4 changes: 2 additions & 2 deletions bumpversion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def get_context(
{c: c for c in ("#", ";")},
)
if current_version:
ctx.new_child({f"current_{part}": current_version[part].value for part in current_version})
ctx = ctx.new_child({f"current_{part}": current_version[part].value for part in current_version})
if new_version:
ctx.new_child({f"new_{part}": new_version[part].value for part in new_version})
ctx = ctx.new_child({f"new_{part}": new_version[part].value for part in new_version})
return ctx


Expand Down
41 changes: 41 additions & 0 deletions tests/fixtures/csharp/.bumpversion.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[tool.bumpversion]
current_version = "3.1.0-rc+build.1031"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(-(?P<release>[0-9A-Za-z]+))?(\\+build\\.(?P<build>.[0-9A-Za-z]+))?"
serialize = ["{major}.{minor}.{patch}-{release}+build.{build}", "{major}.{minor}.{patch}+build.{build}"]
commit = true
message = "Bump version: {current_version} -> {new_version}"
tag = false
tag_name = "{new_version}"
tag_message = "Version {new_version}"
allow_dirty = true

[[tool.bumpversion.files]]
filename = "FULL_VERSION.txt"

[[tool.bumpversion.files]]
filename = "AssemblyInfo.cs"
search = "[assembly: AssemblyFileVersion(\"{current_version}\")]"
replace = "[assembly: AssemblyFileVersion(\"{new_version}\")]"

[[tool.bumpversion.files]]
filename = "AssemblyInfo.cs"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<build>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{build}.{patch}"]
search = "[assembly: AssemblyVersion(\"{current_version}\")]"
replace = "[assembly: AssemblyVersion(\"{new_version}\")]"

[[tool.bumpversion.files]]
filename = "Version.csv"
parse = "(?P<major>\\d+);(?P<minor>\\d+);(?P<patch>\\d+);(?P<release>[0-9A-Za-z]+)?;(build\\.(?P<build>.[0-9A-Za-z]+))?"
serialize = ["{major};{minor};{patch};{release};build.{build}", "{major};{minor};{patch};;build.{build}"]
search = "1;{current_version}"
replace = "1;{new_version}"

[tool.bumpversion.parts]
[tool.bumpversion.parts.release]
values = ["beta", "rc", "final"]
optional_value = "final"

[tool.bumpversion.parts.build]
first_value = 1000
independent = true
2 changes: 2 additions & 0 deletions tests/fixtures/csharp/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[assembly: AssemblyFileVersion("3.1.0-rc+build.1031")]
[assembly: AssemblyVersion("3.1.1031.0")]
1 change: 1 addition & 0 deletions tests/fixtures/csharp/FULL_VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.0-rc+build.1031
1 change: 1 addition & 0 deletions tests/fixtures/csharp/Version.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1;3;1;0;rc;build.1031
101 changes: 39 additions & 62 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""File processing tests."""
import os
import shutil
from datetime import datetime, timezone
from pathlib import Path
from textwrap import dedent

import pytest
from pytest import param

from bumpversion import exceptions, files
from bumpversion import exceptions, files, config, bump
from bumpversion.utils import get_context
from bumpversion.exceptions import VersionNotFoundError
from bumpversion.version_part import VersionConfig
from tests.conftest import get_config_data, inside_dir


Expand Down Expand Up @@ -154,71 +156,46 @@ def test_multi_file_configuration(tmp_path: Path):
assert build_num_path.read_text() == "2.0.1+jane+38945"


def test_raises_correct_missing_version_string(tmp_path: Path):
full_vers_path = tmp_path / "FULL_VERSION.txt"
full_vers_path.write_text("3.1.0-rc+build.1031")
assembly_path = tmp_path / "AssemblyInfo.cs"
assembly_path.write_text(
'[assembly: AssemblyFileVersion("3.1.0-rc+build.1031")]\n' '[assembly: AssemblyVersion("3.1.1031.0")]'
)
csv_path = tmp_path / "Version.csv"
csv_path.write_text("1;3-1;0;rc;build.1031")
def test_raises_correct_missing_version_string(tmp_path: Path, fixtures_path: Path):
"""When a file is missing the version string, the error should indicate the correct serialization missing."""
csharp_path = fixtures_path / "csharp"
dst_path: Path = shutil.copytree(csharp_path, tmp_path / "csharp")

overrides = {
"current_version": "3.1.0-rc+build.1031",
"parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(-(?P<release>[0-9A-Za-z]+))?(\+build\.(?P<build>.[0-9A-Za-z]+))?",
"serialize": ["{major}.{minor}.{patch}-{release}+build.{build}", "{major}.{minor}.{patch}+build.{build}"],
"commit": True,
"message": "Bump version: {current_version} -> {new_version}",
"tag": False,
"tag_name": "{new_version}",
"tag_message": "Version {new_version}",
"allow_dirty": True,
"files": [
{
"filename": str(full_vers_path),
},
{
"filename": str(assembly_path),
"search": '[assembly: AssemblyFileVersion("{current_version}")]',
"replace": '[assembly: AssemblyFileVersion("{new_version}")]',
},
{
"filename": str(assembly_path),
"parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<build>\d+)\.(?P<patch>\d+)",
"serialize": ["{major}.{minor}.{build}.{patch}"],
"search": '[assembly: AssemblyVersion("{current_version}")]',
"replace": '[assembly: AssemblyVersion("{new_version}")]',
},
{
"filename": str(csv_path),
"parse": r"(?P<major>\d+);(?P<minor>\d+);(?P<patch>\d+);(?P<release>[0-9A-Za-z]+)?;(build\.(?P<build>.[0-9A-Za-z]+))?",
"serialize": [
"{major};{minor};{patch};{release};build.{build}",
"{major};{minor};{patch};;build.{build}",
],
"search": "1;{current_version}",
"replace": "1;{new_version}",
},
],
"parts": {
"release": {"values": ["beta", "rc", "final"], "optional_value": "final"},
"build": {
"first_value": 1000,
"independent": True,
},
},
}
conf, version_config, current_version = get_config_data(overrides)
major_version = current_version.bump("patch", version_config.order)
with inside_dir(dst_path):
conf = config.get_configuration(config_file=dst_path.joinpath(".bumpversion.toml"))
version_config = VersionConfig(conf.parse, conf.serialize, conf.search, conf.replace, conf.parts)
current_version = version_config.parse(conf.current_version)
dst_path.joinpath("Version.csv").write_text("1;3-1;0;rc;build.1031")

ctx = get_context(conf)
major_version = current_version.bump("patch", version_config.order)
ctx = get_context(conf)

configured_files = [files.ConfiguredFile(file_cfg, version_config) for file_cfg in conf.files]

with pytest.raises(VersionNotFoundError) as e:
files.modify_files(configured_files, current_version, major_version, ctx)
assert e.message.startswith("Did not find '1;3;1;0;rc;build.1031'")


def test_uses_correct_serialization_for_tag_and_commit_messages(git_repo: Path, fixtures_path: Path, caplog):
"""The tag and commit messages should use the root configured serialization."""
import subprocess
import logging

caplog.set_level(logging.INFO)

csharp_path = fixtures_path / "csharp"
dst_path: Path = shutil.copytree(csharp_path, git_repo / "csharp")
subprocess.check_call(["git", "add", "."], cwd=git_repo)
subprocess.check_call(["git", "commit", "-m", "Initial commit"], cwd=git_repo)
subprocess.check_call(["git", "tag", "3.1.0-rc+build.1031"], cwd=git_repo)

configured_files = [files.ConfiguredFile(file_cfg, version_config) for file_cfg in conf.files]
with inside_dir(dst_path):
config_file = dst_path.joinpath(".bumpversion.toml")
conf = config.get_configuration(config_file=config_file)
bump.do_bump("patch", None, conf, config_file, dry_run=True)

with pytest.raises(VersionNotFoundError) as e:
files.modify_files(configured_files, current_version, major_version, ctx)
assert e.message.startswith("Did not find '1;3;1;0;rc;build.1031'")
assert "Bump version: 3.1.0-rc+build.1031 -> 3.1.1" in caplog.text


def test_search_replace_to_avoid_updating_unconcerned_lines(tmp_path: Path, caplog):
Expand Down

0 comments on commit 2319c0c

Please sign in to comment.