gnu: Update minimap2 to 2.17.
[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>
1b3e9685
DT
5;;;
6;;; This file is part of GNU Guix.
7;;;
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.
12;;;
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.
17;;;
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/>.
20
21(define-module (test-pypi)
22 #:use-module (guix import pypi)
23 #:use-module (guix base32)
c799ad72 24 #:use-module (guix memoization)
ca719424 25 #:use-module (gcrypt hash)
f0190a5d 26 #:use-module (guix memoization)
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
1b3e9685
DT
33(define test-json
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\",
49 \"packagetype\": \"bdist_egg\",
50 }, {
51 \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
52 \"packagetype\": \"sdist\",
266785d2
CR
53 }, {
54 \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
55 \"packagetype\": \"bdist_wheel\",
1b3e9685
DT
56 }
57 ]
58 }
59}")
60
ff986890
CR
61(define test-source-hash
62 "")
63
803fb336
MC
64(define test-specifications
65 '("Fizzy [foo, bar]"
66 "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
67 "SomethingWithMarker[foo]>1.0;python_version<\"2.7\""
68 "requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < \"2.7\""
69 "pip @ https://github.com/pypa/pip/archive/1.3.1.zip#\
70sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
71
01589acc
MC
72(define test-requires.txt "\
73# A comment
ff986890 74 # A comment after a space
c4797121
MC
75foo ~= 3
76bar != 2
77
78[test]
79pytest (>=2.5.0)
80")
81
d514276b
MC
82;; Beaker contains only optional dependencies.
83(define test-requires.txt-beaker "\
84[crypto]
85pycryptopp>=0.5.12
86
87[cryptography]
88cryptography
89
90[testsuite]
91Mock
92coverage
93")
94
f0190a5d
MC
95(define test-metadata "\
96Classifier: Programming Language :: Python :: 3.7
97Requires-Dist: baz ~= 3
98Requires-Dist: bar != 2
99Provides-Extra: test
d514276b 100Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
f0190a5d
MC
101")
102
103(define test-metadata-with-extras "
104Classifier: Programming Language :: Python :: 3.7
105Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
106Requires-Dist: wrapt (<2,>=1)
107Requires-Dist: bar
108
109Provides-Extra: dev
110Requires-Dist: tox ; extra == 'dev'
111Requires-Dist: bumpversion (<1) ; extra == 'dev'
112")
113
114;;; Provides-Extra can appear before Requires-Dist.
115(define test-metadata-with-extras-jedi "\
116Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
117Provides-Extra: testing
118Requires-Dist: parso (>=0.3.0)
119Provides-Extra: testing
120Requires-Dist: pytest (>=3.1.0); extra == 'testing'
121")
266785d2 122
1b3e9685
DT
123(test-begin "pypi")
124
8173ceee
LC
125(test-equal "guix-package->pypi-name, old URL style"
126 "psutil"
127 (guix-package->pypi-name
128 (dummy-package "foo"
129 (source (dummy-origin
130 (uri
7277d06d 131 "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
8173ceee
LC
132
133(test-equal "guix-package->pypi-name, new URL style"
134 "certbot"
135 (guix-package->pypi-name
136 (dummy-package "foo"
137 (source (dummy-origin
138 (uri
8440db45 139 "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
8173ceee 140
4eaac4b7
LC
141(test-equal "guix-package->pypi-name, several URLs"
142 "cram"
143 (guix-package->pypi-name
144 (dummy-package "foo"
145 (source
146 (dummy-origin
147 (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
148 (pypi-uri "cram" "0.7"))))))))
149
803fb336
MC
150(test-equal "specification->requirement-name"
151 '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
152 (map specification->requirement-name test-specifications))
153
d514276b
MC
154(test-equal "parse-requires.txt"
155 (list '("foo" "bar") '("pytest"))
c4797121
MC
156 (mock ((ice-9 ports) call-with-input-file
157 call-with-input-string)
d514276b
MC
158 (parse-requires.txt test-requires.txt)))
159
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)))
c4797121 165
f0190a5d 166(test-equal "parse-wheel-metadata, with extras"
d514276b 167 (list '("wrapt" "bar") '("tox" "bumpversion"))
f0190a5d
MC
168 (mock ((ice-9 ports) call-with-input-file
169 call-with-input-string)
170 (parse-wheel-metadata test-metadata-with-extras)))
171
172(test-equal "parse-wheel-metadata, with extras - Jedi"
d514276b 173 (list '("parso") '("pytest"))
f0190a5d
MC
174 (mock ((ice-9 ports) call-with-input-file
175 call-with-input-string)
176 (parse-wheel-metadata test-metadata-with-extras-jedi)))
177
d514276b 178(test-assert "pypi->guix-package, no wheel"
1b3e9685 179 ;; Replace network resources with sample data.
506abddb
RW
180 (mock ((guix import utils) url-fetch
181 (lambda (url file-name)
182 (match url
183 ("https://example.com/foo-1.0.0.tar.gz"
184 (begin
c799ad72
MC
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"
506abddb 188 (lambda ()
01589acc 189 (display test-requires.txt)))
a853aceb
MC
190 (parameterize ((current-output-port (%make-void-port "rw+")))
191 (system* "tar" "czvf" file-name "foo-1.0.0/"))
506abddb
RW
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
ce8963c5 198 (lambda (url . rest)
506abddb 199 (match url
8440db45 200 ("https://pypi.org/pypi/foo/json"
506abddb
RW
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")
206 (('package
207 ('name "python-foo")
208 ('version "1.0.0")
209 ('source ('origin
210 ('method 'url-fetch)
b3d8153d 211 ('uri ('pypi-uri "foo" 'version))
506abddb
RW
212 ('sha256
213 ('base32
214 (? string? hash)))))
215 ('build-system 'python-build-system)
216 ('propagated-inputs
217 ('quasiquote
218 (("python-bar" ('unquote 'python-bar))
d514276b
MC
219 ("python-foo" ('unquote 'python-foo)))))
220 ('native-inputs
221 ('quasiquote
222 (("python-pytest" ('unquote 'python-pytest)))))
506abddb
RW
223 ('home-page "http://example.com")
224 ('synopsis "summary")
225 ('description "summary")
226 ('license 'license:lgpl2.0))
227 (string=? (bytevector->nix-base32-string
228 test-source-hash)
229 hash))
230 (x
231 (pk 'fail x #f))))))
266785d2
CR
232
233(test-skip (if (which "zip") 0 1))
234(test-assert "pypi->guix-package, wheels"
235 ;; Replace network resources with sample data.
236 (mock ((guix import utils) url-fetch
237 (lambda (url file-name)
238 (match url
266785d2 239 ("https://example.com/foo-1.0.0.tar.gz"
01589acc
MC
240 (begin
241 (mkdir-p "foo-1.0.0/foo.egg-info/")
242 (with-output-to-file "foo-1.0.0/foo.egg-info/requires.txt"
d514276b
MC
243 (lambda ()
244 (display "wrong data to make sure we're testing wheels ")))
a853aceb
MC
245 (parameterize ((current-output-port (%make-void-port "rw+")))
246 (system* "tar" "czvf" file-name "foo-1.0.0/"))
d514276b
MC
247 (delete-file-recursively "foo-1.0.0")
248 (set! test-source-hash
249 (call-with-input-file file-name port-sha256))))
266785d2 250 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
d514276b
MC
251 (begin
252 (mkdir "foo-1.0.0.dist-info")
253 (with-output-to-file "foo-1.0.0.dist-info/METADATA"
254 (lambda ()
255 (display test-metadata)))
256 (let ((zip-file (string-append file-name ".zip")))
257 ;; zip always adds a "zip" extension to the file it creates,
258 ;; so we need to rename it.
259 (system* "zip" "-q" zip-file "foo-1.0.0.dist-info/METADATA")
260 (rename-file zip-file file-name))
261 (delete-file-recursively "foo-1.0.0.dist-info")))
ff986890 262 (_ (error "Unexpected URL: " url)))))
239f4632 263 (mock ((guix http-client) http-fetch
ce8963c5 264 (lambda (url . rest)
239f4632 265 (match url
8440db45 266 ("https://pypi.org/pypi/foo/json"
239f4632
RW
267 (values (open-input-string test-json)
268 (string-length test-json)))
269 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
270 (_ (error "Unexpected URL: " url)))))
f0190a5d
MC
271 ;; Not clearing the memoization cache here would mean returning the value
272 ;; computed in the previous test.
273 (invalidate-memoization! pypi->guix-package)
239f4632
RW
274 (match (pypi->guix-package "foo")
275 (('package
276 ('name "python-foo")
277 ('version "1.0.0")
278 ('source ('origin
279 ('method 'url-fetch)
b3d8153d 280 ('uri ('pypi-uri "foo" 'version))
239f4632
RW
281 ('sha256
282 ('base32
283 (? string? hash)))))
284 ('build-system 'python-build-system)
285 ('propagated-inputs
286 ('quasiquote
287 (("python-bar" ('unquote 'python-bar))
b45dbfc9 288 ("python-baz" ('unquote 'python-baz)))))
d514276b
MC
289 ('native-inputs
290 ('quasiquote
291 (("python-pytest" ('unquote 'python-pytest)))))
239f4632
RW
292 ('home-page "http://example.com")
293 ('synopsis "summary")
294 ('description "summary")
295 ('license 'license:lgpl2.0))
296 (string=? (bytevector->nix-base32-string
297 test-source-hash)
298 hash))
299 (x
300 (pk 'fail x #f))))))
1b3e9685 301
c799ad72
MC
302(test-assert "pypi->guix-package, no usable requirement file."
303 ;; Replace network resources with sample data.
304 (mock ((guix import utils) url-fetch
305 (lambda (url file-name)
306 (match url
307 ("https://example.com/foo-1.0.0.tar.gz"
308 (mkdir-p "foo-1.0.0/foo.egg-info/")
309 (parameterize ((current-output-port (%make-void-port "rw+")))
310 (system* "tar" "czvf" file-name "foo-1.0.0/"))
311 (delete-file-recursively "foo-1.0.0")
312 (set! test-source-hash
313 (call-with-input-file file-name port-sha256)))
314 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
315 (_ (error "Unexpected URL: " url)))))
316 (mock ((guix http-client) http-fetch
317 (lambda (url . rest)
318 (match url
319 ("https://pypi.org/pypi/foo/json"
320 (values (open-input-string test-json)
321 (string-length test-json)))
322 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
323 (_ (error "Unexpected URL: " url)))))
324 ;; Not clearing the memoization cache here would mean returning the value
325 ;; computed in the previous test.
326 (invalidate-memoization! pypi->guix-package)
327 (match (pypi->guix-package "foo")
328 (('package
329 ('name "python-foo")
330 ('version "1.0.0")
331 ('source ('origin
332 ('method 'url-fetch)
333 ('uri ('pypi-uri "foo" 'version))
334 ('sha256
335 ('base32
336 (? string? hash)))))
337 ('build-system 'python-build-system)
338 ('home-page "http://example.com")
339 ('synopsis "summary")
340 ('description "summary")
341 ('license 'license:lgpl2.0))
342 (string=? (bytevector->nix-base32-string
343 test-source-hash)
344 hash))
345 (x
346 (pk 'fail x #f))))))
347
1b3e9685 348(test-end "pypi")