build: Don't auto-compile tests.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
341c6fdd
LC
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; Guix is free software; you can redistribute it and/or modify it
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;;;
11;;; Guix is distributed in the hope that it will be useful, but
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
17;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
18
19
20(define-module (test-derivations)
21 #:use-module (guix derivations)
26bbbb95 22 #:use-module (guix store)
de4c3f26 23 #:use-module (guix utils)
b37eb5ed 24 #:use-module (srfi srfi-1)
fb3eec83 25 #:use-module (srfi srfi-11)
341c6fdd 26 #:use-module (srfi srfi-26)
99634e3f 27 #:use-module (srfi srfi-34)
341c6fdd 28 #:use-module (srfi srfi-64)
fb3eec83 29 #:use-module (rnrs io ports)
749c6567 30 #:use-module (rnrs bytevectors)
b37eb5ed 31 #:use-module (ice-9 rdelim)
db393b33 32 #:use-module (ice-9 regex)
99634e3f
LC
33 #:use-module (ice-9 ftw)
34 #:use-module (ice-9 match))
341c6fdd 35
26bbbb95
LC
36(define %store
37 (false-if-exception (open-connection)))
38
b37eb5ed
LC
39(define (directory-contents dir)
40 "Return an alist representing the contents of DIR."
41 (define prefix-len (string-length dir))
42 (sort (file-system-fold (const #t) ; enter?
43 (lambda (path stat result) ; leaf
44 (alist-cons (string-drop path prefix-len)
45 (call-with-input-file path
46 get-bytevector-all)
47 result))
48 (lambda (path stat result) result) ; down
49 (lambda (path stat result) result) ; up
50 (lambda (path stat result) result) ; skip
51 (lambda (path stat errno result) result) ; error
52 '()
53 dir)
54 (lambda (e1 e2)
55 (string<? (car e1) (car e2)))))
56
341c6fdd
LC
57(test-begin "derivations")
58
59(test-assert "parse & export"
33594aa4
LC
60 (let* ((f (search-path %load-path "tests/test.drv"))
61 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
62 (d1 (read-derivation (open-bytevector-input-port b1)))
63 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
64 (d2 (read-derivation (open-bytevector-input-port b2))))
65 (and (equal? b1 b2)
66 (equal? d1 d2))))
67
b37eb5ed
LC
68(test-skip (if %store 0 4))
69
d1b1c424
LC
70(test-assert "add-to-store, flat"
71 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
72 (drv (add-to-store %store "flat-test" #t #f "sha256" file)))
73 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 74 (valid-path? %store drv)
d1b1c424
LC
75 (equal? (call-with-input-file file get-bytevector-all)
76 (call-with-input-file drv get-bytevector-all)))))
77
b37eb5ed
LC
78(test-assert "add-to-store, recursive"
79 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
80 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
81 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 82 (valid-path? %store drv)
b37eb5ed
LC
83 (equal? (directory-contents dir)
84 (directory-contents drv)))))
26bbbb95
LC
85
86(test-assert "derivation with no inputs"
31ef99a8
LC
87 (let* ((builder (add-text-to-store %store "my-builder.sh"
88 "#!/bin/sh\necho hello, world\n"
89 '()))
90 (drv-path (derivation %store "foo" (%current-system) builder
91 '() '(("HOME" . "/homeless")) '())))
92 (and (store-path? drv-path)
93 (valid-path? %store drv-path))))
26bbbb95 94
fb3eec83
LC
95(test-assert "build derivation with 1 source"
96 (let*-values (((builder)
97 (add-text-to-store %store "my-builder.sh"
de4c3f26 98 "echo hello, world > \"$out\"\n"
fb3eec83
LC
99 '()))
100 ((drv-path drv)
98090557 101 (derivation %store "foo" (%current-system)
fb3eec83 102 "/bin/sh" `(,builder)
af7f9e5f
LC
103 '(("HOME" . "/homeless")
104 ("zzz" . "Z!")
105 ("AAA" . "A!"))
fb3eec83
LC
106 `((,builder))))
107 ((succeeded?)
108 (build-derivations %store (list drv-path))))
109 (and succeeded?
110 (let ((path (derivation-output-path
111 (assoc-ref (derivation-outputs drv) "out"))))
31ef99a8
LC
112 (and (valid-path? %store path)
113 (string=? (call-with-input-file path read-line)
114 "hello, world"))))))
fb3eec83 115
749c6567
LC
116(test-assert "fixed-output derivation"
117 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
118 "echo -n hello > $out" '()))
119 (hash (sha256 (string->utf8 "hello")))
98090557 120 (drv-path (derivation %store "fixed" (%current-system)
749c6567
LC
121 "/bin/sh" `(,builder)
122 '() `((,builder))
123 #:hash hash #:hash-algo 'sha256))
124 (succeeded? (build-derivations %store (list drv-path))))
125 (and succeeded?
126 (let ((p (derivation-path->output-path drv-path)))
127 (equal? (string->utf8 "hello")
128 (call-with-input-file p get-bytevector-all))))))
129
7946c4e7
LC
130(test-assert "multiple-output derivation"
131 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
132 "echo one > $out ; echo two > $second"
133 '()))
98090557 134 (drv-path (derivation %store "fixed" (%current-system)
7946c4e7
LC
135 "/bin/sh" `(,builder)
136 '(("HOME" . "/homeless")
137 ("zzz" . "Z!")
138 ("AAA" . "A!"))
139 `((,builder))
140 #:outputs '("out" "second")))
141 (succeeded? (build-derivations %store (list drv-path))))
142 (and succeeded?
143 (let ((one (derivation-path->output-path drv-path "out"))
144 (two (derivation-path->output-path drv-path "second")))
145 (and (eq? 'one (call-with-input-file one read))
146 (eq? 'two (call-with-input-file two read)))))))
147
4b1786aa
LC
148(test-assert "multiple-output derivation, non-alphabetic order"
149 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
150 ;; path computation must reorder them first.
151 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
152 "echo one > $out ; echo two > $AAA"
153 '()))
154 (drv-path (derivation %store "fixed" (%current-system)
155 "/bin/sh" `(,builder)
156 '()
157 `((,builder))
158 #:outputs '("out" "AAA")))
159 (succeeded? (build-derivations %store (list drv-path))))
160 (and succeeded?
161 (let ((one (derivation-path->output-path drv-path "out"))
162 (two (derivation-path->output-path drv-path "AAA")))
163 (and (eq? 'one (call-with-input-file one read))
164 (eq? 'two (call-with-input-file two read)))))))
165
de4c3f26
LC
166\f
167(define %coreutils
168 (false-if-exception (nixpkgs-derivation "coreutils")))
169
170(test-skip (if %coreutils 0 1))
171
172(test-assert "build derivation with coreutils"
173 (let* ((builder
174 (add-text-to-store %store "build-with-coreutils.sh"
175 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
176 '()))
177 (drv-path
98090557 178 (derivation %store "foo" (%current-system)
de4c3f26
LC
179 "/bin/sh" `(,builder)
180 `(("PATH" .
181 ,(string-append
182 (derivation-path->output-path %coreutils)
183 "/bin")))
184 `((,builder)
185 (,%coreutils))))
186 (succeeded?
187 (build-derivations %store (list drv-path))))
188 (and succeeded?
189 (let ((p (derivation-path->output-path drv-path)))
31ef99a8
LC
190 (and (valid-path? %store p)
191 (file-exists? (string-append p "/good")))))))
de4c3f26 192
9a20830e
LC
193(test-skip (if (%guile-for-build) 0 6))
194
195(test-assert "build-expression->derivation and derivation-prerequisites"
196 (let-values (((drv-path drv)
197 (build-expression->derivation %store "fail" (%current-system)
198 #f '())))
199 (any (match-lambda
200 (($ <derivation-input> path)
201 (string=? path (%guile-for-build))))
202 (derivation-prerequisites drv))))
d9085c23
LC
203
204(test-assert "build-expression->derivation without inputs"
205 (let* ((builder '(begin
206 (mkdir %output)
207 (call-with-output-file (string-append %output "/test")
208 (lambda (p)
209 (display '(hello guix) p)))))
98090557 210 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
211 builder '()))
212 (succeeded? (build-derivations %store (list drv-path))))
213 (and succeeded?
214 (let ((p (derivation-path->output-path drv-path)))
215 (equal? '(hello guix)
216 (call-with-input-file (string-append p "/test") read))))))
217
9a20830e
LC
218(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
219 (let-values (((drv-path drv)
220 (build-expression->derivation %store "fail" (%current-system)
221 #f '())))
222 ;; The only direct dependency is (%guile-for-build) and it's already
223 ;; built.
224 (null? (derivation-prerequisites-to-build %store drv))))
225
db393b33
LC
226(test-assert "build-expression->derivation with expression returning #f"
227 (let* ((builder '(begin
228 (mkdir %output)
229 #f)) ; fail!
230 (drv-path (build-expression->derivation %store "fail" (%current-system)
31ef99a8
LC
231 builder '()))
232 (out-path (derivation-path->output-path drv-path)))
db393b33
LC
233 (guard (c ((nix-protocol-error? c)
234 ;; Note that the output path may exist at this point, but it
235 ;; is invalid.
31ef99a8
LC
236 (and (string-match "build .* failed"
237 (nix-protocol-error-message c))
238 (not (valid-path? %store out-path)))))
db393b33
LC
239 (build-derivations %store (list drv-path))
240 #f)))
241
9bc07f4d
LC
242(test-assert "build-expression->derivation with two outputs"
243 (let* ((builder '(begin
244 (call-with-output-file (assoc-ref %outputs "out")
245 (lambda (p)
246 (display '(hello) p)))
247 (call-with-output-file (assoc-ref %outputs "second")
248 (lambda (p)
249 (display '(world) p)))))
250 (drv-path (build-expression->derivation %store "double"
98090557 251 (%current-system)
9bc07f4d
LC
252 builder '()
253 #:outputs '("out"
254 "second")))
255 (succeeded? (build-derivations %store (list drv-path))))
256 (and succeeded?
257 (let ((one (derivation-path->output-path drv-path))
258 (two (derivation-path->output-path drv-path "second")))
259 (and (equal? '(hello) (call-with-input-file one read))
260 (equal? '(world) (call-with-input-file two read)))))))
261
d9085c23
LC
262(test-assert "build-expression->derivation with one input"
263 (let* ((builder '(call-with-output-file %output
264 (lambda (p)
265 (let ((cu (assoc-ref %build-inputs "cu")))
266 (close 1)
267 (dup2 (port->fdes p) 1)
268 (execl (string-append cu "/bin/uname")
269 "uname" "-a")))))
98090557 270 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 271 builder
2acb2cb6 272 `(("cu" ,%coreutils))))
d9085c23
LC
273 (succeeded? (build-derivations %store (list drv-path))))
274 (and succeeded?
275 (let ((p (derivation-path->output-path drv-path)))
276 (string-contains (call-with-input-file p read-line) "GNU")))))
277
99634e3f
LC
278(test-assert "imported-files"
279 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
280 ("a/b/c" . ,(search-path %load-path
281 "guix/derivations.scm"))
224f7ad6
LC
282 ("p/q" . ,(search-path %load-path "guix.scm"))
283 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
284 (drv-path (imported-files %store files)))
285 (and (build-derivations %store (list drv-path))
286 (let ((dir (derivation-path->output-path drv-path)))
287 (every (match-lambda
288 ((path . source)
289 (equal? (call-with-input-file (string-append dir "/" path)
290 get-bytevector-all)
291 (call-with-input-file source
292 get-bytevector-all))))
293 files)))))
294
26b969de
LC
295(test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
296 0
297 1))
298
299(test-assert "build-expression->derivation for fixed-output derivation"
300 (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
0e383c76
LC
301 (builder
302 `(begin
303 (use-modules (web client) (web uri)
304 (rnrs io ports) (srfi srfi-11))
305 (let-values (((resp bv)
306 (http-get (string->uri ,url) #:decode-body? #f)))
307 (call-with-output-file %output
308 (lambda (p)
309 (put-bytevector p bv))))))
26b969de 310 (drv-path (build-expression->derivation
98090557 311 %store "hello-2.8.tar.gz" (%current-system) builder '()
26b969de
LC
312 #:hash (nix-base32-string->bytevector
313 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
314 #:hash-algo 'sha256))
315 (succeeded? (build-derivations %store (list drv-path))))
316 (and succeeded?
317 (file-exists? (derivation-path->output-path drv-path)))))
318
341c6fdd
LC
319(test-end)
320
321\f
322(exit (= (test-runner-fail-count (test-runner-current)) 0))
323
324;;; Local Variables:
325;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 326;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 327;;; End: