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