From 16342836d1441d17e01b54139d1bb601f9a3f1bc Mon Sep 17 00:00:00 2001 From: Nicolas Graves Date: Tue, 17 Mar 2026 01:24:59 +0100 Subject: [PATCH] build-system: pyproject: Add pytest-guix-options. * guix/build/pyproject-build-system.scm (check): Pass pytest-guix-options to python through a hidden json file. * guix/build-system/pyproject.scm (%default-pytest-guix-options): Add variable. (pyproject-build): Use it. * gnu/packages/aux-files/python/pytest_guix.py: Import pytest-guix-options from guile through a hidden json file. Change-Id: Ice2ea72452c335b900bc354a04bd9657dc03d7d4 Signed-off-by: Sharlatan Hellseher --- gnu/packages/aux-files/python/pytest_guix.py | 43 ++++++-------------- guix/build-system/pyproject.scm | 28 +++++++++++++ guix/build/pyproject-build-system.scm | 7 +++- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/gnu/packages/aux-files/python/pytest_guix.py b/gnu/packages/aux-files/python/pytest_guix.py index 750b1e99e99..dbe8dfe136b 100644 --- a/gnu/packages/aux-files/python/pytest_guix.py +++ b/gnu/packages/aux-files/python/pytest_guix.py @@ -17,6 +17,7 @@ # along with GNU Guix. If not, see . import importlib.util +import json def pytest_addoption(parser): @@ -34,35 +35,15 @@ def pytest_addoption(parser): time while at the same time avoiding the need to adjust test options in pyproject.toml or other configuration files. """ - plugin_options = { - "cov": [ - "--cov", - "--cov-reset", - "--cov-report", - "--cov-config", - "--no-cov-on-fail", - "--no-cov", - "--cov-fail-under", - "--cov-append", - "--cov-branch", - "--cov-context", - ], - "mypy": ["--mypy", "--mypy-config-file", "--mypy-ignore-missing-imports"], - "isort": ["--isort"], - "flake8": ["--flake8"], - "black": ["--black"], - "flakes": ["--flakes"], - "pep8": ["--pep8"], - "html": ["--html", "--self-contained-html", "--css"], - } + with open(".pytest_guix_options.json", "r") as options_file: + plugin_options = json.load(options_file) + group = parser.getgroup( + "guix", "Options ignored by the Guix pyproject-build-system" + ) - group = parser.getgroup( - "guix", "Options ignored by the Guix pyproject-build-system" - ) - - # Only add options for plugins that are not present. - for key, options in plugin_options.items(): - if importlib.util.find_spec(f"pytest_{key}") is None: - # Plugin not found, add stub options - for option in options: - group.addoption(option, action="append", nargs="?") + # Only add options for plugins that are not present. + for key, options in plugin_options.items(): + if importlib.util.find_spec(f"pytest_{key}") is None: + # Plugin not found, add stub options + for option in options: + group.addoption(option, action="append", nargs="?") diff --git a/guix/build-system/pyproject.scm b/guix/build-system/pyproject.scm index d833be10b47..a46bd7751b8 100644 --- a/guix/build-system/pyproject.scm +++ b/guix/build-system/pyproject.scm @@ -97,6 +97,28 @@ build-system level.") (license license:gpl3+)))) +(define %default-pytest-guix-options + #~'(("cov" + "--cov" + "--cov-reset" + "--cov-report" + "--cov-config" + "--no-cov-on-fail" + "--no-cov" + "--cov-fail-under" + "--cov-append" + "--cov-branch" + "--cov-context") + ("html" + "--html" "--self-contained-html" "--css") + ("mypy" + "--mypy" "--mypy-config-file" "--mypy-ignore-missing-imports") + ("isort" "isort") + ("flake8" "flake8") + ("black" "black") + ("flakes" "flakes") + ("pep8" "pep8"))) + ;; TODO: On the next iteration of python-team, migrate the sanity-check to ;; importlib_metadata instead of setuptools. (define (default-sanity-check.py) @@ -153,6 +175,7 @@ build-system level.") (build-backend #f) (test-backend #f) (test-flags ''()) + (pytest-guix-options %default-pytest-guix-options) (phases '%standard-phases) (outputs '("out" "wheel")) (search-paths '()) @@ -182,6 +205,11 @@ build-system level.") #:test-backend #$test-backend #:test-flags #$test-flags #:tests? #$tests? + #$@(let ((labels (map car inputs))) + (if (or (member "python-pytest-bootstrap" labels) + (member "python-pytest" labels)) + #~(#:pytest-guix-options #$pytest-guix-options) + #~())) #:phases #$(if (pair? phases) (sexp->gexp phases) phases) diff --git a/guix/build/pyproject-build-system.scm b/guix/build/pyproject-build-system.scm index 5685ddc033f..8cf2d2c599c 100644 --- a/guix/build/pyproject-build-system.scm +++ b/guix/build/pyproject-build-system.scm @@ -21,7 +21,7 @@ ;;; along with GNU Guix. If not, see . (define-module (guix build pyproject-build-system) - #:autoload (json builder) (scm->json-string) + #:autoload (json builder) (scm->json scm->json-string) #:use-module ((guix build gnu-build-system) #:prefix gnu:) #:use-module (guix build utils) #:use-module (guix build toml) @@ -314,7 +314,8 @@ without errors." (with-directory-excursion "/tmp" (invoke "python" sanity-check.py (site-packages inputs outputs))))) -(define* (check #:key inputs tests? test-backend test-flags #:allow-other-keys) +(define* (check #:key inputs tests? test-backend test-flags pytest-guix-options + #:allow-other-keys) "Run the test suite of a given Python package." (if tests? ;; Unfortunately with PEP 517 there is no common method to specify test @@ -347,6 +348,8 @@ without errors." (format #t "Using ~a~%" use-test-backend) (match use-test-backend ('pytest-with-guix-plugin + (call-with-output-file ".pytest_guix_options.json" + (cut scm->json pytest-guix-options <>)) (apply invoke pytest "-vv" "-p" "pytest_guix" test-flags)) ('pytest (apply invoke pytest "-vv" test-flags))