Skip to content

Commit

Permalink
Support Poetry projects that use PEP 621
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Oct 8, 2024
1 parent 89b245d commit dc11979
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 151 deletions.
17 changes: 13 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,35 @@ def mypy(session: Session) -> None:
@nox.parametrize(
"python,poetry",
[
(python_versions[0], "1.6.1"),
(python_versions[0], "poetry==1.6.1"),
(python_versions[0], "poetry==1.8.3"),
(
python_versions[0],
"poetry @ git+https://github.com/python-poetry/poetry.git@main",
),
*((python, None) for python in python_versions),
],
)
def tests(session: Session, poetry: Optional[str]) -> None:
"""Run the test suite."""
# Install poetry first to ensure the correct version is used for 'poetry build'.
if poetry is not None:
session.run_always("python", "-m", "pip", "install", poetry, silent=True)

session.install(".")
session.install(
"coverage[toml]",
"poetry",
"poetry-plugin-export",
"pytest",
"pytest-datadir",
"pygments",
"typing_extensions",
)

# Override nox-poetry's locked Poetry version.
if poetry is not None:
session.run_always(
"python", "-m", "pip", "install", f"poetry=={poetry}", silent=True
)
session.run_always("python", "-m", "pip", "install", poetry, silent=True)

try:
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
Expand Down
288 changes: 144 additions & 144 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ furo = ">=2022.1.2"
Pygments = ">=2.11.2"
# TODO: Remove the 'python' constraint once poetry drops its own constraint
poetry = {version=">=1.1.12", python="<4"}
poetry-plugin-export = {version="*", python="<4"}
pytest-datadir = ">=1.3.1"
typing-extensions = ">=4.0.1"
myst-parser = ">=0.16.1"
Expand Down
5 changes: 3 additions & 2 deletions src/nox_poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ def __init__(self, project: Path) -> None:
path = project / "pyproject.toml"
text = path.read_text(encoding="utf-8")
data: Any = tomlkit.parse(text)
self._config = data["tool"]["poetry"]
self._config = data.get("tool", {}).get("poetry", {})
self._pyproject = data.get("project", {})

@property
def name(self) -> str:
"""Return the package name."""
name = self._config["name"]
name = self._config.get("name", self._pyproject.get("name"))
assert isinstance(name, str) # noqa: S101
return name

Expand Down
4 changes: 3 additions & 1 deletion tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def _read_toml(self, filename: str) -> Any:

def _get_config(self, key: str) -> Any:
data: Any = self._read_toml("pyproject.toml")
return data["tool"]["poetry"][key]
poetry_config = data.get("tool", {}).get("poetry", {})
pyproject = data.get("project", {})
return poetry_config.get(key, pyproject.get(key))

def get_dependency(self, name: str, data: Any = None) -> Package:
"""Return the package with the given name."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""A project that uses PEP 621."""
17 changes: 17 additions & 0 deletions tests/functional/data/pep-621/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/functional/data/pep-621/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "pep-621-pyproject"
version = "0.1.0"
description = ""
authors = [{ name = "Your Name", email = "<[email protected]>" }]
requires-python = ">=3.7"
dependencies = ["pyflakes>=2.1.1"]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
21 changes: 21 additions & 0 deletions tests/functional/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,24 @@ def test(session: nox_poetry.Session) -> None:
packages = list_packages(project, test)

assert set(expected) == set(packages)


@pytest.mark.skipif(
Version(version("poetry")) < Version("2.0.0.dev0"),
reason=f"Poetry {version('poetry')} < 2.0 does not support PEP 621",
)
def test_pep621_pyproject_support(shared_datadir: Path) -> None:
"""It installs packages from PEP 621 pyproject.toml."""
project = Project(shared_datadir / "pep-621")

@nox_poetry.session
def test(session: nox_poetry.Session) -> None:
"""Install the dependencies."""
session.install(".")

run_nox_with_noxfile(project, [test], [nox_poetry])

expected = [project.package, *project.locked_packages]
packages = list_packages(project, test)

assert set(expected) == set(packages)

0 comments on commit dc11979

Please sign in to comment.