ui.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013-2017, 2019-2020, 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-ui)
  20. #:use-module (guix ui)
  21. #:use-module (guix profiles)
  22. #:use-module (guix store)
  23. #:use-module (guix derivations)
  24. #:use-module ((gnu packages) #:select (specification->package))
  25. #:use-module (guix tests)
  26. #:use-module (guix utils)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-11)
  29. #:use-module (srfi srfi-19)
  30. #:use-module (srfi srfi-26)
  31. #:use-module (srfi srfi-64)
  32. #:use-module (ice-9 regex))
  33. ;; Test the (guix ui) module.
  34. (define %paragraph
  35. "GNU Guile is an implementation of the Scheme programming language, with
  36. support for many SRFIs, packaged for use in a wide variety of environments.
  37. In addition to implementing the R5RS Scheme standard and a large subset of
  38. R6RS, Guile includes a module system, full access to POSIX system calls,
  39. networking support, multiple threads, dynamic linking, a foreign function call
  40. interface, and powerful string processing.")
  41. (define guile-1.8.8
  42. (manifest-entry
  43. (name "guile")
  44. (version "1.8.8")
  45. (item "/gnu/store/...")
  46. (output "out")))
  47. (define guile-2.0.9
  48. (manifest-entry
  49. (name "guile")
  50. (version "2.0.9")
  51. (item "/gnu/store/...")
  52. (output "out")))
  53. (test-begin "ui")
  54. (test-assert "fill-paragraph"
  55. (every (lambda (column)
  56. (every (lambda (width)
  57. (every (lambda (line)
  58. (<= (string-length line) width))
  59. (string-split (fill-paragraph %paragraph
  60. width column)
  61. #\newline)))
  62. '(15 30 35 40 45 50 60 70 80 90 100)))
  63. '(0 5)))
  64. (test-assert "fill-paragraph, consecutive newlines"
  65. (every (lambda (width)
  66. (any (lambda (line)
  67. (string-prefix? "When STR" line))
  68. (string-split
  69. (fill-paragraph (procedure-documentation fill-paragraph)
  70. width)
  71. #\newline)))
  72. '(15 20 25 30 40 50 60)))
  73. (test-equal "fill-paragraph, large unbreakable word"
  74. '("Here is a" "very-very-long-word"
  75. "and that's" "it.")
  76. (string-split
  77. (fill-paragraph "Here is a very-very-long-word and that's it."
  78. 10)
  79. #\newline))
  80. (test-equal "fill-paragraph, two spaces after period"
  81. "First line. Second line"
  82. (fill-paragraph "First line.
  83. Second line" 24))
  84. (test-equal "package-description-string vs. Unicode"
  85. "b•ll•t\n\n" ;see <http://bugs.gnu.org/21536>
  86. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  87. (package-description-string
  88. (dummy-package "foo" (description "b•ll•t")))))
  89. (test-equal "package-specification->name+version+output"
  90. '(("guile" #f "out")
  91. ("guile" "2.0.9" "out")
  92. ("guile" #f "debug")
  93. ("guile" "2.0.9" "debug")
  94. ("guile-cairo" "1.4.1" "out"))
  95. (map (lambda (spec)
  96. (call-with-values
  97. (lambda ()
  98. (package-specification->name+version+output spec))
  99. list))
  100. '("guile"
  101. "guile@2.0.9"
  102. "guile:debug"
  103. "guile@2.0.9:debug"
  104. "guile-cairo@1.4.1")))
  105. (test-equal "integer"
  106. '(1)
  107. (string->generations "1"))
  108. (test-equal "comma-separated integers"
  109. '(3 7 1 4 6)
  110. (string->generations "3,7,1,4,6"))
  111. (test-equal "closed range"
  112. '(4 5 6 7 8 9 10 11 12)
  113. (string->generations "4..12"))
  114. (test-equal "closed range, equal endpoints"
  115. '(3)
  116. (string->generations "3..3"))
  117. (test-equal "indefinite end range"
  118. '(>= 7)
  119. (string->generations "7.."))
  120. (test-equal "indefinite start range"
  121. '(<= 42)
  122. (string->generations "..42"))
  123. (test-equal "integer, char"
  124. #f
  125. (string->generations "a"))
  126. (test-equal "comma-separated integers, consecutive comma"
  127. #f
  128. (string->generations "1,,2"))
  129. (test-equal "comma-separated integers, trailing comma"
  130. #f
  131. (string->generations "1,2,"))
  132. (test-equal "comma-separated integers, chars"
  133. #f
  134. (string->generations "a,b"))
  135. (test-equal "closed range, start > end"
  136. #f
  137. (string->generations "9..2"))
  138. (test-equal "closed range, chars"
  139. #f
  140. (string->generations "a..b"))
  141. (test-equal "indefinite end range, char"
  142. #f
  143. (string->generations "a.."))
  144. (test-equal "indefinite start range, char"
  145. #f
  146. (string->generations "..a"))
  147. (test-equal "duration, 1 day"
  148. (make-time time-duration 0 (* 3600 24))
  149. (string->duration "1d"))
  150. (test-equal "duration, 1 week"
  151. (make-time time-duration 0 (* 3600 24 7))
  152. (string->duration "1w"))
  153. (test-equal "duration, 1 month"
  154. (make-time time-duration 0 (* 3600 24 30))
  155. (string->duration "1m"))
  156. (test-equal "duration, 1 week == 7 days"
  157. (string->duration "1w")
  158. (string->duration "7d"))
  159. (test-equal "duration, 1 month == 30 days"
  160. (string->duration "1m")
  161. (string->duration "30d"))
  162. (test-equal "duration, 2 hours"
  163. 7200
  164. (time-second (string->duration "2h")))
  165. (test-equal "duration, 1 second"
  166. (make-time time-duration 0 1)
  167. (string->duration "1s"))
  168. (test-equal "duration, integer"
  169. #f
  170. (string->duration "1"))
  171. (test-equal "duration, char"
  172. #f
  173. (string->duration "d"))
  174. (test-equal "size->number, bytes"
  175. 42
  176. (size->number "42"))
  177. (test-equal "size->number, MiB"
  178. (* 42 (expt 2 20))
  179. (size->number "42MiB"))
  180. (test-equal "size->number, GiB"
  181. (* 3 (expt 2 30))
  182. (size->number "3GiB"))
  183. (test-equal "size->number, 1.2GiB"
  184. (inexact->exact (round (* 1.2 (expt 2 30))))
  185. (size->number "1.2GiB"))
  186. (test-equal "size->number, 1T"
  187. (expt 2 40)
  188. (size->number "1T"))
  189. (test-equal "size->number, 1.M"
  190. (expt 2 20)
  191. (size->number "1.M"))
  192. (test-assert "size->number, invalid unit"
  193. (catch 'quit
  194. (lambda ()
  195. (size->number "9X"))
  196. (lambda args
  197. #t)))
  198. (test-equal "show-what-to-build, zero outputs"
  199. ""
  200. (with-store store
  201. (let ((drv (derivation store "zero" "/bin/sh" '()
  202. #:outputs '())))
  203. (with-error-to-string
  204. (lambda ()
  205. ;; This should print nothing.
  206. (show-what-to-build store (list drv)))))))
  207. (test-assert "show-manifest-transaction"
  208. (let* ((m (manifest (list guile-1.8.8)))
  209. (t (manifest-transaction (install (list guile-2.0.9)))))
  210. (with-store store
  211. (and (string-match "guile +1.8.8 → 2.0.9"
  212. (with-fluids ((%default-port-encoding "UTF-8"))
  213. (with-error-to-string
  214. (lambda ()
  215. (show-manifest-transaction store m t)))))
  216. (string-match "guile +1.8.8 -> 2.0.9"
  217. (with-error-to-string
  218. (lambda ()
  219. ;; In Guile 2.2, %DEFAULT-PORT-ENCODING doesn't
  220. ;; influence the encoding of string ports.
  221. (set-port-encoding! (current-error-port)
  222. "ISO-8859-1")
  223. (show-manifest-transaction store m t))))))))
  224. (test-assert "package-relevance"
  225. (let ((guile (specification->package "guile"))
  226. (gcrypt (specification->package "guile-gcrypt"))
  227. (go (specification->package "go"))
  228. (gnugo (specification->package "gnugo"))
  229. (libb2 (specification->package "libb2"))
  230. (rx (cut make-regexp <> regexp/icase))
  231. (>0 (cut > <> 0))
  232. (=0 zero?))
  233. (and (>0 (package-relevance guile
  234. (map rx '("scheme"))))
  235. (>0 (package-relevance guile
  236. (map rx '("scheme" "implementation"))))
  237. (>0 (package-relevance gcrypt
  238. (map rx '("guile" "crypto"))))
  239. (=0 (package-relevance guile
  240. (map rx '("guile" "crypto"))))
  241. (>0 (package-relevance go
  242. (map rx '("go"))))
  243. (=0 (package-relevance go
  244. (map rx '("go" "game"))))
  245. (>0 (package-relevance gnugo
  246. (map rx '("go" "game"))))
  247. (>0 (package-relevance libb2
  248. (map rx '("crypto" "library")))))))
  249. (test-assert "package-relevance and upstream name"
  250. ;; https://issues.guix.gnu.org/58136
  251. (let ((ggplot2 (specification->package "r-ggplot2"))
  252. (ggstance (specification->package "r-ggstance"))
  253. (rx (make-regexp "ggplot2" regexp/icase)))
  254. (> (package-relevance ggplot2 (list rx))
  255. (package-relevance ggstance (list rx))
  256. 0)))
  257. (define (make-empty-file directory file)
  258. ;; Create FILE in DIRECTORY.
  259. (close-port (open-output-file (in-vicinity directory file))))
  260. (define (assert-equals-find-available-pager expected)
  261. ;; Use 'with-paginated-output-port' and return true if it invoked EXPECTED.
  262. (define used-command "")
  263. (mock ((ice-9 popen) open-pipe*
  264. (lambda (mode command . args)
  265. (unless (string-null? used-command)
  266. (error "open-pipe* should only be called once"))
  267. (set! used-command command)
  268. (%make-void-port "")))
  269. (mock ((ice-9 popen) close-pipe (const 'ok))
  270. (mock ((guix colors) isatty?* (const #t))
  271. (with-paginated-output-port port 'ok)
  272. (string=? expected used-command)))))
  273. (test-assert "find-available-pager, GUIX_PAGER takes precedence"
  274. (call-with-temporary-directory
  275. (lambda (dir)
  276. (with-environment-variables `(("PATH" ,dir)
  277. ("GUIX_PAGER" "guix-pager")
  278. ("PAGER" "pager"))
  279. (make-empty-file dir "less")
  280. (make-empty-file dir "more")
  281. (assert-equals-find-available-pager "guix-pager")))))
  282. (test-assert "find-available-pager, PAGER takes precedence"
  283. (call-with-temporary-directory
  284. (lambda (dir)
  285. (with-environment-variables `(("PATH" ,dir)
  286. ("GUIX_PAGER" #false)
  287. ("PAGER" "pager"))
  288. (make-empty-file dir "less")
  289. (make-empty-file dir "more")
  290. (assert-equals-find-available-pager "pager")))))
  291. (test-assert "find-available-pager, 'less' takes precedence"
  292. (call-with-temporary-directory
  293. (lambda (dir)
  294. (with-environment-variables `(("PATH" ,dir)
  295. ("GUIX_PAGER" #false)
  296. ("PAGER" #false))
  297. (make-empty-file dir "less")
  298. (make-empty-file dir "more")
  299. (assert-equals-find-available-pager (in-vicinity dir "less"))))))
  300. (test-assert "find-available-pager, 'more' takes precedence"
  301. (call-with-temporary-directory
  302. (lambda (dir)
  303. (with-environment-variables `(("PATH" ,dir)
  304. ("GUIX_PAGER" #false)
  305. ("PAGER" #false))
  306. (make-empty-file dir "more")
  307. (assert-equals-find-available-pager (in-vicinity dir "more"))))))
  308. (test-assert "find-available-pager, no pager"
  309. (call-with-temporary-directory
  310. (lambda (dir)
  311. (with-environment-variables `(("PATH" ,dir)
  312. ("GUIX_PAGER" #false)
  313. ("PAGER" #false))
  314. (assert-equals-find-available-pager "")))))
  315. (test-end "ui")