Skip to content

Commit

Permalink
#190: Fix out-of-order tables in TOML edit
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Sep 10, 2024
1 parent 84c9338 commit aa2658a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

* Fixed:
* The `enable` command would fail when the pyproject.toml tables were out of order.

## v1.4.0 (2024-06-17)

* Added:
Expand Down
6 changes: 3 additions & 3 deletions poetry_dynamic_versioning/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def _enable_in_doc(doc: tomlkit.TOMLDocument) -> tomlkit.TOMLDocument:
tool_table = tomlkit.table().add(Key.pdv, pdv_table)

if doc.get(Key.tool) is None:
doc.add(Key.tool, tool_table)
doc[Key.tool] = tool_table
elif doc[Key.tool].get(Key.pdv) is None: # type: ignore
doc[Key.tool].add(Key.pdv, pdv_table) # type: ignore
doc[Key.tool][Key.pdv] = pdv_table # type: ignore
else:
doc[Key.tool][Key.pdv].update(pdv_table) # type: ignore

Expand All @@ -116,7 +116,7 @@ def _enable_in_doc(doc: tomlkit.TOMLDocument) -> tomlkit.TOMLDocument:
)

if doc.get(Key.build_system) is None:
doc.add(Key.build_system, build_system_table)
doc[Key.build_system] = build_system_table
else:
doc[Key.build_system].update(build_system_table) # type: ignore

Expand Down
91 changes: 76 additions & 15 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ def test__get_override_version__combined():
def test__enable_in_doc__empty():
doc = tomlkit.parse("")
updated = cli._enable_in_doc(doc)
assert updated[cli.Key.tool][cli.Key.pdv][cli.Key.enable] is True
assert updated[cli.Key.build_system][cli.Key.requires] == cli._DEFAULT_REQUIRES
assert updated[cli.Key.build_system][cli.Key.build_backend] == cli._DEFAULT_BUILD_BACKEND
assert (
tomlkit.dumps(updated)
== textwrap.dedent(
Expand All @@ -139,9 +136,19 @@ def test__enable_in_doc__added_pdv():
)
)
updated = cli._enable_in_doc(doc)
assert updated[cli.Key.tool][cli.Key.pdv][cli.Key.enable] is True
assert updated[cli.Key.build_system][cli.Key.requires] == cli._DEFAULT_REQUIRES
assert updated[cli.Key.build_system][cli.Key.build_backend] == cli._DEFAULT_BUILD_BACKEND
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[tool]
foo = 1
[tool.poetry-dynamic-versioning]
enable = true
[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__updated_enable():
Expand All @@ -154,9 +161,16 @@ def test__enable_in_doc__updated_enable():
)
)
updated = cli._enable_in_doc(doc)
assert updated[cli.Key.tool][cli.Key.pdv][cli.Key.enable] is True
assert updated[cli.Key.build_system][cli.Key.requires] == cli._DEFAULT_REQUIRES
assert updated[cli.Key.build_system][cli.Key.build_backend] == cli._DEFAULT_BUILD_BACKEND
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[tool.poetry-dynamic-versioning]
enable = true
[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__updated_requires():
Expand All @@ -169,9 +183,16 @@ def test__enable_in_doc__updated_requires():
)
)
updated = cli._enable_in_doc(doc)
assert updated[cli.Key.tool][cli.Key.pdv][cli.Key.enable] is True
assert updated[cli.Key.build_system][cli.Key.requires] == cli._DEFAULT_REQUIRES
assert updated[cli.Key.build_system][cli.Key.build_backend] == cli._DEFAULT_BUILD_BACKEND
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
build-backend = "poetry_dynamic_versioning.backend"
[tool.poetry-dynamic-versioning]
enable = true
"""
)


def test__enable_in_doc__updated_build_backend():
Expand All @@ -184,9 +205,49 @@ def test__enable_in_doc__updated_build_backend():
)
)
updated = cli._enable_in_doc(doc)
assert updated[cli.Key.tool][cli.Key.pdv][cli.Key.enable] is True
assert updated[cli.Key.build_system][cli.Key.requires] == cli._DEFAULT_REQUIRES
assert updated[cli.Key.build_system][cli.Key.build_backend] == cli._DEFAULT_BUILD_BACKEND
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[build-system]
build-backend = "poetry_dynamic_versioning.backend"
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
[tool.poetry-dynamic-versioning]
enable = true
"""
)


def test__enable_in_doc__out_of_order_tables():
doc = tomlkit.parse(
textwrap.dedent(
"""
[tool.poetry]
name = "foo"
[build-system]
build-backend = ""
[tool.poetry.dependencies]
python = "^3.10"
"""
)
)
updated = cli._enable_in_doc(doc)
assert tomlkit.dumps(updated) == textwrap.dedent(
"""
[tool.poetry]
name = "foo"
[tool.poetry-dynamic-versioning]
enable = true
[build-system]
build-backend = "poetry_dynamic_versioning.backend"
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
[tool.poetry.dependencies]
python = "^3.10"
"""
)


def test__substitute_version_in_text__integers_only():
Expand Down

0 comments on commit aa2658a

Please sign in to comment.