import: crate: Separate crates.io API from actual conversion.
[jackhill/guix/guix.git] / tests / crate.scm
CommitLineData
3e0c0365
DC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 David Thompson <davet@gnu.org>
3;;; Copyright © 2016 David Craven <david@craven.ch>
2791870d 4;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
3e0c0365
DC
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)
ca719424 25 #:use-module (gcrypt hash)
3e0c0365
DC
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-crate
32 "{
33 \"crate\": {
34 \"max_version\": \"1.0.0\",
35 \"name\": \"foo\",
3e0c0365
DC
36 \"description\": \"summary\",
37 \"homepage\": \"http://example.com\",
f53a5514 38 \"repository\": \"http://example.com\",
2791870d
LC
39 \"keywords\": [\"dummy\" \"test\"],
40 \"categories\": [\"test\"]
41 \"actual_versions\": [
42 { \"id\": \"foo\",
43 \"num\": \"1.0.0\",
44 \"license\": \"MIT/Apache-2.0\",
45 \"links\": {
46 \"dependencies\": \"/api/v1/crates/foo/1.0.0/dependencies\"
47 }
48 }
49 ]
3e0c0365
DC
50 }
51}")
52
53(define test-dependencies
54 "{
55 \"dependencies\": [
56 {
57 \"crate_id\": \"bar\",
58 \"kind\": \"normal\",
59 }
60 ]
61}")
62
63(define test-source-hash
64 "")
65
66(test-begin "crate")
67
68(test-equal "guix-package->crate-name"
69 "rustc-serialize"
70 (guix-package->crate-name
71 (dummy-package
72 "rust-rustc-serialize"
73 (source (dummy-origin
74 (uri (crate-uri "rustc-serialize" "1.0")))))))
75
76(test-assert "crate->guix-package"
77 ;; Replace network resources with sample data.
78 (mock ((guix http-client) http-fetch
ce8963c5 79 (lambda (url . rest)
3e0c0365
DC
80 (match url
81 ("https://crates.io/api/v1/crates/foo"
82 (open-input-string test-crate))
83 ("https://crates.io/api/v1/crates/foo/1.0.0/download"
84 (set! test-source-hash
85 (bytevector->nix-base32-string
86 (sha256 (string->bytevector "empty file\n" "utf-8"))))
87 (open-input-string "empty file\n"))
88 ("https://crates.io/api/v1/crates/foo/1.0.0/dependencies"
89 (open-input-string test-dependencies))
90 (_ (error "Unexpected URL: " url)))))
91 (match (crate->guix-package "foo")
92 (('package
93 ('name "rust-foo")
94 ('version "1.0.0")
95 ('source ('origin
96 ('method 'url-fetch)
97 ('uri ('crate-uri "foo" 'version))
98 ('file-name ('string-append 'name "-" 'version ".tar.gz"))
99 ('sha256
100 ('base32
101 (? string? hash)))))
102 ('build-system 'cargo-build-system)
5a9ef8a9 103 ('arguments
3e0c0365 104 ('quasiquote
022288ba 105 ('#:cargo-inputs (("rust-bar" ('unquote rust-bar))))))
3e0c0365
DC
106 ('home-page "http://example.com")
107 ('synopsis "summary")
108 ('description "summary")
109 ('license ('list 'license:expat 'license:asl2.0)))
110 (string=? test-source-hash hash))
111 (x
112 (pk 'fail x #f)))))
113
114(test-end "crate")