Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "." in install() instead of DistributionFormat #183

Merged
merged 5 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ installs the wheel as well as the ``pytest`` package, and
invokes ``pytest`` to run the test suite against the installation.

If you prefer a more explicit approach,
you can invoke ``nox_poetry.install`` and ``nox_poetry.installroot`` instead of ``session.install``.
invoke ``nox_poetry.install`` and ``nox_poetry.installroot`` instead of ``session.install``.
Use the ``nox_poetry.WHEEL`` or ``nox_poetry.SDIST`` constants to specify the distribution format for the local package.

Here is that same example using the more explicit approach:
Here is the example above using the more explicit approach:

.. code:: python

Expand Down
47 changes: 11 additions & 36 deletions src/nox_poetry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import hashlib
from pathlib import Path
from typing import Any
from typing import List
from typing import Union

from nox.sessions import Session

Expand Down Expand Up @@ -84,20 +82,17 @@ def build_package(session: Session, *, distribution_format: DistributionFormat)
return url


def install(
session: Session, *args: Union[DistributionFormat, str], **kwargs: Any
) -> None:
def install(session: Session, *args: str, **kwargs: Any) -> None:
"""Install packages into a Nox session using Poetry.

This function installs packages into the session's virtual environment. It
is a wrapper for `nox.sessions.Session.install`_, whose positional
arguments are command-line arguments for `pip install`_, and whose keyword
arguments are the same as those for `nox.sessions.Session.run`_.

If a positional argument is :const:`WHEEL` or :const:`SDIST`, a
distribution archive is built using :func:`build_package`, and the argument
is replaced with the file URL returned by that function. Otherwise, the
argument is forwarded unchanged.
If a positional argument is ".", a wheel is built using
:func:`build_package`, and the argument is replaced with the file URL
returned by that function. Otherwise, the argument is forwarded unchanged.

In addition, a `constraints file`_ is generated for the package
dependencies using :func:`export_requirements`, and passed to ``pip
Expand All @@ -117,30 +112,17 @@ def install(

Args:
session: The Session object.
args: Command-line arguments for ``pip install``. The constants
:const:`WHEEL` and :const:`SDIST` are replaced by a distribution
archive built for the local package.
args: Command-line arguments for ``pip install``.
kwargs: Keyword-arguments for ``session.install``. These are the same
as those for `nox.sessions.Session.run`_.
"""
resolved = {
arg: (
build_package(session, distribution_format=arg)
if isinstance(arg, DistributionFormat)
else arg
)
for arg in args
}

for distribution_format in DistributionFormat:
package = resolved.get(distribution_format)
if package is not None:
session.run("pip", "uninstall", "--yes", package, silent=True)
if "." in args:
package = build_package(session, distribution_format=DistributionFormat.WHEEL)
args = tuple(package if arg == "." else arg for arg in args)
session.run("pip", "uninstall", "--yes", package, silent=True)

requirements = export_requirements(session)
Session_install(
session, f"--constraint={requirements}", *resolved.values(), **kwargs
)
Session_install(session, f"--constraint={requirements}", *args, **kwargs)


def installroot(
Expand Down Expand Up @@ -189,11 +171,4 @@ def patch(
distribution_format: The distribution format to use when the ``"."``
argument is encountered in calls to ``session.install``.
"""

def patched_install(self: Session, *args: str, **kwargs: Any) -> None:
newargs: List[Union[DistributionFormat, str]] = [
distribution_format if arg == "." else arg for arg in args
]
install(self, *newargs, **kwargs)

Session.install = patched_install # type: ignore[assignment]
Session.install = install # type: ignore[assignment]
24 changes: 2 additions & 22 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,7 @@ def test_install_local_wheel(
@nox.session
def test(session: nox.sessions.Session) -> None:
"""Install the local package."""
nox_poetry.install(session, nox_poetry.WHEEL)

run_nox_with_noxfile([test], [nox, nox.sessions, nox_poetry])

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

assert set(expected) == set(packages)


def test_install_local_sdist(
project: Project,
run_nox_with_noxfile: RunNoxWithNoxfile,
list_packages: ListPackages,
) -> None:
"""It builds and installs an sdist from the local package."""

@nox.session
def test(session: nox.sessions.Session) -> None:
"""Install the local package."""
nox_poetry.install(session, nox_poetry.SDIST)
nox_poetry.install(session, ".")

run_nox_with_noxfile([test], [nox, nox.sessions, nox_poetry])

Expand Down Expand Up @@ -181,7 +161,7 @@ def test_install_local_wheel_and_dependency_without_patch(
@nox.session
def test(session: nox.sessions.Session) -> None:
"""Install the dependency."""
nox_poetry.install(session, nox_poetry.WHEEL, "pycodestyle")
nox_poetry.install(session, ".", "pycodestyle")

run_nox_with_noxfile([test], [nox, nox.sessions, nox_poetry])

Expand Down
12 changes: 8 additions & 4 deletions tests/unit/test_nox_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
from nox_poetry.poetry import DistributionFormat


@pytest.mark.parametrize("distribution_format", [nox_poetry.WHEEL, nox_poetry.SDIST])
def test_install(session: Session, distribution_format: DistributionFormat) -> None:
"""It installs the dependencies."""
nox_poetry.install(session, distribution_format, "pip")
def test_install_package(session: Session) -> None:
"""It installs the package."""
nox_poetry.install(session, ".")


def test_install_dependency(session: Session) -> None:
"""It installs the dependency."""
nox_poetry.install(session, "pip")


@pytest.mark.parametrize("distribution_format", [nox_poetry.WHEEL, nox_poetry.SDIST])
Expand Down