opam.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  4. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (test-opam)
  21. #:use-module (guix import opam)
  22. #:use-module (guix base32)
  23. #:use-module (gcrypt hash)
  24. #:use-module (guix tests)
  25. #:use-module ((guix build syscalls) #:select (mkdtemp!))
  26. #:use-module ((guix build utils) #:select (delete-file-recursively mkdir-p which))
  27. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-64)
  30. #:use-module (web uri)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 peg))
  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. license: \"BSD-3-Clause\"
  59. url {
  60. src: \"https://example.org/foo-1.0.0.tar.gz\"
  61. checksum: \"md5=74c6e897658e820006106f45f736381f\"
  62. }")
  63. (define test-source-hash
  64. "")
  65. (define test-repo
  66. (mkdtemp! "/tmp/opam-repo.XXXXXX"))
  67. (test-begin "opam")
  68. (test-assert "opam->guix-package"
  69. (mock ((guix import opam) get-opam-repository
  70. (const test-repo))
  71. (mock ((guix import utils) url-fetch
  72. (lambda (url file-name)
  73. (match url
  74. ("https://example.org/foo-1.0.0.tar.gz"
  75. (begin
  76. (mkdir-p "foo-1.0.0")
  77. (system* "tar" "czvf" file-name "foo-1.0.0/")
  78. (delete-file-recursively "foo-1.0.0")
  79. (set! test-source-hash
  80. (call-with-input-file file-name port-sha256))))
  81. (_ (error "Unexpected URL: " url)))))
  82. (let ((my-package (string-append test-repo
  83. "/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" #:repo (list 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 ,(? string? hash)))))
  97. (build-system ocaml-build-system)
  98. (propagated-inputs (list ocaml-zarith))
  99. (native-inputs
  100. (list ocaml-alcotest ocamlbuild))
  101. (home-page "https://example.org/")
  102. (synopsis "Some example package")
  103. (description "This package is just an example.")
  104. (license license:bsd-3))
  105. (string=? (bytevector->nix-base32-string
  106. test-source-hash)
  107. hash))
  108. (x
  109. (pk 'fail x #f))))))
  110. ;; Test the opam file parser
  111. ;; We fold over some test cases. Each case is a pair of the string to parse and the
  112. ;; expected result.
  113. (define (test-opam-syntax name pattern test-cases)
  114. (test-assert name
  115. (fold (lambda (test acc)
  116. (display test) (newline)
  117. (match test
  118. ((str . expected)
  119. (and acc
  120. (let ((result (peg:tree (match-pattern pattern str))))
  121. (if (equal? result expected)
  122. #t
  123. (pk 'fail (list str result expected) #f)))))))
  124. #t test-cases)))
  125. (test-opam-syntax
  126. "parse-strings" string-pat
  127. '(("" . #f)
  128. ("\"hello\"" . (string-pat "hello"))
  129. ("\"hello world\"" . (string-pat "hello world"))
  130. ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
  131. ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
  132. ("\"今日は\"" . (string-pat "今日は"))))
  133. (test-opam-syntax
  134. "parse-multiline-strings" multiline-string
  135. '(("" . #f)
  136. ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
  137. ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
  138. ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!"))))
  139. (test-opam-syntax
  140. "parse-lists" list-pat
  141. '(("" . #f)
  142. ("[]" . list-pat)
  143. ("[make]" . (list-pat (var "make")))
  144. ("[\"make\"]" . (list-pat (string-pat "make")))
  145. ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
  146. ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c")))
  147. ;; complex lists
  148. ("[(a & b)]" . (list-pat (choice-pat (group-pat (var "a") (var "b")))))
  149. ("[(a | b & c)]" . (list-pat (choice-pat (var "a") (group-pat (var "b") (var "c")))))
  150. ("[a (b | c) d]" . (list-pat (var "a") (choice-pat (var "b") (var "c")) (var "d")))))
  151. (test-opam-syntax
  152. "parse-dicts" dict
  153. '(("" . #f)
  154. ("{}" . dict)
  155. ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
  156. ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d"))))))
  157. (test-opam-syntax
  158. "parse-conditions" condition
  159. '(("" . #f)
  160. ("{}" . #f)
  161. ("{build}" . (condition-var "build"))
  162. ("{>= \"0.2.0\"}" . (condition-greater-or-equal
  163. (condition-string "0.2.0")))
  164. ("{>= \"0.2.0\" & test}" . (condition-and
  165. (condition-greater-or-equal
  166. (condition-string "0.2.0"))
  167. (condition-var "test")))
  168. ("{>= \"0.2.0\" | build}" . (condition-or
  169. (condition-greater-or-equal
  170. (condition-string "0.2.0"))
  171. (condition-var "build")))
  172. ("{ = \"1.0+beta19\" }" . (condition-eq
  173. (condition-string "1.0+beta19")))))
  174. (test-opam-syntax
  175. "parse-comment" list-pat
  176. '(("" . #f)
  177. ("[#comment\n]" . list-pat)))
  178. (test-end "opam")