gnu: shadow: Make source file timestamps deterministic.
[jackhill/guix/guix.git] / tests / utils.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
2cd5c038 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
e3deeebb 3;;;
233e7676 4;;; This file is part of GNU Guix.
e3deeebb 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
e3deeebb
LC
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
e3deeebb
LC
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3deeebb 18
e3deeebb 19(define-module (test-utils)
e0fbbc88 20 #:use-module ((guix config) #:select (%gzip))
e3deeebb 21 #:use-module (guix utils)
b980f0f9 22 #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
e3deeebb 23 #:use-module (srfi srfi-1)
98090557 24 #:use-module (srfi srfi-11)
e3deeebb 25 #:use-module (srfi srfi-64)
f9c7080a 26 #:use-module (rnrs bytevectors)
e0fbbc88 27 #:use-module (rnrs io ports)
72d86963 28 #:use-module (ice-9 match))
e3deeebb 29
827d5563
LC
30(define temp-file
31 (string-append "t-utils-" (number->string (getpid))))
32
e3deeebb
LC
33(test-begin "utils")
34
6d800a80
LC
35(test-assert "bytevector->base16-string->bytevector"
36 (every (lambda (bv)
37 (equal? (base16-string->bytevector
38 (bytevector->base16-string bv))
39 bv))
40 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
41
98090557
LC
42(test-assert "gnu-triplet->nix-system"
43 (let ((samples '(("i586-gnu0.3" "i686-gnu")
44 ("x86_64-unknown-linux-gnu" "x86_64-linux")
45 ("i386-pc-linux-gnu" "i686-linux")
46 ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
47 ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
48 ("i686-pc-cygwin" "i686-cygwin"))))
49 (let-values (((gnu nix) (unzip2 samples)))
50 (every (lambda (gnu nix)
51 (equal? nix (gnu-triplet->nix-system gnu)))
52 gnu nix))))
53
9b48fb88
LC
54(test-assert "package-name->name+version"
55 (every (match-lambda
56 ((name version)
57 (let*-values (((full-name)
58 (if version
59 (string-append name "-" version)
60 name))
61 ((name* version*)
62 (package-name->name+version full-name)))
63 (and (equal? name* name)
64 (equal? version* version)))))
65 '(("foo" "0.9.1b")
66 ("foo-bar" "1.0")
67 ("foo-bar2" #f)
68 ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
69 ("nixpkgs" "1.0pre22125_a28fe19")
70 ("gtk2" "2.38.0"))))
71
7db3ff4a
LC
72(test-assert "guile-version>? 1.8"
73 (guile-version>? "1.8"))
74
75(test-assert "guile-version>? 10.5"
76 (not (guile-version>? "10.5")))
77
2bcfb9e0
LC
78(test-equal "string-tokenize*"
79 '(("foo")
80 ("foo" "bar" "baz")
81 ("foo" "bar" "")
82 ("foo" "bar" "baz"))
83 (list (string-tokenize* "foo" ":")
84 (string-tokenize* "foo;bar;baz" ";")
85 (string-tokenize* "foo!bar!" "!")
86 (string-tokenize* "foo+-+bar+-+baz" "+-+")))
87
56b943de
LC
88(test-equal "string-replace-substring"
89 '("foo BAR! baz"
90 "/gnu/store/chbouib"
91 "")
92 (list (string-replace-substring "foo bar baz" "bar" "BAR!")
93 (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/")
94 (string-replace-substring "" "foo" "bar")))
95
04fd96ca
LC
96(test-equal "fold2, 1 list"
97 (list (reverse (iota 5))
98 (map - (reverse (iota 5))))
99 (call-with-values
100 (lambda ()
101 (fold2 (lambda (i r1 r2)
102 (values (cons i r1)
103 (cons (- i) r2)))
104 '() '()
105 (iota 5)))
106 list))
107
108(test-equal "fold2, 2 lists"
109 (list (reverse '((a . 0) (b . 1) (c . 2) (d . 3)))
110 (reverse '((a . 0) (b . -1) (c . -2) (d . -3))))
111 (call-with-values
112 (lambda ()
113 (fold2 (lambda (k v r1 r2)
114 (values (alist-cons k v r1)
115 (alist-cons k (- v) r2)))
116 '() '()
117 '(a b c d)
118 '(0 1 2 3)))
119 list))
120
e0fbbc88 121(test-assert "filtered-port, file"
101d9f3f 122 (let* ((file (search-path %load-path "guix.scm"))
b6952cad 123 (input (open-file file "r0b")))
101d9f3f
LC
124 (let*-values (((compressed pids1)
125 (filtered-port `(,%gzip "-c" "--fast") input))
126 ((decompressed pids2)
127 (filtered-port `(,%gzip "-d") compressed)))
128 (and (every (compose zero? cdr waitpid)
129 (append pids1 pids2))
130 (equal? (get-bytevector-all decompressed)
131 (call-with-input-file file get-bytevector-all))))))
e0fbbc88
LC
132
133(test-assert "filtered-port, non-file"
134 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
135 get-bytevector-all)))
136 (let*-values (((compressed pids1)
137 (filtered-port `(,%gzip "-c" "--fast")
138 (open-bytevector-input-port data)))
139 ((decompressed pids2)
140 (filtered-port `(,%gzip "-d") compressed)))
141 (and (pk (every (compose zero? cdr waitpid)
142 (append pids1 pids2)))
143 (equal? (get-bytevector-all decompressed) data)))))
144
443eb4e9
LC
145(test-assert "filtered-port, does not exist"
146 (let* ((file (search-path %load-path "guix.scm"))
147 (input (open-file file "r0b")))
148 (let-values (((port pids)
149 (filtered-port '("/does/not/exist") input)))
150 (any (compose (negate zero?) cdr waitpid)
151 pids))))
152
7a8024a3
LC
153(test-assert "compressed-port, decompressed-port, non-file"
154 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
155 get-bytevector-all)))
156 (let*-values (((compressed pids1)
157 (compressed-port 'xz (open-bytevector-input-port data)))
158 ((decompressed pids2)
159 (decompressed-port 'xz compressed)))
160 (and (every (compose zero? cdr waitpid)
161 (append pids1 pids2))
162 (equal? (get-bytevector-all decompressed) data)))))
163
80dea563 164(false-if-exception (delete-file temp-file))
01ac19dc
LC
165(test-assert "compressed-output-port + decompressed-port"
166 (let* ((file (search-path %load-path "guix/derivations.scm"))
167 (data (call-with-input-file file get-bytevector-all)))
168 (call-with-compressed-output-port 'xz (open-file temp-file "w0b")
169 (lambda (compressed)
170 (put-bytevector compressed data)))
171
172 (bytevector=? data
173 (call-with-decompressed-port 'xz (open-file temp-file "r0b")
174 get-bytevector-all))))
80dea563 175
827d5563 176(false-if-exception (delete-file temp-file))
c7445833 177(test-equal "fcntl-flock wait"
827d5563
LC
178 42 ; the child's exit status
179 (let ((file (open-file temp-file "w0")))
180 ;; Acquire an exclusive lock.
181 (fcntl-flock file 'write-lock)
2cd5c038
LC
182 (match (primitive-fork)
183 (0
184 (dynamic-wind
185 (const #t)
186 (lambda ()
827d5563
LC
187 ;; Reopen FILE read-only so we can have a read lock.
188 (let ((file (open-file temp-file "r")))
189 ;; Wait until we can acquire the lock.
190 (fcntl-flock file 'read-lock)
191 (primitive-exit (read file)))
2cd5c038
LC
192 (primitive-exit 1))
193 (lambda ()
194 (primitive-exit 2))))
195 (pid
827d5563
LC
196 ;; Write garbage and wait.
197 (display "hello, world!" file)
198 (force-output file)
199 (sleep 1)
200
201 ;; Write the real answer.
202 (seek file 0 SEEK_SET)
203 (truncate-file file 0)
204 (write 42 file)
205 (force-output file)
206
207 ;; Unlock, which should let the child continue.
208 (fcntl-flock file 'unlock)
209
2cd5c038
LC
210 (match (waitpid pid)
211 ((_ . status)
212 (let ((result (status:exit-val status)))
2cd5c038
LC
213 (close-port file)
214 result)))))))
215
c7445833
LC
216(test-equal "fcntl-flock non-blocking"
217 EAGAIN ; the child's exit status
218 (match (pipe)
219 ((input . output)
220 (match (primitive-fork)
221 (0
222 (dynamic-wind
223 (const #t)
224 (lambda ()
225 (close-port output)
226
227 ;; Wait for the green light.
228 (read-char input)
229
230 ;; Open FILE read-only so we can have a read lock.
231 (let ((file (open-file temp-file "w")))
232 (catch 'flock-error
233 (lambda ()
234 ;; This attempt should throw EAGAIN.
235 (fcntl-flock file 'write-lock #:wait? #f))
236 (lambda (key errno)
237 (primitive-exit errno))))
238 (primitive-exit -1))
239 (lambda ()
240 (primitive-exit -2))))
241 (pid
242 (close-port input)
243 (let ((file (open-file temp-file "w")))
244 ;; Acquire an exclusive lock.
245 (fcntl-flock file 'write-lock)
246
247 ;; Tell the child to continue.
248 (write 'green-light output)
249 (force-output output)
250
251 (match (waitpid pid)
252 ((_ . status)
253 (let ((result (status:exit-val status)))
254 (fcntl-flock file 'unlock)
255 (close-port file)
256 result)))))))))
257
e3d74106
LC
258;; This is actually in (guix store).
259(test-equal "store-path-package-name"
260 "bash-4.2-p24"
261 (store-path-package-name
b980f0f9
LC
262 (string-append (%store-prefix)
263 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
e3d74106 264
e3deeebb
LC
265(test-end)
266
827d5563
LC
267(false-if-exception (delete-file temp-file))
268
e3deeebb
LC
269\f
270(exit (= (test-runner-fail-count (test-runner-current)) 0))