utils.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 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. (test-assert "compressed-port, decompressed-port, non-file"
  160. (let ((data (call-with-input-file (search-path %load-path "guix.scm")
  161. get-bytevector-all)))
  162. (let*-values (((compressed pids1)
  163. (compressed-port 'xz (open-bytevector-input-port data)))
  164. ((decompressed pids2)
  165. (decompressed-port 'xz compressed)))
  166. (and (every (compose zero? cdr waitpid)
  167. (append pids1 pids2))
  168. (equal? (get-bytevector-all decompressed) data)))))
  169. (false-if-exception (delete-file temp-file))
  170. (test-assert "compressed-output-port + decompressed-port"
  171. (let* ((file (search-path %load-path "guix/derivations.scm"))
  172. (data (call-with-input-file file get-bytevector-all))
  173. (port (open-file temp-file "w0b")))
  174. (call-with-compressed-output-port 'xz port
  175. (lambda (compressed)
  176. (put-bytevector compressed data)))
  177. (close-port port)
  178. (bytevector=? data
  179. (call-with-decompressed-port 'xz (open-file temp-file "r0b")
  180. get-bytevector-all))))
  181. ;; This is actually in (guix store).
  182. (test-equal "store-path-package-name"
  183. "bash-4.2-p24"
  184. (store-path-package-name
  185. (string-append (%store-prefix)
  186. "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
  187. (test-equal "canonical-newline-port"
  188. "This is a journey\nInto the sound\nA journey ...\n"
  189. (let ((port (open-string-input-port
  190. "This is a journey\r\nInto the sound\r\nA journey ...\n")))
  191. (get-string-all (canonical-newline-port port))))
  192. (test-equal "edit-expression"
  193. "(display \"GNU Guix\")\n(newline)\n"
  194. (begin
  195. (call-with-output-file temp-file
  196. (lambda (port)
  197. (display "(display \"xiuG UNG\")\n(newline)\n" port)))
  198. (edit-expression `((filename . ,temp-file)
  199. (line . 0)
  200. (column . 9))
  201. string-reverse)
  202. (call-with-input-file temp-file get-string-all)))
  203. (test-end)
  204. (false-if-exception (delete-file temp-file))