Add missing `set-build-options' parameters.
[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)))
74 (equal? (call-with-input-file file get-bytevector-all)
75 (call-with-input-file drv get-bytevector-all)))))
76
b37eb5ed
LC
77(test-assert "add-to-store, recursive"
78 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
79 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
80 (and (eq? 'directory (stat:type (stat drv)))
81 (equal? (directory-contents dir)
82 (directory-contents drv)))))
26bbbb95
LC
83
84(test-assert "derivation with no inputs"
85 (let ((builder (add-text-to-store %store "my-builder.sh"
86 "#!/bin/sh\necho hello, world\n"
87 '())))
98090557 88 (store-path? (derivation %store "foo" (%current-system) builder
26bbbb95
LC
89 '() '(("HOME" . "/homeless")) '()))))
90
fb3eec83
LC
91(test-assert "build derivation with 1 source"
92 (let*-values (((builder)
93 (add-text-to-store %store "my-builder.sh"
de4c3f26 94 "echo hello, world > \"$out\"\n"
fb3eec83
LC
95 '()))
96 ((drv-path drv)
98090557 97 (derivation %store "foo" (%current-system)
fb3eec83 98 "/bin/sh" `(,builder)
af7f9e5f
LC
99 '(("HOME" . "/homeless")
100 ("zzz" . "Z!")
101 ("AAA" . "A!"))
fb3eec83
LC
102 `((,builder))))
103 ((succeeded?)
104 (build-derivations %store (list drv-path))))
105 (and succeeded?
106 (let ((path (derivation-output-path
107 (assoc-ref (derivation-outputs drv) "out"))))
108 (string=? (call-with-input-file path read-line)
109 "hello, world")))))
110
749c6567
LC
111(test-assert "fixed-output derivation"
112 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
113 "echo -n hello > $out" '()))
114 (hash (sha256 (string->utf8 "hello")))
98090557 115 (drv-path (derivation %store "fixed" (%current-system)
749c6567
LC
116 "/bin/sh" `(,builder)
117 '() `((,builder))
118 #:hash hash #:hash-algo 'sha256))
119 (succeeded? (build-derivations %store (list drv-path))))
120 (and succeeded?
121 (let ((p (derivation-path->output-path drv-path)))
122 (equal? (string->utf8 "hello")
123 (call-with-input-file p get-bytevector-all))))))
124
7946c4e7
LC
125(test-assert "multiple-output derivation"
126 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
127 "echo one > $out ; echo two > $second"
128 '()))
98090557 129 (drv-path (derivation %store "fixed" (%current-system)
7946c4e7
LC
130 "/bin/sh" `(,builder)
131 '(("HOME" . "/homeless")
132 ("zzz" . "Z!")
133 ("AAA" . "A!"))
134 `((,builder))
135 #:outputs '("out" "second")))
136 (succeeded? (build-derivations %store (list drv-path))))
137 (and succeeded?
138 (let ((one (derivation-path->output-path drv-path "out"))
139 (two (derivation-path->output-path drv-path "second")))
140 (and (eq? 'one (call-with-input-file one read))
141 (eq? 'two (call-with-input-file two read)))))))
142
de4c3f26
LC
143\f
144(define %coreutils
145 (false-if-exception (nixpkgs-derivation "coreutils")))
146
147(test-skip (if %coreutils 0 1))
148
149(test-assert "build derivation with coreutils"
150 (let* ((builder
151 (add-text-to-store %store "build-with-coreutils.sh"
152 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
153 '()))
154 (drv-path
98090557 155 (derivation %store "foo" (%current-system)
de4c3f26
LC
156 "/bin/sh" `(,builder)
157 `(("PATH" .
158 ,(string-append
159 (derivation-path->output-path %coreutils)
160 "/bin")))
161 `((,builder)
162 (,%coreutils))))
163 (succeeded?
164 (build-derivations %store (list drv-path))))
165 (and succeeded?
166 (let ((p (derivation-path->output-path drv-path)))
167 (file-exists? (string-append p "/good"))))))
168
99634e3f 169(test-skip (if (%guile-for-build) 0 4))
d9085c23
LC
170
171(test-assert "build-expression->derivation without inputs"
172 (let* ((builder '(begin
173 (mkdir %output)
174 (call-with-output-file (string-append %output "/test")
175 (lambda (p)
176 (display '(hello guix) p)))))
98090557 177 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
178 builder '()))
179 (succeeded? (build-derivations %store (list drv-path))))
180 (and succeeded?
181 (let ((p (derivation-path->output-path drv-path)))
182 (equal? '(hello guix)
183 (call-with-input-file (string-append p "/test") read))))))
184
db393b33
LC
185(test-assert "build-expression->derivation with expression returning #f"
186 (let* ((builder '(begin
187 (mkdir %output)
188 #f)) ; fail!
189 (drv-path (build-expression->derivation %store "fail" (%current-system)
190 builder '())))
191 (guard (c ((nix-protocol-error? c)
192 ;; Note that the output path may exist at this point, but it
193 ;; is invalid.
194 (not (not (string-match "build .* failed"
195 (nix-protocol-error-message c))))))
196 (build-derivations %store (list drv-path))
197 #f)))
198
9bc07f4d
LC
199(test-assert "build-expression->derivation with two outputs"
200 (let* ((builder '(begin
201 (call-with-output-file (assoc-ref %outputs "out")
202 (lambda (p)
203 (display '(hello) p)))
204 (call-with-output-file (assoc-ref %outputs "second")
205 (lambda (p)
206 (display '(world) p)))))
207 (drv-path (build-expression->derivation %store "double"
98090557 208 (%current-system)
9bc07f4d
LC
209 builder '()
210 #:outputs '("out"
211 "second")))
212 (succeeded? (build-derivations %store (list drv-path))))
213 (and succeeded?
214 (let ((one (derivation-path->output-path drv-path))
215 (two (derivation-path->output-path drv-path "second")))
216 (and (equal? '(hello) (call-with-input-file one read))
217 (equal? '(world) (call-with-input-file two read)))))))
218
d9085c23
LC
219(test-assert "build-expression->derivation with one input"
220 (let* ((builder '(call-with-output-file %output
221 (lambda (p)
222 (let ((cu (assoc-ref %build-inputs "cu")))
223 (close 1)
224 (dup2 (port->fdes p) 1)
225 (execl (string-append cu "/bin/uname")
226 "uname" "-a")))))
98090557 227 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 228 builder
2acb2cb6 229 `(("cu" ,%coreutils))))
d9085c23
LC
230 (succeeded? (build-derivations %store (list drv-path))))
231 (and succeeded?
232 (let ((p (derivation-path->output-path drv-path)))
233 (string-contains (call-with-input-file p read-line) "GNU")))))
234
99634e3f
LC
235(test-assert "imported-files"
236 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
237 ("a/b/c" . ,(search-path %load-path
238 "guix/derivations.scm"))
224f7ad6
LC
239 ("p/q" . ,(search-path %load-path "guix.scm"))
240 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
241 (drv-path (imported-files %store files)))
242 (and (build-derivations %store (list drv-path))
243 (let ((dir (derivation-path->output-path drv-path)))
244 (every (match-lambda
245 ((path . source)
246 (equal? (call-with-input-file (string-append dir "/" path)
247 get-bytevector-all)
248 (call-with-input-file source
249 get-bytevector-all))))
250 files)))))
251
26b969de
LC
252(test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
253 0
254 1))
255
256(test-assert "build-expression->derivation for fixed-output derivation"
257 (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
0e383c76
LC
258 (builder
259 `(begin
260 (use-modules (web client) (web uri)
261 (rnrs io ports) (srfi srfi-11))
262 (let-values (((resp bv)
263 (http-get (string->uri ,url) #:decode-body? #f)))
264 (call-with-output-file %output
265 (lambda (p)
266 (put-bytevector p bv))))))
26b969de 267 (drv-path (build-expression->derivation
98090557 268 %store "hello-2.8.tar.gz" (%current-system) builder '()
26b969de
LC
269 #:hash (nix-base32-string->bytevector
270 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
271 #:hash-algo 'sha256))
272 (succeeded? (build-derivations %store (list drv-path))))
273 (and succeeded?
274 (file-exists? (derivation-path->output-path drv-path)))))
275
341c6fdd
LC
276(test-end)
277
278\f
279(exit (= (test-runner-fail-count (test-runner-current)) 0))
280
281;;; Local Variables:
282;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 283;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 284;;; End: