opam.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  97. (? string? hash)))))
  98. ('build-system 'ocaml-build-system)
  99. ('propagated-inputs ('list 'ocaml-zarith))
  100. ('native-inputs
  101. ('list 'ocaml-alcotest 'ocamlbuild))
  102. ('home-page "https://example.org/")
  103. ('synopsis "Some example package")
  104. ('description "This package is just an example.")
  105. ('license 'license:bsd-3))
  106. (string=? (bytevector->nix-base32-string
  107. test-source-hash)
  108. hash))
  109. (x
  110. (pk 'fail x #f))))))
  111. ;; Test the opam file parser
  112. ;; We fold over some test cases. Each case is a pair of the string to parse and the
  113. ;; expected result.
  114. (define (test-opam-syntax name pattern test-cases)
  115. (test-assert name
  116. (fold (lambda (test acc)
  117. (display test) (newline)
  118. (match test
  119. ((str . expected)
  120. (and acc
  121. (let ((result (peg:tree (match-pattern pattern str))))
  122. (if (equal? result expected)
  123. #t
  124. (pk 'fail (list str result expected) #f)))))))
  125. #t test-cases)))
  126. (test-opam-syntax
  127. "parse-strings" string-pat
  128. '(("" . #f)
  129. ("\"hello\"" . (string-pat "hello"))
  130. ("\"hello world\"" . (string-pat "hello world"))
  131. ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
  132. ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
  133. ("\"今日は\"" . (string-pat "今日は"))))
  134. (test-opam-syntax
  135. "parse-multiline-strings" multiline-string
  136. '(("" . #f)
  137. ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
  138. ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
  139. ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!"))))
  140. (test-opam-syntax
  141. "parse-lists" list-pat
  142. '(("" . #f)
  143. ("[]" . list-pat)
  144. ("[make]" . (list-pat (var "make")))
  145. ("[\"make\"]" . (list-pat (string-pat "make")))
  146. ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
  147. ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c")))
  148. ;; complex lists
  149. ("[(a & b)]" . (list-pat (choice-pat (group-pat (var "a") (var "b")))))
  150. ("[(a | b & c)]" . (list-pat (choice-pat (var "a") (group-pat (var "b") (var "c")))))
  151. ("[a (b | c) d]" . (list-pat (var "a") (choice-pat (var "b") (var "c")) (var "d")))))
  152. (test-opam-syntax
  153. "parse-dicts" dict
  154. '(("" . #f)
  155. ("{}" . dict)
  156. ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
  157. ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d"))))))
  158. (test-opam-syntax
  159. "parse-conditions" condition
  160. '(("" . #f)
  161. ("{}" . #f)
  162. ("{build}" . (condition-var "build"))
  163. ("{>= \"0.2.0\"}" . (condition-greater-or-equal
  164. (condition-string "0.2.0")))
  165. ("{>= \"0.2.0\" & test}" . (condition-and
  166. (condition-greater-or-equal
  167. (condition-string "0.2.0"))
  168. (condition-var "test")))
  169. ("{>= \"0.2.0\" | build}" . (condition-or
  170. (condition-greater-or-equal
  171. (condition-string "0.2.0"))
  172. (condition-var "build")))
  173. ("{ = \"1.0+beta19\" }" . (condition-eq
  174. (condition-string "1.0+beta19")))))
  175. (test-opam-syntax
  176. "parse-comment" list-pat
  177. '(("" . #f)
  178. ("[#comment\n]" . list-pat)))
  179. (test-end "opam")