Skip to content

Commit 3c3dd66

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "update toxnox with newer coverage/junit approach" into main
2 parents 7ec36e9 + b071743 commit 3c3dd66

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

noxfile.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
if True:
1616
sys.path.insert(0, ".")
1717
from tools.toxnox import tox_parameters
18-
from tools.toxnox import extract_opts
18+
from tools.toxnox import apply_pytest_opts
1919
from tools.toxnox import OUR_PYTHON
2020

2121

@@ -173,12 +173,14 @@ def _tests(
173173
if backendonly:
174174
cmd.append("--backend-only")
175175

176-
posargs, opts = extract_opts(session.posargs, "generate-junit")
177-
178-
if opts.generate_junit:
179-
# produce individual junit files that are per-database
180-
junitfile = f"junit-{database}.xml"
181-
cmd.extend(["--junitxml", junitfile])
176+
posargs = apply_pytest_opts(
177+
session,
178+
"alembic",
179+
[
180+
database,
181+
],
182+
coverage=coverage,
183+
)
182184

183185
cmd.extend(posargs)
184186

@@ -243,9 +245,13 @@ def test_pyoptimize(session: nox.Session) -> None:
243245
cmd.extend(os.environ.get("TOX_WORKERS", "-n4").split())
244246
cmd.append("tests/test_script_consumption.py")
245247

246-
posargs, opts = extract_opts(session.posargs, "generate-junit")
247-
if opts.generate_junit:
248-
cmd.extend(["--junitxml", "junit-pyoptimize.xml"])
248+
posargs = apply_pytest_opts(
249+
session,
250+
"alembic",
251+
[
252+
"pyoptimize",
253+
],
254+
)
249255

250256
cmd.extend(posargs)
251257

tools/toxnox.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import annotations
1212

1313
import collections
14+
import os
1415
import re
1516
import sys
1617
from typing import Any
@@ -204,3 +205,44 @@ def extract(arg: str) -> bool:
204205
return [arg for arg in posargs if not extract(arg)], return_tuple(
205206
*return_args
206207
)
208+
209+
210+
def apply_pytest_opts(
211+
session: nox.Session,
212+
cov: str,
213+
tokens: list[str],
214+
*,
215+
coverage: bool = False,
216+
) -> list[str]:
217+
posargs, opts = extract_opts(session.posargs, "generate-junit")
218+
219+
file_suffix = "-".join(t for t in tokens if not t.startswith("_"))
220+
221+
if coverage:
222+
223+
session.env["COVERAGE_FILE"] = coverage_file = (
224+
f".coverage.{file_suffix}"
225+
)
226+
coverage_xml_file = f"coverage-{file_suffix}.xml"
227+
228+
if os.path.exists(coverage_file):
229+
os.unlink(coverage_file)
230+
posargs.extend(
231+
[
232+
f"--cov={cov}",
233+
"--cov-append",
234+
"--cov-report",
235+
"term",
236+
"--cov-report",
237+
f"xml:{coverage_xml_file}",
238+
],
239+
)
240+
session.log(f"Will store coverage data in {coverage_file}")
241+
session.log(f"Will write xml coverage data to {coverage_xml_file}")
242+
243+
if opts.generate_junit:
244+
junitfile = f"junit-{file_suffix}.xml"
245+
session.log(f"Will store junit xml in {junitfile}")
246+
posargs.extend(["--junitxml", junitfile])
247+
248+
return posargs

0 commit comments

Comments
 (0)