From 0c92d942ecc2d0905d1b94293801427607bbe094 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Thu, 2 Jul 2026 17:20:08 +0200 Subject: [PATCH] py-test.mk: Generate conftest.py Generate a standard conftest.py, mostly for customizing pytest's bombastic test header, which otherwise lets the more informative make output look too pale in comparison. Signed-off-by: Jan Lindemann --- make/py-test.mk | 19 ++++++++++++++++++- scripts/python-tools.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/make/py-test.mk b/make/py-test.mk index 3d77f7e0..69634d8f 100644 --- a/make/py-test.mk +++ b/make/py-test.mk @@ -1,6 +1,23 @@ -PYTHON_RUNNER ?= pytest -v +PY_CONFTEST ?= conftest.py +PY_UPDATE_PY_CONFTEST ?= true +ifeq ($(PY_UPDATE_PY_CONFTEST),true) + PY_GENERATED += $(PY_CONFTEST) +endif + +PY_CONFTEST_CMD ?= /bin/bash $(JWB_SCRIPT_DIR)/python-tools.sh create-conftest-py +PY_TEST_ARGS ?= -v +PYTHON_RUNNER := pytest $(PY_TEST_ARGS) include $(JWBDIR)/make/py-run.mk all: test: run +run: $(PY_GENERATED) + +$(PY_CONFTEST): + $(PY_CONFTEST_CMD) > $@.tmp + mv $@.tmp $@ + +clean: clean.generated +clean.generated: + rm -f $(PY_GENERATED) diff --git a/scripts/python-tools.sh b/scripts/python-tools.sh index 56950a0f..0b189d7b 100644 --- a/scripts/python-tools.sh +++ b/scripts/python-tools.sh @@ -32,6 +32,39 @@ module_path() fi } +cmd_create_conftest_py() +{ + cat <<-'EOT' + import os + import sys + import shutil + + def pytest_configure(config): + import _pytest.terminal + import _pytest.timing as timing + + # orig = _pytest.terminal.TerminalReporter.pytest_sessionstart + + def patched(self, session): + width = shutil.get_terminal_size().columns + d = os.path.basename(os.getcwd()) + text = f"Running pytest for {d}" + # Center text within terminal width, matching pytest footer style + padding = width - len(text) - 2 + if padding > 0: + left = padding // 2 + right = padding - left + banner = "=" * left + f" {text} " + "=" * right + else: + banner = text + print(banner, file = sys.stderr) + self._session = session + self._session_start = timing.Instant() + + _pytest.terminal.TerminalReporter.pytest_sessionstart = patched +EOT +} + cmd_create_init() { __add_seen() {