diff --git a/noxfile.py b/noxfile.py index 403a44fd..895a45fe 100644 --- a/noxfile.py +++ b/noxfile.py @@ -168,10 +168,21 @@ def mypy(session: Session) -> None: @session @nox.parametrize( "python,poetry", - [(python_versions[0], "1.6.1"), *((python, None) for python in python_versions)], + [ + (python_versions[0], "poetry==1.6.1"), + ( + python_versions[0], + "poetry @ git+https://github.com/radoering/poetry.git@pep621-support", + ), + *((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]", @@ -182,10 +193,9 @@ def tests(session: Session, poetry: Optional[str]) -> None: "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) diff --git a/src/nox_poetry/poetry.py b/src/nox_poetry/poetry.py index c2aae71e..98872a74 100644 --- a/src/nox_poetry/poetry.py +++ b/src/nox_poetry/poetry.py @@ -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 diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index fedfee60..34d20f35 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -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.""" diff --git a/tests/functional/data/pep-621/pep_621_pyproject/__init__.py b/tests/functional/data/pep-621/pep_621_pyproject/__init__.py new file mode 100644 index 00000000..0142a443 --- /dev/null +++ b/tests/functional/data/pep-621/pep_621_pyproject/__init__.py @@ -0,0 +1 @@ +"""A project that uses PEP 621.""" diff --git a/tests/functional/data/pep-621/poetry.lock b/tests/functional/data/pep-621/poetry.lock new file mode 100644 index 00000000..b35d2f04 --- /dev/null +++ b/tests/functional/data/pep-621/poetry.lock @@ -0,0 +1,17 @@ +# This file is automatically @generated by Poetry 1.9.0.dev0 and should not be changed by hand. + +[[package]] +name = "pyflakes" +version = "3.0.1" +description = "passive checker of Python programs" +optional = false +python-versions = ">=3.6" +files = [ + {file = "pyflakes-3.0.1-py2.py3-none-any.whl", hash = "sha256:ec55bf7fe21fff7f1ad2f7da62363d749e2a470500eab1b555334b67aa1ef8cf"}, + {file = "pyflakes-3.0.1.tar.gz", hash = "sha256:ec8b276a6b60bd80defed25add7e439881c19e64850afd9b346283d4165fd0fd"}, +] + +[metadata] +lock-version = "2.0" +python-versions = ">=3.7" +content-hash = "fde4426d488839cd45ad2f892640146bbbf95d4c86183effa3ed6be3e6e5d6ae" diff --git a/tests/functional/data/pep-621/pyproject.toml b/tests/functional/data/pep-621/pyproject.toml new file mode 100644 index 00000000..f07d2b67 --- /dev/null +++ b/tests/functional/data/pep-621/pyproject.toml @@ -0,0 +1,11 @@ +[project] +name = "pep-621-pyproject" +version = "0.1.0" +description = "" +authors = [{ name = "Your Name", email = "" }] +requires-python = ">=3.7" +dependencies = ["pyflakes>=2.1.1"] + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/functional/test_session.py b/tests/functional/test_session.py index 047417e8..777a68ee 100644 --- a/tests/functional/test_session.py +++ b/tests/functional/test_session.py @@ -188,3 +188,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("1.9.0.dev0"), + reason=f"Poetry {version('poetry')} < 1.9 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)