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