services: shepherd: Replace spaces with hyphens in file names.
[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>
1b3e9685
DT
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)
694b317c 24 #:use-module (guix tests)
266785d2 25 #:use-module ((guix build utils) #:select (delete-file-recursively which))
1b3e9685
DT
26 #:use-module (srfi srfi-64)
27 #:use-module (ice-9 match))
28
1b3e9685
DT
29(define test-json
30 "{
31 \"info\": {
32 \"version\": \"1.0.0\",
33 \"name\": \"foo\",
34 \"license\": \"GNU LGPL\",
35 \"summary\": \"summary\",
36 \"home_page\": \"http://example.com\",
37 },
38 \"releases\": {
39 \"1.0.0\": [
40 {
41 \"url\": \"https://example.com/foo-1.0.0.egg\",
42 \"packagetype\": \"bdist_egg\",
43 }, {
44 \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
45 \"packagetype\": \"sdist\",
266785d2
CR
46 }, {
47 \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
48 \"packagetype\": \"bdist_wheel\",
1b3e9685
DT
49 }
50 ]
51 }
52}")
53
ff986890
CR
54(define test-source-hash
55 "")
56
57(define test-requirements
58"# A comment
59 # A comment after a space
60bar
61baz > 13.37")
1b3e9685 62
266785d2
CR
63(define test-metadata
64 "{
65 \"run_requires\": [
66 {
67 \"requires\": [
68 \"bar\",
69 \"baz (>13.37)\"
70 ]
71 }
72 ]
73}")
74
1b3e9685
DT
75(test-begin "pypi")
76
8173ceee
LC
77(test-equal "guix-package->pypi-name, old URL style"
78 "psutil"
79 (guix-package->pypi-name
80 (dummy-package "foo"
81 (source (dummy-origin
82 (uri
83 "https://pypi.io/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
84
85(test-equal "guix-package->pypi-name, new URL style"
86 "certbot"
87 (guix-package->pypi-name
88 (dummy-package "foo"
89 (source (dummy-origin
90 (uri
91 "https://pypi.python.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
92
1b3e9685
DT
93(test-assert "pypi->guix-package"
94 ;; Replace network resources with sample data.
506abddb
RW
95 (mock ((guix import utils) url-fetch
96 (lambda (url file-name)
97 (match url
98 ("https://example.com/foo-1.0.0.tar.gz"
99 (begin
100 (mkdir "foo-1.0.0")
101 (with-output-to-file "foo-1.0.0/requirements.txt"
102 (lambda ()
103 (display test-requirements)))
104 (system* "tar" "czvf" file-name "foo-1.0.0/")
105 (delete-file-recursively "foo-1.0.0")
106 (set! test-source-hash
107 (call-with-input-file file-name port-sha256))))
108 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
109 (_ (error "Unexpected URL: " url)))))
110 (mock ((guix http-client) http-fetch
111 (lambda (url)
112 (match url
113 ("https://pypi.python.org/pypi/foo/json"
114 (values (open-input-string test-json)
115 (string-length test-json)))
116 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
117 (_ (error "Unexpected URL: " url)))))
118 (match (pypi->guix-package "foo")
119 (('package
120 ('name "python-foo")
121 ('version "1.0.0")
122 ('source ('origin
123 ('method 'url-fetch)
124 ('uri (string-append "https://example.com/foo-"
125 version ".tar.gz"))
126 ('sha256
127 ('base32
128 (? string? hash)))))
129 ('build-system 'python-build-system)
130 ('propagated-inputs
131 ('quasiquote
132 (("python-bar" ('unquote 'python-bar))
b45dbfc9 133 ("python-baz" ('unquote 'python-baz)))))
506abddb
RW
134 ('home-page "http://example.com")
135 ('synopsis "summary")
136 ('description "summary")
137 ('license 'license:lgpl2.0))
138 (string=? (bytevector->nix-base32-string
139 test-source-hash)
140 hash))
141 (x
142 (pk 'fail x #f))))))
266785d2
CR
143
144(test-skip (if (which "zip") 0 1))
145(test-assert "pypi->guix-package, wheels"
146 ;; Replace network resources with sample data.
147 (mock ((guix import utils) url-fetch
148 (lambda (url file-name)
149 (match url
266785d2
CR
150 ("https://example.com/foo-1.0.0.tar.gz"
151 (begin
152 (mkdir "foo-1.0.0")
153 (with-output-to-file "foo-1.0.0/requirements.txt"
154 (lambda ()
155 (display test-requirements)))
156 (system* "tar" "czvf" file-name "foo-1.0.0/")
157 (delete-file-recursively "foo-1.0.0")
158 (set! test-source-hash
159 (call-with-input-file file-name port-sha256))))
160 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
161 (begin
162 (mkdir "foo-1.0.0.dist-info")
163 (with-output-to-file "foo-1.0.0.dist-info/metadata.json"
164 (lambda ()
165 (display test-metadata)))
166 (let ((zip-file (string-append file-name ".zip")))
167 ;; zip always adds a "zip" extension to the file it creates,
168 ;; so we need to rename it.
169 (system* "zip" zip-file "foo-1.0.0.dist-info/metadata.json")
170 (rename-file zip-file file-name))
171 (delete-file-recursively "foo-1.0.0.dist-info")))
ff986890 172 (_ (error "Unexpected URL: " url)))))
239f4632
RW
173 (mock ((guix http-client) http-fetch
174 (lambda (url)
175 (match url
176 ("https://pypi.python.org/pypi/foo/json"
177 (values (open-input-string test-json)
178 (string-length test-json)))
179 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
180 (_ (error "Unexpected URL: " url)))))
181 (match (pypi->guix-package "foo")
182 (('package
183 ('name "python-foo")
184 ('version "1.0.0")
185 ('source ('origin
186 ('method 'url-fetch)
187 ('uri (string-append "https://example.com/foo-"
188 version ".tar.gz"))
189 ('sha256
190 ('base32
191 (? string? hash)))))
192 ('build-system 'python-build-system)
193 ('propagated-inputs
194 ('quasiquote
195 (("python-bar" ('unquote 'python-bar))
b45dbfc9 196 ("python-baz" ('unquote 'python-baz)))))
239f4632
RW
197 ('home-page "http://example.com")
198 ('synopsis "summary")
199 ('description "summary")
200 ('license 'license:lgpl2.0))
201 (string=? (bytevector->nix-base32-string
202 test-source-hash)
203 hash))
204 (x
205 (pk 'fail x #f))))))
1b3e9685
DT
206
207(test-end "pypi")