Skip to content

Commit

Permalink
fix(Poetry 2): add support for PEP 621 pyproject configuration (#73)
Browse files Browse the repository at this point in the history
* fix(Poetry 2): add support for PEP 621 pyproject configuration

* bump version to 1.8.1
  • Loading branch information
DavidVujic authored Jan 6, 2025
1 parent 3fe9d26 commit f7fd5c6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
9 changes: 7 additions & 2 deletions poetry_multiproject_plugin/components/toml/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@ def generate_valid_dist_project_file(

copy["tool"]["poetry"]["packages"].multiline(True)

scripts = copy["tool"]["poetry"].get("scripts", {})
scripts_pep_621 = copy.get("project", {}).get("scripts", {})
scripts_poetry = copy["tool"]["poetry"].get("scripts", {})

scripts = scripts_pep_621 or scripts_poetry
if top_ns and scripts:
rewritten_scripts = {
k: to_valid_entry(v, top_ns, data) for k, v in scripts.items()
}

copy["tool"]["poetry"]["scripts"] = rewritten_scripts
if scripts_pep_621:
copy["project"]["scripts"] = rewritten_scripts
else:
copy["tool"]["poetry"]["scripts"] = rewritten_scripts

return tomlkit.dumps(copy)
4 changes: 3 additions & 1 deletion poetry_multiproject_plugin/components/toml/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def package_paths(path: Path) -> List[Path]:
def project_name(path: Path) -> str:
data: dict = toml(path)

return data["tool"]["poetry"]["name"]
pep_621_name = data.get("project", {}).get("name")

return pep_621_name or data["tool"]["poetry"]["name"]


def parse_exclude_path(data: dict) -> Union[str, None]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetry-multiproject-plugin"
version = "1.8.0"
version = "1.8.1"
description = "A Poetry plugin that makes it possible to use relative package includes."
authors = ["David Vujic"]
license = "MIT"
Expand Down
16 changes: 16 additions & 0 deletions test/components/toml/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
my_cli = "hello.console.app:run"
"""

pyproject_pep_621_cli = """\
[tool.poetry]
packages = [
{include = "hello", from = "../world"},
]
[project.scripts]
my_cli = "hello.console.app:run"
"""

pyproject_cli_with_local_modules = """\
[tool.poetry]
packages = [
Expand Down Expand Up @@ -58,6 +68,12 @@ def test_generate_project_file_with_custom_namespace_in_script_entry_point():
assert data["tool"]["poetry"]["scripts"] == {"my_cli": "xyz.hello.console.app:run"}


def test_generate_project_file_with_custom_namespace_in_script_entry_point_for_pep_621_project():
data = generate_toml(pyproject_pep_621_cli, "xyz")

assert data["project"]["scripts"] == {"my_cli": "xyz.hello.console.app:run"}


def test_generate_project_file_with_unchanged_script_entry_point():
data = generate_toml(pyproject_cli, "hello")

Expand Down
27 changes: 27 additions & 0 deletions test/components/toml/test_read.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from pathlib import Path

import tomlkit

from poetry_multiproject_plugin.components.toml import read

pyproject = """\
[tool.poetry]
name = "unit test"
"""

pyproject_with_exclude_pattern = """\
Expand All @@ -21,6 +24,14 @@
exclude = [{"path" = "testing.json", "format" = "wheel"}]
"""

pyproject_pep_621 = """\
[tool.poetry]
packages = []
[project]
name = "unit test"
"""


def test_read_exclude_pattern_should_return_empty_result():
data = tomlkit.loads(pyproject)
Expand Down Expand Up @@ -52,3 +63,19 @@ def test_read_complex_exclude_pattern_with_format_should_return_empty_result():
res = read.parse_exclude_patterns(data)

assert res == {"testing.json"}


def test_get_project_name(monkeypatch):
monkeypatch.setattr(read, "toml", lambda _: tomlkit.loads(pyproject))

name = read.project_name(Path.cwd())

assert name == "unit test"


def test_get_project_name_for_pep_621_project(monkeypatch):
monkeypatch.setattr(read, "toml", lambda _: tomlkit.loads(pyproject_pep_621))

name = read.project_name(Path.cwd())

assert name == "unit test"

0 comments on commit f7fd5c6

Please sign in to comment.