1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
4 ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
5 ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
6 ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (test-utils)
24 #:use-module ((guix config) #:select (%gzip))
25 #:use-module (guix utils)
26 #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
27 #:use-module ((guix search-paths) #:select (string-tokenize*))
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-11)
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs bytevectors)
32 #:use-module (rnrs io ports)
33 #:use-module (ice-9 match)
34 #:use-module (ice-9 vlist))
37 (string-append "t-utils-" (number->string (getpid))))
41 (test-assert "gnu-triplet->nix-system"
42 (let ((samples '(("i586-gnu0.3" "i686-gnu")
43 ("x86_64-unknown-linux-gnu" "x86_64-linux")
44 ("i386-pc-linux-gnu" "i686-linux")
45 ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
46 ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
47 ("i686-pc-cygwin" "i686-cygwin"))))
48 (let-values (((gnu nix) (unzip2 samples)))
49 (every (lambda (gnu nix)
50 (equal? nix (gnu-triplet->nix-system gnu)))
53 (test-assert "package-name->name+version"
56 (let*-values (((full-name)
58 (string-append name "@" version)
61 (package-name->name+version full-name)))
62 (and (equal? name* name)
63 (equal? version* version)))))
67 ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
68 ("nixpkgs" "1.0pre22125_a28fe19")
71 (test-assert "guile-version>? 1.8"
72 (guile-version>? "1.8"))
74 (test-assert "guile-version>? 10.5"
75 (not (guile-version>? "10.5")))
77 (test-assert "version-prefix?"
78 (and (version-prefix? "4.1" "4.1.2")
79 (version-prefix? "4.1" "4.1")
80 (not (version-prefix? "4.1" "4.16.2"))
81 (not (version-prefix? "4.1" "4"))))
83 (test-equal "version-unique-prefix"
85 (list (version-unique-prefix "2.0" '("3.0" "2.0"))
86 (version-unique-prefix "2.2" '("3.0.5" "2.0.9" "2.2.7"))
87 (version-unique-prefix "27.1" '("27.1"))))
89 (test-equal "string-tokenize*"
94 (list (string-tokenize* "foo" ":")
95 (string-tokenize* "foo;bar;baz" ";")
96 (string-tokenize* "foo!bar!" "!")
97 (string-tokenize* "foo+-+bar+-+baz" "+-+")))
99 (test-equal "string-replace-substring"
103 (list (string-replace-substring "foo bar baz" "bar" "BAR!")
104 (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/")
105 (string-replace-substring "" "foo" "bar")))
107 (test-equal "strip-keyword-arguments"
109 (strip-keyword-arguments '(#:foo #:bar #:baz)
110 '(a #:foo 42 #:b b #:baz 3
113 (test-equal "ensure-keyword-arguments"
117 (list (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
118 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
119 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))))
121 (test-equal "default-keyword-arguments"
127 (list (default-keyword-arguments '() '(#:foo 2))
128 (default-keyword-arguments '(#:foo 2) '(#:foo 4))
129 (default-keyword-arguments '() '(#:bar 3 #:foo 2))
130 (default-keyword-arguments '(#:bar 3) '(#:foo 2))
131 (default-keyword-arguments '(#:foo 2 #:bar 3) '(#:bar 6))))
133 (test-equal "substitute-keyword-arguments"
136 (#:foo 3 #:bar (1 2))
137 (#:bar (1 2) #:foo 3)
139 (list (substitute-keyword-arguments '(#:foo 2)
141 (substitute-keyword-arguments '()
142 ((#:foo f 2) (1+ f)))
143 (substitute-keyword-arguments '(#:foo 2 #:bar (2))
145 ((#:bar b) (cons 1 b)))
146 (substitute-keyword-arguments '(#:foo 2)
148 ((#:bar b '(2)) (cons 1 b)))
149 (substitute-keyword-arguments '(#:foo 2)
151 ((#:bar b) (cons 42 b)))))
153 (test-assert "filtered-port, file"
154 (let* ((file (search-path %load-path "guix.scm"))
155 (input (open-file file "r0b")))
156 (let*-values (((compressed pids1)
157 (filtered-port `(,%gzip "-c" "--fast") input))
158 ((decompressed pids2)
159 (filtered-port `(,%gzip "-d") compressed)))
160 (and (every (compose zero? cdr waitpid)
161 (append pids1 pids2))
162 (equal? (get-bytevector-all decompressed)
163 (call-with-input-file file get-bytevector-all))))))
165 (test-assert "filtered-port, non-file"
166 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
167 get-bytevector-all)))
168 (let*-values (((compressed pids1)
169 (filtered-port `(,%gzip "-c" "--fast")
170 (open-bytevector-input-port data)))
171 ((decompressed pids2)
172 (filtered-port `(,%gzip "-d") compressed)))
173 (and (pk (every (compose zero? cdr waitpid)
174 (append pids1 pids2)))
175 (equal? (get-bytevector-all decompressed) data)))))
177 (test-assert "filtered-port, does not exist"
178 (let* ((file (search-path %load-path "guix.scm"))
179 (input (open-file file "r0b")))
180 (let-values (((port pids)
181 (filtered-port '("/does/not/exist") input)))
182 (any (compose (negate zero?) cdr waitpid)
185 (define (test-compression/decompression method run?)
186 "Test METHOD, a symbol such as 'gzip. Call RUN? to determine whether to
188 (unless (run?) (test-skip 1))
189 (test-assert (format #f "compressed-port, decompressed-port, non-file [~a]"
191 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
192 get-bytevector-all)))
193 (call-with-temporary-output-file
194 (lambda (output port)
196 (let*-values (((compressed pids)
197 ;; Note: 'compressed-output-port' only supports file
199 (compressed-output-port method
200 (open-file output "w0"))))
201 (put-bytevector compressed data)
202 (close-port compressed)
203 (and (every (compose zero? cdr waitpid)
204 (pk 'pids method pids))
205 (let*-values (((decompressed pids)
206 (decompressed-port method
207 (open-bytevector-input-port
208 (call-with-input-file output
209 get-bytevector-all))))
211 (get-bytevector-all decompressed)))
212 (close-port decompressed)
214 (if (bytevector? result)
215 (bytevector-length result)
217 (bytevector-length data))
218 (and (every (compose zero? cdr waitpid)
219 (pk 'pids method pids))
220 (equal? result data)))))))))
222 (false-if-exception (delete-file temp-file))
223 (unless (run?) (test-skip 1))
224 (test-assert (format #f "compressed-output-port + decompressed-port [~a]"
226 (let* ((file (search-path %load-path "guix/derivations.scm"))
227 (data (call-with-input-file file get-bytevector-all))
228 (port (open-file temp-file "w0b")))
229 (call-with-compressed-output-port method port
231 (put-bytevector compressed data)))
235 (call-with-decompressed-port method (open-file temp-file "r0b")
236 get-bytevector-all)))))
238 (for-each test-compression/decompression
240 (list (const #t) (const #t) (const #t)
242 (resolve-module '(zstd) #t #f #:ensure #f))))
244 ;; This is actually in (guix store).
245 (test-equal "store-path-package-name"
247 (store-path-package-name
248 (string-append (%store-prefix)
249 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
251 (test-equal "canonical-newline-port"
252 "This is a journey\nInto the sound\nA journey ...\n"
253 (let ((port (open-string-input-port
254 "This is a journey\r\nInto the sound\r\nA journey ...\n")))
255 (get-string-all (canonical-newline-port port))))
257 (test-equal "canonical-newline-port-1024"
258 (string-concatenate (make-list 100 "0123456789abcde\n"))
259 (let ((port (open-string-input-port
261 (make-list 100 "0123456789abcde\r\n")))))
262 (get-string-all (canonical-newline-port port))))
264 (test-equal "edit-expression"
265 "(display \"GNU Guix\")\n(newline)\n"
267 (call-with-output-file temp-file
269 (display "(display \"xiuG UNG\")\n(newline)\n" port)))
270 (edit-expression `((filename . ,temp-file)
274 (call-with-input-file temp-file get-string-all)))
276 (test-equal "string-distance"
279 (string-distance "hello" "hello")
280 (string-distance "hello" "helo")
281 (string-distance "helo" "hello")
282 (string-distance "" "hello")
283 (string-distance "hello" "")))
285 (test-equal "string-closest"
286 '("hello" "hello" "helo" #f)
288 (string-closest "hello" '("hello"))
289 (string-closest "hello" '("helo" "hello" "halo"))
290 (string-closest "hello" '("kikoo" "helo" "hihihi" "halo"))
291 (string-closest "hello" '("aaaaa" "12345" "hellohello" "h"))))
293 (test-equal "target-linux?"
296 '("i686-linux-gnu" "i686-w64-mingw32"
297 ;; Checking that "gnu" is present is not sufficient,
298 ;; as GNU/Hurd exists.
300 ;; Some targets have a suffix.
301 "arm-linux-gnueabihf")))
303 (test-equal "target-mingw?"
306 '("i686-linux-gnu" "i686-pc-gnu"
307 "i686-w64-mingw32")))
309 (test-equal "target-x86-32?"
310 '(#f #f #f #t #t #t #t #f)
311 ;; These are (according to Wikipedia) two RISC architectures
312 ;; by Intel and presumably not compatible with the x86-32 series.
314 '("i860-gnu" "i960-gnu"
315 ;; This is a 16-bit architecture
317 ;; These are part of the x86-32 series.
318 "i386-gnu" "i486-gnu" "i586-gnu" "i686-gnu"
319 ;; Maybe this one will exist some day, but not yet.
322 (test-equal "target-x86-64?"
325 `("x86_64-linux-gnu" "i386-linux-gnu"
326 ;; Just because it includes "64" doesn't make it 64-bit.
328 ;; Note that (expt 2 109) in decimal notation starts with 64.
329 ;; However, it isn't 32-bit.
330 ,(format #f "x86_~a-linux-gnu" (expt 2 109)))))
334 (false-if-exception (delete-file temp-file))