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