1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (test-pypi)
22 #:use-module (guix import pypi)
23 #:use-module (guix base32)
24 #:use-module (guix memoization)
25 #:use-module (gcrypt hash)
26 #:use-module (guix tests)
27 #:use-module (guix build-system python)
28 #:use-module ((guix build utils) #:select (delete-file-recursively which mkdir-p))
29 #:use-module (srfi srfi-64)
30 #:use-module (ice-9 match))
35 \"version\": \"1.0.0\",
37 \"license\": \"GNU LGPL\",
38 \"summary\": \"summary\",
39 \"home_page\": \"http://example.com\",
41 \"download_url\": \"\"
47 \"url\": \"https://example.com/foo-1.0.0.egg\",
48 \"packagetype\": \"bdist_egg\"
50 \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
51 \"packagetype\": \"sdist\"
53 \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
54 \"packagetype\": \"bdist_wheel\"
60 (define test-source-hash
63 (define test-specifications
65 "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
66 "SomethingWithMarker[foo]>1.0;python_version<\"2.7\""
67 "requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < \"2.7\""
68 "pip @ https://github.com/pypa/pip/archive/1.3.1.zip#\
69 sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
71 (define test-requires.txt "\
73 # A comment after a space
81 ;; Beaker contains only optional dependencies.
82 (define test-requires.txt-beaker "\
94 (define test-metadata "\
95 Classifier: Programming Language :: Python :: 3.7
96 Requires-Dist: baz ~= 3
97 Requires-Dist: bar != 2
99 Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
102 (define test-metadata-with-extras "
103 Classifier: Programming Language :: Python :: 3.7
104 Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
105 Requires-Dist: wrapt (<2,>=1)
109 Requires-Dist: tox ; extra == 'dev'
110 Requires-Dist: bumpversion (<1) ; extra == 'dev'
113 ;;; Provides-Extra can appear before Requires-Dist.
114 (define test-metadata-with-extras-jedi "\
115 Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
116 Provides-Extra: testing
117 Requires-Dist: parso (>=0.3.0)
118 Provides-Extra: testing
119 Requires-Dist: pytest (>=3.1.0); extra == 'testing'
125 (test-equal "guix-package->pypi-name, old URL style"
127 (guix-package->pypi-name
129 (source (dummy-origin
131 "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
133 (test-equal "guix-package->pypi-name, new URL style"
135 (guix-package->pypi-name
137 (source (dummy-origin
139 "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
141 (test-equal "guix-package->pypi-name, several URLs"
143 (guix-package->pypi-name
147 (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
148 (pypi-uri "cram" "0.7"))))))))
150 (test-equal "specification->requirement-name"
151 '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
152 (map specification->requirement-name test-specifications))
154 (test-equal "parse-requires.txt"
155 (list '("foo" "bar") '("pytest"))
156 (mock ((ice-9 ports) call-with-input-file
157 call-with-input-string)
158 (parse-requires.txt test-requires.txt)))
160 (test-equal "parse-requires.txt - Beaker"
161 (list '() '("Mock" "coverage"))
162 (mock ((ice-9 ports) call-with-input-file
163 call-with-input-string)
164 (parse-requires.txt test-requires.txt-beaker)))
166 (test-equal "parse-wheel-metadata, with extras"
167 (list '("wrapt" "bar") '("tox" "bumpversion"))
168 (mock ((ice-9 ports) call-with-input-file
169 call-with-input-string)
170 (parse-wheel-metadata test-metadata-with-extras)))
172 (test-equal "parse-wheel-metadata, with extras - Jedi"
173 (list '("parso") '("pytest"))
174 (mock ((ice-9 ports) call-with-input-file
175 call-with-input-string)
176 (parse-wheel-metadata test-metadata-with-extras-jedi)))
178 (test-assert "pypi->guix-package, no wheel"
179 ;; Replace network resources with sample data.
180 (mock ((guix import utils) url-fetch
181 (lambda (url file-name)
183 ("https://example.com/foo-1.0.0.tar.gz"
185 ;; Unusual requires.txt location should still be found.
186 (mkdir-p "foo-1.0.0/src/bizarre.egg-info")
187 (with-output-to-file "foo-1.0.0/src/bizarre.egg-info/requires.txt"
189 (display test-requires.txt)))
190 (parameterize ((current-output-port (%make-void-port "rw+")))
191 (system* "tar" "czvf" file-name "foo-1.0.0/"))
192 (delete-file-recursively "foo-1.0.0")
193 (set! test-source-hash
194 (call-with-input-file file-name port-sha256))))
195 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
196 (_ (error "Unexpected URL: " url)))))
197 (mock ((guix http-client) http-fetch
200 ("https://pypi.org/pypi/foo/json"
201 (values (open-input-string test-json)
202 (string-length test-json)))
203 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
204 (_ (error "Unexpected URL: " url)))))
205 (match (pypi->guix-package "foo")
211 ('uri ('pypi-uri "foo" 'version))
215 ('build-system 'python-build-system)
216 ('propagated-inputs ('list 'python-bar 'python-foo))
217 ('native-inputs ('list 'python-pytest))
218 ('home-page "http://example.com")
219 ('synopsis "summary")
220 ('description "summary")
221 ('license 'license:lgpl2.0))
222 (string=? (bytevector->nix-base32-string
228 (test-skip (if (which "zip") 0 1))
229 (test-assert "pypi->guix-package, wheels"
230 ;; Replace network resources with sample data.
231 (mock ((guix import utils) url-fetch
232 (lambda (url file-name)
234 ("https://example.com/foo-1.0.0.tar.gz"
236 (mkdir-p "foo-1.0.0/foo.egg-info/")
237 (with-output-to-file "foo-1.0.0/foo.egg-info/requires.txt"
239 (display "wrong data to make sure we're testing wheels ")))
240 (parameterize ((current-output-port (%make-void-port "rw+")))
241 (system* "tar" "czvf" file-name "foo-1.0.0/"))
242 (delete-file-recursively "foo-1.0.0")
243 (set! test-source-hash
244 (call-with-input-file file-name port-sha256))))
245 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
247 (mkdir "foo-1.0.0.dist-info")
248 (with-output-to-file "foo-1.0.0.dist-info/METADATA"
250 (display test-metadata)))
251 (let ((zip-file (string-append file-name ".zip")))
252 ;; zip always adds a "zip" extension to the file it creates,
253 ;; so we need to rename it.
254 (system* "zip" "-q" zip-file "foo-1.0.0.dist-info/METADATA")
255 (rename-file zip-file file-name))
256 (delete-file-recursively "foo-1.0.0.dist-info")))
257 (_ (error "Unexpected URL: " url)))))
258 (mock ((guix http-client) http-fetch
261 ("https://pypi.org/pypi/foo/json"
262 (values (open-input-string test-json)
263 (string-length test-json)))
264 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
265 (_ (error "Unexpected URL: " url)))))
266 ;; Not clearing the memoization cache here would mean returning the value
267 ;; computed in the previous test.
268 (invalidate-memoization! pypi->guix-package)
269 (match (pypi->guix-package "foo")
275 ('uri ('pypi-uri "foo" 'version))
279 ('build-system 'python-build-system)
280 ('propagated-inputs ('list 'python-bar 'python-baz))
281 ('native-inputs ('list 'python-pytest))
282 ('home-page "http://example.com")
283 ('synopsis "summary")
284 ('description "summary")
285 ('license 'license:lgpl2.0))
286 (string=? (bytevector->nix-base32-string
292 (test-assert "pypi->guix-package, no usable requirement file."
293 ;; Replace network resources with sample data.
294 (mock ((guix import utils) url-fetch
295 (lambda (url file-name)
297 ("https://example.com/foo-1.0.0.tar.gz"
298 (mkdir-p "foo-1.0.0/foo.egg-info/")
299 (parameterize ((current-output-port (%make-void-port "rw+")))
300 (system* "tar" "czvf" file-name "foo-1.0.0/"))
301 (delete-file-recursively "foo-1.0.0")
302 (set! test-source-hash
303 (call-with-input-file file-name port-sha256)))
304 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
305 (_ (error "Unexpected URL: " url)))))
306 (mock ((guix http-client) http-fetch
309 ("https://pypi.org/pypi/foo/json"
310 (values (open-input-string test-json)
311 (string-length test-json)))
312 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
313 (_ (error "Unexpected URL: " url)))))
314 ;; Not clearing the memoization cache here would mean returning the value
315 ;; computed in the previous test.
316 (invalidate-memoization! pypi->guix-package)
317 (match (pypi->guix-package "foo")
323 ('uri ('pypi-uri "foo" 'version))
327 ('build-system 'python-build-system)
328 ('home-page "http://example.com")
329 ('synopsis "summary")
330 ('description "summary")
331 ('license 'license:lgpl2.0))
332 (string=? (bytevector->nix-base32-string