opam.scm 6.8 KB

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