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, 2020 Ludovic Courtès <ludo@gnu.org>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
31 (define test-foo-crate
34 \"max_version\": \"1.0.0\",
36 \"description\": \"summary\",
37 \"homepage\": \"http://example.com\",
38 \"repository\": \"http://example.com\",
39 \"keywords\": [\"dummy\" \"test\"],
40 \"categories\": [\"test\"]
41 \"actual_versions\": [
44 \"license\": \"MIT OR Apache-2.0\",
46 \"dependencies\": \"/api/v1/crates/foo/1.0.0/dependencies\"
53 (define test-foo-dependencies
57 \"crate_id\": \"bar\",
63 (define test-root-crate
66 \"max_version\": \"1.0.0\",
68 \"description\": \"summary\",
69 \"homepage\": \"http://example.com\",
70 \"repository\": \"http://example.com\",
71 \"keywords\": [\"dummy\" \"test\"],
72 \"categories\": [\"test\"]
73 \"actual_versions\": [
76 \"license\": \"MIT OR Apache-2.0\",
78 \"dependencies\": \"/api/v1/crates/root/1.0.0/dependencies\"
85 (define test-root-dependencies
89 \"crate_id\": \"intermediate-1\",
93 \"crate_id\": \"intermediate-2\",
97 \"crate_id\": \"leaf-alice\",
101 \"crate_id\": \"leaf-bob\",
107 (define test-intermediate-1-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\",
120 \"license\": \"MIT OR Apache-2.0\",
122 \"dependencies\": \"/api/v1/crates/intermediate-1/1.0.0/dependencies\"
129 (define test-intermediate-1-dependencies
133 \"crate_id\": \"intermediate-2\",
137 \"crate_id\": \"leaf-alice\",
141 \"crate_id\": \"leaf-bob\",
147 (define test-intermediate-2-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\",
160 \"license\": \"MIT OR Apache-2.0\",
162 \"dependencies\": \"/api/v1/crates/intermediate-2/1.0.0/dependencies\"
169 (define test-intermediate-2-dependencies
173 \"crate_id\": \"leaf-bob\",
179 (define test-leaf-alice-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\",
192 \"license\": \"MIT OR Apache-2.0\",
194 \"dependencies\": \"/api/v1/crates/leaf-alice/1.0.0/dependencies\"
201 (define test-leaf-alice-dependencies
206 (define test-leaf-bob-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\",
219 \"license\": \"MIT OR Apache-2.0\",
221 \"dependencies\": \"/api/v1/crates/leaf-bob/1.0.0/dependencies\"
228 (define test-leaf-bob-dependencies
233 (define test-source-hash
239 (test-equal "guix-package->crate-name"
241 (guix-package->crate-name
243 "rust-rustc-serialize"
244 (source (dummy-origin
245 (uri (crate-uri "rustc-serialize" "1.0")))))))
247 (test-assert "crate->guix-package"
248 ;; Replace network resources with sample data.
249 (mock ((guix http-client) http-fetch
252 ("https://crates.io/api/v1/crates/foo"
253 (open-input-string test-foo-crate))
254 ("https://crates.io/api/v1/crates/foo/1.0.0/download"
255 (set! test-source-hash
256 (bytevector->nix-base32-string
257 (sha256 (string->bytevector "empty file\n" "utf-8"))))
258 (open-input-string "empty file\n"))
259 ("https://crates.io/api/v1/crates/foo/1.0.0/dependencies"
260 (open-input-string test-foo-dependencies))
261 (_ (error "Unexpected URL: " url)))))
262 (match (crate->guix-package "foo")
268 ('uri ('crate-uri "foo" 'version))
269 ('file-name ('string-append 'name "-" 'version ".tar.gz"))
273 ('build-system 'cargo-build-system)
276 ('#:cargo-inputs (("rust-bar" ('unquote rust-bar))))))
277 ('home-page "http://example.com")
278 ('synopsis "summary")
279 ('description "summary")
280 ('license ('list 'license:expat 'license:asl2.0)))
281 (string=? test-source-hash hash))
285 (test-assert "cargo-recursive-import"
286 ;; Replace network resources with sample data.
287 (mock ((guix http-client) http-fetch
290 ("https://crates.io/api/v1/crates/root"
291 (open-input-string test-root-crate))
292 ("https://crates.io/api/v1/crates/root/1.0.0/download"
293 (set! test-source-hash
294 (bytevector->nix-base32-string
295 (sha256 (string->bytevector "empty file\n" "utf-8"))))
296 (open-input-string "empty file\n"))
297 ("https://crates.io/api/v1/crates/root/1.0.0/dependencies"
298 (open-input-string test-root-dependencies))
299 ("https://crates.io/api/v1/crates/intermediate-1"
300 (open-input-string test-intermediate-1-crate))
301 ("https://crates.io/api/v1/crates/intermediate-1/1.0.0/download"
302 (set! test-source-hash
303 (bytevector->nix-base32-string
304 (sha256 (string->bytevector "empty file\n" "utf-8"))))
305 (open-input-string "empty file\n"))
306 ("https://crates.io/api/v1/crates/intermediate-1/1.0.0/dependencies"
307 (open-input-string test-intermediate-1-dependencies))
308 ("https://crates.io/api/v1/crates/intermediate-2"
309 (open-input-string test-intermediate-2-crate))
310 ("https://crates.io/api/v1/crates/intermediate-2/1.0.0/download"
311 (set! test-source-hash
312 (bytevector->nix-base32-string
313 (sha256 (string->bytevector "empty file\n" "utf-8"))))
314 (open-input-string "empty file\n"))
315 ("https://crates.io/api/v1/crates/intermediate-2/1.0.0/dependencies"
316 (open-input-string test-intermediate-2-dependencies))
317 ("https://crates.io/api/v1/crates/leaf-alice"
318 (open-input-string test-leaf-alice-crate))
319 ("https://crates.io/api/v1/crates/leaf-alice/1.0.0/download"
320 (set! test-source-hash
321 (bytevector->nix-base32-string
322 (sha256 (string->bytevector "empty file\n" "utf-8"))))
323 (open-input-string "empty file\n"))
324 ("https://crates.io/api/v1/crates/leaf-alice/1.0.0/dependencies"
325 (open-input-string test-leaf-alice-dependencies))
326 ("https://crates.io/api/v1/crates/leaf-bob"
327 (open-input-string test-leaf-bob-crate))
328 ("https://crates.io/api/v1/crates/leaf-bob/1.0.0/download"
329 (set! test-source-hash
330 (bytevector->nix-base32-string
331 (sha256 (string->bytevector "empty file\n" "utf-8"))))
332 (open-input-string "empty file\n"))
333 ("https://crates.io/api/v1/crates/leaf-bob/1.0.0/dependencies"
334 (open-input-string test-leaf-bob-dependencies))
335 (_ (error "Unexpected URL: " url)))))
336 (match (crate-recursive-import "root")
337 ;; rust-intermediate-2 has no dependency on the rust-leaf-alice package, so this is a valid ordering
339 ('name "rust-leaf-alice")
340 ('version (? string? ver))
344 ('uri ('crate-uri "leaf-alice" 'version))
346 ('string-append 'name "-" 'version ".tar.gz"))
350 ('build-system 'cargo-build-system)
351 ('home-page "http://example.com")
352 ('synopsis "summary")
353 ('description "summary")
354 ('license ('list 'license:expat 'license:asl2.0)))
356 ('name "rust-leaf-bob")
357 ('version (? string? ver))
361 ('uri ('crate-uri "leaf-bob" 'version))
363 ('string-append 'name "-" 'version ".tar.gz"))
367 ('build-system 'cargo-build-system)
368 ('home-page "http://example.com")
369 ('synopsis "summary")
370 ('description "summary")
371 ('license ('list 'license:expat 'license:asl2.0)))
373 ('name "rust-intermediate-2")
374 ('version (? string? ver))
378 ('uri ('crate-uri "intermediate-2" 'version))
380 ('string-append 'name "-" 'version ".tar.gz"))
384 ('build-system 'cargo-build-system)
387 ('#:cargo-inputs (("rust-leaf-bob" ('unquote rust-leaf-bob))))))
388 ('home-page "http://example.com")
389 ('synopsis "summary")
390 ('description "summary")
391 ('license ('list 'license:expat 'license:asl2.0)))
393 ('name "rust-intermediate-1")
394 ('version (? string? ver))
398 ('uri ('crate-uri "intermediate-1" 'version))
400 ('string-append 'name "-" 'version ".tar.gz"))
404 ('build-system 'cargo-build-system)
407 ('#:cargo-inputs (("rust-intermediate-2" ('unquote rust-intermediate-2))
408 ("rust-leaf-alice" ('unquote rust-leaf-alice))
409 ("rust-leaf-bob" ('unquote rust-leaf-bob))))))
410 ('home-page "http://example.com")
411 ('synopsis "summary")
412 ('description "summary")
413 ('license ('list 'license:expat 'license:asl2.0)))
416 ('version (? string? ver))
420 ('uri ('crate-uri "root" 'version))
422 ('string-append 'name "-" 'version ".tar.gz"))
426 ('build-system 'cargo-build-system)
429 ('#:cargo-inputs (("rust-intermediate-1" ('unquote rust-intermediate-1))
430 ("rust-intermediate-2" ('unquote rust-intermediate-2))
431 ("rust-leaf-alice" ('unquote rust-leaf-alice))
432 ("rust-leaf-bob" ('unquote rust-leaf-bob))))))
433 ('home-page "http://example.com")
434 ('synopsis "summary")
435 ('description "summary")
436 ('license ('list 'license:expat 'license:asl2.0))))
441 (test-equal "licenses: MIT OR Apache-2.0"
442 '(license:expat license:asl2.0)
443 (string->license "MIT OR Apache-2.0"))
445 (test-equal "licenses: Apache-2.0 / MIT"
446 '(license:asl2.0 license:expat)
447 (string->license "Apache-2.0 / MIT"))
449 (test-equal "licenses: Apache-2.0 WITH LLVM-exception"
450 '(license:asl2.0 unknown-license!)
451 (string->license "Apache-2.0 WITH LLVM-exception"))
453 (test-equal "licenses: MIT/Apache-2.0 AND BSD-2-Clause"
454 '(license:expat license:asl2.0 unknown-license!)
455 (string->license "MIT/Apache-2.0 AND BSD-2-Clause"))
457 (test-equal "licenses: MIT/Apache-2.0"
458 '(license:expat license:asl2.0)
459 (string->license "MIT/Apache-2.0"))