gnu: utf-8-lineseparator: Update to 7.
[jackhill/guix/guix.git] / tests / pypi.scm
CommitLineData
1b3e9685
DT
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 David Thompson <davet@gnu.org>
506abddb 3;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
d514276b 4;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
7b75f90c 5;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
1b3e9685
DT
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)
c799ad72 25 #:use-module (guix memoization)
ca719424 26 #:use-module (gcrypt hash)
694b317c 27 #:use-module (guix tests)
4eaac4b7 28 #:use-module (guix build-system python)
01589acc 29 #:use-module ((guix build utils) #:select (delete-file-recursively which mkdir-p))
1b3e9685
DT
30 #:use-module (srfi srfi-64)
31 #:use-module (ice-9 match))
32
7b75f90c 33(define test-json-1
1b3e9685
DT
34 "{
35 \"info\": {
36 \"version\": \"1.0.0\",
37 \"name\": \"foo\",
38 \"license\": \"GNU LGPL\",
39 \"summary\": \"summary\",
40 \"home_page\": \"http://example.com\",
7657e61d
LC
41 \"classifiers\": [],
42 \"download_url\": \"\"
1b3e9685 43 },
7657e61d 44 \"urls\": [],
1b3e9685
DT
45 \"releases\": {
46 \"1.0.0\": [
47 {
48 \"url\": \"https://example.com/foo-1.0.0.egg\",
5dfe02c6 49 \"packagetype\": \"bdist_egg\"
1b3e9685
DT
50 }, {
51 \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
5dfe02c6 52 \"packagetype\": \"sdist\"
266785d2
CR
53 }, {
54 \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
5dfe02c6 55 \"packagetype\": \"bdist_wheel\"
1b3e9685
DT
56 }
57 ]
58 }
59}")
60
7b75f90c
XC
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
ff986890
CR
89(define test-source-hash
90 "")
91
803fb336
MC
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#\
98sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
99
01589acc
MC
100(define test-requires.txt "\
101# A comment
ff986890 102 # A comment after a space
c4797121
MC
103foo ~= 3
104bar != 2
105
106[test]
107pytest (>=2.5.0)
108")
109
d514276b
MC
110;; Beaker contains only optional dependencies.
111(define test-requires.txt-beaker "\
112[crypto]
113pycryptopp>=0.5.12
114
115[cryptography]
116cryptography
117
118[testsuite]
119Mock
120coverage
121")
122
f0190a5d
MC
123(define test-metadata "\
124Classifier: Programming Language :: Python :: 3.7
125Requires-Dist: baz ~= 3
126Requires-Dist: bar != 2
127Provides-Extra: test
d514276b 128Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
f0190a5d
MC
129")
130
131(define test-metadata-with-extras "
132Classifier: Programming Language :: Python :: 3.7
133Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
134Requires-Dist: wrapt (<2,>=1)
135Requires-Dist: bar
136
137Provides-Extra: dev
138Requires-Dist: tox ; extra == 'dev'
139Requires-Dist: bumpversion (<1) ; extra == 'dev'
140")
141
142;;; Provides-Extra can appear before Requires-Dist.
143(define test-metadata-with-extras-jedi "\
144Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
145Provides-Extra: testing
146Requires-Dist: parso (>=0.3.0)
147Provides-Extra: testing
148Requires-Dist: pytest (>=3.1.0); extra == 'testing'
149")
266785d2 150
5dfe02c6 151\f
1b3e9685
DT
152(test-begin "pypi")
153
8173ceee
LC
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
7277d06d 160 "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
8173ceee
LC
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
8440db45 168 "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
8173ceee 169
4eaac4b7
LC
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
7b75f90c
XC
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
803fb336
MC
186(test-equal "specification->requirement-name"
187 '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
188 (map specification->requirement-name test-specifications))
189
d514276b
MC
190(test-equal "parse-requires.txt"
191 (list '("foo" "bar") '("pytest"))
c4797121
MC
192 (mock ((ice-9 ports) call-with-input-file
193 call-with-input-string)
d514276b
MC
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)))
c4797121 201
f0190a5d 202(test-equal "parse-wheel-metadata, with extras"
d514276b 203 (list '("wrapt" "bar") '("tox" "bumpversion"))
f0190a5d
MC
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"
d514276b 209 (list '("parso") '("pytest"))
f0190a5d
MC
210 (mock ((ice-9 ports) call-with-input-file
211 call-with-input-string)
212 (parse-wheel-metadata test-metadata-with-extras-jedi)))
213
d514276b 214(test-assert "pypi->guix-package, no wheel"
1b3e9685 215 ;; Replace network resources with sample data.
506abddb
RW
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
c799ad72
MC
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"
506abddb 224 (lambda ()
01589acc 225 (display test-requires.txt)))
a853aceb
MC
226 (parameterize ((current-output-port (%make-void-port "rw+")))
227 (system* "tar" "czvf" file-name "foo-1.0.0/"))
506abddb
RW
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
ce8963c5 234 (lambda (url . rest)
506abddb 235 (match url
8440db45 236 ("https://pypi.org/pypi/foo/json"
7b75f90c
XC
237 (values (open-input-string test-json-1)
238 (string-length test-json-1)))
506abddb
RW
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)
b3d8153d 247 ('uri ('pypi-uri "foo" 'version))
506abddb
RW
248 ('sha256
249 ('base32
250 (? string? hash)))))
251 ('build-system 'python-build-system)
52a9a071
LC
252 ('propagated-inputs ('list 'python-bar 'python-foo))
253 ('native-inputs ('list 'python-pytest))
506abddb
RW
254 ('home-page "http://example.com")
255 ('synopsis "summary")
256 ('description "summary")
257 ('license 'license:lgpl2.0))
b20cd80f
LC
258 (and (string=? (bytevector->nix-base32-string
259 test-source-hash)
260 hash)
261 (equal? (pypi->guix-package "foo" #:version "1.0.0")
262 (pypi->guix-package "foo"))
263 (catch 'quit
264 (lambda ()
265 (pypi->guix-package "foo" #:version "42"))
266 (const #t))))
506abddb
RW
267 (x
268 (pk 'fail x #f))))))
266785d2
CR
269
270(test-skip (if (which "zip") 0 1))
271(test-assert "pypi->guix-package, wheels"
272 ;; Replace network resources with sample data.
273 (mock ((guix import utils) url-fetch
274 (lambda (url file-name)
275 (match url
266785d2 276 ("https://example.com/foo-1.0.0.tar.gz"
01589acc
MC
277 (begin
278 (mkdir-p "foo-1.0.0/foo.egg-info/")
279 (with-output-to-file "foo-1.0.0/foo.egg-info/requires.txt"
d514276b
MC
280 (lambda ()
281 (display "wrong data to make sure we're testing wheels ")))
a853aceb
MC
282 (parameterize ((current-output-port (%make-void-port "rw+")))
283 (system* "tar" "czvf" file-name "foo-1.0.0/"))
d514276b
MC
284 (delete-file-recursively "foo-1.0.0")
285 (set! test-source-hash
286 (call-with-input-file file-name port-sha256))))
266785d2 287 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
d514276b
MC
288 (begin
289 (mkdir "foo-1.0.0.dist-info")
290 (with-output-to-file "foo-1.0.0.dist-info/METADATA"
291 (lambda ()
292 (display test-metadata)))
293 (let ((zip-file (string-append file-name ".zip")))
294 ;; zip always adds a "zip" extension to the file it creates,
295 ;; so we need to rename it.
296 (system* "zip" "-q" zip-file "foo-1.0.0.dist-info/METADATA")
297 (rename-file zip-file file-name))
298 (delete-file-recursively "foo-1.0.0.dist-info")))
ff986890 299 (_ (error "Unexpected URL: " url)))))
239f4632 300 (mock ((guix http-client) http-fetch
ce8963c5 301 (lambda (url . rest)
239f4632 302 (match url
8440db45 303 ("https://pypi.org/pypi/foo/json"
7b75f90c
XC
304 (values (open-input-string test-json-1)
305 (string-length test-json-1)))
239f4632
RW
306 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
307 (_ (error "Unexpected URL: " url)))))
f0190a5d
MC
308 ;; Not clearing the memoization cache here would mean returning the value
309 ;; computed in the previous test.
310 (invalidate-memoization! pypi->guix-package)
239f4632
RW
311 (match (pypi->guix-package "foo")
312 (('package
313 ('name "python-foo")
314 ('version "1.0.0")
315 ('source ('origin
316 ('method 'url-fetch)
b3d8153d 317 ('uri ('pypi-uri "foo" 'version))
239f4632
RW
318 ('sha256
319 ('base32
320 (? string? hash)))))
321 ('build-system 'python-build-system)
52a9a071
LC
322 ('propagated-inputs ('list 'python-bar 'python-baz))
323 ('native-inputs ('list 'python-pytest))
239f4632
RW
324 ('home-page "http://example.com")
325 ('synopsis "summary")
326 ('description "summary")
327 ('license 'license:lgpl2.0))
328 (string=? (bytevector->nix-base32-string
329 test-source-hash)
330 hash))
331 (x
332 (pk 'fail x #f))))))
1b3e9685 333
c799ad72
MC
334(test-assert "pypi->guix-package, no usable requirement file."
335 ;; Replace network resources with sample data.
336 (mock ((guix import utils) url-fetch
337 (lambda (url file-name)
338 (match url
339 ("https://example.com/foo-1.0.0.tar.gz"
340 (mkdir-p "foo-1.0.0/foo.egg-info/")
341 (parameterize ((current-output-port (%make-void-port "rw+")))
342 (system* "tar" "czvf" file-name "foo-1.0.0/"))
343 (delete-file-recursively "foo-1.0.0")
344 (set! test-source-hash
345 (call-with-input-file file-name port-sha256)))
346 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
347 (_ (error "Unexpected URL: " url)))))
348 (mock ((guix http-client) http-fetch
349 (lambda (url . rest)
350 (match url
351 ("https://pypi.org/pypi/foo/json"
7b75f90c
XC
352 (values (open-input-string test-json-1)
353 (string-length test-json-1)))
c799ad72
MC
354 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
355 (_ (error "Unexpected URL: " url)))))
356 ;; Not clearing the memoization cache here would mean returning the value
357 ;; computed in the previous test.
358 (invalidate-memoization! pypi->guix-package)
359 (match (pypi->guix-package "foo")
360 (('package
361 ('name "python-foo")
362 ('version "1.0.0")
363 ('source ('origin
364 ('method 'url-fetch)
365 ('uri ('pypi-uri "foo" 'version))
366 ('sha256
367 ('base32
368 (? string? hash)))))
369 ('build-system 'python-build-system)
370 ('home-page "http://example.com")
371 ('synopsis "summary")
372 ('description "summary")
373 ('license 'license:lgpl2.0))
374 (string=? (bytevector->nix-base32-string
375 test-source-hash)
376 hash))
377 (x
378 (pk 'fail x #f))))))
379
7b75f90c
XC
380(test-assert "pypi->guix-package, package name contains \"-\" followed by digits"
381 ;; Replace network resources with sample data.
382 (mock ((guix import utils) url-fetch
383 (lambda (url file-name)
384 (match url
385 ("https://example.com/foo-99-1.0.0.tar.gz"
386 (begin
387 ;; Unusual requires.txt location should still be found.
388 (mkdir-p "foo-99-1.0.0/src/bizarre.egg-info")
389 (with-output-to-file "foo-99-1.0.0/src/bizarre.egg-info/requires.txt"
390 (lambda ()
391 (display test-requires.txt)))
392 (parameterize ((current-output-port (%make-void-port "rw+")))
393 (system* "tar" "czvf" file-name "foo-99-1.0.0/"))
394 (delete-file-recursively "foo-99-1.0.0")
395 (set! test-source-hash
396 (call-with-input-file file-name port-sha256))))
397 ("https://example.com/foo-99-1.0.0-py2.py3-none-any.whl" #f)
398 (_ (error "Unexpected URL: " url)))))
399 (mock ((guix http-client) http-fetch
400 (lambda (url . rest)
401 (match url
402 ("https://pypi.org/pypi/foo-99/json"
403 (values (open-input-string test-json-2)
404 (string-length test-json-2)))
405 ("https://example.com/foo-99-1.0.0-py2.py3-none-any.whl" #f)
406 (_ (error "Unexpected URL: " url)))))
407 (match (pypi->guix-package "foo-99")
408 (('package
409 ('name "python-foo-99")
410 ('version "1.0.0")
411 ('source ('origin
412 ('method 'url-fetch)
413 ('uri ('pypi-uri "foo-99" 'version))
414 ('sha256
415 ('base32
416 (? string? hash)))))
417 ('properties ('quote (("upstream-name" . "foo-99"))))
418 ('build-system 'python-build-system)
bcff9d63
LC
419 ('propagated-inputs ('list 'python-bar 'python-foo))
420 ('native-inputs ('list 'python-pytest))
7b75f90c
XC
421 ('home-page "http://example.com")
422 ('synopsis "summary")
423 ('description "summary")
424 ('license 'license:lgpl2.0))
425 (string=? (bytevector->nix-base32-string
426 test-source-hash)
427 hash))
428 (x
429 (pk 'fail x #f))))))
430
1b3e9685 431(test-end "pypi")