Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: set intermediate TOML tables to be super table #368

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading