Merge remote-tracking branch 'origin/master' into core-updates
[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 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (test-pypi)
21 #:use-module (guix import pypi)
22 #:use-module (guix base32)
23 #:use-module (guix hash)
24 #:use-module (guix tests)
25 #:use-module (guix build-system python)
26 #:use-module ((guix build utils) #:select (delete-file-recursively which))
27 #:use-module (srfi srfi-64)
28 #:use-module (ice-9 match))
29
30 (define test-json
31 "{
32 \"info\": {
33 \"version\": \"1.0.0\",
34 \"name\": \"foo\",
35 \"license\": \"GNU LGPL\",
36 \"summary\": \"summary\",
37 \"home_page\": \"http://example.com\",
38 },
39 \"releases\": {
40 \"1.0.0\": [
41 {
42 \"url\": \"https://example.com/foo-1.0.0.egg\",
43 \"packagetype\": \"bdist_egg\",
44 }, {
45 \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
46 \"packagetype\": \"sdist\",
47 }, {
48 \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
49 \"packagetype\": \"bdist_wheel\",
50 }
51 ]
52 }
53 }")
54
55 (define test-source-hash
56 "")
57
58 (define test-requirements
59 "# A comment
60 # A comment after a space
61 bar
62 baz > 13.37")
63
64 (define test-metadata
65 "{
66 \"run_requires\": [
67 {
68 \"requires\": [
69 \"bar\",
70 \"baz (>13.37)\"
71 ]
72 }
73 ]
74 }")
75
76 (test-begin "pypi")
77
78 (test-equal "guix-package->pypi-name, old URL style"
79 "psutil"
80 (guix-package->pypi-name
81 (dummy-package "foo"
82 (source (dummy-origin
83 (uri
84 "https://pypi.io/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
85
86 (test-equal "guix-package->pypi-name, new URL style"
87 "certbot"
88 (guix-package->pypi-name
89 (dummy-package "foo"
90 (source (dummy-origin
91 (uri
92 "https://pypi.python.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
93
94 (test-equal "guix-package->pypi-name, several URLs"
95 "cram"
96 (guix-package->pypi-name
97 (dummy-package "foo"
98 (source
99 (dummy-origin
100 (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
101 (pypi-uri "cram" "0.7"))))))))
102
103 (test-assert "pypi->guix-package"
104 ;; Replace network resources with sample data.
105 (mock ((guix import utils) url-fetch
106 (lambda (url file-name)
107 (match url
108 ("https://example.com/foo-1.0.0.tar.gz"
109 (begin
110 (mkdir "foo-1.0.0")
111 (with-output-to-file "foo-1.0.0/requirements.txt"
112 (lambda ()
113 (display test-requirements)))
114 (system* "tar" "czvf" file-name "foo-1.0.0/")
115 (delete-file-recursively "foo-1.0.0")
116 (set! test-source-hash
117 (call-with-input-file file-name port-sha256))))
118 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
119 (_ (error "Unexpected URL: " url)))))
120 (mock ((guix http-client) http-fetch
121 (lambda (url . rest)
122 (match url
123 ("https://pypi.python.org/pypi/foo/json"
124 (values (open-input-string test-json)
125 (string-length test-json)))
126 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
127 (_ (error "Unexpected URL: " url)))))
128 (match (pypi->guix-package "foo")
129 (('package
130 ('name "python-foo")
131 ('version "1.0.0")
132 ('source ('origin
133 ('method 'url-fetch)
134 ('uri (string-append "https://example.com/foo-"
135 version ".tar.gz"))
136 ('sha256
137 ('base32
138 (? string? hash)))))
139 ('build-system 'python-build-system)
140 ('propagated-inputs
141 ('quasiquote
142 (("python-bar" ('unquote 'python-bar))
143 ("python-baz" ('unquote 'python-baz)))))
144 ('home-page "http://example.com")
145 ('synopsis "summary")
146 ('description "summary")
147 ('license 'license:lgpl2.0))
148 (string=? (bytevector->nix-base32-string
149 test-source-hash)
150 hash))
151 (x
152 (pk 'fail x #f))))))
153
154 (test-skip (if (which "zip") 0 1))
155 (test-assert "pypi->guix-package, wheels"
156 ;; Replace network resources with sample data.
157 (mock ((guix import utils) url-fetch
158 (lambda (url file-name)
159 (match url
160 ("https://example.com/foo-1.0.0.tar.gz"
161 (begin
162 (mkdir "foo-1.0.0")
163 (with-output-to-file "foo-1.0.0/requirements.txt"
164 (lambda ()
165 (display test-requirements)))
166 (system* "tar" "czvf" file-name "foo-1.0.0/")
167 (delete-file-recursively "foo-1.0.0")
168 (set! test-source-hash
169 (call-with-input-file file-name port-sha256))))
170 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
171 (begin
172 (mkdir "foo-1.0.0.dist-info")
173 (with-output-to-file "foo-1.0.0.dist-info/metadata.json"
174 (lambda ()
175 (display test-metadata)))
176 (let ((zip-file (string-append file-name ".zip")))
177 ;; zip always adds a "zip" extension to the file it creates,
178 ;; so we need to rename it.
179 (system* "zip" zip-file "foo-1.0.0.dist-info/metadata.json")
180 (rename-file zip-file file-name))
181 (delete-file-recursively "foo-1.0.0.dist-info")))
182 (_ (error "Unexpected URL: " url)))))
183 (mock ((guix http-client) http-fetch
184 (lambda (url . rest)
185 (match url
186 ("https://pypi.python.org/pypi/foo/json"
187 (values (open-input-string test-json)
188 (string-length test-json)))
189 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
190 (_ (error "Unexpected URL: " url)))))
191 (match (pypi->guix-package "foo")
192 (('package
193 ('name "python-foo")
194 ('version "1.0.0")
195 ('source ('origin
196 ('method 'url-fetch)
197 ('uri (string-append "https://example.com/foo-"
198 version ".tar.gz"))
199 ('sha256
200 ('base32
201 (? string? hash)))))
202 ('build-system 'python-build-system)
203 ('propagated-inputs
204 ('quasiquote
205 (("python-bar" ('unquote 'python-bar))
206 ("python-baz" ('unquote 'python-baz)))))
207 ('home-page "http://example.com")
208 ('synopsis "summary")
209 ('description "summary")
210 ('license 'license:lgpl2.0))
211 (string=? (bytevector->nix-base32-string
212 test-source-hash)
213 hash))
214 (x
215 (pk 'fail x #f))))))
216
217 (test-end "pypi")