Skip to content

Commit

Permalink
Merge pull request #17 from claui/fix-key-error
Browse files Browse the repository at this point in the history
Add support for files with interleaved sections
  • Loading branch information
claui committed Jun 29, 2024
2 parents 9716fb5 + 35b759b commit 60536c7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
18 changes: 9 additions & 9 deletions pyproject_patcher/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ class removes all references to `setuptools-git-versioning` from

document: tomlkit.TOMLDocument

def _get_section(self, section_name: str) -> tomlkit.items.Table:
def _get_section(self, section_name: str) -> MutableMapping[str, str | tomlkit.items.Item]:
return _get_subsection(self.document, section_name)

@property
def build_system(self) -> tomlkit.items.Table:
def build_system(self) -> MutableMapping[str, str | tomlkit.items.Item]:
"""Low-level access to the `build-system` section of
`pyproject.toml`."""
return self._get_section("build-system")
Expand Down Expand Up @@ -101,12 +101,12 @@ def dynamic(self) -> MutableSequence[str]:
return section

@property
def project(self) -> tomlkit.items.Table:
def project(self) -> MutableMapping[str, str | tomlkit.items.Item]:
"""Low-level access to the `project` section of `pyproject.toml`."""
return self._get_section("project")

@property
def tool(self) -> tomlkit.items.Table:
def tool(self) -> MutableMapping[str, str | tomlkit.items.Item]:
"""Low-level access to the `tool` section of `pyproject.toml`."""
return self._get_section("tool")

Expand Down Expand Up @@ -158,17 +158,17 @@ def remove_setuptools_git_versioning_section(self) -> None:


def _get_subsection(
parent: MutableMapping[str, tomlkit.items.Item],
parent: MutableMapping[str, str | tomlkit.items.Item],
section_name: str,
) -> tomlkit.items.Table:
) -> MutableMapping[str, str | tomlkit.items.Item]:
section = parent.get(section_name)
if not isinstance(section, tomlkit.items.Table):
raise KeyError(f"Expected TOMLKit table, found {type(section)}: {section}")
if not isinstance(section, MutableMapping):
raise KeyError(f"Expected MutableMapping, found {type(section)}: {section}")
return section


def _get_subsequence(
parent: MutableMapping[str, tomlkit.items.Item],
parent: MutableMapping[str, str | tomlkit.items.Item],
section_name: str,
) -> MutableSequence[str]:
section = parent.get(section_name)
Expand Down
9 changes: 5 additions & 4 deletions pyproject_patcher/tools/setuptools_git_versioning.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
"""Manage the `setuptools_git_versioning` tool in a `pyproject.toml`."""

from collections.abc import MutableMapping
from dataclasses import dataclass
from typing import TYPE_CHECKING

import tomlkit

# This guard avoids circular imports
if TYPE_CHECKING:
from ..patcher import PyprojectPatcher

TOOL_NAME = "setuptools-git-versioning"


@dataclass(frozen=True)
class SetuptoolsGitVersioning:
"""This class wraps a `pyproject.toml` model and provides
methods to interact with the `tools.setuptools_git_versioning`
part and other entries related to it.
"""

patcher: "PyprojectPatcher"

def remove(self) -> None:
Expand All @@ -40,6 +41,6 @@ def template_ignore_dirty_git(self) -> None:
In that case, having a `.dirty` suffix would be misleading.
"""
section = self.patcher.tool.get(TOOL_NAME)
if not isinstance(section, tomlkit.items.Table):
raise KeyError(f"Expected TOMLKit table, found {type(section)}: {section}")
if not isinstance(section, MutableMapping):
raise KeyError(f"Expected MutableMapping, found {type(section)}: {section}")
section["dirty_template"] = "{tag}.post{ccount}+git.{sha}"
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,30 @@ def fixture_toml_with_git_versioning_lt_2(tmp_path: Path) -> Path:
starting_version = "0.1.0"
"""))
return path


@pytest.fixture(name='toml_with_interleaved_sections')
def fixture_toml_with_interleaved_sections(tmp_path: Path) -> Path:
path = tmp_path / 'pyproject_toml'
with open(path, encoding='utf-8', mode='w') as file:
file.write(dedent("""\
[build-system]
requires = ["setuptools", "wheel", "setuptools-git-versioning<2"]
build-backend = "setuptools.build_meta"
[project]
name = "toml_with_interleaved_sections"
description = "Test with interleaved `project` and `tool` sections"
dynamic = ["version"]
[tool.setuptools]
packages = ["toml_with_interleaved_sections"]
[project.scripts]
foo = "toml_with_interleaved_sections.foo:main"
[tool.setuptools-git-versioning]
enabled = true
starting_version = "1.0.0"
"""))
return path
15 changes: 15 additions & 0 deletions tests/test_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ def test_set_project_version(toml_with_git_versioning_lt_2: Path) -> None:
assert section.get("version") == "1.2.3"


def test_set_project_version_with_interleaved_sections(
toml_with_interleaved_sections: Path
) -> None:
# When
with patch_in_place(toml_with_interleaved_sections) as toml:
toml.set_project_version("1.2.3")

# Then
with toml_with_interleaved_sections.open() as file:
section = tomlkit.load(file).get("project")
assert isinstance(section, Mapping)
assert "version" in section
assert section.get("version") == "1.2.3"


def test_set_project_version_from_env(
toml_with_git_versioning_lt_2: Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down

0 comments on commit 60536c7

Please sign in to comment.