Skip to content

Commit

Permalink
Support override env var for 'enable' command
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jan 16, 2025
1 parent b5e1e7d commit 017a1b1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Unreleased

* Added:
* The `enable` command now supports the bypass/override environment variables
to use a different placeholder version than 0.0.0.
* Fixed:
* The `enable` command would add `"version"` to `project.dynamic` even if it was already present.
* The `enable` command would add `"version"` to `project.dynamic` even if it were already present.
([Contributed by waketzheng](https://github.com/mtkennerly/poetry-dynamic-versioning/pull/214))
* The `enable` command would set `tool.poetry.version` to `"0.0.0"`
even if the field were already present with a different value.

## v1.6.0 (2025-01-10)

Expand Down
13 changes: 9 additions & 4 deletions poetry_dynamic_versioning/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from poetry_dynamic_versioning import (
_get_and_apply_version,
_get_config,
_get_override_version,
_get_pyproject_path,
_get_version,
_state,
Expand All @@ -31,6 +32,7 @@ class Key:
poetry = "poetry"
dynamic = "dynamic"
version = "version"
name = "name"


class Command:
Expand Down Expand Up @@ -110,7 +112,10 @@ def enable() -> None:
pyproject_path.write_bytes(tomlkit.dumps(config).encode("utf-8"))


def _enable_in_doc(doc: tomlkit.TOMLDocument) -> tomlkit.TOMLDocument:
def _enable_in_doc(doc: tomlkit.TOMLDocument, env: Optional[Mapping] = None) -> tomlkit.TOMLDocument:
name = doc.get(Key.project, {}).get(Key.name) or doc.get(Key.tool, {}).get(Key.poetry, {}).get(Key.name)
placeholder_version = _get_override_version(name, env) or "0.0.0"

pdv_table = tomlkit.table().add(Key.enable, True)
tool_table = tomlkit.table().add(Key.pdv, pdv_table)

Expand Down Expand Up @@ -141,9 +146,9 @@ def _enable_in_doc(doc: tomlkit.TOMLDocument) -> tomlkit.TOMLDocument:
doc[Key.project][Key.dynamic].append(Key.version)

if doc[Key.tool].get(Key.poetry) is None:
doc[Key.tool][Key.poetry] = tomlkit.table().add(Key.version, "0.0.0")
else:
doc[Key.tool][Key.poetry][Key.version] = "0.0.0"
doc[Key.tool][Key.poetry] = tomlkit.table().add(Key.version, placeholder_version)
elif doc[Key.tool][Key.poetry].get(Key.version) is None:
doc[Key.tool][Key.poetry][Key.version] = placeholder_version

return doc

Expand Down
86 changes: 86 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,92 @@ def test__enable_in_doc__out_of_order_tables():
)


def test__enable_in_doc__project_with_version():
doc = tomlkit.parse(
textwrap.dedent(
"""
[project]
version = "1.2.3"
"""
)
)
updated = cli._enable_in_doc(doc)
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[project]
dynamic = ["version"]
[tool]
[tool.poetry-dynamic-versioning]
enable = true
[tool.poetry]
version = "0.0.0"
[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
build-backend = "poetry_dynamic_versioning.backend"
"""
)


def test__enable_in_doc__project_with_version_override():
doc = tomlkit.parse(
textwrap.dedent(
"""
[project]
"""
)
)
updated = cli._enable_in_doc(doc, env={plugin._BYPASS_ENV: "1.2.3"})
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[project]
dynamic = ["version"]
[tool]
[tool.poetry-dynamic-versioning]
enable = true
[tool.poetry]
version = "1.2.3"
[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
build-backend = "poetry_dynamic_versioning.backend"
"""
)


def test__enable_in_doc__project_with_dynamic():
doc = tomlkit.parse(
textwrap.dedent(
"""
[project]
dynamic = ["version", "dependencies"]
"""
)
)
updated = cli._enable_in_doc(doc)
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[project]
dynamic = ["version", "dependencies"]
[tool]
[tool.poetry-dynamic-versioning]
enable = true
[tool.poetry]
version = "0.0.0"
[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
build-backend = "poetry_dynamic_versioning.backend"
"""
)


def test__substitute_version_in_text__integers_only():
content = textwrap.dedent(
"""
Expand Down

0 comments on commit 017a1b1

Please sign in to comment.