utils.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
  4. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  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-utils)
  21. #:use-module ((guix config) #:select (%gzip))
  22. #:use-module (guix utils)
  23. #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
  24. #:use-module ((guix search-paths) #:select (string-tokenize*))
  25. #:use-module ((guix lzlib) #:select (lzlib-available?))
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-64)
  29. #:use-module (rnrs bytevectors)
  30. #:use-module (rnrs io ports)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 vlist))
  33. (define temp-file
  34. (string-append "t-utils-" (number->string (getpid))))
  35. (test-begin "utils")
  36. (test-assert "gnu-triplet->nix-system"
  37. (let ((samples '(("i586-gnu0.3" "i686-gnu")
  38. ("x86_64-unknown-linux-gnu" "x86_64-linux")
  39. ("i386-pc-linux-gnu" "i686-linux")
  40. ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
  41. ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
  42. ("i686-pc-cygwin" "i686-cygwin"))))
  43. (let-values (((gnu nix) (unzip2 samples)))
  44. (every (lambda (gnu nix)
  45. (equal? nix (gnu-triplet->nix-system gnu)))
  46. gnu nix))))
  47. (test-assert "package-name->name+version"
  48. (every (match-lambda
  49. ((name version)
  50. (let*-values (((full-name)
  51. (if version
  52. (string-append name "@" version)
  53. name))
  54. ((name* version*)
  55. (package-name->name+version full-name)))
  56. (and (equal? name* name)
  57. (equal? version* version)))))
  58. '(("foo" "0.9.1b")
  59. ("foo-14-bar" "320")
  60. ("foo-bar2" #f)
  61. ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
  62. ("nixpkgs" "1.0pre22125_a28fe19")
  63. ("gtk2" "2.38.0"))))
  64. (test-assert "guile-version>? 1.8"
  65. (guile-version>? "1.8"))
  66. (test-assert "guile-version>? 10.5"
  67. (not (guile-version>? "10.5")))
  68. (test-assert "version-prefix?"
  69. (and (version-prefix? "4.1" "4.1.2")
  70. (version-prefix? "4.1" "4.1")
  71. (not (version-prefix? "4.1" "4.16.2"))
  72. (not (version-prefix? "4.1" "4"))))
  73. (test-equal "string-tokenize*"
  74. '(("foo")
  75. ("foo" "bar" "baz")
  76. ("foo" "bar" "")
  77. ("foo" "bar" "baz"))
  78. (list (string-tokenize* "foo" ":")
  79. (string-tokenize* "foo;bar;baz" ";")
  80. (string-tokenize* "foo!bar!" "!")
  81. (string-tokenize* "foo+-+bar+-+baz" "+-+")))
  82. (test-equal "string-replace-substring"
  83. '("foo BAR! baz"
  84. "/gnu/store/chbouib"
  85. "")
  86. (list (string-replace-substring "foo bar baz" "bar" "BAR!")
  87. (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/")
  88. (string-replace-substring "" "foo" "bar")))
  89. (test-equal "strip-keyword-arguments"
  90. '(a #:b b #:c c)
  91. (strip-keyword-arguments '(#:foo #:bar #:baz)
  92. '(a #:foo 42 #:b b #:baz 3
  93. #:c c #:bar 4)))
  94. (test-equal "ensure-keyword-arguments"
  95. '((#:foo 2)
  96. (#:foo 2 #:bar 3)
  97. (#:foo 42 #:bar 3))
  98. (list (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
  99. (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
  100. (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))))
  101. (test-equal "default-keyword-arguments"
  102. '((#:foo 2)
  103. (#:foo 2)
  104. (#:foo 2 #:bar 3)
  105. (#:foo 2 #:bar 3)
  106. (#:foo 2 #:bar 3))
  107. (list (default-keyword-arguments '() '(#:foo 2))
  108. (default-keyword-arguments '(#:foo 2) '(#:foo 4))
  109. (default-keyword-arguments '() '(#:bar 3 #:foo 2))
  110. (default-keyword-arguments '(#:bar 3) '(#:foo 2))
  111. (default-keyword-arguments '(#:foo 2 #:bar 3) '(#:bar 6))))
  112. (test-equal "substitute-keyword-arguments"
  113. '((#:foo 3)
  114. (#:foo 3)
  115. (#:foo 3 #:bar (1 2))
  116. (#:bar (1 2) #:foo 3)
  117. (#:foo 3))
  118. (list (substitute-keyword-arguments '(#:foo 2)
  119. ((#:foo f) (1+ f)))
  120. (substitute-keyword-arguments '()
  121. ((#:foo f 2) (1+ f)))
  122. (substitute-keyword-arguments '(#:foo 2 #:bar (2))
  123. ((#:foo f) (1+ f))
  124. ((#:bar b) (cons 1 b)))
  125. (substitute-keyword-arguments '(#:foo 2)
  126. ((#:foo _) 3)
  127. ((#:bar b '(2)) (cons 1 b)))
  128. (substitute-keyword-arguments '(#:foo 2)
  129. ((#:foo f 1) (1+ f))
  130. ((#:bar b) (cons 42 b)))))
  131. (test-assert "filtered-port, file"
  132. (let* ((file (search-path %load-path "guix.scm"))
  133. (input (open-file file "r0b")))
  134. (let*-values (((compressed pids1)
  135. (filtered-port `(,%gzip "-c" "--fast") input))
  136. ((decompressed pids2)
  137. (filtered-port `(,%gzip "-d") compressed)))
  138. (and (every (compose zero? cdr waitpid)
  139. (append pids1 pids2))
  140. (equal? (get-bytevector-all decompressed)
  141. (call-with-input-file file get-bytevector-all))))))
  142. (test-assert "filtered-port, non-file"
  143. (let ((data (call-with-input-file (search-path %load-path "guix.scm")
  144. get-bytevector-all)))
  145. (let*-values (((compressed pids1)
  146. (filtered-port `(,%gzip "-c" "--fast")
  147. (open-bytevector-input-port data)))
  148. ((decompressed pids2)
  149. (filtered-port `(,%gzip "-d") compressed)))
  150. (and (pk (every (compose zero? cdr waitpid)
  151. (append pids1 pids2)))
  152. (equal? (get-bytevector-all decompressed) data)))))
  153. (test-assert "filtered-port, does not exist"
  154. (let* ((file (search-path %load-path "guix.scm"))
  155. (input (open-file file "r0b")))
  156. (let-values (((port pids)
  157. (filtered-port '("/does/not/exist") input)))
  158. (any (compose (negate zero?) cdr waitpid)
  159. pids))))
  160. (define (test-compression/decompression method run?)
  161. "Test METHOD, a symbol such as 'gzip. Call RUN? to determine whether to
  162. skip these tests."
  163. (unless (run?) (test-skip 1))
  164. (test-assert (format #f "compressed-port, decompressed-port, non-file [~a]"
  165. method)
  166. (let ((data (call-with-input-file (search-path %load-path "guix.scm")
  167. get-bytevector-all)))
  168. (let*-values (((compressed pids1)
  169. (compressed-port method (open-bytevector-input-port data)))
  170. ((decompressed pids2)
  171. (decompressed-port method compressed)))
  172. (and (every (compose zero? cdr waitpid)
  173. (pk 'pids method (append pids1 pids2)))
  174. (let ((result (get-bytevector-all decompressed)))
  175. (pk 'len method
  176. (if (bytevector? result)
  177. (bytevector-length result)
  178. result)
  179. (bytevector-length data))
  180. (equal? result data))))))
  181. (false-if-exception (delete-file temp-file))
  182. (unless (run?) (test-skip 1))
  183. (test-assert (format #f "compressed-output-port + decompressed-port [~a]"
  184. method)
  185. (let* ((file (search-path %load-path "guix/derivations.scm"))
  186. (data (call-with-input-file file get-bytevector-all))
  187. (port (open-file temp-file "w0b")))
  188. (call-with-compressed-output-port method port
  189. (lambda (compressed)
  190. (put-bytevector compressed data)))
  191. (close-port port)
  192. (bytevector=? data
  193. (call-with-decompressed-port method (open-file temp-file "r0b")
  194. get-bytevector-all)))))
  195. (for-each test-compression/decompression
  196. '(gzip xz lzip)
  197. (list (const #t) (const #t) lzlib-available?))
  198. ;; This is actually in (guix store).
  199. (test-equal "store-path-package-name"
  200. "bash-4.2-p24"
  201. (store-path-package-name
  202. (string-append (%store-prefix)
  203. "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
  204. (test-equal "canonical-newline-port"
  205. "This is a journey\nInto the sound\nA journey ...\n"
  206. (let ((port (open-string-input-port
  207. "This is a journey\r\nInto the sound\r\nA journey ...\n")))
  208. (get-string-all (canonical-newline-port port))))
  209. (test-equal "canonical-newline-port-1024"
  210. (string-concatenate (make-list 100 "0123456789abcde\n"))
  211. (let ((port (open-string-input-port
  212. (string-concatenate
  213. (make-list 100 "0123456789abcde\r\n")))))
  214. (get-string-all (canonical-newline-port port))))
  215. (test-equal "edit-expression"
  216. "(display \"GNU Guix\")\n(newline)\n"
  217. (begin
  218. (call-with-output-file temp-file
  219. (lambda (port)
  220. (display "(display \"xiuG UNG\")\n(newline)\n" port)))
  221. (edit-expression `((filename . ,temp-file)
  222. (line . 0)
  223. (column . 9))
  224. string-reverse)
  225. (call-with-input-file temp-file get-string-all)))
  226. (test-end)
  227. (false-if-exception (delete-file temp-file))