1
Fork 0
mirror of https://https.git.savannah.gnu.org/git/guix.git/ synced 2025-07-13 18:40:57 +02:00

tests: pypi: Rewrite tests using a local HTTP server.

* guix/import/pypi.scm (%pypi-base-url): New variable.
(pypi-fetch): Use it.
* tests/pypi.scm (foo-json): Compute URLs relative to '%local-url'.
(test-json-1, test-json-2, test-source-hash): Remove.
(file-dump): New procedure.
(with-pypi): New macro.
("pypi->guix-package, no wheel")
("pypi->guix-package, wheels")
("pypi->guix-package, no usable requirement file.")
("pypi->guix-package, package name contains \"-\" followed by digits"):
Rewrite using 'with-pypi'.
This commit is contained in:
Ludovic Courtès 2023-05-17 12:09:40 +02:00
parent 09526da78f
commit d2f36abd02
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 160 additions and 202 deletions

View file

@ -55,7 +55,8 @@
#:use-module (guix packages) #:use-module (guix packages)
#:use-module (guix upstream) #:use-module (guix upstream)
#:use-module ((guix licenses) #:prefix license:) #:use-module ((guix licenses) #:prefix license:)
#:export (parse-requires.txt #:export (%pypi-base-url
parse-requires.txt
parse-wheel-metadata parse-wheel-metadata
specification->requirement-name specification->requirement-name
guix-package->pypi-name guix-package->pypi-name
@ -67,6 +68,10 @@
;; The PyPI API (notice the rhyme) is "documented" at: ;; The PyPI API (notice the rhyme) is "documented" at:
;; <https://warehouse.readthedocs.io/api-reference/json/>. ;; <https://warehouse.readthedocs.io/api-reference/json/>.
(define %pypi-base-url
;; Base URL of the PyPI API.
(make-parameter "https://pypi.org/pypi/"))
(define non-empty-string-or-false (define non-empty-string-or-false
(match-lambda (match-lambda
("" #f) ("" #f)
@ -123,7 +128,7 @@
(define (pypi-fetch name) (define (pypi-fetch name)
"Return a <pypi-project> record for package NAME, or #f on failure." "Return a <pypi-project> record for package NAME, or #f on failure."
(and=> (json-fetch (string-append "https://pypi.org/pypi/" name "/json")) (and=> (json-fetch (string-append (%pypi-base-url) name "/json"))
json->pypi-project)) json->pypi-project))
;; For packages found on PyPI that lack a source distribution. ;; For packages found on PyPI that lack a source distribution.

View file

@ -27,10 +27,11 @@
#:use-module (guix utils) #:use-module (guix utils)
#:use-module (gcrypt hash) #:use-module (gcrypt hash)
#:use-module (guix tests) #:use-module (guix tests)
#:use-module (guix tests http)
#:use-module (guix build-system python) #:use-module (guix build-system python)
#:use-module ((guix build utils) #:use-module ((guix build utils)
#:select (delete-file-recursively #:select (delete-file-recursively
which mkdir-p which mkdir-p dump-port
with-directory-excursion)) with-directory-excursion))
#:use-module ((guix diagnostics) #:select (guix-warning-port)) #:use-module ((guix diagnostics) #:select (guix-warning-port))
#:use-module ((guix build syscalls) #:select (mkdtemp!)) #:use-module ((guix build syscalls) #:select (mkdtemp!))
@ -57,25 +58,19 @@ optionally using a different @var{name in its URL}."
(urls . #()) (urls . #())
(releases (releases
. ((1.0.0 . ((1.0.0
. #(((url . ,(format #f "https://example.com/~a-1.0.0.egg" . #(((url . ,(format #f "~a/~a-1.0.0.egg"
(%local-url #:path "")
(or name-in-url name))) (or name-in-url name)))
(packagetype . "bdist_egg")) (packagetype . "bdist_egg"))
((url . ,(format #f "https://example.com/~a-1.0.0.tar.gz" ((url . ,(format #f "~a/~a-1.0.0.tar.gz"
(%local-url #:path "")
(or name-in-url name))) (or name-in-url name)))
(packagetype . "sdist")) (packagetype . "sdist"))
((url . ,(format #f "https://example.com/~a-1.0.0-py2.py3-none-any.whl" ((url . ,(format #f "~a/~a-1.0.0-py2.py3-none-any.whl"
(%local-url #:path "")
(or name-in-url name))) (or name-in-url name)))
(packagetype . "bdist_wheel"))))))))) (packagetype . "bdist_wheel")))))))))
(define test-json-1
(foo-json))
(define test-json-2
(foo-json #:name "foo-99"))
(define test-source-hash
"")
(define test-specifications (define test-specifications
'("Fizzy [foo, bar]" '("Fizzy [foo, bar]"
"PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1" "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
@ -187,6 +182,18 @@ files specified by SPECS. Return its file name."
(delete-file-recursively directory) (delete-file-recursively directory)
whl-file)) whl-file))
(define (file-dump file)
"Return a procedure that dumps FILE to the given port."
(lambda (output)
(call-with-input-file file
(lambda (input)
(dump-port input output)))))
(define-syntax-rule (with-pypi responses body ...)
(with-http-server responses
(parameterize ((%pypi-base-url (%local-url #:path "/")))
body ...)))
(test-begin "pypi") (test-begin "pypi")
@ -275,28 +282,15 @@ files specified by SPECS. Return its file name."
"https://files.pythonhosted.org/packages/f0/f00/goo-0.0.0.tar.gz")) "https://files.pythonhosted.org/packages/f0/f00/goo-0.0.0.tar.gz"))
(test-assert "pypi->guix-package, no wheel" (test-assert "pypi->guix-package, no wheel"
;; Replace network resources with sample data. (let ((tarball (pypi-tarball
(mock ((guix import utils) url-fetch "foo-1.0.0"
(lambda (url file-name)
(match url
("https://example.com/foo-1.0.0.tar.gz"
;; Unusual requires.txt location should still be found.
(let ((tarball (pypi-tarball "foo-1.0.0"
`(("src/bizarre.egg-info/requires.txt" `(("src/bizarre.egg-info/requires.txt"
,test-requires.txt))))) ,test-requires.txt))))
(copy-file tarball file-name) (twice (lambda (lst) (append lst lst))))
(set! test-source-hash (with-pypi (twice `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
(call-with-input-file file-name port-sha256)))) ("/foo-1.0.0-py2.py3-none-any.whl" 404 "")
("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f) ("/foo/json" 200 ,(lambda (port)
(_ (error "Unexpected URL: " url))))) (display (foo-json) port)))))
(mock ((guix http-client) http-fetch
(lambda (url . rest)
(match url
("https://pypi.org/pypi/foo/json"
(values (open-input-string test-json-1)
(string-length test-json-1)))
("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
(_ (error "Unexpected URL: " url)))))
(match (pypi->guix-package "foo") (match (pypi->guix-package "foo")
(('package (('package
('name "python-foo") ('name "python-foo")
@ -315,7 +309,7 @@ files specified by SPECS. Return its file name."
('description "summary") ('description "summary")
('license 'license:lgpl2.0)) ('license 'license:lgpl2.0))
(and (string=? (bytevector->nix-base32-string (and (string=? (bytevector->nix-base32-string
test-source-hash) (file-sha256 tarball))
hash) hash)
(equal? (pypi->guix-package "foo" #:version "1.0.0") (equal? (pypi->guix-package "foo" #:version "1.0.0")
(pypi->guix-package "foo")) (pypi->guix-package "foo"))
@ -326,32 +320,18 @@ files specified by SPECS. Return its file name."
(test-skip (if (which "zip") 0 1)) (test-skip (if (which "zip") 0 1))
(test-assert "pypi->guix-package, wheels" (test-assert "pypi->guix-package, wheels"
;; Replace network resources with sample data.
(mock ((guix import utils) url-fetch
(lambda (url file-name)
(match url
("https://example.com/foo-1.0.0.tar.gz"
(let ((tarball (pypi-tarball (let ((tarball (pypi-tarball
"foo-1.0.0" "foo-1.0.0"
'(("foo-1.0.0/foo.egg-info/requires.txt" '(("foo-1.0.0/foo.egg-info/requires.txt"
"wrong data \ "wrong data \
to make sure we're testing wheels"))))) to make sure we're testing wheels"))))
(copy-file tarball file-name) (wheel (wheel-file "foo-1.0.0"
(set! test-source-hash
(call-with-input-file file-name port-sha256))))
("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
(let ((wheel (wheel-file "foo-1.0.0"
`(("METADATA" ,test-metadata))))) `(("METADATA" ,test-metadata)))))
(copy-file wheel file-name))) (with-pypi `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
(_ (error "Unexpected URL: " url))))) ("/foo-1.0.0-py2.py3-none-any.whl"
(mock ((guix http-client) http-fetch 200 ,(file-dump wheel))
(lambda (url . rest) ("/foo/json" 200 ,(lambda (port)
(match url (display (foo-json) port))))
("https://pypi.org/pypi/foo/json"
(values (open-input-string test-json-1)
(string-length test-json-1)))
("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
(_ (error "Unexpected URL: " url)))))
;; Not clearing the memoization cache here would mean returning the value ;; Not clearing the memoization cache here would mean returning the value
;; computed in the previous test. ;; computed in the previous test.
(invalidate-memoization! pypi->guix-package) (invalidate-memoization! pypi->guix-package)
@ -372,35 +352,20 @@ to make sure we're testing wheels")))))
('synopsis "summary") ('synopsis "summary")
('description "summary") ('description "summary")
('license 'license:lgpl2.0)) ('license 'license:lgpl2.0))
(string=? (bytevector->nix-base32-string (string=? (bytevector->nix-base32-string (file-sha256 tarball))
test-source-hash)
hash)) hash))
(x (x
(pk 'fail x #f)))))) (pk 'fail x #f))))))
(test-assert "pypi->guix-package, no usable requirement file." (test-assert "pypi->guix-package, no usable requirement file."
;; Replace network resources with sample data.
(mock ((guix import utils) url-fetch
(lambda (url file-name)
(match url
("https://example.com/foo-1.0.0.tar.gz"
(let ((tarball (pypi-tarball "foo-1.0.0" (let ((tarball (pypi-tarball "foo-1.0.0"
'(("foo.egg-info/.empty" ""))))) '(("foo.egg-info/.empty" "")))))
(copy-file tarball file-name) (with-pypi `(("/foo-1.0.0.tar.gz" 200 ,(file-dump tarball))
(set! test-source-hash ("/foo-1.0.0-py2.py3-none-any.whl" 404 "")
(call-with-input-file file-name port-sha256)))) ("/foo/json" 200 ,(lambda (port)
("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f) (display (foo-json) port))))
(_ (error "Unexpected URL: " url))))) ;; Not clearing the memoization cache here would mean returning the
(mock ((guix http-client) http-fetch ;; value computed in the previous test.
(lambda (url . rest)
(match url
("https://pypi.org/pypi/foo/json"
(values (open-input-string test-json-1)
(string-length test-json-1)))
("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
(_ (error "Unexpected URL: " url)))))
;; Not clearing the memoization cache here would mean returning the value
;; computed in the previous test.
(invalidate-memoization! pypi->guix-package) (invalidate-memoization! pypi->guix-package)
(match (pypi->guix-package "foo") (match (pypi->guix-package "foo")
(('package (('package
@ -417,35 +382,20 @@ to make sure we're testing wheels")))))
('synopsis "summary") ('synopsis "summary")
('description "summary") ('description "summary")
('license 'license:lgpl2.0)) ('license 'license:lgpl2.0))
(string=? (bytevector->nix-base32-string (string=? (bytevector->nix-base32-string (file-sha256 tarball))
test-source-hash)
hash)) hash))
(x (x
(pk 'fail x #f)))))) (pk 'fail x #f))))))
(test-assert "pypi->guix-package, package name contains \"-\" followed by digits" (test-assert "pypi->guix-package, package name contains \"-\" followed by digits"
;; Replace network resources with sample data.
(mock ((guix import utils) url-fetch
(lambda (url file-name)
(match url
("https://example.com/foo-99-1.0.0.tar.gz"
(let ((tarball (pypi-tarball "foo-99-1.0.0" (let ((tarball (pypi-tarball "foo-99-1.0.0"
`(("src/bizarre.egg-info/requires.txt" `(("src/bizarre.egg-info/requires.txt"
,test-requires.txt))))) ,test-requires.txt)))))
;; Unusual requires.txt location should still be found. (with-pypi `(("/foo-99-1.0.0.tar.gz" 200 ,(file-dump tarball))
(copy-file tarball file-name) ("/foo-99-1.0.0-py2.py3-none-any.whl" 404 "")
(set! test-source-hash ("/foo-99/json" 200 ,(lambda (port)
(call-with-input-file file-name port-sha256)))) (display (foo-json #:name "foo-99")
("https://example.com/foo-99-1.0.0-py2.py3-none-any.whl" #f) port))))
(_ (error "Unexpected URL: " url)))))
(mock ((guix http-client) http-fetch
(lambda (url . rest)
(match url
("https://pypi.org/pypi/foo-99/json"
(values (open-input-string test-json-2)
(string-length test-json-2)))
("https://example.com/foo-99-1.0.0-py2.py3-none-any.whl" #f)
(_ (error "Unexpected URL: " url)))))
(match (pypi->guix-package "foo-99") (match (pypi->guix-package "foo-99")
(('package (('package
('name "python-foo-99") ('name "python-foo-99")
@ -464,11 +414,14 @@ to make sure we're testing wheels")))))
('synopsis "summary") ('synopsis "summary")
('description "summary") ('description "summary")
('license 'license:lgpl2.0)) ('license 'license:lgpl2.0))
(string=? (bytevector->nix-base32-string (string=? (bytevector->nix-base32-string (file-sha256 tarball))
test-source-hash)
hash)) hash))
(x (x
(pk 'fail x #f)))))) (pk 'fail x #f))))))
(test-end "pypi") (test-end "pypi")
(delete-file-recursively sample-directory) (delete-file-recursively sample-directory)
;; Local Variables:
;; eval: (put 'with-pypi 'scheme-indent-function 1)
;; End: