opam.scm 7.3 KB

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