From 8269c7c03dd506896e3c8554d64a1ddb60accdf7 Mon Sep 17 00:00:00 2001 From: Bernard Cooke Date: Tue, 23 Aug 2022 21:57:22 +0100 Subject: [PATCH] fix: account for trailing newlines in commit messages Resolves issue #490 --- semantic_release/history/logs.py | 11 +++++-- tests/history/__init__.py | 4 +-- tests/history/test_version_bump.py | 53 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/semantic_release/history/logs.py b/semantic_release/history/logs.py index 3e37dad7d..110c3a513 100644 --- a/semantic_release/history/logs.py +++ b/semantic_release/history/logs.py @@ -38,7 +38,9 @@ def evaluate_version_bump(current_version: str, force: str = None) -> Optional[s current_version_commit = get_formatted_commit(current_version) for _hash, commit_message in get_commit_log(current_version): - if commit_message == current_version_commit: + # https://github.com/relekang/python-semantic-release/issues/490 - + # commit messages (which we compare with ==) have a trailing newline + if commit_message.strip() == current_version_commit.strip(): # Stop once we reach the current version # (we are looping in the order of newest -> oldest) logger.debug( @@ -115,7 +117,12 @@ def generate_changelog( ) found_the_release = True - if from_version_commit and commit_message == from_version_commit: + # See https://github.com/relekang/python-semantic-release/issues/490 - + # commit messages (which we compare with ==) have a trailing newline + if ( + from_version_commit + and commit_message.strip() == from_version_commit.strip() + ): # We reached the previous release logger.debug(f"{from_version} reached, ending changelog generation") break diff --git a/tests/history/__init__.py b/tests/history/__init__.py index d4cbf04a6..5cf16e4f1 100644 --- a/tests/history/__init__.py +++ b/tests/history/__init__.py @@ -39,11 +39,11 @@ PATCH_COMMIT_MESSAGES = [PATCH, PATCH] MAJOR_LAST_RELEASE_MINOR_AFTER = [ MINOR, - ("22", "1.1.0\n\nAutomatically generated by python-semantic-release"), + ("22", "1.1.0\n\nAutomatically generated by python-semantic-release\n"), MAJOR, ] MAJOR_MENTIONING_LAST_VERSION = [ MAJOR_MENTIONING_1_0_0, - ("22", "1.0.0\n\nAutomatically generated by python-semantic-release"), + ("22", "1.0.0\n\nAutomatically generated by python-semantic-release\n"), MAJOR, ] diff --git a/tests/history/test_version_bump.py b/tests/history/test_version_bump.py index 4f9d6c1a2..b168b8564 100644 --- a/tests/history/test_version_bump.py +++ b/tests/history/test_version_bump.py @@ -107,6 +107,44 @@ def test_should_search_for_release_with_commit_subject_pattern(): assert evaluate_version_bump("1.0.0") == "minor" +@mock.patch( + "semantic_release.history.config.get", + wrapped_config_get( + commit_subject="chore(release): {version}", + commit_message="Custom message", + ), +) +@mock.patch( + "semantic_release.history.logs.get_commit_log", + lambda *a, **kw: [ + MINOR, + ("777", "chore(release): 1.0.0\n\nCustom message \n"), + MAJOR, + ], +) +def test_should_search_for_release_with_commit_subject_pattern_including_whitespace(): + assert evaluate_version_bump("1.0.0") == "minor" + + +@mock.patch( + "semantic_release.history.config.get", + wrapped_config_get( + commit_subject="chore(release): {version}", + commit_message="Custom message ", + ), +) +@mock.patch( + "semantic_release.history.logs.get_commit_log", + lambda *a, **kw: [ + MINOR, + ("777", "chore(release): 1.0.0\n\nCustom message\n"), + MAJOR, + ], +) +def test_should_search_for_release_with_commit_subject_pattern_including_whitespace_in_configuration(): + assert evaluate_version_bump("1.0.0") == "minor" + + @mock.patch( "semantic_release.history.config.get", wrapped_config_get( @@ -120,3 +158,18 @@ def test_should_search_for_release_with_commit_subject_pattern(): ) def test_should_not_search_for_release_without_commit_subject_pattern(): assert evaluate_version_bump("1.0.0") == "major" + + +@mock.patch( + "semantic_release.history.config.get", + wrapped_config_get( + commit_subject="chore(release): {version}", + commit_message="Custom message", + ), +) +@mock.patch( + "semantic_release.history.logs.get_commit_log", + lambda *a, **kw: [MINOR, ("777", "1.0.0\n"), MAJOR], +) +def test_should_not_search_for_release_without_commit_subject_pattern_including_whitespace(): + assert evaluate_version_bump("1.0.0") == "major"