packages: Raise an exception for invalid 'license' values.
[jackhill/guix/guix.git] / tests / gem.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
5 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
6 ;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
7 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
14 ;;;
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23 (define-module (test-gem)
24 #:use-module (guix import gem)
25 #:use-module (guix base32)
26 #:use-module (gcrypt hash)
27 #:use-module (guix tests)
28 #:use-module ((guix build utils) #:select (delete-file-recursively))
29 #:use-module (srfi srfi-64)
30 #:use-module (ice-9 match))
31
32 (define test-foo-json
33 "{
34 \"name\": \"foo\",
35 \"version\": \"1.0.0\",
36 \"sha\": \"f3676eafca9987cb5fe263df1edf2538bf6dafc712b30e17be3543a9680547a8\",
37 \"info\": \"A cool gem\",
38 \"homepage_uri\": \"https://example.com\",
39 \"dependencies\": {
40 \"runtime\": [
41 { \"name\": \"bundler\" },
42 { \"name\": \"bar\" }
43 ]
44 },
45 \"licenses\": [\"MIT\", \"Apache 2.0\"]
46 }")
47
48 (define test-foo-v2-json
49 "{
50 \"name\": \"foo\",
51 \"version\": \"2.0.0\",
52 \"sha\": \"f3676eafca9987cb5fe263df1edf2538bf6dafc712b30e17be3543a9680547a8\",
53 \"info\": \"A cool gem\",
54 \"homepage_uri\": \"https://example.com\",
55 \"dependencies\": {
56 \"runtime\": [
57 { \"name\": \"bundler\" },
58 { \"name\": \"bar\" }
59 ]
60 },
61 \"licenses\": [\"MIT\", \"Apache 2.0\"]
62 }")
63
64 (define test-bar-json
65 "{
66 \"name\": \"bar\",
67 \"version\": \"1.0.0\",
68 \"sha\": \"f3676eafca9987cb5fe263df1edf2538bf6dafc712b30e17be3543a9680547a8\",
69 \"info\": \"Another cool gem\",
70 \"homepage_uri\": \"https://example.com\",
71 \"dependencies\": {
72 \"runtime\": [
73 { \"name\": \"bundler\" }
74 ]
75 },
76 \"licenses\": null
77 }")
78
79 (define test-bundler-json
80 "{
81 \"name\": \"bundler\",
82 \"version\": \"1.14.2\",
83 \"sha\": \"3bb53e03db0a8008161eb4c816ccd317120d3c415ba6fee6f90bbc7f7eec8690\",
84 \"info\": \"Ruby gem bundler\",
85 \"homepage_uri\": \"https://bundler.io/\",
86 \"dependencies\": {
87 \"runtime\": []
88 },
89 \"licenses\": [\"MIT\"]
90 }")
91
92 (test-begin "gem")
93
94 (test-assert "gem->guix-package"
95 ;; Replace network resources with sample data.
96 (mock ((guix http-client) http-fetch
97 (lambda (url . rest)
98 (match url
99 ("https://rubygems.org/api/v1/gems/foo.json"
100 (values (open-input-string test-foo-json)
101 (string-length test-foo-json)))
102 (_ (error "Unexpected URL: " url)))))
103 (match (gem->guix-package "foo")
104 (('package
105 ('name "ruby-foo")
106 ('version "1.0.0")
107 ('source ('origin
108 ('method 'url-fetch)
109 ('uri ('rubygems-uri "foo" 'version))
110 ('sha256
111 ('base32
112 "1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
113 ('build-system 'ruby-build-system)
114 ('propagated-inputs ('list 'bundler 'ruby-bar))
115 ('synopsis "A cool gem")
116 ('description "This package provides a cool gem")
117 ('home-page "https://example.com")
118 ('license ('list 'license:expat 'license:asl2.0)))
119 #t)
120 (x
121 (pk 'fail x #f)))))
122
123 (test-assert "gem->guix-package with a specific version"
124 ;; Replace network resources with sample data.
125 (mock ((guix http-client) http-fetch
126 (lambda (url . rest)
127 (match url
128 ("https://rubygems.org/api/v2/rubygems/foo/versions/2.0.0.json"
129 (values (open-input-string test-foo-v2-json)
130 (string-length test-foo-v2-json)))
131 (_ (error "Unexpected URL: " url)))))
132 (match (gem->guix-package "foo" #:version "2.0.0")
133 (('package
134 ('name "ruby-foo")
135 ('version "2.0.0")
136 ('source ('origin
137 ('method 'url-fetch)
138 ('uri ('rubygems-uri "foo" 'version))
139 ('sha256
140 ('base32
141 "1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
142 ('build-system 'ruby-build-system)
143 ('propagated-inputs ('list 'bundler 'ruby-bar))
144 ('synopsis "A cool gem")
145 ('description "This package provides a cool gem")
146 ('home-page "https://example.com")
147 ('license ('list 'license:expat 'license:asl2.0)))
148 #t)
149 (x
150 (pk 'fail x #f)))))
151
152 (test-assert "gem-recursive-import"
153 ;; Replace network resources with sample data.
154 (mock ((guix http-client) http-fetch
155 (lambda (url . rest)
156 (match url
157 ("https://rubygems.org/api/v1/gems/foo.json"
158 (values (open-input-string test-foo-json)
159 (string-length test-foo-json)))
160 ("https://rubygems.org/api/v1/gems/bar.json"
161 (values (open-input-string test-bar-json)
162 (string-length test-bar-json)))
163 ("https://rubygems.org/api/v1/gems/bundler.json"
164 (values (open-input-string test-bundler-json)
165 (string-length test-bundler-json)))
166 (_ (error "Unexpected URL: " url)))))
167 (match (gem-recursive-import "foo")
168 ((('package
169 ('name "ruby-bar")
170 ('version "1.0.0")
171 ('source
172 ('origin
173 ('method 'url-fetch)
174 ('uri ('rubygems-uri "bar" 'version))
175 ('sha256
176 ('base32
177 "1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
178 ('build-system 'ruby-build-system)
179 ('propagated-inputs ('list 'bundler))
180 ('synopsis "Another cool gem")
181 ('description "Another cool gem")
182 ('home-page "https://example.com")
183 ('license #f)) ;no licensing info
184 ('package
185 ('name "ruby-bundler")
186 ('version "1.14.2")
187 ('source
188 ('origin
189 ('method 'url-fetch)
190 ('uri ('rubygems-uri "bundler" 'version))
191 ('sha256
192 ('base32
193 "1446xiz7zg0bz7kgx9jv84y0s4hpsg61dj5l3qb0i00avc1kxd9v"))))
194 ('build-system 'ruby-build-system)
195 ('synopsis "Ruby gem bundler")
196 ('description "Ruby gem bundler")
197 ('home-page "https://bundler.io/")
198 ('license 'license:expat))
199 ('package
200 ('name "ruby-foo")
201 ('version "1.0.0")
202 ('source
203 ('origin
204 ('method 'url-fetch)
205 ('uri ('rubygems-uri "foo" 'version))
206 ('sha256
207 ('base32
208 "1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
209 ('build-system 'ruby-build-system)
210 ('propagated-inputs ('list 'bundler 'ruby-bar))
211 ('synopsis "A cool gem")
212 ('description "This package provides a cool gem")
213 ('home-page "https://example.com")
214 ('license ('list 'license:expat 'license:asl2.0))))
215 #t)
216 (x
217 (pk 'fail x #f)))))
218
219 (test-assert "gem-recursive-import with a specific version"
220 ;; Replace network resources with sample data.
221 (mock ((guix http-client) http-fetch
222 (lambda (url . rest)
223 (match url
224 ("https://rubygems.org/api/v2/rubygems/foo/versions/2.0.0.json"
225 (values (open-input-string test-foo-v2-json)
226 (string-length test-foo-v2-json)))
227 ("https://rubygems.org/api/v1/gems/bar.json"
228 (values (open-input-string test-bar-json)
229 (string-length test-bar-json)))
230 ("https://rubygems.org/api/v1/gems/bundler.json"
231 (values (open-input-string test-bundler-json)
232 (string-length test-bundler-json)))
233 (_ (error "Unexpected URL: " url)))))
234 (match (gem-recursive-import "foo" "2.0.0")
235 ((('package
236 ('name "ruby-bar")
237 ('version "1.0.0")
238 ('source
239 ('origin
240 ('method 'url-fetch)
241 ('uri ('rubygems-uri "bar" 'version))
242 ('sha256
243 ('base32
244 "1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
245 ('build-system 'ruby-build-system)
246 ('propagated-inputs ('list 'bundler))
247 ('synopsis "Another cool gem")
248 ('description "Another cool gem")
249 ('home-page "https://example.com")
250 ('license #f)) ;no licensing info
251 ('package
252 ('name "ruby-bundler")
253 ('version "1.14.2")
254 ('source
255 ('origin
256 ('method 'url-fetch)
257 ('uri ('rubygems-uri "bundler" 'version))
258 ('sha256
259 ('base32
260 "1446xiz7zg0bz7kgx9jv84y0s4hpsg61dj5l3qb0i00avc1kxd9v"))))
261 ('build-system 'ruby-build-system)
262 ('synopsis "Ruby gem bundler")
263 ('description "Ruby gem bundler")
264 ('home-page "https://bundler.io/")
265 ('license 'license:expat))
266 ('package
267 ('name "ruby-foo")
268 ('version "2.0.0")
269 ('source
270 ('origin
271 ('method 'url-fetch)
272 ('uri ('rubygems-uri "foo" 'version))
273 ('sha256
274 ('base32
275 "1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
276 ('build-system 'ruby-build-system)
277 ('propagated-inputs ('list 'bundler 'ruby-bar))
278 ('synopsis "A cool gem")
279 ('description "This package provides a cool gem")
280 ('home-page "https://example.com")
281 ('license ('list 'license:expat 'license:asl2.0))))
282 #t)
283 (x
284 (pk 'fail x #f)))))
285
286 (test-end "gem")