Skip to content

Commit

Permalink
#62: Add VCS bypass option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Mar 9, 2022
1 parent b40453b commit 1fc9aac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 10 additions & 5 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,14 +22,15 @@
# 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:
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"]
Expand Down
5 changes: 5 additions & 0 deletions tests/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions tests/test_poetry_dynamic_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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():
Expand Down

0 comments on commit 1fc9aac

Please sign in to comment.