1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
33 (define test-opam-file
34 "opam-version: \"2.0\"
36 maintainer: \"Alice Doe\"
41 homepage: \"https://example.org/\"
42 bug-reports: \"https://example.org/bugs\"
43 dev-repo: \"https://example.org/git\"
45 [\"ocaml\" \"pkg/pkg.ml\" \"build\" \"--pinned\" \"%{pinned}%\"]
48 [\"ocaml\" \"pkg/pkg.ml\" \"build\" \"--pinned\" \"%{pinned}%\" \"--tests\" \"true\"]
51 \"alcotest\" {test & >= \"0.7.2\"}
52 \"ocamlbuild\" {build & >= \"0.9.2\"}
53 \"zarith\" {>= \"0.7\"}
55 synopsis: \"Some example package\"
57 This package is just an example.\"\"\"
59 src: \"https://example.org/foo-1.0.0.tar.gz\"
60 checksum: \"md5=74c6e897658e820006106f45f736381f\"
63 (define test-source-hash
67 (mkdtemp! "/tmp/opam-repo.XXXXXX"))
71 (test-assert "opam->guix-package"
72 (mock ((guix import utils) url-fetch
73 (lambda (url file-name)
75 ("https://example.org/foo-1.0.0.tar.gz"
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")))
85 (with-output-to-file (string-append my-package "/opam")
87 (format #t "~a" test-opam-file))))
88 (mock ((guix import opam) get-opam-repository
91 (match (opam->guix-package "foo")
97 ('uri "https://example.org/foo-1.0.0.tar.gz")
101 ('build-system 'ocaml-build-system)
104 (("ocaml-zarith" ('unquote 'ocaml-zarith)))))
107 (("ocaml-alcotest" ('unquote 'ocaml-alcotest))
108 ("ocamlbuild" ('unquote 'ocamlbuild)))))
109 ('home-page "https://example.org/")
110 ('synopsis "Some example package")
111 ('description "This package is just an example.")
113 (string=? (bytevector->nix-base32-string
119 ;; Test the opam file parser
120 ;; We fold over some test cases. Each case is a pair of the string to parse and the
122 (test-assert "parse-strings"
123 (fold (lambda (test acc)
124 (display test) (newline)
126 (let ((result (peg:tree (match-pattern (@@ (guix import opam) string-pat) (car test)))))
127 (if (equal? result (cdr test))
129 (pk 'fail (list (car test) result (cdr test)) #f)))))
131 ("\"hello\"" . (string-pat "hello"))
132 ("\"hello world\"" . (string-pat "hello world"))
133 ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
134 ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
135 ("\"今日は\"" . (string-pat "今日は")))))
137 (test-assert "parse-multiline-strings"
138 (fold (lambda (test acc)
139 (display test) (newline)
141 (let ((result (peg:tree (match-pattern (@@ (guix import opam) multiline-string) (car test)))))
142 (if (equal? result (cdr test))
144 (pk 'fail (list (car test) result (cdr test)) #f)))))
146 ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
147 ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
148 ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!")))))
150 (test-assert "parse-lists"
151 (fold (lambda (test acc)
153 (let ((result (peg:tree (match-pattern (@@ (guix import opam) list-pat) (car test)))))
154 (if (equal? result (cdr test))
156 (pk 'fail (list (car test) result (cdr test)) #f)))))
159 ("[make]" . (list-pat (var "make")))
160 ("[\"make\"]" . (list-pat (string-pat "make")))
161 ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
162 ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c"))))))
164 (test-assert "parse-dicts"
165 (fold (lambda (test acc)
167 (let ((result (peg:tree (match-pattern (@@ (guix import opam) dict) (car test)))))
168 (if (equal? result (cdr test))
170 (pk 'fail (list (car test) result (cdr test)) #f)))))
173 ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
174 ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d")))))))
176 (test-assert "parse-conditions"
177 (fold (lambda (test acc)
179 (let ((result (peg:tree (match-pattern (@@ (guix import opam) condition) (car test)))))
180 (if (equal? result (cdr test))
182 (pk 'fail (list (car test) result (cdr test)) #f)))))
185 ("{build}" . (condition-var "build"))
186 ("{>= \"0.2.0\"}" . (condition-greater-or-equal
187 (condition-string "0.2.0")))
188 ("{>= \"0.2.0\" & test}" . (condition-and
189 (condition-greater-or-equal
190 (condition-string "0.2.0"))
191 (condition-var "test")))
192 ("{>= \"0.2.0\" | build}" . (condition-or
193 (condition-greater-or-equal
194 (condition-string "0.2.0"))
195 (condition-var "build")))
196 ("{ = \"1.0+beta19\" }" . (condition-eq
197 (condition-string "1.0+beta19"))))))