Commit | Line | Data |
---|---|---|
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\", | |
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\", | |
266785d2 CR |
50 | }, { |
51 | \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\", | |
52 | \"packagetype\": \"bdist_wheel\", | |
1b3e9685 DT |
53 | } |
54 | ] | |
55 | } | |
56 | }") | |
57 | ||
ff986890 CR |
58 | (define test-source-hash |
59 | "") | |
60 | ||
803fb336 MC |
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 | ||
01589acc MC |
69 | (define test-requires.txt "\ |
70 | # A comment | |
ff986890 | 71 | # A comment after a space |
c4797121 MC |
72 | foo ~= 3 |
73 | bar != 2 | |
74 | ||
75 | [test] | |
76 | pytest (>=2.5.0) | |
77 | ") | |
78 | ||
d514276b MC |
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 | ||
f0190a5d MC |
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 | |
d514276b | 97 | Requires-Dist: pytest (>=2.5.0) ; extra == 'test' |
f0190a5d MC |
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 | ") | |
266785d2 | 119 | |
1b3e9685 DT |
120 | (test-begin "pypi") |
121 | ||
8173ceee LC |
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 | |
7277d06d | 128 | "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz")))))) |
8173ceee LC |
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 | |
8440db45 | 136 | "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz")))))) |
8173ceee | 137 | |
4eaac4b7 LC |
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 | ||
803fb336 MC |
147 | (test-equal "specification->requirement-name" |
148 | '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip") | |
149 | (map specification->requirement-name test-specifications)) | |
150 | ||
d514276b MC |
151 | (test-equal "parse-requires.txt" |
152 | (list '("foo" "bar") '("pytest")) | |
c4797121 MC |
153 | (mock ((ice-9 ports) call-with-input-file |
154 | call-with-input-string) | |
d514276b MC |
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))) | |
c4797121 | 162 | |
f0190a5d | 163 | (test-equal "parse-wheel-metadata, with extras" |
d514276b | 164 | (list '("wrapt" "bar") '("tox" "bumpversion")) |
f0190a5d MC |
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" | |
d514276b | 170 | (list '("parso") '("pytest")) |
f0190a5d MC |
171 | (mock ((ice-9 ports) call-with-input-file |
172 | call-with-input-string) | |
173 | (parse-wheel-metadata test-metadata-with-extras-jedi))) | |
174 | ||
d514276b | 175 | (test-assert "pypi->guix-package, no wheel" |
1b3e9685 | 176 | ;; Replace network resources with sample data. |
506abddb RW |
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 | |
c799ad72 MC |
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" | |
506abddb | 185 | (lambda () |
01589acc | 186 | (display test-requires.txt))) |
a853aceb MC |
187 | (parameterize ((current-output-port (%make-void-port "rw+"))) |
188 | (system* "tar" "czvf" file-name "foo-1.0.0/")) | |
506abddb RW |
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 | |
ce8963c5 | 195 | (lambda (url . rest) |
506abddb | 196 | (match url |
8440db45 | 197 | ("https://pypi.org/pypi/foo/json" |
506abddb RW |
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) | |
b3d8153d | 208 | ('uri ('pypi-uri "foo" 'version)) |
506abddb RW |
209 | ('sha256 |
210 | ('base32 | |
211 | (? string? hash))))) | |
212 | ('build-system 'python-build-system) | |
213 | ('propagated-inputs | |
214 | ('quasiquote | |
215 | (("python-bar" ('unquote 'python-bar)) | |
d514276b MC |
216 | ("python-foo" ('unquote 'python-foo))))) |
217 | ('native-inputs | |
218 | ('quasiquote | |
219 | (("python-pytest" ('unquote 'python-pytest))))) | |
506abddb RW |
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)))))) | |
266785d2 CR |
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 | |
266785d2 | 236 | ("https://example.com/foo-1.0.0.tar.gz" |
01589acc MC |
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" | |
d514276b MC |
240 | (lambda () |
241 | (display "wrong data to make sure we're testing wheels "))) | |
a853aceb MC |
242 | (parameterize ((current-output-port (%make-void-port "rw+"))) |
243 | (system* "tar" "czvf" file-name "foo-1.0.0/")) | |
d514276b MC |
244 | (delete-file-recursively "foo-1.0.0") |
245 | (set! test-source-hash | |
246 | (call-with-input-file file-name port-sha256)))) | |
266785d2 | 247 | ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" |
d514276b MC |
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"))) | |
ff986890 | 259 | (_ (error "Unexpected URL: " url))))) |
239f4632 | 260 | (mock ((guix http-client) http-fetch |
ce8963c5 | 261 | (lambda (url . rest) |
239f4632 | 262 | (match url |
8440db45 | 263 | ("https://pypi.org/pypi/foo/json" |
239f4632 RW |
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))))) | |
f0190a5d MC |
268 | ;; Not clearing the memoization cache here would mean returning the value |
269 | ;; computed in the previous test. | |
270 | (invalidate-memoization! pypi->guix-package) | |
239f4632 RW |
271 | (match (pypi->guix-package "foo") |
272 | (('package | |
273 | ('name "python-foo") | |
274 | ('version "1.0.0") | |
275 | ('source ('origin | |
276 | ('method 'url-fetch) | |
b3d8153d | 277 | ('uri ('pypi-uri "foo" 'version)) |
239f4632 RW |
278 | ('sha256 |
279 | ('base32 | |
280 | (? string? hash))))) | |
281 | ('build-system 'python-build-system) | |
282 | ('propagated-inputs | |
283 | ('quasiquote | |
284 | (("python-bar" ('unquote 'python-bar)) | |
b45dbfc9 | 285 | ("python-baz" ('unquote 'python-baz))))) |
d514276b MC |
286 | ('native-inputs |
287 | ('quasiquote | |
288 | (("python-pytest" ('unquote 'python-pytest))))) | |
239f4632 RW |
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)))))) | |
1b3e9685 | 298 | |
c799ad72 MC |
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 | ||
1b3e9685 | 345 | (test-end "pypi") |