opam.scm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 utils) url-fetch
  70. (lambda (url file-name)
  71. (match url
  72. ("https://example.org/foo-1.0.0.tar.gz"
  73. (begin
  74. (mkdir-p "foo-1.0.0")
  75. (system* "tar" "czvf" file-name "foo-1.0.0/")
  76. (delete-file-recursively "foo-1.0.0")
  77. (set! test-source-hash
  78. (call-with-input-file file-name port-sha256))))
  79. (_ (error "Unexpected URL: " url)))))
  80. (let ((my-package (string-append test-repo
  81. "/packages/foo/foo.1.0.0")))
  82. (mkdir-p my-package)
  83. (with-output-to-file (string-append my-package "/opam")
  84. (lambda _
  85. (format #t "~a" test-opam-file))))
  86. (match (opam->guix-package "foo" #:repo (list test-repo))
  87. (('package
  88. ('name "ocaml-foo")
  89. ('version "1.0.0")
  90. ('source ('origin
  91. ('method 'url-fetch)
  92. ('uri "https://example.org/foo-1.0.0.tar.gz")
  93. ('sha256
  94. ('base32
  95. (? string? hash)))))
  96. ('build-system 'ocaml-build-system)
  97. ('propagated-inputs ('list 'ocaml-zarith))
  98. ('native-inputs ('list 'ocaml-alcotest 'ocamlbuild))
  99. ('home-page "https://example.org/")
  100. ('synopsis "Some example package")
  101. ('description "This package is just an example.")
  102. ('license 'license:bsd-3))
  103. (string=? (bytevector->nix-base32-string
  104. test-source-hash)
  105. hash))
  106. (x
  107. (pk 'fail x #f)))))
  108. ;; Test the opam file parser
  109. ;; We fold over some test cases. Each case is a pair of the string to parse and the
  110. ;; expected result.
  111. (define (test-opam-syntax name pattern test-cases)
  112. (test-assert name
  113. (fold (lambda (test acc)
  114. (display test) (newline)
  115. (match test
  116. ((str . expected)
  117. (and acc
  118. (let ((result (peg:tree (match-pattern pattern str))))
  119. (if (equal? result expected)
  120. #t
  121. (pk 'fail (list str result expected) #f)))))))
  122. #t test-cases)))
  123. (test-opam-syntax
  124. "parse-strings" string-pat
  125. '(("" . #f)
  126. ("\"hello\"" . (string-pat "hello"))
  127. ("\"hello world\"" . (string-pat "hello world"))
  128. ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
  129. ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
  130. ("\"今日は\"" . (string-pat "今日は"))))
  131. (test-opam-syntax
  132. "parse-multiline-strings" multiline-string
  133. '(("" . #f)
  134. ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
  135. ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
  136. ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!"))))
  137. (test-opam-syntax
  138. "parse-lists" list-pat
  139. '(("" . #f)
  140. ("[]" . list-pat)
  141. ("[make]" . (list-pat (var "make")))
  142. ("[\"make\"]" . (list-pat (string-pat "make")))
  143. ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
  144. ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c")))
  145. ;; complex lists
  146. ("[(a & b)]" . (list-pat (choice-pat (group-pat (var "a") (var "b")))))
  147. ("[(a | b & c)]" . (list-pat (choice-pat (var "a") (group-pat (var "b") (var "c")))))
  148. ("[a (b | c) d]" . (list-pat (var "a") (choice-pat (var "b") (var "c")) (var "d")))))
  149. (test-opam-syntax
  150. "parse-dicts" dict
  151. '(("" . #f)
  152. ("{}" . dict)
  153. ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
  154. ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d"))))))
  155. (test-opam-syntax
  156. "parse-conditions" condition
  157. '(("" . #f)
  158. ("{}" . #f)
  159. ("{build}" . (condition-var "build"))
  160. ("{>= \"0.2.0\"}" . (condition-greater-or-equal
  161. (condition-string "0.2.0")))
  162. ("{>= \"0.2.0\" & test}" . (condition-and
  163. (condition-greater-or-equal
  164. (condition-string "0.2.0"))
  165. (condition-var "test")))
  166. ("{>= \"0.2.0\" | build}" . (condition-or
  167. (condition-greater-or-equal
  168. (condition-string "0.2.0"))
  169. (condition-var "build")))
  170. ("{ = \"1.0+beta19\" }" . (condition-eq
  171. (condition-string "1.0+beta19")))))
  172. (test-opam-syntax
  173. "parse-comment" list-pat
  174. '(("" . #f)
  175. ("[#comment\n]" . list-pat)))
  176. (test-end "opam")