Skip to content

Commit 85c856e

Browse files
committed
tests/junit: add --env KEY=VAL flag for setting subprocess environment
Add a generic --env KEY=VAL CLI option (repeatable) to tests/junit.py that injects environment variables into the test subprocess environment. The flag mutates os.environ in the parent before mp.Pool is created, so worker processes inherit the values via the existing init_process() copy. Useful for backend-specific runtime knobs that affect test behavior, for example silencing chipStar runtime informational/warning lines that JUnit otherwise classifies as test failures: make junit BACKENDS='/gpu/hip/ref ...' \ JUNIT_ARGS='--env CHIP_LOGLEVEL=crit' No changes to tests/junit_common.py.
1 parent 3587cbb commit 85c856e

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ NPROC_POOL ?= 1
831831
export NPROC_POOL
832832

833833
run-% : $(OBJDIR)/%
834-
@$(PYTHON) tests/junit.py --mode tap --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)' $(<:$(OBJDIR)/%=%)
834+
@$(PYTHON) tests/junit.py --mode tap --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)' $(JUNIT_ARGS) $(<:$(OBJDIR)/%=%)
835835

836836
# The test and prove targets can be controlled via pattern searches. The
837837
# default is to run tests and those examples that have no external dependencies.
@@ -845,6 +845,8 @@ realsearch = $(search:%=%%)
845845
matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
846846
subsearch ?= .*
847847
JUNIT_BATCH ?= ''
848+
# Extra arguments forwarded to tests/junit.py (e.g. --env CHIP_LOGLEVEL=crit)
849+
JUNIT_ARGS ?=
848850

849851
# Test core libCEED
850852
test : $(matched:$(OBJDIR)/%=run-%)
@@ -858,15 +860,15 @@ ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
858860
# https://testanything.org/tap-specification.html
859861
prove : $(matched)
860862
$(info Testing backends: $(BACKENDS))
861-
$(PROVE) $(PROVE_OPTS) --exec '$(PYTHON) tests/junit.py' $(matched:$(OBJDIR)/%=%) :: --mode tap --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)'
863+
$(PROVE) $(PROVE_OPTS) --exec '$(PYTHON) tests/junit.py' $(matched:$(OBJDIR)/%=%) :: --mode tap --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)' $(JUNIT_ARGS)
862864
# Run prove target in parallel
863865
prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
864866

865867
prove-all :
866868
+$(MAKE) prove realsearch=%
867869

868870
junit-% : $(OBJDIR)/%
869-
@printf " %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)' --junit-batch $(JUNIT_BATCH) $(<:$(OBJDIR)/%=%)
871+
@printf " %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)' --junit-batch $(JUNIT_BATCH) $(JUNIT_ARGS) $(<:$(OBJDIR)/%=%)
870872

871873
junit : $(matched:$(OBJDIR)/%=junit-%)
872874

tests/junit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
from junit_common import *
33

44

5+
def parse_env_assignment(arg: str) -> Tuple[str, str]:
6+
"""Parse a KEY=VAL string from the --env flag.
7+
8+
Args:
9+
arg (str): The KEY=VAL string
10+
11+
Returns:
12+
Tuple[str, str]: (key, value) pair
13+
14+
Raises:
15+
argparse.ArgumentTypeError: if `arg` is not in KEY=VAL form
16+
"""
17+
if '=' not in arg:
18+
raise argparse.ArgumentTypeError(f"--env expects KEY=VAL, got {arg!r}")
19+
key, _, value = arg.partition('=')
20+
if not key:
21+
raise argparse.ArgumentTypeError(f"--env KEY must be non-empty, got {arg!r}")
22+
return key, value
23+
24+
525
def create_argparser() -> argparse.ArgumentParser:
626
"""Creates argument parser to read command line arguments
727
@@ -30,6 +50,11 @@ def create_argparser() -> argparse.ArgumentParser:
3050
help='Search string to filter tests, using `re` package format')
3151
parser.add_argument('-v', '--verbose', action='store_true', default=False,
3252
help='print details for all runs, not just failures')
53+
parser.add_argument('--env', dest='extra_env', action='append', default=[], metavar='KEY=VAL',
54+
type=parse_env_assignment,
55+
help='Set an environment variable for test subprocesses; may be repeated. '
56+
'Useful for backend-specific runtime knobs (e.g. --env CHIP_LOGLEVEL=crit '
57+
'to silence chipStar runtime messages on stderr).')
3358
parser.add_argument('test', help='Test executable', nargs='?')
3459

3560
return parser
@@ -198,6 +223,11 @@ def check_allowed_stdout(self, test: str) -> bool:
198223
if __name__ == '__main__':
199224
args = create_argparser().parse_args()
200225

226+
# Apply --env KEY=VAL settings to the parent environment so worker
227+
# processes (which copy os.environ in init_process) inherit them.
228+
for key, value in args.extra_env:
229+
os.environ[key] = value
230+
201231
result: TestSuite = run_tests(
202232
args.test,
203233
args.ceed_backends,

0 commit comments

Comments
 (0)