Merge remote-tracking branch 'origin/master' into core-updates-frozen.
[jackhill/guix/guix.git] / tests / opam.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
3 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
4 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
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-opam)
22 #:use-module (guix import opam)
23 #:use-module (guix base32)
24 #:use-module (gcrypt hash)
25 #:use-module (guix tests)
26 #:use-module ((guix build syscalls) #:select (mkdtemp!))
27 #:use-module ((guix build utils) #:select (delete-file-recursively mkdir-p which))
28 #:use-module ((guix utils) #:select (call-with-temporary-output-file))
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-64)
31 #:use-module (web uri)
32 #:use-module (ice-9 match)
33 #:use-module (ice-9 peg))
34
35 (define test-opam-file
36 "opam-version: \"2.0\"
37 version: \"1.0.0\"
38 maintainer: \"Alice Doe\"
39 authors: [
40 \"Alice Doe\"
41 \"John Doe\"
42 ]
43 homepage: \"https://example.org/\"
44 bug-reports: \"https://example.org/bugs\"
45 dev-repo: \"https://example.org/git\"
46 build: [
47 [\"ocaml\" \"pkg/pkg.ml\" \"build\" \"--pinned\" \"%{pinned}%\"]
48 ]
49 build-test: [
50 [\"ocaml\" \"pkg/pkg.ml\" \"build\" \"--pinned\" \"%{pinned}%\" \"--tests\" \"true\"]
51 ]
52 depends: [
53 \"alcotest\" {test & >= \"0.7.2\"}
54 \"ocamlbuild\" {build & >= \"0.9.2\"}
55 \"zarith\" {>= \"0.7\"}
56 ]
57 synopsis: \"Some example package\"
58 description: \"\"\"
59 This package is just an example.\"\"\"
60 license: \"BSD-3-Clause\"
61 url {
62 src: \"https://example.org/foo-1.0.0.tar.gz\"
63 checksum: \"md5=74c6e897658e820006106f45f736381f\"
64 }")
65
66 (define test-source-hash
67 "")
68
69 (define test-repo
70 (mkdtemp! "/tmp/opam-repo.XXXXXX"))
71
72 (test-begin "opam")
73
74 (test-assert "opam->guix-package"
75 (mock ((guix import opam) get-opam-repository
76 (const test-repo))
77 (mock ((guix import utils) url-fetch
78 (lambda (url file-name)
79 (match url
80 ("https://example.org/foo-1.0.0.tar.gz"
81 (begin
82 (mkdir-p "foo-1.0.0")
83 (system* "tar" "czvf" file-name "foo-1.0.0/")
84 (delete-file-recursively "foo-1.0.0")
85 (set! test-source-hash
86 (call-with-input-file file-name port-sha256))))
87 (_ (error "Unexpected URL: " url)))))
88 (let ((my-package (string-append test-repo
89 "/packages/foo/foo.1.0.0")))
90 (mkdir-p my-package)
91 (with-output-to-file (string-append my-package "/opam")
92 (lambda _
93 (format #t "~a" test-opam-file))))
94 (match (opam->guix-package "foo" #:repo (list test-repo))
95 (('package
96 ('name "ocaml-foo")
97 ('version "1.0.0")
98 ('source ('origin
99 ('method 'url-fetch)
100 ('uri "https://example.org/foo-1.0.0.tar.gz")
101 ('sha256
102 ('base32
103 (? string? hash)))))
104 ('build-system 'ocaml-build-system)
105 ('propagated-inputs
106 ('quasiquote
107 (("ocaml-zarith" ('unquote 'ocaml-zarith)))))
108 ('native-inputs
109 ('quasiquote
110 (("ocaml-alcotest" ('unquote 'ocaml-alcotest))
111 ("ocamlbuild" ('unquote 'ocamlbuild)))))
112 ('home-page "https://example.org/")
113 ('synopsis "Some example package")
114 ('description "This package is just an example.")
115 ('license 'license:bsd-3))
116 (string=? (bytevector->nix-base32-string
117 test-source-hash)
118 hash))
119 (x
120 (pk 'fail x #f))))))
121
122 ;; Test the opam file parser
123 ;; We fold over some test cases. Each case is a pair of the string to parse and the
124 ;; expected result.
125 (define (test-opam-syntax name pattern test-cases)
126 (test-assert name
127 (fold (lambda (test acc)
128 (display test) (newline)
129 (match test
130 ((str . expected)
131 (and acc
132 (let ((result (peg:tree (match-pattern pattern str))))
133 (if (equal? result expected)
134 #t
135 (pk 'fail (list str result expected) #f)))))))
136 #t test-cases)))
137
138 (test-opam-syntax
139 "parse-strings" string-pat
140 '(("" . #f)
141 ("\"hello\"" . (string-pat "hello"))
142 ("\"hello world\"" . (string-pat "hello world"))
143 ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
144 ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
145 ("\"今日は\"" . (string-pat "今日は"))))
146
147 (test-opam-syntax
148 "parse-multiline-strings" multiline-string
149 '(("" . #f)
150 ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
151 ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
152 ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!"))))
153
154 (test-opam-syntax
155 "parse-lists" list-pat
156 '(("" . #f)
157 ("[]" . list-pat)
158 ("[make]" . (list-pat (var "make")))
159 ("[\"make\"]" . (list-pat (string-pat "make")))
160 ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
161 ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c")))
162 ;; complex lists
163 ("[(a & b)]" . (list-pat (choice-pat (group-pat (var "a") (var "b")))))
164 ("[(a | b & c)]" . (list-pat (choice-pat (var "a") (group-pat (var "b") (var "c")))))
165 ("[a (b | c) d]" . (list-pat (var "a") (choice-pat (var "b") (var "c")) (var "d")))))
166
167 (test-opam-syntax
168 "parse-dicts" dict
169 '(("" . #f)
170 ("{}" . dict)
171 ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
172 ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d"))))))
173
174 (test-opam-syntax
175 "parse-conditions" condition
176 '(("" . #f)
177 ("{}" . #f)
178 ("{build}" . (condition-var "build"))
179 ("{>= \"0.2.0\"}" . (condition-greater-or-equal
180 (condition-string "0.2.0")))
181 ("{>= \"0.2.0\" & test}" . (condition-and
182 (condition-greater-or-equal
183 (condition-string "0.2.0"))
184 (condition-var "test")))
185 ("{>= \"0.2.0\" | build}" . (condition-or
186 (condition-greater-or-equal
187 (condition-string "0.2.0"))
188 (condition-var "build")))
189 ("{ = \"1.0+beta19\" }" . (condition-eq
190 (condition-string "1.0+beta19")))))
191
192 (test-opam-syntax
193 "parse-comment" list-pat
194 '(("" . #f)
195 ("[#comment\n]" . list-pat)))
196
197 (test-end "opam")