Merge branch 'master' into staging
[jackhill/guix/guix.git] / tests / opam.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (test-opam)
20 #:use-module (guix import opam)
21 #:use-module (guix base32)
22 #:use-module (gcrypt hash)
23 #:use-module (guix tests)
24 #:use-module ((guix build syscalls) #:select (mkdtemp!))
25 #:use-module ((guix build utils) #:select (delete-file-recursively mkdir-p which))
26 #:use-module ((guix utils) #:select (call-with-temporary-output-file))
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-64)
29 #:use-module (web uri)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 peg))
32
33 (define test-opam-file
34 "opam-version: \"2.0\"
35 version: \"1.0.0\"
36 maintainer: \"Alice Doe\"
37 authors: [
38 \"Alice Doe\"
39 \"John Doe\"
40 ]
41 homepage: \"https://example.org/\"
42 bug-reports: \"https://example.org/bugs\"
43 dev-repo: \"https://example.org/git\"
44 build: [
45 [\"ocaml\" \"pkg/pkg.ml\" \"build\" \"--pinned\" \"%{pinned}%\"]
46 ]
47 build-test: [
48 [\"ocaml\" \"pkg/pkg.ml\" \"build\" \"--pinned\" \"%{pinned}%\" \"--tests\" \"true\"]
49 ]
50 depends: [
51 \"alcotest\" {test & >= \"0.7.2\"}
52 \"ocamlbuild\" {build & >= \"0.9.2\"}
53 \"zarith\" {>= \"0.7\"}
54 ]
55 synopsis: \"Some example package\"
56 description: \"\"\"
57 This package is just an example.\"\"\"
58 url {
59 src: \"https://example.org/foo-1.0.0.tar.gz\"
60 checksum: \"md5=74c6e897658e820006106f45f736381f\"
61 }")
62
63 (define test-source-hash
64 "")
65
66 (define test-repo
67 (mkdtemp! "/tmp/opam-repo.XXXXXX"))
68
69 (test-begin "opam")
70
71 (test-assert "opam->guix-package"
72 (mock ((guix import utils) url-fetch
73 (lambda (url file-name)
74 (match url
75 ("https://example.org/foo-1.0.0.tar.gz"
76 (begin
77 (mkdir-p "foo-1.0.0")
78 (system* "tar" "czvf" file-name "foo-1.0.0/")
79 (delete-file-recursively "foo-1.0.0")
80 (set! test-source-hash
81 (call-with-input-file file-name port-sha256))))
82 (_ (error "Unexpected URL: " url)))))
83 (let ((my-package (string-append test-repo "/packages/foo/foo.1.0.0")))
84 (mkdir-p my-package)
85 (with-output-to-file (string-append my-package "/opam")
86 (lambda _
87 (format #t "~a" test-opam-file))))
88 (match (opam->guix-package "foo" #:repository test-repo)
89 (('package
90 ('name "ocaml-foo")
91 ('version "1.0.0")
92 ('source ('origin
93 ('method 'url-fetch)
94 ('uri "https://example.org/foo-1.0.0.tar.gz")
95 ('sha256
96 ('base32
97 (? string? hash)))))
98 ('build-system 'ocaml-build-system)
99 ('propagated-inputs
100 ('quasiquote
101 (("ocaml-zarith" ('unquote 'ocaml-zarith)))))
102 ('native-inputs
103 ('quasiquote
104 (("ocaml-alcotest" ('unquote 'ocaml-alcotest))
105 ("ocamlbuild" ('unquote 'ocamlbuild)))))
106 ('home-page "https://example.org/")
107 ('synopsis "Some example package")
108 ('description "This package is just an example.")
109 ('license #f))
110 (string=? (bytevector->nix-base32-string
111 test-source-hash)
112 hash))
113 (x
114 (pk 'fail x #f)))))
115
116 ;; Test the opam file parser
117 ;; We fold over some test cases. Each case is a pair of the string to parse and the
118 ;; expected result.
119 (define (test-opam-syntax name pattern test-cases)
120 (test-assert name
121 (fold (lambda (test acc)
122 (display test) (newline)
123 (match test
124 ((str . expected)
125 (and acc
126 (let ((result (peg:tree (match-pattern pattern str))))
127 (if (equal? result expected)
128 #t
129 (pk 'fail (list str result expected) #f)))))))
130 #t test-cases)))
131
132 (test-opam-syntax
133 "parse-strings" string-pat
134 '(("" . #f)
135 ("\"hello\"" . (string-pat "hello"))
136 ("\"hello world\"" . (string-pat "hello world"))
137 ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
138 ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
139 ("\"今日は\"" . (string-pat "今日は"))))
140
141 (test-opam-syntax
142 "parse-multiline-strings" multiline-string
143 '(("" . #f)
144 ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
145 ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
146 ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!"))))
147
148 (test-opam-syntax
149 "parse-lists" list-pat
150 '(("" . #f)
151 ("[]" . list-pat)
152 ("[make]" . (list-pat (var "make")))
153 ("[\"make\"]" . (list-pat (string-pat "make")))
154 ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
155 ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c")))
156 ;; complex lists
157 ("[(a & b)]" . (list-pat (choice-pat (group-pat (var "a") (var "b")))))
158 ("[(a | b & c)]" . (list-pat (choice-pat (var "a") (group-pat (var "b") (var "c")))))
159 ("[a (b | c) d]" . (list-pat (var "a") (choice-pat (var "b") (var "c")) (var "d")))))
160
161 (test-opam-syntax
162 "parse-dicts" dict
163 '(("" . #f)
164 ("{}" . dict)
165 ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
166 ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d"))))))
167
168 (test-opam-syntax
169 "parse-conditions" condition
170 '(("" . #f)
171 ("{}" . #f)
172 ("{build}" . (condition-var "build"))
173 ("{>= \"0.2.0\"}" . (condition-greater-or-equal
174 (condition-string "0.2.0")))
175 ("{>= \"0.2.0\" & test}" . (condition-and
176 (condition-greater-or-equal
177 (condition-string "0.2.0"))
178 (condition-var "test")))
179 ("{>= \"0.2.0\" | build}" . (condition-or
180 (condition-greater-or-equal
181 (condition-string "0.2.0"))
182 (condition-var "build")))
183 ("{ = \"1.0+beta19\" }" . (condition-eq
184 (condition-string "1.0+beta19")))))
185
186 (test-opam-syntax
187 "parse-comment" list-pat
188 '(("" . #f)
189 ("[#comment\n]" . list-pat)))
190
191 (test-end "opam")