Merge branch 'master' into staging
[jackhill/guix/guix.git] / tests / crate.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2016 David Craven <david@craven.ch>
4 ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (test-crate)
22 #:use-module (guix import crate)
23 #:use-module (guix base32)
24 #:use-module (guix build-system cargo)
25 #:use-module (gcrypt hash)
26 #:use-module (guix tests)
27 #:use-module (ice-9 iconv)
28 #:use-module (ice-9 match)
29 #:use-module (srfi srfi-64))
30
31 (define test-foo-crate
32 "{
33 \"crate\": {
34 \"max_version\": \"1.0.0\",
35 \"name\": \"foo\",
36 \"description\": \"summary\",
37 \"homepage\": \"http://example.com\",
38 \"repository\": \"http://example.com\",
39 \"keywords\": [\"dummy\" \"test\"],
40 \"categories\": [\"test\"]
41 \"actual_versions\": [
42 { \"id\": \"foo\",
43 \"num\": \"1.0.0\",
44 \"license\": \"MIT OR Apache-2.0\",
45 \"links\": {
46 \"dependencies\": \"/api/v1/crates/foo/1.0.0/dependencies\"
47 }
48 }
49 ]
50 }
51 }")
52
53 (define test-foo-dependencies
54 "{
55 \"dependencies\": [
56 {
57 \"crate_id\": \"bar\",
58 \"kind\": \"normal\",
59 }
60 ]
61 }")
62
63 (define test-root-crate
64 "{
65 \"crate\": {
66 \"max_version\": \"1.0.0\",
67 \"name\": \"root\",
68 \"description\": \"summary\",
69 \"homepage\": \"http://example.com\",
70 \"repository\": \"http://example.com\",
71 \"keywords\": [\"dummy\" \"test\"],
72 \"categories\": [\"test\"]
73 \"actual_versions\": [
74 { \"id\": \"foo\",
75 \"num\": \"1.0.0\",
76 \"license\": \"MIT OR Apache-2.0\",
77 \"links\": {
78 \"dependencies\": \"/api/v1/crates/root/1.0.0/dependencies\"
79 }
80 }
81 ]
82 }
83 }")
84
85 (define test-root-dependencies
86 "{
87 \"dependencies\": [
88 {
89 \"crate_id\": \"intermediate-1\",
90 \"kind\": \"normal\",
91 },
92 {
93 \"crate_id\": \"intermediate-2\",
94 \"kind\": \"normal\",
95 }
96 {
97 \"crate_id\": \"leaf-alice\",
98 \"kind\": \"normal\",
99 },
100 {
101 \"crate_id\": \"leaf-bob\",
102 \"kind\": \"normal\",
103 },
104 ]
105 }")
106
107 (define test-intermediate-1-crate
108 "{
109 \"crate\": {
110 \"max_version\": \"1.0.0\",
111 \"name\": \"intermediate-1\",
112 \"description\": \"summary\",
113 \"homepage\": \"http://example.com\",
114 \"repository\": \"http://example.com\",
115 \"keywords\": [\"dummy\" \"test\"],
116 \"categories\": [\"test\"]
117 \"actual_versions\": [
118 { \"id\": \"intermediate-1\",
119 \"num\": \"1.0.0\",
120 \"license\": \"MIT OR Apache-2.0\",
121 \"links\": {
122 \"dependencies\": \"/api/v1/crates/intermediate-1/1.0.0/dependencies\"
123 }
124 }
125 ]
126 }
127 }")
128
129 (define test-intermediate-1-dependencies
130 "{
131 \"dependencies\": [
132 {
133 \"crate_id\": \"intermediate-2\",
134 \"kind\": \"normal\",
135 },
136 {
137 \"crate_id\": \"leaf-alice\",
138 \"kind\": \"normal\",
139 },
140 {
141 \"crate_id\": \"leaf-bob\",
142 \"kind\": \"normal\",
143 }
144 ]
145 }")
146
147 (define test-intermediate-2-crate
148 "{
149 \"crate\": {
150 \"max_version\": \"1.0.0\",
151 \"name\": \"intermediate-2\",
152 \"description\": \"summary\",
153 \"homepage\": \"http://example.com\",
154 \"repository\": \"http://example.com\",
155 \"keywords\": [\"dummy\" \"test\"],
156 \"categories\": [\"test\"]
157 \"actual_versions\": [
158 { \"id\": \"intermediate-2\",
159 \"num\": \"1.0.0\",
160 \"license\": \"MIT OR Apache-2.0\",
161 \"links\": {
162 \"dependencies\": \"/api/v1/crates/intermediate-2/1.0.0/dependencies\"
163 }
164 }
165 ]
166 }
167 }")
168
169 (define test-intermediate-2-dependencies
170 "{
171 \"dependencies\": [
172 {
173 \"crate_id\": \"leaf-bob\",
174 \"kind\": \"normal\",
175 },
176 ]
177 }")
178
179 (define test-leaf-alice-crate
180 "{
181 \"crate\": {
182 \"max_version\": \"1.0.0\",
183 \"name\": \"leaf-alice\",
184 \"description\": \"summary\",
185 \"homepage\": \"http://example.com\",
186 \"repository\": \"http://example.com\",
187 \"keywords\": [\"dummy\" \"test\"],
188 \"categories\": [\"test\"]
189 \"actual_versions\": [
190 { \"id\": \"leaf-alice\",
191 \"num\": \"1.0.0\",
192 \"license\": \"MIT OR Apache-2.0\",
193 \"links\": {
194 \"dependencies\": \"/api/v1/crates/leaf-alice/1.0.0/dependencies\"
195 }
196 }
197 ]
198 }
199 }")
200
201 (define test-leaf-alice-dependencies
202 "{
203 \"dependencies\": []
204 }")
205
206 (define test-leaf-bob-crate
207 "{
208 \"crate\": {
209 \"max_version\": \"1.0.0\",
210 \"name\": \"leaf-bob\",
211 \"description\": \"summary\",
212 \"homepage\": \"http://example.com\",
213 \"repository\": \"http://example.com\",
214 \"keywords\": [\"dummy\" \"test\"],
215 \"categories\": [\"test\"]
216 \"actual_versions\": [
217 { \"id\": \"leaf-bob\",
218 \"num\": \"1.0.0\",
219 \"license\": \"MIT OR Apache-2.0\",
220 \"links\": {
221 \"dependencies\": \"/api/v1/crates/leaf-bob/1.0.0/dependencies\"
222 }
223 }
224 ]
225 }
226 }")
227
228 (define test-leaf-bob-dependencies
229 "{
230 \"dependencies\": []
231 }")
232
233 (define test-source-hash
234 "")
235
236 (define string->license
237 (@@ (guix import crate) string->license))
238
239 (test-begin "crate")
240
241 (test-equal "guix-package->crate-name"
242 "rustc-serialize"
243 (guix-package->crate-name
244 (dummy-package
245 "rust-rustc-serialize"
246 (source (dummy-origin
247 (uri (crate-uri "rustc-serialize" "1.0")))))))
248
249 (test-assert "crate->guix-package"
250 ;; Replace network resources with sample data.
251 (mock ((guix http-client) http-fetch
252 (lambda (url . rest)
253 (match url
254 ("https://crates.io/api/v1/crates/foo"
255 (open-input-string test-foo-crate))
256 ("https://crates.io/api/v1/crates/foo/1.0.0/download"
257 (set! test-source-hash
258 (bytevector->nix-base32-string
259 (sha256 (string->bytevector "empty file\n" "utf-8"))))
260 (open-input-string "empty file\n"))
261 ("https://crates.io/api/v1/crates/foo/1.0.0/dependencies"
262 (open-input-string test-foo-dependencies))
263 (_ (error "Unexpected URL: " url)))))
264 (match (crate->guix-package "foo")
265 (('package
266 ('name "rust-foo")
267 ('version "1.0.0")
268 ('source ('origin
269 ('method 'url-fetch)
270 ('uri ('crate-uri "foo" 'version))
271 ('file-name ('string-append 'name "-" 'version ".tar.gz"))
272 ('sha256
273 ('base32
274 (? string? hash)))))
275 ('build-system 'cargo-build-system)
276 ('arguments
277 ('quasiquote
278 ('#:cargo-inputs (("rust-bar" ('unquote rust-bar))))))
279 ('home-page "http://example.com")
280 ('synopsis "summary")
281 ('description "summary")
282 ('license ('list 'license:expat 'license:asl2.0)))
283 (string=? test-source-hash hash))
284 (x
285 (pk 'fail x #f)))))
286
287 (test-assert "cargo-recursive-import"
288 ;; Replace network resources with sample data.
289 (mock ((guix http-client) http-fetch
290 (lambda (url . rest)
291 (match url
292 ("https://crates.io/api/v1/crates/root"
293 (open-input-string test-root-crate))
294 ("https://crates.io/api/v1/crates/root/1.0.0/download"
295 (set! test-source-hash
296 (bytevector->nix-base32-string
297 (sha256 (string->bytevector "empty file\n" "utf-8"))))
298 (open-input-string "empty file\n"))
299 ("https://crates.io/api/v1/crates/root/1.0.0/dependencies"
300 (open-input-string test-root-dependencies))
301 ("https://crates.io/api/v1/crates/intermediate-1"
302 (open-input-string test-intermediate-1-crate))
303 ("https://crates.io/api/v1/crates/intermediate-1/1.0.0/download"
304 (set! test-source-hash
305 (bytevector->nix-base32-string
306 (sha256 (string->bytevector "empty file\n" "utf-8"))))
307 (open-input-string "empty file\n"))
308 ("https://crates.io/api/v1/crates/intermediate-1/1.0.0/dependencies"
309 (open-input-string test-intermediate-1-dependencies))
310 ("https://crates.io/api/v1/crates/intermediate-2"
311 (open-input-string test-intermediate-2-crate))
312 ("https://crates.io/api/v1/crates/intermediate-2/1.0.0/download"
313 (set! test-source-hash
314 (bytevector->nix-base32-string
315 (sha256 (string->bytevector "empty file\n" "utf-8"))))
316 (open-input-string "empty file\n"))
317 ("https://crates.io/api/v1/crates/intermediate-2/1.0.0/dependencies"
318 (open-input-string test-intermediate-2-dependencies))
319 ("https://crates.io/api/v1/crates/leaf-alice"
320 (open-input-string test-leaf-alice-crate))
321 ("https://crates.io/api/v1/crates/leaf-alice/1.0.0/download"
322 (set! test-source-hash
323 (bytevector->nix-base32-string
324 (sha256 (string->bytevector "empty file\n" "utf-8"))))
325 (open-input-string "empty file\n"))
326 ("https://crates.io/api/v1/crates/leaf-alice/1.0.0/dependencies"
327 (open-input-string test-leaf-alice-dependencies))
328 ("https://crates.io/api/v1/crates/leaf-bob"
329 (open-input-string test-leaf-bob-crate))
330 ("https://crates.io/api/v1/crates/leaf-bob/1.0.0/download"
331 (set! test-source-hash
332 (bytevector->nix-base32-string
333 (sha256 (string->bytevector "empty file\n" "utf-8"))))
334 (open-input-string "empty file\n"))
335 ("https://crates.io/api/v1/crates/leaf-bob/1.0.0/dependencies"
336 (open-input-string test-leaf-bob-dependencies))
337 (_ (error "Unexpected URL: " url)))))
338 (match (crate-recursive-import "root")
339 ;; rust-intermediate-2 has no dependency on the rust-leaf-alice package, so this is a valid ordering
340 ((('package
341 ('name "rust-leaf-alice")
342 ('version (? string? ver))
343 ('source
344 ('origin
345 ('method 'url-fetch)
346 ('uri ('crate-uri "leaf-alice" 'version))
347 ('file-name
348 ('string-append 'name "-" 'version ".tar.gz"))
349 ('sha256
350 ('base32
351 (? string? hash)))))
352 ('build-system 'cargo-build-system)
353 ('home-page "http://example.com")
354 ('synopsis "summary")
355 ('description "summary")
356 ('license ('list 'license:expat 'license:asl2.0)))
357 ('package
358 ('name "rust-leaf-bob")
359 ('version (? string? ver))
360 ('source
361 ('origin
362 ('method 'url-fetch)
363 ('uri ('crate-uri "leaf-bob" 'version))
364 ('file-name
365 ('string-append 'name "-" 'version ".tar.gz"))
366 ('sha256
367 ('base32
368 (? string? hash)))))
369 ('build-system 'cargo-build-system)
370 ('home-page "http://example.com")
371 ('synopsis "summary")
372 ('description "summary")
373 ('license ('list 'license:expat 'license:asl2.0)))
374 ('package
375 ('name "rust-intermediate-2")
376 ('version (? string? ver))
377 ('source
378 ('origin
379 ('method 'url-fetch)
380 ('uri ('crate-uri "intermediate-2" 'version))
381 ('file-name
382 ('string-append 'name "-" 'version ".tar.gz"))
383 ('sha256
384 ('base32
385 (? string? hash)))))
386 ('build-system 'cargo-build-system)
387 ('arguments
388 ('quasiquote
389 ('#:cargo-inputs (("rust-leaf-bob" ('unquote rust-leaf-bob))))))
390 ('home-page "http://example.com")
391 ('synopsis "summary")
392 ('description "summary")
393 ('license ('list 'license:expat 'license:asl2.0)))
394 ('package
395 ('name "rust-intermediate-1")
396 ('version (? string? ver))
397 ('source
398 ('origin
399 ('method 'url-fetch)
400 ('uri ('crate-uri "intermediate-1" 'version))
401 ('file-name
402 ('string-append 'name "-" 'version ".tar.gz"))
403 ('sha256
404 ('base32
405 (? string? hash)))))
406 ('build-system 'cargo-build-system)
407 ('arguments
408 ('quasiquote
409 ('#:cargo-inputs (("rust-intermediate-2" ('unquote rust-intermediate-2))
410 ("rust-leaf-alice" ('unquote rust-leaf-alice))
411 ("rust-leaf-bob" ('unquote rust-leaf-bob))))))
412 ('home-page "http://example.com")
413 ('synopsis "summary")
414 ('description "summary")
415 ('license ('list 'license:expat 'license:asl2.0)))
416 ('package
417 ('name "rust-root")
418 ('version (? string? ver))
419 ('source
420 ('origin
421 ('method 'url-fetch)
422 ('uri ('crate-uri "root" 'version))
423 ('file-name
424 ('string-append 'name "-" 'version ".tar.gz"))
425 ('sha256
426 ('base32
427 (? string? hash)))))
428 ('build-system 'cargo-build-system)
429 ('arguments
430 ('quasiquote
431 ('#:cargo-inputs (("rust-intermediate-1" ('unquote rust-intermediate-1))
432 ("rust-intermediate-2" ('unquote rust-intermediate-2))
433 ("rust-leaf-alice" ('unquote rust-leaf-alice))
434 ("rust-leaf-bob" ('unquote rust-leaf-bob))))))
435 ('home-page "http://example.com")
436 ('synopsis "summary")
437 ('description "summary")
438 ('license ('list 'license:expat 'license:asl2.0))))
439 #t)
440 (x
441 (pk 'fail x #f)))))
442
443 (test-equal "licenses: MIT OR Apache-2.0"
444 '(license:expat license:asl2.0)
445 (string->license "MIT OR Apache-2.0"))
446
447 (test-equal "licenses: Apache-2.0 / MIT"
448 '(license:asl2.0 license:expat)
449 (string->license "Apache-2.0 / MIT"))
450
451 (test-equal "licenses: Apache-2.0 WITH LLVM-exception"
452 '(license:asl2.0 unknown-license!)
453 (string->license "Apache-2.0 WITH LLVM-exception"))
454
455 (test-equal "licenses: MIT/Apache-2.0 AND BSD-2-Clause"
456 '(license:expat license:asl2.0 unknown-license!)
457 (string->license "MIT/Apache-2.0 AND BSD-2-Clause"))
458
459 (test-equal "licenses: MIT/Apache-2.0"
460 '(license:expat license:asl2.0)
461 (string->license "MIT/Apache-2.0"))
462
463 (test-end "crate")