Commit | Line | Data |
---|---|---|
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 | |
60 | bar | |
61 | baz > 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)) | |
133 | ("python-baz" ('unquote 'python-baz)) | |
134 | ("python-setuptools" ('unquote 'python-setuptools))))) | |
135 | ('home-page "http://example.com") | |
136 | ('synopsis "summary") | |
137 | ('description "summary") | |
138 | ('license 'license:lgpl2.0)) | |
139 | (string=? (bytevector->nix-base32-string | |
140 | test-source-hash) | |
141 | hash)) | |
142 | (x | |
143 | (pk 'fail x #f)))))) | |
266785d2 CR |
144 | |
145 | (test-skip (if (which "zip") 0 1)) | |
146 | (test-assert "pypi->guix-package, wheels" | |
147 | ;; Replace network resources with sample data. | |
148 | (mock ((guix import utils) url-fetch | |
149 | (lambda (url file-name) | |
150 | (match url | |
151 | ("https://pypi.python.org/pypi/foo/json" | |
152 | (with-output-to-file file-name | |
153 | (lambda () | |
154 | (display test-json)))) | |
155 | ("https://example.com/foo-1.0.0.tar.gz" | |
156 | (begin | |
157 | (mkdir "foo-1.0.0") | |
158 | (with-output-to-file "foo-1.0.0/requirements.txt" | |
159 | (lambda () | |
160 | (display test-requirements))) | |
161 | (system* "tar" "czvf" file-name "foo-1.0.0/") | |
162 | (delete-file-recursively "foo-1.0.0") | |
163 | (set! test-source-hash | |
164 | (call-with-input-file file-name port-sha256)))) | |
165 | ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" | |
166 | (begin | |
167 | (mkdir "foo-1.0.0.dist-info") | |
168 | (with-output-to-file "foo-1.0.0.dist-info/metadata.json" | |
169 | (lambda () | |
170 | (display test-metadata))) | |
171 | (let ((zip-file (string-append file-name ".zip"))) | |
172 | ;; zip always adds a "zip" extension to the file it creates, | |
173 | ;; so we need to rename it. | |
174 | (system* "zip" zip-file "foo-1.0.0.dist-info/metadata.json") | |
175 | (rename-file zip-file file-name)) | |
176 | (delete-file-recursively "foo-1.0.0.dist-info"))) | |
ff986890 | 177 | (_ (error "Unexpected URL: " url))))) |
1b3e9685 DT |
178 | (match (pypi->guix-package "foo") |
179 | (('package | |
180 | ('name "python-foo") | |
181 | ('version "1.0.0") | |
182 | ('source ('origin | |
183 | ('method 'url-fetch) | |
522773b7 LC |
184 | ('uri (string-append "https://example.com/foo-" |
185 | version ".tar.gz")) | |
1b3e9685 DT |
186 | ('sha256 |
187 | ('base32 | |
188 | (? string? hash))))) | |
189 | ('build-system 'python-build-system) | |
d6aca583 | 190 | ('propagated-inputs |
1b3e9685 | 191 | ('quasiquote |
ff986890 CR |
192 | (("python-bar" ('unquote 'python-bar)) |
193 | ("python-baz" ('unquote 'python-baz)) | |
194 | ("python-setuptools" ('unquote 'python-setuptools))))) | |
1b3e9685 DT |
195 | ('home-page "http://example.com") |
196 | ('synopsis "summary") | |
197 | ('description "summary") | |
a34b236c | 198 | ('license 'license:lgpl2.0)) |
1b3e9685 | 199 | (string=? (bytevector->nix-base32-string |
ff986890 | 200 | test-source-hash) |
1b3e9685 DT |
201 | hash)) |
202 | (x | |
203 | (pk 'fail x #f))))) | |
204 | ||
205 | (test-end "pypi") |