licenses: Fix Nixpkgs license URL.
[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
827d5563 145(false-if-exception (delete-file temp-file))
c7445833 146(test-equal "fcntl-flock wait"
827d5563
LC
147 42 ; the child's exit status
148 (let ((file (open-file temp-file "w0")))
149 ;; Acquire an exclusive lock.
150 (fcntl-flock file 'write-lock)
2cd5c038
LC
151 (match (primitive-fork)
152 (0
153 (dynamic-wind
154 (const #t)
155 (lambda ()
827d5563
LC
156 ;; Reopen FILE read-only so we can have a read lock.
157 (let ((file (open-file temp-file "r")))
158 ;; Wait until we can acquire the lock.
159 (fcntl-flock file 'read-lock)
160 (primitive-exit (read file)))
2cd5c038
LC
161 (primitive-exit 1))
162 (lambda ()
163 (primitive-exit 2))))
164 (pid
827d5563
LC
165 ;; Write garbage and wait.
166 (display "hello, world!" file)
167 (force-output file)
168 (sleep 1)
169
170 ;; Write the real answer.
171 (seek file 0 SEEK_SET)
172 (truncate-file file 0)
173 (write 42 file)
174 (force-output file)
175
176 ;; Unlock, which should let the child continue.
177 (fcntl-flock file 'unlock)
178
2cd5c038
LC
179 (match (waitpid pid)
180 ((_ . status)
181 (let ((result (status:exit-val status)))
2cd5c038
LC
182 (close-port file)
183 result)))))))
184
c7445833
LC
185(test-equal "fcntl-flock non-blocking"
186 EAGAIN ; the child's exit status
187 (match (pipe)
188 ((input . output)
189 (match (primitive-fork)
190 (0
191 (dynamic-wind
192 (const #t)
193 (lambda ()
194 (close-port output)
195
196 ;; Wait for the green light.
197 (read-char input)
198
199 ;; Open FILE read-only so we can have a read lock.
200 (let ((file (open-file temp-file "w")))
201 (catch 'flock-error
202 (lambda ()
203 ;; This attempt should throw EAGAIN.
204 (fcntl-flock file 'write-lock #:wait? #f))
205 (lambda (key errno)
206 (primitive-exit errno))))
207 (primitive-exit -1))
208 (lambda ()
209 (primitive-exit -2))))
210 (pid
211 (close-port input)
212 (let ((file (open-file temp-file "w")))
213 ;; Acquire an exclusive lock.
214 (fcntl-flock file 'write-lock)
215
216 ;; Tell the child to continue.
217 (write 'green-light output)
218 (force-output output)
219
220 (match (waitpid pid)
221 ((_ . status)
222 (let ((result (status:exit-val status)))
223 (fcntl-flock file 'unlock)
224 (close-port file)
225 result)))))))))
226
e3d74106
LC
227;; This is actually in (guix store).
228(test-equal "store-path-package-name"
229 "bash-4.2-p24"
230 (store-path-package-name
b980f0f9
LC
231 (string-append (%store-prefix)
232 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
e3d74106 233
e3deeebb
LC
234(test-end)
235
827d5563
LC
236(false-if-exception (delete-file temp-file))
237
e3deeebb
LC
238\f
239(exit (= (test-runner-fail-count (test-runner-current)) 0))