[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug#70858] [PATCH 02/32] build-system/pyproject: Ignore unwanted pytest
From: |
Nicolas Graves |
Subject: |
[bug#70858] [PATCH 02/32] build-system/pyproject: Ignore unwanted pytest flags. |
Date: |
Fri, 10 May 2024 09:55:06 +0200 |
* guix/build/pyproject-build-system.scm : Ignore unwanted pytest flags.
Change-Id: Ib9f1602e5af11227e5b7ce124f0f9be4fa2b78e4
---
guix/build/pyproject-build-system.scm | 99 ++++++++++++++++++++++++++-
1 file changed, 96 insertions(+), 3 deletions(-)
diff --git a/guix/build/pyproject-build-system.scm
b/guix/build/pyproject-build-system.scm
index 947d240114a..2df9f2f4798 100644
--- a/guix/build/pyproject-build-system.scm
+++ b/guix/build/pyproject-build-system.scm
@@ -1,6 +1,7 @@
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Lars-Dominik Braun <lars@6xq.net>
;;; Copyright © 2022 Marius Bakke <marius@gnu.org>
+;;; Copyright © 2024 Nicolas Graves <ngraves@ngraves.fr>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -35,7 +36,8 @@ (define-module (guix build pyproject-build-system)
add-installed-pythonpath
site-packages
python-version
- pyproject-build))
+ pyproject-build
+ with-guix-pytest-plugin))
;;; Commentary:
;;;
@@ -142,7 +144,94 @@ (define* (build #:key outputs build-backend backend-path
configure-flags #:allow
wheel-dir
config-settings)))
-(define* (check #:key tests? test-backend test-flags #:allow-other-keys)
+(define pytest-default-ignore-alist
+ '(("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"))))
+
+(define (pytest-ignore-flags-plugin flags)
+ "This function converts an list of flags into a string that can
+ be instantiated as a python pytest plugin."
+ (format #f "\
+import pytest
+
+def pytest_addoption(parser):
+ group = parser.getgroup('guix','Guix ignored options')
+ options = [~{~s, ~}]
+ for option in options:
+ group.addoption(option, action='append', nargs='?')"
+ flags))
+
+(define (call-with-guix-pytest-plugin inputs thunk)
+ "This function emulates command line options provided by pytest plugins in
+the absence of the plugins defining these options.
+
+This is done by selecting absent plugins, gettings their flags defined in
+PYTEST-DEFAULT-IGNORE-ALIST, and generating the plugin from there with
+PYTEST-IGNORE-FLAGS-PLUGIN."
+ (let* ((former-path (getenv "PYTHONPATH"))
+ (input-names
+ (map car
+ (filter (match-lambda
+ ((name . _)
+ (if (string-prefix? "python-pytest-" name)
+ name
+ #f))
+ ( _ #f))
+ inputs)))
+ (filtered-flags
+ (filter identity
+ (append-map
+ (match-lambda
+ ((group . flags)
+ (if (member (string-append "python-pytest-" group)
+ input-names)
+ (list #f)
+ flags))
+ (_ (list #f)))
+ pytest-default-ignore-alist)))
+ (dir (string-append
+ (if (access? (dirname (getcwd)) W_OK)
+ (dirname (getcwd))
+ ;; fallback if we're already out-of-source
+ (getcwd))
+ "/.guix-pytest")))
+ (dynamic-wind
+ (lambda ()
+ (setenv "PYTHONPATH"
+ (string-append
+ (if former-path
+ (string-append former-path ":")
+ "")
+ dir))
+ (setenv "PYTEST_PLUGINS"
+ (string-append
+ (if (getenv "PYTEST_PLUGINS")
+ (string-append former-path ",")
+ "")
+ "pytest_guix_plugin"))
+ (mkdir-p dir)
+ (with-output-to-file (string-append dir "/__init__.py")
+ (lambda _ (display "")))
+ (with-output-to-file (string-append dir "/pytest_guix_plugin.py")
+ (lambda _
+ (display (pytest-ignore-flags-plugin filtered-flags)))))
+ thunk
+ (lambda ()
+ (setenv "PYTHONPATH" former-path)
+ (unsetenv "PYTEST_PLUGINS")))))
+
+(define-syntax-rule (with-guix-pytest-plugin inputs exp ...)
+ "Evaluate EXP in a context where the Guix pytest plugin is added."
+ (call-with-guix-pytest-plugin inputs (lambda () exp ...)))
+
+(define* (check #:key inputs tests? test-backend test-flags #: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
@@ -165,7 +254,8 @@ (define* (check #:key tests? test-backend test-flags
#:allow-other-keys)
(format #t "Using ~a~%" use-test-backend)
(match use-test-backend
('pytest
- (apply invoke pytest "-vv" test-flags))
+ (with-guix-pytest-plugin inputs
+ (apply invoke pytest "-vv" test-flags)))
('nose
(apply invoke nosetests "-v" test-flags))
('nose2
@@ -386,3 +476,6 @@ (define* (pyproject-build #:key inputs (phases
%standard-phases)
(apply python:python-build #:inputs inputs #:phases phases args))
;;; pyproject-build-system.scm ends here
+;;; Local Variables:
+;;; eval: (put 'with-guix-pytest-plugin 'scheme-indent-function 1)
+;;; End:
--
2.41.0
- [bug#70858] [PATCH 31/32] gnu: python-linear-operator: Remove python-flake8-print native-input, (continued)
- [bug#70858] [PATCH 31/32] gnu: python-linear-operator: Remove python-flake8-print native-input, Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 30/32] build-system/pyproject: Stop hiding options., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 18/32] build-system/pyproject: Remove python-mypy native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 11/32] build-system/pyproject: Remove python-coveralls native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 13/32] gnu: python-versioneer: Remove python-pycodestyle native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 12/32] build-system/pyproject: Remove python-pycodestyle native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 24/32] gnu: python-django-contact-form: Remove python-pytest-cov native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 22/32] build-system/pyproject: Remove python-pytest-cov native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 32/32] gnu: abjad-ext-ipython: Remove uneeded inputs., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 28/32] build-system/pyproject: Remove python-pytest-flake8 native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 02/32] build-system/pyproject: Ignore unwanted pytest flags.,
Nicolas Graves <=
- [bug#70858] [PATCH 05/32] build-system/pyproject: Remove python-flake8 inputs., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 07/32] gnu: python-cram: Remove python-coverage native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 06/32] build-system/pyproject: Remove python-coverage input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 19/32] gnu: python-immutables: Remove python-mypy native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 26/32] build-system/pyproject: Remove python-pytest-isort native-input., Nicolas Graves, 2024/05/10
- [bug#70858] [PATCH 29/32] build-system/pyproject: Remove python-pytest-mypy inputs., Nicolas Graves, 2024/05/10