diff --git a/CHANGELOG.md b/CHANGELOG.md index fcb19b8..7dcbee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/poetry_dynamic_versioning/cli.py b/poetry_dynamic_versioning/cli.py index 4323f42..1445411 100644 --- a/poetry_dynamic_versioning/cli.py +++ b/poetry_dynamic_versioning/cli.py @@ -10,6 +10,7 @@ from poetry_dynamic_versioning import ( _get_and_apply_version, _get_config, + _get_override_version, _get_pyproject_path, _get_version, _state, @@ -31,6 +32,7 @@ class Key: poetry = "poetry" dynamic = "dynamic" version = "version" + name = "name" class Command: @@ -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) @@ -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 diff --git a/tests/test_unit.py b/tests/test_unit.py index 9ea7792..c9b061e 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -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( """