diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-06-29 09:49:22 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-06-29 18:17:19 +0200 |
commit | 8efabe9e7f23515a1d4eeaf9c1e01ff5b48cdf68 (patch) | |
tree | bf1b7776c54399a4d7346126a851395878abe299 | |
parent | f3aaac38547e0dd9e1942a647e603465cf09ecff (diff) | |
download | astroid-git-8efabe9e7f23515a1d4eeaf9c1e01ff5b48cdf68.tar.gz |
Create a check function in bumb changelog script
-rw-r--r-- | script/bump_changelog.py | 73 | ||||
-rw-r--r-- | script/test_bump_changelog.py | 9 |
2 files changed, 51 insertions, 31 deletions
diff --git a/script/bump_changelog.py b/script/bump_changelog.py index 4478b169..69801536 100644 --- a/script/bump_changelog.py +++ b/script/bump_changelog.py @@ -9,16 +9,11 @@ from datetime import datetime from pathlib import Path DEFAULT_CHANGELOG_PATH = Path("ChangeLog") -err = "in the changelog, fix that first!" -NEW_VERSION_ERROR_MSG = "The text for this version '{version}' did not exists %s" % err -NEXT_VERSION_ERROR_MSG = ( - "The text for the next version '{version}' already exists %s" % err -) -TODAY = datetime.now() +RELEASE_DATE_TEXT = "Release Date: TBA" WHATS_NEW_TEXT = "What's New in astroid" +TODAY = datetime.now() FULL_WHATS_NEW_TEXT = WHATS_NEW_TEXT + " {version}?" -RELEASE_DATE_TEXT = "Release Date: TBA" NEW_RELEASE_DATE_MESSAGE = "Release Date: {}".format(TODAY.strftime("%Y-%m-%d")) @@ -68,10 +63,50 @@ def get_version_type(version: str) -> VersionType: return version_type +def get_whats_new( + version: str, add_date: bool = False, change_date: bool = False +) -> str: + whats_new_text = FULL_WHATS_NEW_TEXT.format(version=version) + result = [whats_new_text, "=" * len(whats_new_text)] + if add_date and change_date: + result += [NEW_RELEASE_DATE_MESSAGE] + elif add_date: + result += [RELEASE_DATE_TEXT] + elif change_date: + raise ValueError("Can't use change_date=True with add_date=False") + logging.debug( + f"version='{version}', add_date='{add_date}', change_date='{change_date}': {result}" + ) + return "\n".join(result) + + def transform_content(content: str, version: str) -> str: version_type = get_version_type(version) next_version = get_next_version(version, version_type) - wn_next_version = FULL_WHATS_NEW_TEXT.format(version=next_version) + old_date = get_whats_new(version, add_date=True) + new_date = get_whats_new(version, add_date=True, change_date=True) + next_version_with_date = get_whats_new(next_version, add_date=True) + do_checks(content, next_version, old_date, version, version_type) + index = content.find(old_date) + logging.debug(f"Replacing\n'{old_date}'\nby\n'{new_date}'\n") + content = content.replace(old_date, new_date) + end_content = content[index:] + content = content[:index] + logging.debug(f"Adding:\n'{next_version_with_date}'\n") + content += next_version_with_date + "\n" * 4 + end_content + return content + + +def do_checks(content, next_version, old_date, version, version_type): + err = "in the changelog, fix that first!" + NEW_VERSION_ERROR_MSG = ( + "The text for this version '{version}' did not exists %s" % err + ) + NEXT_VERSION_ERROR_MSG = ( + "The text for the next version '{version}' already exists %s" % err + ) + wn_next_version = get_whats_new(next_version) + wn_this_version = get_whats_new(version) # There is only one field where the release date is TBA if version_type in [VersionType.MAJOR, VersionType.MINOR]: assert ( @@ -83,29 +118,13 @@ def transform_content(content: str, version: str) -> str: content.count(RELEASE_DATE_TEXT) <= 2 ), f"There should be only two release dates 'TBA' ({version} and {next_minor_version}) {err}" # There is already a release note for the version we want to release - assert ( - content.count(FULL_WHATS_NEW_TEXT.format(version=version)) == 1 - ), NEW_VERSION_ERROR_MSG.format(version=version) + assert content.count(wn_this_version) == 1, NEW_VERSION_ERROR_MSG.format( + version=version + ) # There is no release notes for the next version assert content.count(wn_next_version) == 0, NEXT_VERSION_ERROR_MSG.format( version=next_version ) - index = content.find(WHATS_NEW_TEXT) - logging.debug(f"Replacing '{RELEASE_DATE_TEXT}' by '{NEW_RELEASE_DATE_MESSAGE}'") - content = content.replace(RELEASE_DATE_TEXT, NEW_RELEASE_DATE_MESSAGE) - end_content = content[index:] - content = content[:index] - to_add = ( - wn_next_version - + "\n" - + "=" * len(wn_next_version) - + "\n" - + RELEASE_DATE_TEXT - + "\n" * 4 - ) - logging.debug(f"Adding:'{to_add}'") - content += to_add + end_content - return content if __name__ == "__main__": diff --git a/script/test_bump_changelog.py b/script/test_bump_changelog.py index cfb27e42..a52c1c72 100644 --- a/script/test_bump_changelog.py +++ b/script/test_bump_changelog.py @@ -65,10 +65,10 @@ Release Date: TBA What's New in astroid 2.6.1? ============================ -Release Date: 2012-02-05 +Release Date: TBA """, "2.6.1", - "the next version '2.6.2' already exists", + "The text for the next version '2.6.2' already exists", ], [ """ @@ -98,7 +98,8 @@ Release Date: TBA ], ], ) -def test_update_content_error(old_content, version, expected_error): +def test_update_content_error(old_content, version, expected_error, caplog): + caplog.set_level(logging.DEBUG) with pytest.raises(AssertionError, match=expected_error): transform_content(old_content, version) @@ -132,4 +133,4 @@ Release Date: 20""" new_content = transform_content(old_content, "2.6.1") assert new_content.startswith( expected_beginning - ), f"Wrong start for:'{new_content[len(expected_beginning):]}'" + ), f"Wrong start for:'{new_content[:len(expected_beginning)]}'" |