Merge remote-tracking branch 'origin/master' into core-updates-frozen
[jackhill/guix/guix.git] / tests / pypi.scm
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>
5 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (test-pypi)
23 #:use-module (guix import pypi)
24 #:use-module (guix base32)
25 #:use-module (guix memoization)
26 #:use-module (gcrypt hash)
27 #:use-module (guix tests)
28 #:use-module (guix build-system python)
29 #:use-module ((guix build utils) #:select (delete-file-recursively which mkdir-p))
30 #:use-module (srfi srfi-64)
31 #:use-module (ice-9 match))
32
33 (define test-json-1
34 "{
35 \"info\": {
36 \"version\": \"1.0.0\",
37 \"name\": \"foo\",
38 \"license\": \"GNU LGPL\",
39 \"summary\": \"summary\",
40 \"home_page\": \"http://example.com\",
41 \"classifiers\": [],
42 \"download_url\": \"\"
43 },
44 \"urls\": [],
45 \"releases\": {
46 \"1.0.0\": [
47 {
48 \"url\": \"https://example.com/foo-1.0.0.egg\",
49 \"packagetype\": \"bdist_egg\"
50 }, {
51 \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
52 \"packagetype\": \"sdist\"
53 }, {
54 \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
55 \"packagetype\": \"bdist_wheel\"
56 }
57 ]
58 }
59 }")
60
61 (define test-json-2
62 "{
63 \"info\": {
64 \"version\": \"1.0.0\",
65 \"name\": \"foo-99\",
66 \"license\": \"GNU LGPL\",
67 \"summary\": \"summary\",
68 \"home_page\": \"http://example.com\",
69 \"classifiers\": [],
70 \"download_url\": \"\"
71 },
72 \"urls\": [],
73 \"releases\": {
74 \"1.0.0\": [
75 {
76 \"url\": \"https://example.com/foo-99-1.0.0.egg\",
77 \"packagetype\": \"bdist_egg\"
78 }, {
79 \"url\": \"https://example.com/foo-99-1.0.0.tar.gz\",
80 \"packagetype\": \"sdist\"
81 }, {
82 \"url\": \"https://example.com/foo-99-1.0.0-py2.py3-none-any.whl\",
83 \"packagetype\": \"bdist_wheel\"
84 }
85 ]
86 }
87 }")
88
89 (define test-source-hash
90 "")
91
92 (define test-specifications
93 '("Fizzy [foo, bar]"
94 "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
95 "SomethingWithMarker[foo]>1.0;python_version<\"2.7\""
96 "requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < \"2.7\""
97 "pip @ https://github.com/pypa/pip/archive/1.3.1.zip#\
98 sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
99
100 (define test-requires.txt "\
101 # A comment
102 # A comment after a space
103 foo ~= 3
104 bar != 2
105
106 [test]
107 pytest (>=2.5.0)
108 ")
109
110 ;; Beaker contains only optional dependencies.
111 (define test-requires.txt-beaker "\
112 [crypto]
113 pycryptopp>=0.5.12
114
115 [cryptography]
116 cryptography
117
118 [testsuite]
119 Mock
120 coverage
121 ")
122
123 (define test-metadata "\
124 Classifier: Programming Language :: Python :: 3.7
125 Requires-Dist: baz ~= 3
126 Requires-Dist: bar != 2
127 Provides-Extra: test
128 Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
129 ")
130
131 (define test-metadata-with-extras "
132 Classifier: Programming Language :: Python :: 3.7
133 Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
134 Requires-Dist: wrapt (<2,>=1)
135 Requires-Dist: bar
136
137 Provides-Extra: dev
138 Requires-Dist: tox ; extra == 'dev'
139 Requires-Dist: bumpversion (<1) ; extra == 'dev'
140 ")
141
142 ;;; Provides-Extra can appear before Requires-Dist.
143 (define test-metadata-with-extras-jedi "\
144 Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
145 Provides-Extra: testing
146 Requires-Dist: parso (>=0.3.0)
147 Provides-Extra: testing
148 Requires-Dist: pytest (>=3.1.0); extra == 'testing'
149 ")
150
151 \f
152 (test-begin "pypi")
153
154 (test-equal "guix-package->pypi-name, old URL style"
155 "psutil"
156 (guix-package->pypi-name
157 (dummy-package "foo"
158 (source (dummy-origin
159 (uri
160 "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
161
162 (test-equal "guix-package->pypi-name, new URL style"
163 "certbot"
164 (guix-package->pypi-name
165 (dummy-package "foo"
166 (source (dummy-origin
167 (uri
168 "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
169
170 (test-equal "guix-package->pypi-name, several URLs"
171 "cram"
172 (guix-package->pypi-name
173 (dummy-package "foo"
174 (source
175 (dummy-origin
176 (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
177 (pypi-uri "cram" "0.7"))))))))
178
179 (test-equal "guix-package->pypi-name, honor 'upstream-name'"
180 "bar-3"
181 (guix-package->pypi-name
182 (dummy-package "foo"
183 (properties
184 '((upstream-name . "bar-3"))))))
185
186 (test-equal "specification->requirement-name"
187 '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
188 (map specification->requirement-name test-specifications))
189
190 (test-equal "parse-requires.txt"
191 (list '("foo" "bar") '("pytest"))
192 (mock ((ice-9 ports) call-with-input-file
193 call-with-input-string)
194 (parse-requires.txt test-requires.txt)))
195
196 (test-equal "parse-requires.txt - Beaker"
197 (list '() '("Mock" "coverage"))
198 (mock ((ice-9 ports) call-with-input-file
199 call-with-input-string)
200 (parse-requires.txt test-requires.txt-beaker)))
201
202 (test-equal "parse-wheel-metadata, with extras"
203 (list '("wrapt" "bar") '("tox" "bumpversion"))
204 (mock ((ice-9 ports) call-with-input-file
205 call-with-input-string)
206 (parse-wheel-metadata test-metadata-with-extras)))
207
208 (test-equal "parse-wheel-metadata, with extras - Jedi"
209 (list '("parso") '("pytest"))
210 (mock ((ice-9 ports) call-with-input-file
211 call-with-input-string)
212 (parse-wheel-metadata test-metadata-with-extras-jedi)))
213
214 (test-assert "pypi->guix-package, no wheel"
215 ;; Replace network resources with sample data.
216 (mock ((guix import utils) url-fetch
217 (lambda (url file-name)
218 (match url
219 ("https://example.com/foo-1.0.0.tar.gz"
220 (begin
221 ;; Unusual requires.txt location should still be found.
222 (mkdir-p "foo-1.0.0/src/bizarre.egg-info")
223 (with-output-to-file "foo-1.0.0/src/bizarre.egg-info/requires.txt"
224 (lambda ()
225 (display test-requires.txt)))
226 (parameterize ((current-output-port (%make-void-port "rw+")))
227 (system* "tar" "czvf" file-name "foo-1.0.0/"))
228 (delete-file-recursively "foo-1.0.0")
229 (set! test-source-hash
230 (call-with-input-file file-name port-sha256))))
231 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
232 (_ (error "Unexpected URL: " url)))))
233 (mock ((guix http-client) http-fetch
234 (lambda (url . rest)
235 (match url
236 ("https://pypi.org/pypi/foo/json"
237 (values (open-input-string test-json-1)
238 (string-length test-json-1)))
239 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
240 (_ (error "Unexpected URL: " url)))))
241 (match (pypi->guix-package "foo")
242 (('package
243 ('name "python-foo")
244 ('version "1.0.0")
245 ('source ('origin
246 ('method 'url-fetch)
247 ('uri ('pypi-uri "foo" 'version))
248 ('sha256
249 ('base32
250 (? string? hash)))))
251 ('build-system 'python-build-system)
252 ('propagated-inputs ('list 'python-bar 'python-foo))
253 ('native-inputs ('list 'python-pytest))
254 ('home-page "http://example.com")
255 ('synopsis "summary")
256 ('description "summary")
257 ('license 'license:lgpl2.0))
258 (string=? (bytevector->nix-base32-string
259 test-source-hash)
260 hash))
261 (x
262 (pk 'fail x #f))))))
263
264 (test-skip (if (which "zip") 0 1))
265 (test-assert "pypi->guix-package, wheels"
266 ;; Replace network resources with sample data.
267 (mock ((guix import utils) url-fetch
268 (lambda (url file-name)
269 (match url
270 ("https://example.com/foo-1.0.0.tar.gz"
271 (begin
272 (mkdir-p "foo-1.0.0/foo.egg-info/")
273 (with-output-to-file "foo-1.0.0/foo.egg-info/requires.txt"
274 (lambda ()
275 (display "wrong data to make sure we're testing wheels ")))
276 (parameterize ((current-output-port (%make-void-port "rw+")))
277 (system* "tar" "czvf" file-name "foo-1.0.0/"))
278 (delete-file-recursively "foo-1.0.0")
279 (set! test-source-hash
280 (call-with-input-file file-name port-sha256))))
281 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
282 (begin
283 (mkdir "foo-1.0.0.dist-info")
284 (with-output-to-file "foo-1.0.0.dist-info/METADATA"
285 (lambda ()
286 (display test-metadata)))
287 (let ((zip-file (string-append file-name ".zip")))
288 ;; zip always adds a "zip" extension to the file it creates,
289 ;; so we need to rename it.
290 (system* "zip" "-q" zip-file "foo-1.0.0.dist-info/METADATA")
291 (rename-file zip-file file-name))
292 (delete-file-recursively "foo-1.0.0.dist-info")))
293 (_ (error "Unexpected URL: " url)))))
294 (mock ((guix http-client) http-fetch
295 (lambda (url . rest)
296 (match url
297 ("https://pypi.org/pypi/foo/json"
298 (values (open-input-string test-json-1)
299 (string-length test-json-1)))
300 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
301 (_ (error "Unexpected URL: " url)))))
302 ;; Not clearing the memoization cache here would mean returning the value
303 ;; computed in the previous test.
304 (invalidate-memoization! pypi->guix-package)
305 (match (pypi->guix-package "foo")
306 (('package
307 ('name "python-foo")
308 ('version "1.0.0")
309 ('source ('origin
310 ('method 'url-fetch)
311 ('uri ('pypi-uri "foo" 'version))
312 ('sha256
313 ('base32
314 (? string? hash)))))
315 ('build-system 'python-build-system)
316 ('propagated-inputs ('list 'python-bar 'python-baz))
317 ('native-inputs ('list 'python-pytest))
318 ('home-page "http://example.com")
319 ('synopsis "summary")
320 ('description "summary")
321 ('license 'license:lgpl2.0))
322 (string=? (bytevector->nix-base32-string
323 test-source-hash)
324 hash))
325 (x
326 (pk 'fail x #f))))))
327
328 (test-assert "pypi->guix-package, no usable requirement file."
329 ;; Replace network resources with sample data.
330 (mock ((guix import utils) url-fetch
331 (lambda (url file-name)
332 (match url
333 ("https://example.com/foo-1.0.0.tar.gz"
334 (mkdir-p "foo-1.0.0/foo.egg-info/")
335 (parameterize ((current-output-port (%make-void-port "rw+")))
336 (system* "tar" "czvf" file-name "foo-1.0.0/"))
337 (delete-file-recursively "foo-1.0.0")
338 (set! test-source-hash
339 (call-with-input-file file-name port-sha256)))
340 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
341 (_ (error "Unexpected URL: " url)))))
342 (mock ((guix http-client) http-fetch
343 (lambda (url . rest)
344 (match url
345 ("https://pypi.org/pypi/foo/json"
346 (values (open-input-string test-json-1)
347 (string-length test-json-1)))
348 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
349 (_ (error "Unexpected URL: " url)))))
350 ;; Not clearing the memoization cache here would mean returning the value
351 ;; computed in the previous test.
352 (invalidate-memoization! pypi->guix-package)
353 (match (pypi->guix-package "foo")
354 (('package
355 ('name "python-foo")
356 ('version "1.0.0")
357 ('source ('origin
358 ('method 'url-fetch)
359 ('uri ('pypi-uri "foo" 'version))
360 ('sha256
361 ('base32
362 (? string? hash)))))
363 ('build-system 'python-build-system)
364 ('home-page "http://example.com")
365 ('synopsis "summary")
366 ('description "summary")
367 ('license 'license:lgpl2.0))
368 (string=? (bytevector->nix-base32-string
369 test-source-hash)
370 hash))
371 (x
372 (pk 'fail x #f))))))
373
374 (test-assert "pypi->guix-package, package name contains \"-\" followed by digits"
375 ;; Replace network resources with sample data.
376 (mock ((guix import utils) url-fetch
377 (lambda (url file-name)
378 (match url
379 ("https://example.com/foo-99-1.0.0.tar.gz"
380 (begin
381 ;; Unusual requires.txt location should still be found.
382 (mkdir-p "foo-99-1.0.0/src/bizarre.egg-info")
383 (with-output-to-file "foo-99-1.0.0/src/bizarre.egg-info/requires.txt"
384 (lambda ()
385 (display test-requires.txt)))
386 (parameterize ((current-output-port (%make-void-port "rw+")))
387 (system* "tar" "czvf" file-name "foo-99-1.0.0/"))
388 (delete-file-recursively "foo-99-1.0.0")
389 (set! test-source-hash
390 (call-with-input-file file-name port-sha256))))
391 ("https://example.com/foo-99-1.0.0-py2.py3-none-any.whl" #f)
392 (_ (error "Unexpected URL: " url)))))
393 (mock ((guix http-client) http-fetch
394 (lambda (url . rest)
395 (match url
396 ("https://pypi.org/pypi/foo-99/json"
397 (values (open-input-string test-json-2)
398 (string-length test-json-2)))
399 ("https://example.com/foo-99-1.0.0-py2.py3-none-any.whl" #f)
400 (_ (error "Unexpected URL: " url)))))
401 (match (pypi->guix-package "foo-99")
402 (('package
403 ('name "python-foo-99")
404 ('version "1.0.0")
405 ('source ('origin
406 ('method 'url-fetch)
407 ('uri ('pypi-uri "foo-99" 'version))
408 ('sha256
409 ('base32
410 (? string? hash)))))
411 ('properties ('quote (("upstream-name" . "foo-99"))))
412 ('build-system 'python-build-system)
413 ('propagated-inputs
414 ('quasiquote
415 (("python-bar" ('unquote 'python-bar))
416 ("python-foo" ('unquote 'python-foo)))))
417 ('native-inputs
418 ('quasiquote
419 (("python-pytest" ('unquote 'python-pytest)))))
420 ('home-page "http://example.com")
421 ('synopsis "summary")
422 ('description "summary")
423 ('license 'license:lgpl2.0))
424 (string=? (bytevector->nix-base32-string
425 test-source-hash)
426 hash))
427 (x
428 (pk 'fail x #f))))))
429
430 (test-end "pypi")