tests: Remove obsolete comment.
[jackhill/guix/guix.git] / tests / utils.scm
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 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (test-utils)
23 #:use-module ((guix config) #:select (%gzip))
24 #:use-module (guix utils)
25 #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
26 #:use-module ((guix search-paths) #:select (string-tokenize*))
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-11)
29 #:use-module (srfi srfi-64)
30 #:use-module (rnrs bytevectors)
31 #:use-module (rnrs io ports)
32 #:use-module (ice-9 match)
33 #:use-module (ice-9 vlist))
34
35 (define temp-file
36 (string-append "t-utils-" (number->string (getpid))))
37
38 (test-begin "utils")
39
40 (test-assert "gnu-triplet->nix-system"
41 (let ((samples '(("i586-gnu0.3" "i686-gnu")
42 ("x86_64-unknown-linux-gnu" "x86_64-linux")
43 ("i386-pc-linux-gnu" "i686-linux")
44 ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
45 ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
46 ("i686-pc-cygwin" "i686-cygwin"))))
47 (let-values (((gnu nix) (unzip2 samples)))
48 (every (lambda (gnu nix)
49 (equal? nix (gnu-triplet->nix-system gnu)))
50 gnu nix))))
51
52 (test-assert "package-name->name+version"
53 (every (match-lambda
54 ((name version)
55 (let*-values (((full-name)
56 (if version
57 (string-append name "@" version)
58 name))
59 ((name* version*)
60 (package-name->name+version full-name)))
61 (and (equal? name* name)
62 (equal? version* version)))))
63 '(("foo" "0.9.1b")
64 ("foo-14-bar" "320")
65 ("foo-bar2" #f)
66 ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
67 ("nixpkgs" "1.0pre22125_a28fe19")
68 ("gtk2" "2.38.0"))))
69
70 (test-assert "guile-version>? 1.8"
71 (guile-version>? "1.8"))
72
73 (test-assert "guile-version>? 10.5"
74 (not (guile-version>? "10.5")))
75
76 (test-assert "version-prefix?"
77 (and (version-prefix? "4.1" "4.1.2")
78 (version-prefix? "4.1" "4.1")
79 (not (version-prefix? "4.1" "4.16.2"))
80 (not (version-prefix? "4.1" "4"))))
81
82 (test-equal "version-unique-prefix"
83 '("2" "2.2" "")
84 (list (version-unique-prefix "2.0" '("3.0" "2.0"))
85 (version-unique-prefix "2.2" '("3.0.5" "2.0.9" "2.2.7"))
86 (version-unique-prefix "27.1" '("27.1"))))
87
88 (test-equal "string-tokenize*"
89 '(("foo")
90 ("foo" "bar" "baz")
91 ("foo" "bar" "")
92 ("foo" "bar" "baz"))
93 (list (string-tokenize* "foo" ":")
94 (string-tokenize* "foo;bar;baz" ";")
95 (string-tokenize* "foo!bar!" "!")
96 (string-tokenize* "foo+-+bar+-+baz" "+-+")))
97
98 (test-equal "string-replace-substring"
99 '("foo BAR! baz"
100 "/gnu/store/chbouib"
101 "")
102 (list (string-replace-substring "foo bar baz" "bar" "BAR!")
103 (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/")
104 (string-replace-substring "" "foo" "bar")))
105
106 (test-equal "strip-keyword-arguments"
107 '(a #:b b #:c c)
108 (strip-keyword-arguments '(#:foo #:bar #:baz)
109 '(a #:foo 42 #:b b #:baz 3
110 #:c c #:bar 4)))
111
112 (test-equal "ensure-keyword-arguments"
113 '((#:foo 2)
114 (#:foo 2 #:bar 3)
115 (#:foo 42 #:bar 3))
116 (list (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
117 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
118 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))))
119
120 (test-equal "default-keyword-arguments"
121 '((#:foo 2)
122 (#:foo 2)
123 (#:foo 2 #:bar 3)
124 (#:foo 2 #:bar 3)
125 (#:foo 2 #:bar 3))
126 (list (default-keyword-arguments '() '(#:foo 2))
127 (default-keyword-arguments '(#:foo 2) '(#:foo 4))
128 (default-keyword-arguments '() '(#:bar 3 #:foo 2))
129 (default-keyword-arguments '(#:bar 3) '(#:foo 2))
130 (default-keyword-arguments '(#:foo 2 #:bar 3) '(#:bar 6))))
131
132 (test-equal "substitute-keyword-arguments"
133 '((#:foo 3)
134 (#:foo 3)
135 (#:foo 3 #:bar (1 2))
136 (#:bar (1 2) #:foo 3)
137 (#:foo 3))
138 (list (substitute-keyword-arguments '(#:foo 2)
139 ((#:foo f) (1+ f)))
140 (substitute-keyword-arguments '()
141 ((#:foo f 2) (1+ f)))
142 (substitute-keyword-arguments '(#:foo 2 #:bar (2))
143 ((#:foo f) (1+ f))
144 ((#:bar b) (cons 1 b)))
145 (substitute-keyword-arguments '(#:foo 2)
146 ((#:foo _) 3)
147 ((#:bar b '(2)) (cons 1 b)))
148 (substitute-keyword-arguments '(#:foo 2)
149 ((#:foo f 1) (1+ f))
150 ((#:bar b) (cons 42 b)))))
151
152 (test-assert "filtered-port, file"
153 (let* ((file (search-path %load-path "guix.scm"))
154 (input (open-file file "r0b")))
155 (let*-values (((compressed pids1)
156 (filtered-port `(,%gzip "-c" "--fast") input))
157 ((decompressed pids2)
158 (filtered-port `(,%gzip "-d") compressed)))
159 (and (every (compose zero? cdr waitpid)
160 (append pids1 pids2))
161 (equal? (get-bytevector-all decompressed)
162 (call-with-input-file file get-bytevector-all))))))
163
164 (test-assert "filtered-port, non-file"
165 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
166 get-bytevector-all)))
167 (let*-values (((compressed pids1)
168 (filtered-port `(,%gzip "-c" "--fast")
169 (open-bytevector-input-port data)))
170 ((decompressed pids2)
171 (filtered-port `(,%gzip "-d") compressed)))
172 (and (pk (every (compose zero? cdr waitpid)
173 (append pids1 pids2)))
174 (equal? (get-bytevector-all decompressed) data)))))
175
176 (test-assert "filtered-port, does not exist"
177 (let* ((file (search-path %load-path "guix.scm"))
178 (input (open-file file "r0b")))
179 (let-values (((port pids)
180 (filtered-port '("/does/not/exist") input)))
181 (any (compose (negate zero?) cdr waitpid)
182 pids))))
183
184 (define (test-compression/decompression method run?)
185 "Test METHOD, a symbol such as 'gzip. Call RUN? to determine whether to
186 skip these tests."
187 (unless (run?) (test-skip 1))
188 (test-assert (format #f "compressed-port, decompressed-port, non-file [~a]"
189 method)
190 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
191 get-bytevector-all)))
192 (call-with-temporary-output-file
193 (lambda (output port)
194 (close-port port)
195 (let*-values (((compressed pids)
196 ;; Note: 'compressed-output-port' only supports file
197 ;; ports.
198 (compressed-output-port method
199 (open-file output "w0"))))
200 (put-bytevector compressed data)
201 (close-port compressed)
202 (and (every (compose zero? cdr waitpid)
203 (pk 'pids method pids))
204 (let*-values (((decompressed pids)
205 (decompressed-port method
206 (open-bytevector-input-port
207 (call-with-input-file output
208 get-bytevector-all))))
209 ((result)
210 (get-bytevector-all decompressed)))
211 (close-port decompressed)
212 (pk 'len method
213 (if (bytevector? result)
214 (bytevector-length result)
215 result)
216 (bytevector-length data))
217 (and (every (compose zero? cdr waitpid)
218 (pk 'pids method pids))
219 (equal? result data)))))))))
220
221 (false-if-exception (delete-file temp-file))
222 (unless (run?) (test-skip 1))
223 (test-assert (format #f "compressed-output-port + decompressed-port [~a]"
224 method)
225 (let* ((file (search-path %load-path "guix/derivations.scm"))
226 (data (call-with-input-file file get-bytevector-all))
227 (port (open-file temp-file "w0b")))
228 (call-with-compressed-output-port method port
229 (lambda (compressed)
230 (put-bytevector compressed data)))
231 (close-port port)
232
233 (bytevector=? data
234 (call-with-decompressed-port method (open-file temp-file "r0b")
235 get-bytevector-all)))))
236
237 (for-each test-compression/decompression
238 `(gzip xz lzip zstd)
239 (list (const #t) (const #t) (const #t)
240 (lambda ()
241 (resolve-module '(zstd) #t #f #:ensure #f))))
242
243 ;; This is actually in (guix store).
244 (test-equal "store-path-package-name"
245 "bash-4.2-p24"
246 (store-path-package-name
247 (string-append (%store-prefix)
248 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
249
250 (test-equal "canonical-newline-port"
251 "This is a journey\nInto the sound\nA journey ...\n"
252 (let ((port (open-string-input-port
253 "This is a journey\r\nInto the sound\r\nA journey ...\n")))
254 (get-string-all (canonical-newline-port port))))
255
256 (test-equal "canonical-newline-port-1024"
257 (string-concatenate (make-list 100 "0123456789abcde\n"))
258 (let ((port (open-string-input-port
259 (string-concatenate
260 (make-list 100 "0123456789abcde\r\n")))))
261 (get-string-all (canonical-newline-port port))))
262
263 (test-equal "edit-expression"
264 "(display \"GNU Guix\")\n(newline)\n"
265 (begin
266 (call-with-output-file temp-file
267 (lambda (port)
268 (display "(display \"xiuG UNG\")\n(newline)\n" port)))
269 (edit-expression `((filename . ,temp-file)
270 (line . 0)
271 (column . 9))
272 string-reverse)
273 (call-with-input-file temp-file get-string-all)))
274
275 (test-equal "string-distance"
276 '(0 1 1 5 5)
277 (list
278 (string-distance "hello" "hello")
279 (string-distance "hello" "helo")
280 (string-distance "helo" "hello")
281 (string-distance "" "hello")
282 (string-distance "hello" "")))
283
284 (test-equal "string-closest"
285 '("hello" "hello" "helo" #f)
286 (list
287 (string-closest "hello" '("hello"))
288 (string-closest "hello" '("helo" "hello" "halo"))
289 (string-closest "hello" '("kikoo" "helo" "hihihi" "halo"))
290 (string-closest "hello" '("aaaaa" "12345" "hellohello" "h"))))
291
292 (test-end)
293
294 (false-if-exception (delete-file temp-file))