Skip to content

Commit

Permalink
Allow distribution format to be specified as a string (#261)
Browse files Browse the repository at this point in the history
* feat(poetry): Allow use of str instead of DistributionFormat

* feat(sessions): Allow use of str instead of DistributionFormat

* feat(core): Allow use of str instead of DistributionFormat

* feat(package): Annotate WHEEL and SDIST constants as `str`

* test: Add test cases for installroot with distribution format as string

* test: Add test case for installroot with invalid distribution format
  • Loading branch information
cjolowicz authored Feb 5, 2021
1 parent 38d3a94 commit 5aa7c25
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/nox_poetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@


#: A wheel archive.
WHEEL = DistributionFormat.WHEEL
WHEEL: str = DistributionFormat.WHEEL

#: A source archive.
SDIST = DistributionFormat.SDIST
SDIST: str = DistributionFormat.SDIST

__all__ = [
"build_package",
Expand Down
14 changes: 5 additions & 9 deletions src/nox_poetry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ def export_requirements(session: nox.sessions.Session) -> Path:
return Session(session).poetry.export_requirements()


def build_package(
session: nox.sessions.Session, *, distribution_format: DistributionFormat
) -> str: # noqa: D
"""Build a distribution archive for the package.
def build_package(session: nox.sessions.Session, *, distribution_format: str) -> str:
"""Build a distribution archive for the package. # noqa: DAR
.. deprecated:: 0.8
Use :func:`session` instead.
Expand All @@ -61,8 +59,8 @@ def install(session: nox.sessions.Session, *args: str, **kwargs: Any) -> None:

def installroot(
session: nox.sessions.Session,
*, # noqa: D
distribution_format: DistributionFormat,
*, # noqa: DAR
distribution_format: str,
extras: Iterable[str] = (),
) -> None:
"""Install the root package into a Nox session using Poetry.
Expand All @@ -76,9 +74,7 @@ def installroot(
)


def patch(
*, distribution_format: DistributionFormat = DistributionFormat.WHEEL
) -> None:
def patch(*, distribution_format: str = DistributionFormat.WHEEL) -> None:
"""Monkey-patch Nox to intercept ``session.install``.
.. deprecated:: 0.8
Expand Down
9 changes: 6 additions & 3 deletions src/nox_poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from nox.sessions import Session


class DistributionFormat(Enum):
class DistributionFormat(str, Enum):
"""Type of distribution archive for a Python package."""

WHEEL = "wheel"
Expand Down Expand Up @@ -78,7 +78,7 @@ def export(self, path: Path) -> None:
external=True,
)

def build(self, *, format: DistributionFormat) -> str:
def build(self, *, format: str) -> str:
"""Build the package.
The filename of the archive is extracted from the output Poetry writes
Expand All @@ -99,10 +99,13 @@ def build(self, *, format: DistributionFormat) -> str:
Returns:
The basename of the wheel built by Poetry.
"""
if not isinstance(format, DistributionFormat):
format = DistributionFormat(format)

output = self.session.run(
"poetry",
"build",
f"--format={format.value}",
f"--format={format}",
external=True,
silent=True,
stderr=None,
Expand Down
6 changes: 3 additions & 3 deletions src/nox_poetry/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def rewrite(arg: str, extras: Optional[str]) -> str:
def installroot(
self,
*,
distribution_format: DistributionFormat,
distribution_format: str,
extras: Iterable[str] = (),
) -> None:
"""Install the root package into a Nox session using Poetry.
Expand Down Expand Up @@ -184,7 +184,7 @@ def export_requirements(self) -> Path:

return path

def build_package(self, *, distribution_format: DistributionFormat) -> str:
def build_package(self, *, distribution_format: str) -> str:
"""Build a distribution archive for the package.
This function uses `poetry build`_ to build a wheel or sdist archive for
Expand All @@ -202,7 +202,7 @@ def build_package(self, *, distribution_format: DistributionFormat) -> str:
wheel = Path("dist") / self.poetry.build(format=distribution_format)
url = f"file://{wheel.resolve().as_posix()}"

if distribution_format is DistributionFormat.SDIST:
if DistributionFormat(distribution_format) is DistributionFormat.SDIST:
url += f"#egg={self.poetry.config.name}"

return url
Expand Down
6 changes: 2 additions & 4 deletions src/nox_poetry/sessions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ from typing import Union
import nox.sessions
import nox.virtualenv

from nox_poetry.poetry import DistributionFormat

Python = Optional[Union[str, Sequence[str], bool]]

class _PoetrySession:
def install(self, *args: str, **kwargs: Any) -> None: ...
def installroot(
self, *, distribution_format: DistributionFormat, extras: Iterable[str] = ...
self, *, distribution_format: str, extras: Iterable[str] = ...
) -> None: ...
def export_requirements(self) -> Path: ...
def build_package(self, *, distribution_format: DistributionFormat) -> str: ...
def build_package(self, *, distribution_format: str) -> str: ...

class Session:
poetry: _PoetrySession
Expand Down
37 changes: 29 additions & 8 deletions tests/unit/test_nox_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from nox.sessions import Session

import nox_poetry
from nox_poetry.poetry import DistributionFormat
from nox_poetry.poetry import Poetry


Expand All @@ -25,16 +24,40 @@ def test_install(session: Session, args: Iterable[str]) -> None:
nox_poetry.install(session, *args)


@pytest.mark.parametrize("distribution_format", [nox_poetry.WHEEL, nox_poetry.SDIST])
def test_installroot(session: Session, distribution_format: DistributionFormat) -> None:
@pytest.mark.parametrize(
"distribution_format",
[
nox_poetry.WHEEL,
nox_poetry.SDIST,
"wheel",
"sdist",
],
)
def test_installroot(session: Session, distribution_format: str) -> None:
"""It installs the package."""
nox_poetry.installroot(session, distribution_format=distribution_format)


@pytest.mark.parametrize("distribution_format", [nox_poetry.WHEEL, nox_poetry.SDIST])
def test_installroot_invalid_format(session: Session) -> None:
"""It raises an error."""
with pytest.raises(ValueError):
nox_poetry.installroot(session, distribution_format="egg")


@pytest.mark.parametrize(
"distribution_format",
[
nox_poetry.WHEEL,
nox_poetry.SDIST,
"wheel",
"sdist",
],
)
@pytest.mark.parametrize("extras", [[], ["noodles"], ["spicy", "noodles"]])
def test_installroot_with_extras(
session: Session, distribution_format: DistributionFormat, extras: Iterable[str]
session: Session,
distribution_format: str,
extras: Iterable[str],
) -> None:
"""It installs the package with extras."""
nox_poetry.installroot(
Expand All @@ -43,9 +66,7 @@ def test_installroot_with_extras(


@pytest.mark.parametrize("distribution_format", [nox_poetry.WHEEL, nox_poetry.SDIST])
def test_build_package(
session: Session, distribution_format: DistributionFormat
) -> None:
def test_build_package(session: Session, distribution_format: str) -> None:
"""It builds the package."""
nox_poetry.build_package(session, distribution_format=distribution_format)

Expand Down

0 comments on commit 5aa7c25

Please sign in to comment.