Skip to content

Commit

Permalink
feat: add session.noxfile and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Nov 1, 2024
1 parent 5d294f1 commit b4aa701
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ def virtualenv(self) -> ProcessEnv:
raise ValueError("A virtualenv has not been created for this session")
return venv

@property
def noxfile(self) -> pathlib.Path:
"""The path to the Noxfile that defines this session.
If the noxfile is a symlink, this does not resolve that last symlink; it
has been resolved up to that point. Use `session.noxfile.resolve()` to
get the original file path.
"""
return pathlib.Path(self._runner.global_config.noxfile)

@property
def venv_backend(self) -> str:
"""The venv_backend selected."""
Expand Down
17 changes: 17 additions & 0 deletions tests/resources/orig_dir/noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pathlib import Path

import nox

DIR = Path(__file__).parent.resolve()


@nox.session(venv_backend="none", default=False)
def orig(session: nox.Session) -> None:
assert Path("orig_file.txt").exists()


@nox.session(venv_backend="none", default=False)
def sym(session: nox.Session) -> None:
assert Path("sym_file.txt").exists()

assert session.noxfile.resolve().parent.joinpath("orig_file.txt").exists()
Empty file.
1 change: 1 addition & 0 deletions tests/resources/sym_dir/noxfile.py
Empty file.
23 changes: 23 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import contextlib
import os
import subprocess
import sys
from importlib import metadata
from pathlib import Path
Expand Down Expand Up @@ -928,3 +929,25 @@ def test_noxfile_options_cant_be_set():
def test_noxfile_options_cant_be_set_long():
with pytest.raises(AttributeError, match="i_am_clearly_not_an_option"):
nox.options.i_am_clearly_not_an_option = True


def test_symlink_orig(monkeypatch):
monkeypatch.chdir(Path(RESOURCES) / "orig_dir")
subprocess.run([sys.executable, "-m", "nox", "-s", "orig"], check=True)


def test_symlink_orig_not(monkeypatch):
monkeypatch.chdir(Path(RESOURCES) / "orig_dir")
res = subprocess.run([sys.executable, "-m", "nox", "-s", "sym"], check=False)
assert res.returncode == 1


def test_symlink_sym(monkeypatch):
monkeypatch.chdir(Path(RESOURCES) / "sym_dir")
subprocess.run([sys.executable, "-m", "nox", "-s", "sym"], check=True)


def test_symlink_sym_not(monkeypatch):
monkeypatch.chdir(Path(RESOURCES) / "sym_dir")
res = subprocess.run([sys.executable, "-m", "nox", "-s", "orig"], check=False)
assert res.returncode == 1

0 comments on commit b4aa701

Please sign in to comment.