utils.scm 9.5 KB

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