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