opam.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. (mock ((guix import opam) get-opam-repository
  83. (lambda _
  84. test-repo))
  85. (match (opam->guix-package "foo")
  86. (('package
  87. ('name "ocaml-foo")
  88. ('version "1.0.0")
  89. ('source ('origin
  90. ('method 'url-fetch)
  91. ('uri "https://example.org/foo-1.0.0.tar.gz")
  92. ('sha256
  93. ('base32
  94. (? string? hash)))))
  95. ('build-system 'ocaml-build-system)
  96. ('propagated-inputs
  97. ('quasiquote
  98. (("ocaml-zarith" ('unquote 'ocaml-zarith)))))
  99. ('native-inputs
  100. ('quasiquote
  101. (("ocaml-alcotest" ('unquote 'ocaml-alcotest))
  102. ("ocamlbuild" ('unquote 'ocamlbuild)))))
  103. ('home-page "https://example.org/")
  104. ('synopsis "Some example package")
  105. ('description "This package is just an example.")
  106. ('license #f))
  107. (string=? (bytevector->nix-base32-string
  108. test-source-hash)
  109. hash))
  110. (x
  111. (pk 'fail x #f))))))
  112. ;; Test the opam file parser
  113. ;; We fold over some test cases. Each case is a pair of the string to parse and the
  114. ;; expected result.
  115. (test-assert "parse-strings"
  116. (fold (lambda (test acc)
  117. (display test) (newline)
  118. (and acc
  119. (let ((result (peg:tree (match-pattern (@@ (guix import opam) string-pat) (car test)))))
  120. (if (equal? result (cdr test))
  121. #t
  122. (pk 'fail (list (car test) result (cdr test)) #f)))))
  123. #t '(("" . #f)
  124. ("\"hello\"" . (string-pat "hello"))
  125. ("\"hello world\"" . (string-pat "hello world"))
  126. ("\"The dreaded \\\"é\\\"\"" . (string-pat "The dreaded \"é\""))
  127. ("\"Have some \\\\\\\\ :)\"" . (string-pat "Have some \\\\ :)"))
  128. ("\"今日は\"" . (string-pat "今日は")))))
  129. (test-assert "parse-multiline-strings"
  130. (fold (lambda (test acc)
  131. (display test) (newline)
  132. (and acc
  133. (let ((result (peg:tree (match-pattern (@@ (guix import opam) multiline-string) (car test)))))
  134. (if (equal? result (cdr test))
  135. #t
  136. (pk 'fail (list (car test) result (cdr test)) #f)))))
  137. #t '(("" . #f)
  138. ("\"\"\"hello\"\"\"" . (multiline-string "hello"))
  139. ("\"\"\"hello \"world\"!\"\"\"" . (multiline-string "hello \"world\"!"))
  140. ("\"\"\"hello \"\"world\"\"!\"\"\"" . (multiline-string "hello \"\"world\"\"!")))))
  141. (test-assert "parse-lists"
  142. (fold (lambda (test acc)
  143. (and acc
  144. (let ((result (peg:tree (match-pattern (@@ (guix import opam) list-pat) (car test)))))
  145. (if (equal? result (cdr test))
  146. #t
  147. (pk 'fail (list (car test) result (cdr test)) #f)))))
  148. #t '(("" . #f)
  149. ("[]" . list-pat)
  150. ("[make]" . (list-pat (var "make")))
  151. ("[\"make\"]" . (list-pat (string-pat "make")))
  152. ("[\n a\n b\n c]" . (list-pat (var "a") (var "b") (var "c")))
  153. ("[a b \"c\"]" . (list-pat (var "a") (var "b") (string-pat "c"))))))
  154. (test-assert "parse-dicts"
  155. (fold (lambda (test acc)
  156. (and acc
  157. (let ((result (peg:tree (match-pattern (@@ (guix import opam) dict) (car test)))))
  158. (if (equal? result (cdr test))
  159. #t
  160. (pk 'fail (list (car test) result (cdr test)) #f)))))
  161. #t '(("" . #f)
  162. ("{}" . dict)
  163. ("{a: \"b\"}" . (dict (record "a" (string-pat "b"))))
  164. ("{a: \"b\"\nc: \"d\"}" . (dict (record "a" (string-pat "b")) (record "c" (string-pat "d")))))))
  165. (test-assert "parse-conditions"
  166. (fold (lambda (test acc)
  167. (and acc
  168. (let ((result (peg:tree (match-pattern (@@ (guix import opam) condition) (car test)))))
  169. (if (equal? result (cdr test))
  170. #t
  171. (pk 'fail (list (car test) result (cdr test)) #f)))))
  172. #t '(("" . #f)
  173. ("{}" . #f)
  174. ("{build}" . (condition-var "build"))
  175. ("{>= \"0.2.0\"}" . (condition-greater-or-equal
  176. (condition-string "0.2.0")))
  177. ("{>= \"0.2.0\" & test}" . (condition-and
  178. (condition-greater-or-equal
  179. (condition-string "0.2.0"))
  180. (condition-var "test")))
  181. ("{>= \"0.2.0\" | build}" . (condition-or
  182. (condition-greater-or-equal
  183. (condition-string "0.2.0"))
  184. (condition-var "build")))
  185. ("{ = \"1.0+beta19\" }" . (condition-eq
  186. (condition-string "1.0+beta19"))))))
  187. (test-end "opam")