Skip to content

Commit

Permalink
fix: support noxfile being a symlink (#829)
Browse files Browse the repository at this point in the history
* fix: support noxfile being a symlink

Signed-off-by: Henry Schreiner <[email protected]>

* feat: add session.noxfile and add tests

Signed-off-by: Henry Schreiner <[email protected]>

* refactor: remove session.noxfile in prefernce for __path__

Signed-off-by: Henry Schreiner <[email protected]>

---------

Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Nov 11, 2024
1 parent f4a91df commit 2c43214
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
10 changes: 6 additions & 4 deletions nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ def load_nox_module(global_config: Namespace) -> types.ModuleType | int:
# Be sure to expand variables
global_config_noxfile = os.path.expandvars(global_config.noxfile)

# Make sure we only expand the parent dir just in case the noxfile is a symlink
noxfile_parent_dir = os.path.realpath(os.path.dirname(global_config_noxfile))

# Save the absolute path to the Noxfile.
# This will inoculate it if Nox changes paths because of an implicit
# or explicit chdir (like the one below).
global_config.noxfile = os.path.realpath(global_config_noxfile)

# Make sure we only expand the parent dir just in case the noxfile is a symlink
noxfile_parent_dir = os.path.realpath(os.path.dirname(global_config.noxfile))
global_config.noxfile = os.path.join(
noxfile_parent_dir, os.path.basename(global_config_noxfile)
)

try:
# Check ``nox.needs_version`` by parsing the AST.
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

FILE = Path(__file__).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 FILE.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 2c43214

Please sign in to comment.