From 1fc9aacd981dfb65a1e2718e30ddfe6c5636e122 Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Tue, 8 Mar 2022 22:45:36 -0500 Subject: [PATCH] #62: Add VCS bypass option --- CHANGELOG.md | 4 ++++ poetry_dynamic_versioning/__init__.py | 15 ++++++++++----- tests/integration.sh | 5 +++++ tests/test_poetry_dynamic_versioning.py | 13 ++++--------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7760a5f..454e0db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * The default list of `substitution.patterns` now handles `__version__` when it has a type annotation. ([Draft by da2ce7](https://github.com/mtkennerly/poetry-dynamic-versioning/pull/64)) +* Added: + * Option to bypass the version control system and set a hard-coded version + in an environment variable called `POETRY_DYNAMIC_VERSIONING_BYPASS`. + ([Draft by jonringer](https://github.com/mtkennerly/poetry-dynamic-versioning/pull/69)) ## v0.13.1 (2021-08-09) diff --git a/poetry_dynamic_versioning/__init__.py b/poetry_dynamic_versioning/__init__.py index 5ba96ad..ebab064 100644 --- a/poetry_dynamic_versioning/__init__.py +++ b/poetry_dynamic_versioning/__init__.py @@ -8,7 +8,7 @@ import re from importlib import import_module from pathlib import Path -from typing import Any, Mapping, MutableMapping, Optional, Sequence, Tuple +from typing import Any, Mapping, MutableMapping, Optional, Sequence import tomlkit @@ -22,6 +22,7 @@ # This is a placeholder for type hint docs since the actual type can't be # imported yet. _DUNAMAI_VERSION_ANY = Any +_BYPASS_ENV = "POETRY_DYNAMIC_VERSIONING_BYPASS" class _ProjectState: @@ -29,7 +30,7 @@ def __init__( self, path: Path = None, original_version: str = None, - version: Tuple[_DUNAMAI_VERSION_ANY, str] = None, + version: str = None, substitutions: MutableMapping[Path, str] = None, ) -> None: self.path = path @@ -155,7 +156,11 @@ def _bump_version_per_config(version: _DUNAMAI_VERSION_ANY, bump: bool) -> _DUNA return version -def _get_version(config: Mapping) -> Tuple[_DUNAMAI_VERSION_ANY, str]: +def _get_version(config: Mapping) -> str: + bypass = os.environ.get(_BYPASS_ENV) + if bypass is not None: + return bypass + import jinja2 from dunamai import ( bump_version, @@ -217,7 +222,7 @@ def _get_version(config: Mapping) -> Tuple[_DUNAMAI_VERSION_ANY, str]: tagged_metadata=config["tagged-metadata"], ) - return (version, serialized) + return serialized def _substitute_version( @@ -361,7 +366,7 @@ def alt_poetry_create(cls, *args, **kwargs): finally: os.chdir(str(current_dir)) - dynamic_version = _state.project(name).version[1] + dynamic_version = _state.project(name).version if first_time: if not _state.project(name).original_version: _state.project(name).original_version = pyproject["tool"]["poetry"]["version"] diff --git a/tests/integration.sh b/tests/integration.sh index 5cc21d5..d2c5b84 100755 --- a/tests/integration.sh +++ b/tests/integration.sh @@ -98,6 +98,11 @@ function test_bumping_enabled { ls $dummy/dist | should_fail grep .post } +function test_bypass { + POETRY_DYNAMIC_VERSIONING_BYPASS=1.2.3 $do_poetry build -v && \ + ls $dummy/dist | grep 1.2.3 +} + function run_test { cd $dummy git checkout -- $dummy diff --git a/tests/test_poetry_dynamic_versioning.py b/tests/test_poetry_dynamic_versioning.py index 89c5b74..73f798d 100644 --- a/tests/test_poetry_dynamic_versioning.py +++ b/tests/test_poetry_dynamic_versioning.py @@ -47,9 +47,7 @@ def test__get_config__with_plugin_customizations(): def test__get_version__defaults(config): - v, s = plugin._get_version(config) - assert v == Version.from_git() - assert s == Version.from_git().serialize() + assert plugin._get_version(config) == Version.from_git().serialize() def test__get_version__invalid_vcs(config): @@ -67,8 +65,7 @@ def test__get_version__invalid_style(config): def test__get_version__format_jinja(config): os.environ["FOO"] = "foo" config["format-jinja"] = "{% if true %}v1+{{ env['FOO'] }}{% endif %}" - _, v = plugin._get_version(config) - assert v == "v1+foo" + assert plugin._get_version(config) == "v1+foo" def test__get_version__format_jinja_with_enforced_style(config): @@ -81,15 +78,13 @@ def test__get_version__format_jinja_with_enforced_style(config): def test__get_version__format_jinja_imports_with_module_only(config): config["format-jinja"] = "{{ math.pow(2, 2) }}" config["format-jinja-imports"] = [{"module": "math"}] - _, v = plugin._get_version(config) - assert v == "4.0" + assert plugin._get_version(config) == "4.0" def test__get_version__format_jinja_imports_with_module_and_item(config): config["format-jinja"] = "{{ pow(2, 3) }}" config["format-jinja-imports"] = [{"module": "math", "item": "pow"}] - _, v = plugin._get_version(config) - assert v == "8.0" + assert plugin._get_version(config) == "8.0" def test__bump_version_per_config__bumping_disabled():