From: Mike Bayer Date: Wed, 29 Oct 2025 12:38:39 +0000 (-0400) Subject: update toxnox with newer coverage/junit approach X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b07174379668d03bc2fe0fca7dbfa5586121230e;p=thirdparty%2Fsqlalchemy%2Falembic.git update toxnox with newer coverage/junit approach Change-Id: Ieecafab1f929ed9afe0b0675acbeceaecc25e48e --- diff --git a/noxfile.py b/noxfile.py index 965c5875..079df2d5 100644 --- a/noxfile.py +++ b/noxfile.py @@ -15,7 +15,7 @@ nox.needs_version = ">=2025.10.16" if True: sys.path.insert(0, ".") from tools.toxnox import tox_parameters - from tools.toxnox import extract_opts + from tools.toxnox import apply_pytest_opts from tools.toxnox import OUR_PYTHON @@ -173,12 +173,14 @@ def _tests( if backendonly: cmd.append("--backend-only") - posargs, opts = extract_opts(session.posargs, "generate-junit") - - if opts.generate_junit: - # produce individual junit files that are per-database - junitfile = f"junit-{database}.xml" - cmd.extend(["--junitxml", junitfile]) + posargs = apply_pytest_opts( + session, + "alembic", + [ + database, + ], + coverage=coverage, + ) cmd.extend(posargs) @@ -243,9 +245,13 @@ def test_pyoptimize(session: nox.Session) -> None: cmd.extend(os.environ.get("TOX_WORKERS", "-n4").split()) cmd.append("tests/test_script_consumption.py") - posargs, opts = extract_opts(session.posargs, "generate-junit") - if opts.generate_junit: - cmd.extend(["--junitxml", "junit-pyoptimize.xml"]) + posargs = apply_pytest_opts( + session, + "alembic", + [ + "pyoptimize", + ], + ) cmd.extend(posargs) diff --git a/tools/toxnox.py b/tools/toxnox.py index 891fc70f..64bd298d 100644 --- a/tools/toxnox.py +++ b/tools/toxnox.py @@ -11,6 +11,7 @@ would fall back to defaults. from __future__ import annotations import collections +import os import re import sys from typing import Any @@ -204,3 +205,44 @@ def extract_opts(posargs: list[str], *args: str) -> tuple[list[str], Any]: return [arg for arg in posargs if not extract(arg)], return_tuple( *return_args ) + + +def apply_pytest_opts( + session: nox.Session, + cov: str, + tokens: list[str], + *, + coverage: bool = False, +) -> list[str]: + posargs, opts = extract_opts(session.posargs, "generate-junit") + + file_suffix = "-".join(t for t in tokens if not t.startswith("_")) + + if coverage: + + session.env["COVERAGE_FILE"] = coverage_file = ( + f".coverage.{file_suffix}" + ) + coverage_xml_file = f"coverage-{file_suffix}.xml" + + if os.path.exists(coverage_file): + os.unlink(coverage_file) + posargs.extend( + [ + f"--cov={cov}", + "--cov-append", + "--cov-report", + "term", + "--cov-report", + f"xml:{coverage_xml_file}", + ], + ) + session.log(f"Will store coverage data in {coverage_file}") + session.log(f"Will write xml coverage data to {coverage_xml_file}") + + if opts.generate_junit: + junitfile = f"junit-{file_suffix}.xml" + session.log(f"Will store junit xml in {junitfile}") + posargs.extend(["--junitxml", junitfile]) + + return posargs