From 8dfcea90c446401d2e84a2c3bf4ef79af3eaeffd Mon Sep 17 00:00:00 2001 From: Manuel Barkhau Date: Sat, 22 Dec 2018 09:49:27 +0100 Subject: [PATCH] fix: patterns/replacements for "-final" releases --- CHANGELOG.md | 7 ++++++- README.md | 44 ++++++++++++++++++++++++---------------- src/pycalver/__main__.py | 6 +++--- src/pycalver/parse.py | 10 +++++---- src/pycalver/version.py | 7 +++++++ test/test_rewrite.py | 30 +++++++++++++++++---------- test/test_version.py | 14 ++++++++++--- 7 files changed, 78 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4367af6..6873c77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,12 @@ # Changelog for https://gitlab.com/mbarkhau/pycalver -## v201812.0016 +## v201812.0018 + + - Fixed: Better handling of pattern replacements with "-final" releases. + + +## v201812.0017 - Fixed #2 on github. `pycalver init` was broken. - Fixed pattern escaping issues. diff --git a/README.md b/README.md index c386286..091f40e 100644 --- a/README.md +++ b/README.md @@ -441,8 +441,8 @@ INFO - New Version: v201809.0002-rc +[![PyCalVer v201812.0017][version_img]][version_ref] [![PyPI Releases][pypi_img]][pypi_ref] ---- myprojcet/__init__.py -+++ myprojcet/__init__.py +--- myproject/__init__.py ++++ myproject/__init__.py @@ -1,1 +1,1 @@ -__version__ = "v201809.0001-beta" +__version__ = "v201809.0002-rc" @@ -478,34 +478,42 @@ use, everything else in a pattern is treated as literal text. | `{release_tag}` | alpha | -Note that the separator/prefix characters can be part of what is -matched and generated for a given placeholder. In other words, -assuming you have the following text in your README.md (note the -two dashes before alpha): +There are two limitations to keep in mind: + + 1. A version string cannot span multiple lines. + 2. There is no way to escape "-", "." characters (yet). + +The lack of escaping may for example be an issue with badge URLs. +You may want to put the following text in your README.md (note the +two dashes before `beta` are parsed as a literal dash by shields.io): ``` -https://img.shields.io/badge/PyCalVer-v201812.0016-None-blue.svg +https://img.shields.io/badge/myproject-v201812.0116--beta-blue.svg ``` -An appropriate pattern would be: +While could use the following pattern, which will work for a while: ```ini README.md = - /badge/PyCalVer {calver}{build}-{release}-blue.svg + /badge/myproject-v{year}{month}.{build_no}--{release_tag}-blue.svg ``` -Notice that neither the "v" prefix, nor the "." and "-" -separators are included in the pattern text, as they are -respectively part of the `calver`, `build` and `release` -placeholders. Alternatively you can be more explicit. +This will eventually break though, when you do a `final` release, at +which point the following will be put in your README.md: -```ini -README.md = - /badge/PyCalVer v{year}{month}.{build_no}--{release_tag}-blue.svg +``` +https://img.shields.io/badge/myproject-v201812.0117--final-blue.svg ``` -One limitation to keep in mind is that a version string cannot -span multiple lines. +Whereas what you probably wanted was this: + +``` +https://img.shields.io/badge/myproject-v201812.0117-blue.svg +``` + +I think we can all agree that this is a travesty and the author +should be ashamed for releasing PyCalVer with such a monumental +deficiency. ### Bump It Up diff --git a/src/pycalver/__main__.py b/src/pycalver/__main__.py index 727e856..60421a1 100644 --- a/src/pycalver/__main__.py +++ b/src/pycalver/__main__.py @@ -57,11 +57,11 @@ def _init_logging(verbose: int = 0) -> None: def _validate_release_tag(release: str) -> None: - if release == 'final' or release in parse.VALID_RELEASE_VALUES: + if release in parse.VALID_RELEASE_VALUES: return log.error(f"Invalid argument --release={release}") - log.error(f"Valid arguments are: final, {', '.join(parse.VALID_RELEASE_VALUES)}") + log.error(f"Valid arguments are: {', '.join(parse.VALID_RELEASE_VALUES)}") sys.exit(1) @@ -235,7 +235,7 @@ def _bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> No metavar="", help=( f"Override release name of current_version. Valid options are: " - f"{', '.join(parse.VALID_RELEASE_VALUES)} and final." + f"{', '.join(parse.VALID_RELEASE_VALUES)}." ), ) @click.option( diff --git a/src/pycalver/parse.py b/src/pycalver/parse.py index 973d78a..0364ac5 100644 --- a/src/pycalver/parse.py +++ b/src/pycalver/parse.py @@ -12,7 +12,7 @@ import typing as typ log = logging.getLogger("pycalver.parse") -VALID_RELEASE_VALUES = ("alpha", "beta", "dev", "rc", "post") +VALID_RELEASE_VALUES = ("alpha", "beta", "dev", "rc", "post", "final") PATTERN_ESCAPES = [ @@ -38,12 +38,14 @@ PATTERN_ESCAPES = [ RE_PATTERN_PARTS = { 'pep440_version': r"\d{6}\.[1-9]\d*(a|b|dev|rc|post)?\d*", - 'version' : r"v\d{6}\.\d{4,}(\-(alpha|beta|dev|rc|post))?", + 'version' : r"v\d{6}\.\d{4,}(\-(alpha|beta|dev|rc|post|final))?", 'calver' : r"v\d{6}", + 'year' : r"\d{4}", + 'month' : r"\d{2}", 'build' : r"\.\d{4,}", 'build_no' : r"\d{4,}", - 'release' : r"(\-(alpha|beta|dev|rc|post))?", - 'release_tag' : r"(alpha|beta|dev|rc|post)?", + 'release' : r"(\-(alpha|beta|dev|rc|post|final))?", + 'release_tag' : r"(alpha|beta|dev|rc|post|final)?", } diff --git a/src/pycalver/version.py b/src/pycalver/version.py index ad68bab..99a7314 100644 --- a/src/pycalver/version.py +++ b/src/pycalver/version.py @@ -100,6 +100,10 @@ def parse_version_info(version_str: str) -> VersionInfo: kwargs = match.groupdict() kwargs['pep440_version'] = pycalver_to_pep440(kwargs['version']) + if kwargs['release'] is None: + kwargs['release'] = "-final" + if kwargs['release_tag'] is None: + kwargs['release_tag'] = "final" return VersionInfo(**kwargs) @@ -144,6 +148,9 @@ def incr(old_version: str, *, release: str = None) -> str: else: new_release = release + if new_release == 'final': + new_release = None + new_version = new_calver + "." + new_build if new_release: new_version += "-" + new_release diff --git a/test/test_rewrite.py b/test/test_rewrite.py index 973f771..f5a5ded 100644 --- a/test/test_rewrite.py +++ b/test/test_rewrite.py @@ -2,22 +2,30 @@ from pycalver import rewrite REWRITE_FIXTURE = """ -# This file is part of the pycalver project -# https://github.com/mbarkhau/pycalver -# -# (C) 2018 Manuel Barkhau (@mbarkhau) # SPDX-License-Identifier: MIT - __version__ = "v201809.0002-beta" """ def test_rewrite_lines(): - old_lines = REWRITE_FIXTURE.splitlines() - patterns = ['__version__ = "{version}"'] - new_version = "v201809.0003" - new_lines = rewrite.rewrite_lines(patterns, new_version, old_lines) + old_lines = REWRITE_FIXTURE.splitlines() + patterns = ['__version__ = "{version}"'] + new_lines = rewrite.rewrite_lines(patterns, "v201911.0003", old_lines) assert len(new_lines) == len(old_lines) - assert new_version not in "\n".join(old_lines) - assert new_version in "\n".join(new_lines) + assert "v201911.0003" not in "\n".join(old_lines) + assert "v201911.0003" in "\n".join(new_lines) + + +def test_rewrite_final(): + # Patterns written with {release_tag} placeholder preserve + # the release tag even if the new version is -final + + old_lines = REWRITE_FIXTURE.splitlines() + patterns = ['__version__ = "v{year}{month}.{build_no}-{release_tag}"'] + new_lines = rewrite.rewrite_lines(patterns, "v201911.0003", old_lines) + + assert len(new_lines) == len(old_lines) + assert "v201911.0003" not in "\n".join(old_lines) + assert "None" not in "\n".join(new_lines) + assert "v201911.0003-final" in "\n".join(new_lines) diff --git a/test/test_version.py b/test/test_version.py index a994715..c106365 100644 --- a/test/test_version.py +++ b/test/test_version.py @@ -24,9 +24,13 @@ def test_bump_final(): calver = version.current_calver() cur_version = calver + ".0001" assert cur_version < version.incr(cur_version) + assert version.incr(cur_version).endswith(".0002") assert version.incr(cur_version, release="alpha").endswith("-alpha") - assert version.incr(cur_version, release="final").endswith("0002") - assert version.incr(cur_version).endswith("0002") + + assert version.incr(cur_version, release="final").endswith(".0002") + + pre_version = cur_version + "-beta" + assert version.incr(pre_version, release="final").endswith(".0002") def test_bump_future(): @@ -65,6 +69,8 @@ def test_parse_version_info(): assert version_nfo.month == "12" assert version_nfo.build == ".0001" assert version_nfo.release == "-alpha" + assert version_nfo.build_no == "0001" + assert version_nfo.release_tag == "alpha" version_str = "v201712.0001" version_nfo = version.parse_version_info(version_str) @@ -75,7 +81,9 @@ def test_parse_version_info(): assert version_nfo.year == "2017" assert version_nfo.month == "12" assert version_nfo.build == ".0001" - assert version_nfo.release is None + assert version_nfo.release == "-final" + assert version_nfo.build_no == "0001" + assert version_nfo.release_tag == "final" def test_readme_pycalver1():