Fixing noxfiles a bit more.

Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
2025-09-21 07:07:56 -04:00
parent 74ff483c95
commit 60ef61c3e1
3 changed files with 32 additions and 88 deletions

View File

@@ -1,33 +1,23 @@
"""Nox sessions."""
"""Nox sessions for backend tasks.
Run with `nox -s <session>` from the backend directory.
"""
import os
import shlex
import shutil
import sys
from pathlib import Path
from textwrap import dedent
import nox
try:
from nox_poetry import Session
from nox_poetry import session
except ImportError:
message = f"""\
Nox failed to import the 'nox-poetry' package.
Please install it using the following command:
{sys.executable} -m pip install nox-poetry"""
raise SystemExit(dedent(message)) from None
from nox_poetry import Session
from nox_poetry import session
package = "backend"
python_versions = ["3.13"]
nox.needs_version = ">= 2025.5.1"
nox.options.sessions = (
"pre-commit",
"precommit",
"safety",
"mypy",
"tests",
@@ -37,81 +27,17 @@ nox.options.sessions = (
)
def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
"""Activate virtualenv in hooks installed by pre-commit.
This function patches git hooks installed by pre-commit to activate the
session's virtual environment. This allows pre-commit to locate hooks in
that environment when invoked from git.
@session(name="pre-commit", python=python_versions[0])
def precommit(session: Session) -> None:
"""Run pre-commit checks by delegating to the root noxfile.
Args:
session: The Nox session object.
Returns:
None, as the function modifies hooks in place or exits early.
"""
assert session.bin is not None # noqa: S101
session.run("nox", "-f", "../noxfile.py", "-s", "pre-commit", external=True)
# Only patch hooks containing a reference to this session's bindir. Support
# quoting rules for Python and bash, but strip the outermost quotes so we
# can detect paths within the bindir, like <bindir>/python.
bindirs = [
bindir[1:-1] if bindir[0] in "'\"" else bindir
for bindir in (repr(session.bin), shlex.quote(session.bin))
]
virtualenv = session.env.get("VIRTUAL_ENV")
if virtualenv is None:
return
headers = {
# pre-commit < 2.16.0
"python": f"""\
import os
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
os.environ["PATH"] = os.pathsep.join((
{session.bin!r},
os.environ.get("PATH", ""),
))
""",
# pre-commit >= 2.16.0
"bash": f"""\
VIRTUAL_ENV={shlex.quote(virtualenv)}
PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH"
""",
# pre-commit >= 2.17.0 on Windows forces sh shebang
"/bin/sh": f"""\
VIRTUAL_ENV={shlex.quote(virtualenv)}
PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH"
""",
}
hookdir = Path(".git") / "hooks"
if not hookdir.is_dir():
return
for hook in hookdir.iterdir():
if hook.name.endswith(".sample") or not hook.is_file():
continue
if not hook.read_bytes().startswith(b"#!"):
continue
text = hook.read_text()
if not any(
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
for bindir in bindirs
):
continue
lines = text.splitlines()
for executable, header in headers.items():
if executable in lines[0].lower():
lines.insert(1, dedent(header))
hook.write_text("\n".join(lines))
break
package = "backend"
@session(python=python_versions[0])

View File

@@ -1,6 +1,6 @@
"""Nox sessions for frontend tasks.
Run with `nox -s <session>` from the project root or frontend directory.
Run with `nox -s <session>` from the frontend directory.
"""
import os
@@ -9,6 +9,25 @@ from pathlib import Path
import nox
nox.options.sessions = (
"precommit",
"coverage",
"audit",
"storybook",
"jest",
)
@nox.session(name="pre-commit")
def precommit(session: nox.Session) -> None:
"""Run pre-commit checks by delegating to the root noxfile.
Args:
session: The Nox session object.
"""
session.run("nox", "-f", "../noxfile.py", "-s", "pre-commit", external=True)
@nox.session(name="typecheck")
def typecheck(session: nox.Session) -> None:
"""Run TypeScript type checks.

View File

@@ -46,7 +46,6 @@ def precommit(session: Session) -> None:
backend_sessions = [
"pre-commit",
"safety",
"mypy",
"tests",