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