elm.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Philip McGrath <philip@philipmcgrath.com>
  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-elm)
  19. #:use-module (guix build-system elm)
  20. #:use-module (guix import elm)
  21. #:use-module (guix base32)
  22. #:use-module (guix hash)
  23. #:use-module (guix utils)
  24. #:autoload (gcrypt hash) (hash-algorithm sha256)
  25. #:use-module (json)
  26. #:use-module (ice-9 match)
  27. #:use-module (srfi srfi-64))
  28. (test-begin "elm")
  29. (test-group "elm->package-name and infer-elm-package-name"
  30. (test-group "round trip"
  31. ;; Cases when our heuristics can find the upstream name.
  32. (define-syntax-rule (test-round-trip elm guix)
  33. (test-group elm
  34. (test-equal "elm->package-name" guix
  35. (elm->package-name elm))
  36. (test-equal "infer-elm-package-name" elm
  37. (infer-elm-package-name guix))))
  38. (test-round-trip "elm/core" "elm-core")
  39. (test-round-trip "elm/html" "elm-html")
  40. (test-round-trip "elm-explorations/markdown" "elm-explorations-markdown")
  41. (test-round-trip "elm-explorations/test" "elm-explorations-test")
  42. (test-round-trip "elm-explorations/foo-bar" "elm-explorations-foo-bar")
  43. (test-round-trip "elm/explorations" "elm-explorations")
  44. (test-round-trip "terezka/intervals" "elm-terezka-intervals")
  45. (test-round-trip "justinmimbs/time-extra" "elm-justinmimbs-time-extra")
  46. (test-round-trip "danhandrea/elm-date-format"
  47. "elm-danhandrea-elm-date-format"))
  48. (test-group "upstream-name needed"
  49. ;; Upstream names that our heuristic can't infer. We still check that the
  50. ;; round-trip behavior of 'infer-elm-package-name' works as promised for
  51. ;; the hypothetical Elm name it doesn't infer.
  52. (define-syntax-rule (test-upstream-needed elm guix inferred)
  53. (test-group elm
  54. (test-equal "elm->package-name" guix
  55. (elm->package-name elm))
  56. (test-group "infer-elm-package-name"
  57. (test-equal "infers other name" inferred
  58. (infer-elm-package-name guix))
  59. (test-equal "infered name round-trips" guix
  60. (elm->package-name inferred)))))
  61. (test-upstream-needed "elm/virtual-dom"
  62. "elm-virtual-dom"
  63. "virtual/dom")
  64. (test-upstream-needed "elm/project-metadata-utils"
  65. "elm-project-metadata-utils"
  66. "project/metadata-utils")
  67. (test-upstream-needed "explorations/foo"
  68. "elm-explorations-foo"
  69. "elm-explorations/foo")
  70. (test-upstream-needed "explorations/foo-bar"
  71. "elm-explorations-foo-bar"
  72. "elm-explorations/foo-bar")
  73. (test-upstream-needed "explorations-central/foo"
  74. "elm-explorations-central-foo"
  75. "elm-explorations/central-foo")
  76. (test-upstream-needed "explorations-central/foo-bar"
  77. "elm-explorations-central-foo-bar"
  78. "elm-explorations/central-foo-bar")
  79. (test-upstream-needed "elm-xyz/foo"
  80. "elm-xyz-foo"
  81. "xyz/foo")
  82. (test-upstream-needed "elm-xyz/foo-bar"
  83. "elm-xyz-foo-bar"
  84. "xyz/foo-bar")
  85. (test-upstream-needed "elm-explorations-xyz/foo"
  86. "elm-explorations-xyz-foo"
  87. "elm-explorations/xyz-foo")
  88. (test-upstream-needed "elm-explorations-xyz/foo-bar"
  89. "elm-explorations-xyz-foo-bar"
  90. "elm-explorations/xyz-foo-bar"))
  91. (test-group "no inferred Elm name"
  92. ;; Cases that 'infer-elm-package-name' should not attempt to handle,
  93. ;; because 'elm->package-name' would never produce such names.
  94. (define-syntax-rule (test-not-inferred guix)
  95. (test-assert guix (not (infer-elm-package-name guix))))
  96. (test-not-inferred "elm")
  97. (test-not-inferred "guile")
  98. (test-not-inferred "gcc-toolchain")
  99. (test-not-inferred "font-adobe-source-sans-pro")))
  100. (define test-package-registry-json
  101. ;; we intentionally list versions in different orders here
  102. "{
  103. \"elm/core\": [\"1.0.0\", \"1.0.1\", \"1.0.2\", \"1.0.3\", \"1.0.4\"],
  104. \"elm-guix/demo\": [\"2.0.0\", \"3.0.0\", \"1.0.0\"]
  105. }")
  106. (define test-elm-core-json
  107. "{
  108. \"type\": \"package\",
  109. \"name\": \"elm/core\",
  110. \"summary\": \"Elm's standard libraries\",
  111. \"license\": \"BSD-3-Clause\",
  112. \"version\": \"1.0.4\",
  113. \"exposed-modules\": {
  114. \"Primitives\": [
  115. \"Basics\",
  116. \"String\",
  117. \"Char\",
  118. \"Bitwise\",
  119. \"Tuple\"
  120. ],
  121. \"Collections\": [
  122. \"List\",
  123. \"Dict\",
  124. \"Set\",
  125. \"Array\"
  126. ],
  127. \"Error Handling\": [
  128. \"Maybe\",
  129. \"Result\"
  130. ],
  131. \"Debug\": [
  132. \"Debug\"
  133. ],
  134. \"Effects\": [
  135. \"Platform.Cmd\",
  136. \"Platform.Sub\",
  137. \"Platform\",
  138. \"Process\",
  139. \"Task\"
  140. ]
  141. },
  142. \"elm-version\": \"0.19.0 <= v < 0.20.0\",
  143. \"dependencies\": {},
  144. \"test-dependencies\": {}
  145. }")
  146. (define test-elm-core-readme
  147. "# Core Libraries
  148. Every Elm project needs this package!
  149. It provides **basic functionality** like addition and subtraction as well as
  150. **data structures** like lists, dictionaries, and sets.")
  151. (define test-elm-guix-demo-json
  152. "{
  153. \"type\": \"package\",
  154. \"name\": \"elm-guix/demo\",
  155. \"summary\": \"A test for `(guix import elm)`\",
  156. \"license\": \"GPL-3.0-or-later\",
  157. \"version\": \"3.0.0\",
  158. \"exposed-modules\": [
  159. \"Guix.Demo\"
  160. ],
  161. \"elm-version\": \"0.19.0 <= v < 0.20.0\",
  162. \"dependencies\": {
  163. \"elm/core\": \"1.0.0 <= v < 2.0.0\"
  164. },
  165. \"test-dependencies\": {
  166. \"elm/json\": \"1.0.0 <= v < 2.0.0\"
  167. }
  168. }")
  169. (define test-elm-guix-demo-readme
  170. ;; intentionally left blank
  171. "")
  172. (define (directory-sha256 directory)
  173. "Returns the string representing the hash of DIRECTORY as would be used in a
  174. package definition."
  175. (bytevector->nix-base32-string
  176. (file-hash* directory
  177. #:algorithm (hash-algorithm sha256)
  178. #:recursive? #t)))
  179. (test-group "(guix import elm)"
  180. (call-with-temporary-directory
  181. (lambda (dir)
  182. ;; Initialize our fake git checkouts.
  183. (define elm-core-dir
  184. (string-append dir "/test-elm-core-1.0.4"))
  185. (define elm-guix-demo-dir
  186. (string-append dir "/test-elm-guix-demo-3.0.0"))
  187. (for-each (match-lambda
  188. ((dir json readme)
  189. (mkdir dir)
  190. (with-output-to-file (string-append dir "/elm.json")
  191. (lambda ()
  192. (display json)))
  193. (with-output-to-file (string-append dir "/README.md")
  194. (lambda ()
  195. (display readme)))))
  196. `((,elm-core-dir ,test-elm-core-json ,test-elm-core-readme)
  197. (,elm-guix-demo-dir
  198. ,test-elm-guix-demo-json
  199. ,test-elm-guix-demo-readme)))
  200. ;; Replace network resources with sample data.
  201. (parameterize ((%elm-package-registry
  202. (lambda ()
  203. (json-string->scm test-package-registry-json)))
  204. (%current-elm-checkout
  205. (lambda (name version)
  206. (match (list name version)
  207. (("elm/core" "1.0.4")
  208. elm-core-dir)
  209. (("elm-guix/demo" "3.0.0")
  210. elm-guix-demo-dir)))))
  211. (test-assert "(elm->guix-package \"elm/core\")"
  212. (match (elm->guix-package "elm/core")
  213. (`(package
  214. (name "elm-core")
  215. (version "1.0.4")
  216. (source (elm-package-origin
  217. "elm/core"
  218. version
  219. (base32 ,(? string? hash))))
  220. (build-system elm-build-system)
  221. (home-page
  222. "https://package.elm-lang.org/packages/elm/core/1.0.4")
  223. (synopsis "Elm's standard libraries")
  224. (description "Every Elm project needs this package!")
  225. (license license:bsd-3))
  226. (equal? (directory-sha256 elm-core-dir)
  227. hash))
  228. (x
  229. (raise-exception x))))
  230. (test-assert "(elm-recursive-import \"elm-guix/demo\")"
  231. (match (elm-recursive-import "elm-guix/demo")
  232. (`((package
  233. (name "elm-guix-demo")
  234. (version "3.0.0")
  235. (source (elm-package-origin
  236. "elm-guix/demo"
  237. version
  238. (base32 ,(? string? hash))))
  239. (build-system elm-build-system)
  240. (propagated-inputs
  241. ,'`(("elm-core" ,elm-core)))
  242. (inputs
  243. ,'`(("elm-json" ,elm-json)))
  244. (home-page
  245. "https://package.elm-lang.org/packages/elm-guix/demo/3.0.0")
  246. (synopsis "A test for `(guix import elm)`")
  247. (description
  248. "This package provides a test for `(guix import elm)`")
  249. (properties '((upstream-name . "elm-guix/demo")))
  250. (license license:gpl3+)))
  251. (equal? (directory-sha256 elm-guix-demo-dir)
  252. hash))
  253. (x
  254. (raise-exception x))))))))
  255. (test-end "elm")