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

fix: support noxfile being a symlink #829

Merged
merged 3 commits into from
Nov 11, 2024
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
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
Loading