all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Graves via Guix-patches via <guix-patches@gnu.org>
To: 70570@debbugs.gnu.org
Subject: [bug#70570] [PATCH v2 2/2] guix: pyproject-build-system: Ignore unwanted pytest flags.
Date: Sun, 28 Apr 2024 12:14:42 +0200	[thread overview]
Message-ID: <874jblvlnx.fsf@ngraves.fr> (raw)
In-Reply-To: <20240427165504.8843-2-ngraves@ngraves.fr>

On 2024-04-27 18:54, Nicolas Graves wrote:

> * guix/build/pyproject-build-system.scm : Ignore unwanted pytest flags.
>
> Change-Id: Ib9f1602e5af11227e5b7ce124f0f9be4fa2b78e4
> ---
>  guix/build/pyproject-build-system.scm | 91 ++++++++++++++++++++++++++-
>  1 file changed, 89 insertions(+), 2 deletions(-)
>
> diff --git a/guix/build/pyproject-build-system.scm b/guix/build/pyproject-build-system.scm
> index 947d240114..ebe4e1941d 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.
>  ;;;
> @@ -142,7 +143,89 @@ (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
> +          (filter (match-lambda
> +                    (((name . _) ...)
> +                     (if (string-prefix? "python-pytest-" name)
> +                         name
> +                         #f))
> +                    ( _ #f))
> +                  inputs))

This filter is not working properly as it doesn't output names but
pairs. Will be changed in the next revision with a map car.

> +         (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))))
> +    (dynamic-wind
> +      (lambda ()
> +        (setenv "PYTHONPATH"
> +                (string-append
> +                 (if former-path
> +                     (string-append former-path ":")
> +                     "")
> +                 ".guix-pytest"))

I found that in some edge cases, a hidden directory in the current dir
can cause tests to fail. It only happened for one package which was
failing on the __init__.py from .guix-pytest, meaning that tests could
consider this directory.

So it's better to put that out-of-source in ../.guix-pytest. Will be
done in the next revision.

> +        (setenv "PYTEST_PLUGINS"
> +                (string-append
> +                 (if (getenv "PYTEST_PLUGINS")
> +                     (string-append former-path ",")
> +                     "")
> +                 "pytest_guix_plugin"))
> +        (mkdir-p ".guix-pytest")
> +        (with-output-to-file ".guix-pytest/__init__.py"
> +          (lambda _ (display "")))
> +        (with-output-to-file ".guix-pytest/pytest_guix_plugin.py"
> +          (lambda _
> +            (display (pytest-ignore-flags-plugin filtered-flags)))))
> +      thunk
> +      (lambda ()
> +        (setenv "PYTHONPATH" former-path)
> +        (unsetenv "PYTEST_PLUGINS")
> +        (when (file-exists? ".guix-pytest")
> +          (delete-file-recursively ".guix-pytest"))))))
> +
> +(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 +248,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 +470,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:

-- 
Best regards,
Nicolas Graves




      reply	other threads:[~2024-04-28 10:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-25 15:42 [bug#70570] [PATCH 0/2] Python: Ignore unwanted development inputs Nicolas Graves via Guix-patches via
2024-04-25 15:59 ` [bug#70570] [PATCH 1/2] guix: import: pypi: Ignore pypi-ignored-inputs Nicolas Graves via Guix-patches via
2024-04-25 15:59   ` [bug#70570] [PATCH 2/2] guix: pyproject-build-system: Ignore unwanted pytest flags Nicolas Graves via Guix-patches via
2024-04-26  8:47     ` Lars-Dominik Braun
2024-04-26 10:14       ` Nicolas Graves via Guix-patches via
2024-04-27 16:09         ` Nicolas Graves via Guix-patches via
2024-04-26  8:26   ` [bug#70570] [PATCH 1/2] guix: import: pypi: Ignore pypi-ignored-inputs Lars-Dominik Braun
2024-04-26 10:23     ` Nicolas Graves via Guix-patches via
2024-04-27 16:54 ` [bug#70570] [PATCH v2 " Nicolas Graves via Guix-patches via
2024-04-27 16:54   ` [bug#70570] [PATCH v2 2/2] guix: pyproject-build-system: Ignore unwanted pytest flags Nicolas Graves via Guix-patches via
2024-04-28 10:14     ` Nicolas Graves via Guix-patches via [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874jblvlnx.fsf@ngraves.fr \
    --to=guix-patches@gnu.org \
    --cc=70570@debbugs.gnu.org \
    --cc=ngraves@ngraves.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.