Skip to content

Commit

Permalink
Fix crash when exporting requirements in PassthroughEnv (#354)
Browse files Browse the repository at this point in the history
* Add test for exporting requirements in PassthroughEnv

* Fix crash when exporting requirements in PassthroughEnv

* Adapt fake session to provide Session._runner

* Adapt type stubs to provide Session._runner

* mypy: Remove unused `type: ignore` comment
  • Loading branch information
cjolowicz authored Apr 29, 2021
1 parent a835215 commit 3709ed0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def tests(session: Session, poetry: Optional[str]) -> None:
def coverage(session: Session) -> None:
"""Produce the coverage report."""
# Do not use session.posargs unless this is the only session.
nsessions = len(session._runner.manifest) # type: ignore[attr-defined]
nsessions = len(session._runner.manifest)
has_args = session.posargs and nsessions == 1
args = session.posargs if has_args else ["report"]

Expand Down
7 changes: 5 additions & 2 deletions src/nox_poetry/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ def export_requirements(self) -> Path:
Returns:
The path to the requirements file.
"""
tmpdir = Path(self.session.virtualenv.location) / "tmp"
tmpdir.mkdir(exist_ok=True)
# Avoid ``session.virtualenv.location`` because PassthroughEnv does not
# have it. We'll just create a fake virtualenv directory in this case.

tmpdir = Path(self.session._runner.envdir) / "tmp"
tmpdir.mkdir(exist_ok=True, parents=True)

path = tmpdir / "requirements.txt"
hashfile = tmpdir / f"{path.name}.hash"
Expand Down
1 change: 1 addition & 0 deletions src/nox_poetry/sessions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class _PoetrySession:
class Session:
poetry: _PoetrySession
_session: nox.Session
_runner: nox.sessions.SessionRunner
def __init__(self, session: nox.Session) -> None: ...
def install(self, *args: str, **kwargs: Any) -> None: ...
@property
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,15 @@ def test(session: nox_poetry.Session) -> None:
packages = list_packages(project, test)

assert set(expected) == set(packages)


def test_passthrough_env(project: Project) -> None:
"""It stores requirements even without a virtualenv directory."""
# /~https://github.com/cjolowicz/nox-poetry/issues/347

@nox_poetry.session(venv_backend="none")
def test(session: nox_poetry.Session) -> None:
"""Export the requirements."""
session.poetry.export_requirements()

run_nox_with_noxfile(project, [test], [nox_poetry])
8 changes: 4 additions & 4 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
from nox.sessions import Session


class FakeVirtualenv:
"""Fake virtual environment."""
class FakeSessionRunner:
"""Fake session runner."""

def __init__(self, path: Path) -> None:
"""Initialize."""
self.location = str(path)
self.envdir = str(path)


class FakeSession:
"""Fake session."""

def __init__(self, path: Path) -> None:
"""Initialize."""
self.virtualenv = FakeVirtualenv(path)
self._runner = FakeSessionRunner(path)

def run_always(self, *args: str, **kargs: Any) -> str:
"""Run."""
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ def proxy(session: nox.Session) -> nox_poetry.Session:

def test_session_getattr(proxy: nox_poetry.Session) -> None:
"""It delegates to the real session."""
# Fixed in /~https://github.com/theacodes/nox/pull/377
assert proxy.virtualenv.location # type: ignore[attr-defined]
assert proxy._runner.envdir


def test_session_install(proxy: nox_poetry.Session) -> None:
Expand Down

0 comments on commit 3709ed0

Please sign in to comment.