ui.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  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-ui)
  19. #:use-module (guix ui)
  20. #:use-module (guix profiles)
  21. #:use-module (guix store)
  22. #:use-module (guix derivations)
  23. #:use-module ((gnu packages) #:select (specification->package))
  24. #:use-module (guix tests)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-19)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-64)
  30. #:use-module (ice-9 regex))
  31. ;; Test the (guix ui) module.
  32. (define %paragraph
  33. "GNU Guile is an implementation of the Scheme programming language, with
  34. support for many SRFIs, packaged for use in a wide variety of environments.
  35. In addition to implementing the R5RS Scheme standard and a large subset of
  36. R6RS, Guile includes a module system, full access to POSIX system calls,
  37. networking support, multiple threads, dynamic linking, a foreign function call
  38. interface, and powerful string processing.")
  39. (define guile-1.8.8
  40. (manifest-entry
  41. (name "guile")
  42. (version "1.8.8")
  43. (item "/gnu/store/...")
  44. (output "out")))
  45. (define guile-2.0.9
  46. (manifest-entry
  47. (name "guile")
  48. (version "2.0.9")
  49. (item "/gnu/store/...")
  50. (output "out")))
  51. (test-begin "ui")
  52. (test-assert "fill-paragraph"
  53. (every (lambda (column)
  54. (every (lambda (width)
  55. (every (lambda (line)
  56. (<= (string-length line) width))
  57. (string-split (fill-paragraph %paragraph
  58. width column)
  59. #\newline)))
  60. '(15 30 35 40 45 50 60 70 80 90 100)))
  61. '(0 5)))
  62. (test-assert "fill-paragraph, consecutive newlines"
  63. (every (lambda (width)
  64. (any (lambda (line)
  65. (string-prefix? "When STR" line))
  66. (string-split
  67. (fill-paragraph (procedure-documentation fill-paragraph)
  68. width)
  69. #\newline)))
  70. '(15 20 25 30 40 50 60)))
  71. (test-equal "fill-paragraph, large unbreakable word"
  72. '("Here is a" "very-very-long-word"
  73. "and that's" "it.")
  74. (string-split
  75. (fill-paragraph "Here is a very-very-long-word and that's it."
  76. 10)
  77. #\newline))
  78. (test-equal "fill-paragraph, two spaces after period"
  79. "First line. Second line"
  80. (fill-paragraph "First line.
  81. Second line" 24))
  82. (test-equal "package-description-string vs. Unicode"
  83. "b•ll•t\n\n" ;see <http://bugs.gnu.org/21536>
  84. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  85. (package-description-string
  86. (dummy-package "foo" (description "b•ll•t")))))
  87. (test-equal "package-specification->name+version+output"
  88. '(("guile" #f "out")
  89. ("guile" "2.0.9" "out")
  90. ("guile" #f "debug")
  91. ("guile" "2.0.9" "debug")
  92. ("guile-cairo" "1.4.1" "out"))
  93. (map (lambda (spec)
  94. (call-with-values
  95. (lambda ()
  96. (package-specification->name+version+output spec))
  97. list))
  98. '("guile"
  99. "guile@2.0.9"
  100. "guile:debug"
  101. "guile@2.0.9:debug"
  102. "guile-cairo@1.4.1")))
  103. (test-equal "integer"
  104. '(1)
  105. (string->generations "1"))
  106. (test-equal "comma-separated integers"
  107. '(3 7 1 4 6)
  108. (string->generations "3,7,1,4,6"))
  109. (test-equal "closed range"
  110. '(4 5 6 7 8 9 10 11 12)
  111. (string->generations "4..12"))
  112. (test-equal "closed range, equal endpoints"
  113. '(3)
  114. (string->generations "3..3"))
  115. (test-equal "indefinite end range"
  116. '(>= 7)
  117. (string->generations "7.."))
  118. (test-equal "indefinite start range"
  119. '(<= 42)
  120. (string->generations "..42"))
  121. (test-equal "integer, char"
  122. #f
  123. (string->generations "a"))
  124. (test-equal "comma-separated integers, consecutive comma"
  125. #f
  126. (string->generations "1,,2"))
  127. (test-equal "comma-separated integers, trailing comma"
  128. #f
  129. (string->generations "1,2,"))
  130. (test-equal "comma-separated integers, chars"
  131. #f
  132. (string->generations "a,b"))
  133. (test-equal "closed range, start > end"
  134. #f
  135. (string->generations "9..2"))
  136. (test-equal "closed range, chars"
  137. #f
  138. (string->generations "a..b"))
  139. (test-equal "indefinite end range, char"
  140. #f
  141. (string->generations "a.."))
  142. (test-equal "indefinite start range, char"
  143. #f
  144. (string->generations "..a"))
  145. (test-equal "duration, 1 day"
  146. (make-time time-duration 0 (* 3600 24))
  147. (string->duration "1d"))
  148. (test-equal "duration, 1 week"
  149. (make-time time-duration 0 (* 3600 24 7))
  150. (string->duration "1w"))
  151. (test-equal "duration, 1 month"
  152. (make-time time-duration 0 (* 3600 24 30))
  153. (string->duration "1m"))
  154. (test-equal "duration, 1 week == 7 days"
  155. (string->duration "1w")
  156. (string->duration "7d"))
  157. (test-equal "duration, 1 month == 30 days"
  158. (string->duration "1m")
  159. (string->duration "30d"))
  160. (test-equal "duration, 2 hours"
  161. 7200
  162. (time-second (string->duration "2h")))
  163. (test-equal "duration, 1 second"
  164. (make-time time-duration 0 1)
  165. (string->duration "1s"))
  166. (test-equal "duration, integer"
  167. #f
  168. (string->duration "1"))
  169. (test-equal "duration, char"
  170. #f
  171. (string->duration "d"))
  172. (test-equal "size->number, bytes"
  173. 42
  174. (size->number "42"))
  175. (test-equal "size->number, MiB"
  176. (* 42 (expt 2 20))
  177. (size->number "42MiB"))
  178. (test-equal "size->number, GiB"
  179. (* 3 (expt 2 30))
  180. (size->number "3GiB"))
  181. (test-equal "size->number, 1.2GiB"
  182. (inexact->exact (round (* 1.2 (expt 2 30))))
  183. (size->number "1.2GiB"))
  184. (test-equal "size->number, 1T"
  185. (expt 2 40)
  186. (size->number "1T"))
  187. (test-assert "size->number, invalid unit"
  188. (catch 'quit
  189. (lambda ()
  190. (size->number "9X"))
  191. (lambda args
  192. #t)))
  193. (test-equal "show-what-to-build, zero outputs"
  194. ""
  195. (with-store store
  196. (let ((drv (derivation store "zero" "/bin/sh" '()
  197. #:outputs '())))
  198. (with-error-to-string
  199. (lambda ()
  200. ;; This should print nothing.
  201. (show-what-to-build store (list drv)))))))
  202. (test-assert "show-manifest-transaction"
  203. (let* ((m (manifest (list guile-1.8.8)))
  204. (t (manifest-transaction (install (list guile-2.0.9)))))
  205. (with-store store
  206. (and (string-match "guile\t1.8.8 → 2.0.9"
  207. (with-fluids ((%default-port-encoding "UTF-8"))
  208. (with-error-to-string
  209. (lambda ()
  210. (show-manifest-transaction store m t)))))
  211. (string-match "guile\t1.8.8 -> 2.0.9"
  212. (with-error-to-string
  213. (lambda ()
  214. ;; In Guile 2.2, %DEFAULT-PORT-ENCODING doesn't
  215. ;; influence the encoding of string ports.
  216. (set-port-encoding! (current-error-port)
  217. "ISO-8859-1")
  218. (show-manifest-transaction store m t))))))))
  219. (test-assert "package-relevance"
  220. (let ((guile (specification->package "guile"))
  221. (gcrypt (specification->package "guile-gcrypt"))
  222. (go (specification->package "go"))
  223. (gnugo (specification->package "gnugo"))
  224. (libb2 (specification->package "libb2"))
  225. (rx (cut make-regexp <> regexp/icase))
  226. (>0 (cut > <> 0))
  227. (=0 zero?))
  228. (and (>0 (package-relevance guile
  229. (map rx '("scheme"))))
  230. (>0 (package-relevance guile
  231. (map rx '("scheme" "implementation"))))
  232. (>0 (package-relevance gcrypt
  233. (map rx '("guile" "crypto"))))
  234. (=0 (package-relevance guile
  235. (map rx '("guile" "crypto"))))
  236. (>0 (package-relevance go
  237. (map rx '("go"))))
  238. (=0 (package-relevance go
  239. (map rx '("go" "game"))))
  240. (>0 (package-relevance gnugo
  241. (map rx '("go" "game"))))
  242. (>0 (package-relevance libb2
  243. (map rx '("crypto" "library")))))))
  244. (test-end "ui")