Skip to content

Commit

Permalink
FIX: set intermediate TOML tables to be super table (#368)
Browse files Browse the repository at this point in the history
* DX: add `create_sub_table()` test for super tables
  • Loading branch information
redeboer authored Aug 12, 2024
1 parent 84e5d7f commit 21e5a77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/compwa_policy/utilities/pyproject/setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ def _sort_taplo(items: Iterable[str]) -> list[str]:
def create_sub_table(config: Mapping[str, Any], dotted_header: str) -> Table:
"""Create a TOML sub-table through a dotted header key."""
current_table: Any = config
for header in dotted_header.split("."):
header_hierarchy = dotted_header.split(".")
for i, header in enumerate(header_hierarchy, 1):
if header not in current_table:
current_table[header] = tomlkit.table()
is_last_header = i == len(header_hierarchy)
current_table[header] = tomlkit.table(is_super_table=not is_last_header)
current_table = current_table[header]
return current_table

Expand Down
20 changes: 20 additions & 0 deletions tests/utilities/pyproject/test_setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,23 @@ def test_create_sub_table(table_key: str):
lint = ["ruff"]
""")
assert new_content.strip() == expected.strip()


def test_create_sub_table_with_super_table():
pyproject = load_pyproject_toml("", modifiable=True)
pixi = create_sub_table(pyproject, "tool.pixi")
pixi["channels"] = ["conda-forge"]
pixi["platforms"] = ["linux-64"]
task_table = create_sub_table(pyproject, "tool.pixi.feature.dev.tasks.test")
task_table["cmd"] = "pytest"

new_content = tomlkit.dumps(pyproject)
expected = dedent("""
[tool.pixi]
channels = ["conda-forge"]
platforms = ["linux-64"]
[tool.pixi.feature.dev.tasks.test]
cmd = "pytest"
""")
assert new_content.strip() == expected.strip()

0 comments on commit 21e5a77

Please sign in to comment.