import: pypi: read requirements from wheels.
[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-assert "pypi->guix-package"
77 ;; Replace network resources with sample data.
78 (mock ((guix import utils) url-fetch
79 (lambda (url file-name)
80 (match url
81 ("https://pypi.python.org/pypi/foo/json"
82 (with-output-to-file file-name
83 (lambda ()
84 (display test-json))))
85 ("https://example.com/foo-1.0.0.tar.gz"
86 (begin
87 (mkdir "foo-1.0.0")
88 (with-output-to-file "foo-1.0.0/requirements.txt"
89 (lambda ()
90 (display test-requirements)))
91 (system* "tar" "czvf" file-name "foo-1.0.0/")
92 (delete-file-recursively "foo-1.0.0")
93 (set! test-source-hash
94 (call-with-input-file file-name port-sha256))))
95 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
96 (_ (error "Unexpected URL: " url)))))
97 (match (pypi->guix-package "foo")
98 (('package
99 ('name "python-foo")
100 ('version "1.0.0")
101 ('source ('origin
102 ('method 'url-fetch)
103 ('uri (string-append "https://example.com/foo-"
104 version ".tar.gz"))
105 ('sha256
106 ('base32
107 (? string? hash)))))
108 ('build-system 'python-build-system)
109 ('inputs
110 ('quasiquote
111 (("python-bar" ('unquote 'python-bar))
112 ("python-baz" ('unquote 'python-baz))
113 ("python-setuptools" ('unquote 'python-setuptools)))))
114 ('home-page "http://example.com")
115 ('synopsis "summary")
116 ('description "summary")
117 ('license 'lgpl2.0))
118 (string=? (bytevector->nix-base32-string
119 test-source-hash)
120 hash))
121 (x
122 (pk 'fail x #f)))))
123
124 (test-skip (if (which "zip") 0 1))
125 (test-assert "pypi->guix-package, wheels"
126 ;; Replace network resources with sample data.
127 (mock ((guix import utils) url-fetch
128 (lambda (url file-name)
129 (match url
130 ("https://pypi.python.org/pypi/foo/json"
131 (with-output-to-file file-name
132 (lambda ()
133 (display test-json))))
134 ("https://example.com/foo-1.0.0.tar.gz"
135 (begin
136 (mkdir "foo-1.0.0")
137 (with-output-to-file "foo-1.0.0/requirements.txt"
138 (lambda ()
139 (display test-requirements)))
140 (system* "tar" "czvf" file-name "foo-1.0.0/")
141 (delete-file-recursively "foo-1.0.0")
142 (set! test-source-hash
143 (call-with-input-file file-name port-sha256))))
144 ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
145 (begin
146 (mkdir "foo-1.0.0.dist-info")
147 (with-output-to-file "foo-1.0.0.dist-info/metadata.json"
148 (lambda ()
149 (display test-metadata)))
150 (let ((zip-file (string-append file-name ".zip")))
151 ;; zip always adds a "zip" extension to the file it creates,
152 ;; so we need to rename it.
153 (system* "zip" zip-file "foo-1.0.0.dist-info/metadata.json")
154 (rename-file zip-file file-name))
155 (delete-file-recursively "foo-1.0.0.dist-info")))
156 (_ (error "Unexpected URL: " url)))))
157 (match (pypi->guix-package "foo")
158 (('package
159 ('name "python-foo")
160 ('version "1.0.0")
161 ('source ('origin
162 ('method 'url-fetch)
163 ('uri (string-append "https://example.com/foo-"
164 version ".tar.gz"))
165 ('sha256
166 ('base32
167 (? string? hash)))))
168 ('build-system 'python-build-system)
169 ('inputs
170 ('quasiquote
171 (("python-bar" ('unquote 'python-bar))
172 ("python-baz" ('unquote 'python-baz))
173 ("python-setuptools" ('unquote 'python-setuptools)))))
174 ('home-page "http://example.com")
175 ('synopsis "summary")
176 ('description "summary")
177 ('license 'lgpl2.0))
178 (string=? (bytevector->nix-base32-string
179 test-source-hash)
180 hash))
181 (x
182 (pk 'fail x #f)))))
183
184 (test-end "pypi")