gnu: Add libpcap and jnettop.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
36bbbbd1 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
341c6fdd 3;;;
233e7676 4;;; This file is part of GNU Guix.
341c6fdd 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
341c6fdd
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
341c6fdd
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/>.
341c6fdd
LC
18
19
20(define-module (test-derivations)
21 #:use-module (guix derivations)
26bbbb95 22 #:use-module (guix store)
de4c3f26 23 #:use-module (guix utils)
72626a71 24 #:use-module (guix hash)
ddc29a78 25 #:use-module (guix base32)
36bbbbd1
LC
26 #:use-module ((guix packages) #:select (package-derivation base32))
27 #:use-module ((guix build utils) #:select (executable-file?))
59a43334 28 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
1ffa7090 29 #:use-module (gnu packages bootstrap)
e387ab7c 30 #:use-module ((gnu packages guile) #:select (guile-1.8))
b37eb5ed 31 #:use-module (srfi srfi-1)
fb3eec83 32 #:use-module (srfi srfi-11)
341c6fdd 33 #:use-module (srfi srfi-26)
99634e3f 34 #:use-module (srfi srfi-34)
341c6fdd 35 #:use-module (srfi srfi-64)
fb3eec83 36 #:use-module (rnrs io ports)
749c6567 37 #:use-module (rnrs bytevectors)
dd36b51b 38 #:use-module (web uri)
b37eb5ed 39 #:use-module (ice-9 rdelim)
db393b33 40 #:use-module (ice-9 regex)
99634e3f
LC
41 #:use-module (ice-9 ftw)
42 #:use-module (ice-9 match))
341c6fdd 43
26bbbb95
LC
44(define %store
45 (false-if-exception (open-connection)))
46
b272c474 47(when %store
81dbd783
LC
48 ;; Make sure we build everything by ourselves.
49 (set-build-options %store #:use-substitutes? #f)
50
b272c474
LC
51 ;; By default, use %BOOTSTRAP-GUILE for the current system.
52 (let ((drv (package-derivation %store %bootstrap-guile)))
53 (%guile-for-build drv)))
54
5b0c9d16
LC
55(define (bootstrap-binary name)
56 (let ((bin (search-bootstrap-binary name (%current-system))))
97d3998e 57 (and %store
5b0c9d16
LC
58 (add-to-store %store name #t "sha256" bin))))
59
60(define %bash
61 (bootstrap-binary "bash"))
62(define %mkdir
63 (bootstrap-binary "mkdir"))
97d3998e 64
5b0c9d16 65(define* (directory-contents dir #:optional (slurp get-bytevector-all))
b37eb5ed
LC
66 "Return an alist representing the contents of DIR."
67 (define prefix-len (string-length dir))
68 (sort (file-system-fold (const #t) ; enter?
69 (lambda (path stat result) ; leaf
70 (alist-cons (string-drop path prefix-len)
5b0c9d16 71 (call-with-input-file path slurp)
b37eb5ed
LC
72 result))
73 (lambda (path stat result) result) ; down
74 (lambda (path stat result) result) ; up
75 (lambda (path stat result) result) ; skip
76 (lambda (path stat errno result) result) ; error
77 '()
78 dir)
79 (lambda (e1 e2)
80 (string<? (car e1) (car e2)))))
81
341c6fdd
LC
82(test-begin "derivations")
83
84(test-assert "parse & export"
33594aa4
LC
85 (let* ((f (search-path %load-path "tests/test.drv"))
86 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
87 (d1 (read-derivation (open-bytevector-input-port b1)))
88 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
89 (d2 (read-derivation (open-bytevector-input-port b2))))
90 (and (equal? b1 b2)
91 (equal? d1 d2))))
92
5b0c9d16 93(test-skip (if %store 0 12))
b37eb5ed 94
d1b1c424
LC
95(test-assert "add-to-store, flat"
96 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
a9ebd9ef 97 (drv (add-to-store %store "flat-test" #f "sha256" file)))
d1b1c424 98 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 99 (valid-path? %store drv)
d1b1c424
LC
100 (equal? (call-with-input-file file get-bytevector-all)
101 (call-with-input-file drv get-bytevector-all)))))
102
b37eb5ed
LC
103(test-assert "add-to-store, recursive"
104 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
a9ebd9ef 105 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
b37eb5ed 106 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 107 (valid-path? %store drv)
b37eb5ed
LC
108 (equal? (directory-contents dir)
109 (directory-contents drv)))))
26bbbb95
LC
110
111(test-assert "derivation with no inputs"
31ef99a8 112 (let* ((builder (add-text-to-store %store "my-builder.sh"
97d3998e 113 "echo hello, world\n"
31ef99a8 114 '()))
59688fc4 115 (drv (derivation %store "foo"
97d3998e 116 %bash `("-e" ,builder)
a987d2c0 117 #:env-vars '(("HOME" . "/homeless")))))
59688fc4
LC
118 (and (store-path? (derivation-file-name drv))
119 (valid-path? %store (derivation-file-name drv)))))
26bbbb95 120
fb3eec83 121(test-assert "build derivation with 1 source"
59688fc4
LC
122 (let* ((builder (add-text-to-store %store "my-builder.sh"
123 "echo hello, world > \"$out\"\n"
124 '()))
125 (drv (derivation %store "foo"
126 %bash `(,builder)
127 #:env-vars '(("HOME" . "/homeless")
128 ("zzz" . "Z!")
129 ("AAA" . "A!"))
bde2d9cf 130 #:inputs `((,%bash) (,builder))))
59688fc4
LC
131 (succeeded?
132 (build-derivations %store (list drv))))
fb3eec83 133 (and succeeded?
59688fc4 134 (let ((path (derivation->output-path drv)))
31ef99a8
LC
135 (and (valid-path? %store path)
136 (string=? (call-with-input-file path read-line)
137 "hello, world"))))))
fb3eec83 138
860a6f1a
LC
139(test-assert "derivation with local file as input"
140 (let* ((builder (add-text-to-store
141 %store "my-builder.sh"
bbb76f6f 142 "(while read line ; do echo \"$line\" ; done) < $in > $out"
860a6f1a
LC
143 '()))
144 (input (search-path %load-path "ice-9/boot-9.scm"))
a987d2c0
LC
145 (input* (add-to-store %store (basename input)
146 #t "sha256" input))
59688fc4 147 (drv (derivation %store "derivation-with-input-file"
97d3998e 148 %bash `(,builder)
a987d2c0
LC
149
150 ;; Cheat to pass the actual file name to the
151 ;; builder.
152 #:env-vars `(("in" . ,input*))
153
bde2d9cf
LC
154 #:inputs `((,%bash)
155 (,builder)
a987d2c0 156 (,input))))) ; ← local file name
59688fc4 157 (and (build-derivations %store (list drv))
bbb76f6f
LC
158 ;; Note: we can't compare the files because the above trick alters
159 ;; the contents.
59688fc4 160 (valid-path? %store (derivation->output-path drv)))))
860a6f1a 161
749c6567
LC
162(test-assert "fixed-output derivation"
163 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
164 "echo -n hello > $out" '()))
165 (hash (sha256 (string->utf8 "hello")))
59688fc4 166 (drv (derivation %store "fixed"
97d3998e 167 %bash `(,builder)
a987d2c0 168 #:inputs `((,builder)) ; optional
749c6567 169 #:hash hash #:hash-algo 'sha256))
59688fc4 170 (succeeded? (build-derivations %store (list drv))))
749c6567 171 (and succeeded?
59688fc4 172 (let ((p (derivation->output-path drv)))
82058eff
LC
173 (and (equal? (string->utf8 "hello")
174 (call-with-input-file p get-bytevector-all))
175 (bytevector? (query-path-hash %store p)))))))
749c6567 176
813986ac
LC
177(test-assert "fixed-output derivation: output paths are equal"
178 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
179 "echo -n hello > $out" '()))
180 (builder2 (add-text-to-store %store "fixed-builder2.sh"
181 "echo hey; echo -n hello > $out" '()))
182 (hash (sha256 (string->utf8 "hello")))
59688fc4 183 (drv1 (derivation %store "fixed"
97d3998e 184 %bash `(,builder1)
813986ac 185 #:hash hash #:hash-algo 'sha256))
59688fc4 186 (drv2 (derivation %store "fixed"
97d3998e 187 %bash `(,builder2)
813986ac 188 #:hash hash #:hash-algo 'sha256))
59688fc4 189 (succeeded? (build-derivations %store (list drv1 drv2))))
813986ac 190 (and succeeded?
59688fc4
LC
191 (equal? (derivation->output-path drv1)
192 (derivation->output-path drv2)))))
813986ac 193
36bbbbd1
LC
194(test-assert "fixed-output derivation, recursive"
195 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
196 "echo -n hello > $out" '()))
197 (hash (sha256 (string->utf8 "hello")))
198 (drv (derivation %store "fixed-rec"
199 %bash `(,builder)
200 #:inputs `((,builder))
201 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
202 #:hash-algo 'sha256
203 #:recursive? #t))
204 (succeeded? (build-derivations %store (list drv))))
205 (and succeeded?
206 (let ((p (derivation->output-path drv)))
207 (and (equal? (string->utf8 "hello")
208 (call-with-input-file p get-bytevector-all))
209 (bytevector? (query-path-hash %store p)))))))
210
813986ac
LC
211(test-assert "derivation with a fixed-output input"
212 ;; A derivation D using a fixed-output derivation F doesn't has the same
213 ;; output path when passed F or F', as long as F and F' have the same output
214 ;; path.
215 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
216 "echo -n hello > $out" '()))
217 (builder2 (add-text-to-store %store "fixed-builder2.sh"
218 "echo hey; echo -n hello > $out" '()))
219 (hash (sha256 (string->utf8 "hello")))
a987d2c0 220 (fixed1 (derivation %store "fixed"
97d3998e 221 %bash `(,builder1)
813986ac 222 #:hash hash #:hash-algo 'sha256))
a987d2c0 223 (fixed2 (derivation %store "fixed"
97d3998e 224 %bash `(,builder2)
813986ac 225 #:hash hash #:hash-algo 'sha256))
59688fc4 226 (fixed-out (derivation->output-path fixed1))
813986ac
LC
227 (builder3 (add-text-to-store
228 %store "final-builder.sh"
229 ;; Use Bash hackery to avoid Coreutils.
230 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
a987d2c0 231 (final1 (derivation %store "final"
97d3998e 232 %bash `(,builder3)
a987d2c0 233 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 234 #:inputs `((,%bash) (,builder3) (,fixed1))))
a987d2c0 235 (final2 (derivation %store "final"
97d3998e 236 %bash `(,builder3)
a987d2c0 237 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 238 #:inputs `((,%bash) (,builder3) (,fixed2))))
813986ac
LC
239 (succeeded? (build-derivations %store
240 (list final1 final2))))
241 (and succeeded?
59688fc4
LC
242 (equal? (derivation->output-path final1)
243 (derivation->output-path final2)))))
813986ac 244
7946c4e7
LC
245(test-assert "multiple-output derivation"
246 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
247 "echo one > $out ; echo two > $second"
248 '()))
59688fc4 249 (drv (derivation %store "fixed"
97d3998e 250 %bash `(,builder)
a987d2c0
LC
251 #:env-vars '(("HOME" . "/homeless")
252 ("zzz" . "Z!")
253 ("AAA" . "A!"))
bde2d9cf 254 #:inputs `((,%bash) (,builder))
7946c4e7 255 #:outputs '("out" "second")))
59688fc4 256 (succeeded? (build-derivations %store (list drv))))
7946c4e7 257 (and succeeded?
59688fc4
LC
258 (let ((one (derivation->output-path drv "out"))
259 (two (derivation->output-path drv "second")))
7244a5f7 260 (and (lset= equal?
59688fc4 261 (derivation->output-paths drv)
7244a5f7
LC
262 `(("out" . ,one) ("second" . ,two)))
263 (eq? 'one (call-with-input-file one read))
7946c4e7
LC
264 (eq? 'two (call-with-input-file two read)))))))
265
4b1786aa
LC
266(test-assert "multiple-output derivation, non-alphabetic order"
267 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
268 ;; path computation must reorder them first.
269 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
270 "echo one > $out ; echo two > $AAA"
271 '()))
59688fc4 272 (drv (derivation %store "fixed"
97d3998e 273 %bash `(,builder)
bde2d9cf 274 #:inputs `((,%bash) (,builder))
4b1786aa 275 #:outputs '("out" "AAA")))
59688fc4 276 (succeeded? (build-derivations %store (list drv))))
4b1786aa 277 (and succeeded?
59688fc4
LC
278 (let ((one (derivation->output-path drv "out"))
279 (two (derivation->output-path drv "AAA")))
4b1786aa
LC
280 (and (eq? 'one (call-with-input-file one read))
281 (eq? 'two (call-with-input-file two read)))))))
282
d0dc4907
LC
283(test-assert "multiple-output derivation, derivation-path->output-path"
284 (let* ((builder (add-text-to-store %store "builder.sh"
285 "echo one > $out ; echo two > $second"
286 '()))
287 (drv (derivation %store "multiple"
288 %bash `(,builder)
289 #:outputs '("out" "second")))
290 (drv-file (derivation-file-name drv))
291 (one (derivation->output-path drv "out"))
292 (two (derivation->output-path drv "second"))
293 (first (derivation-path->output-path drv-file "out"))
294 (second (derivation-path->output-path drv-file "second")))
295 (and (not (string=? one two))
296 (string-suffix? "-second" two)
297 (string=? first one)
298 (string=? second two))))
299
d66ac374
LC
300(test-assert "user of multiple-output derivation"
301 ;; Check whether specifying several inputs coming from the same
302 ;; multiple-output derivation works.
303 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
304 "echo one > $out ; echo two > $two"
305 '()))
a987d2c0 306 (mdrv (derivation %store "multiple-output"
97d3998e 307 %bash `(,builder1)
bde2d9cf 308 #:inputs `((,%bash) (,builder1))
d66ac374
LC
309 #:outputs '("out" "two")))
310 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
311 "read x < $one;
312 read y < $two;
313 echo \"($x $y)\" > $out"
314 '()))
315 (udrv (derivation %store "multiple-output-user"
97d3998e 316 %bash `(,builder2)
a987d2c0 317 #:env-vars `(("one"
59688fc4 318 . ,(derivation->output-path
a987d2c0
LC
319 mdrv "out"))
320 ("two"
59688fc4 321 . ,(derivation->output-path
a987d2c0 322 mdrv "two")))
bde2d9cf
LC
323 #:inputs `((,%bash)
324 (,builder2)
a987d2c0
LC
325 ;; two occurrences of MDRV:
326 (,mdrv)
327 (,mdrv "two")))))
d66ac374 328 (and (build-derivations %store (list (pk 'udrv udrv)))
59688fc4 329 (let ((p (derivation->output-path udrv)))
d66ac374
LC
330 (and (valid-path? %store p)
331 (equal? '(one two) (call-with-input-file p read)))))))
332
858e9282 333(test-assert "derivation with #:references-graphs"
5b0c9d16
LC
334 (let* ((input1 (add-text-to-store %store "foo" "hello"
335 (list %bash)))
336 (input2 (add-text-to-store %store "bar"
337 (number->string (random 7777))
338 (list input1)))
339 (builder (add-text-to-store %store "build-graph"
340 (format #f "
341~a $out
342 (while read l ; do echo $l ; done) < bash > $out/bash
343 (while read l ; do echo $l ; done) < input1 > $out/input1
344 (while read l ; do echo $l ; done) < input2 > $out/input2"
345 %mkdir)
346 (list %mkdir)))
347 (drv (derivation %store "closure-graphs"
348 %bash `(,builder)
858e9282 349 #:references-graphs
5b0c9d16
LC
350 `(("bash" . ,%bash)
351 ("input1" . ,input1)
352 ("input2" . ,input2))
353 #:inputs `((,%bash) (,builder))))
59688fc4 354 (out (derivation->output-path drv)))
5b0c9d16
LC
355 (define (deps path . deps)
356 (let ((count (length deps)))
357 (string-append path "\n\n" (number->string count) "\n"
358 (string-join (sort deps string<?) "\n")
359 (if (zero? count) "" "\n"))))
360
361 (and (build-derivations %store (list drv))
362 (equal? (directory-contents out get-string-all)
363 `(("/bash" . ,(string-append %bash "\n\n0\n"))
364 ("/input1" . ,(if (string>? input1 %bash)
365 (string-append (deps %bash)
366 (deps input1 %bash))
367 (string-append (deps input1 %bash)
368 (deps %bash))))
369 ("/input2" . ,(string-concatenate
370 (map cdr
371 (sort
372 (map (lambda (p d)
373 (cons p (apply deps p d)))
374 (list %bash input1 input2)
375 (list '() (list %bash) (list input1)))
376 (lambda (x y)
377 (match x
378 ((p1 . _)
379 (match y
380 ((p2 . _)
381 (string<? p1 p2)))))))))))))))
382
de4c3f26
LC
383\f
384(define %coreutils
8f3ecbd7 385 (false-if-exception
ad1ebab3
LC
386 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
387 (or (package-derivation %store %bootstrap-coreutils&co)
388 (nixpkgs-derivation "coreutils")))))
de4c3f26
LC
389
390(test-skip (if %coreutils 0 1))
391
392(test-assert "build derivation with coreutils"
393 (let* ((builder
394 (add-text-to-store %store "build-with-coreutils.sh"
395 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
396 '()))
59688fc4 397 (drv
a987d2c0 398 (derivation %store "foo"
97d3998e 399 %bash `(,builder)
a987d2c0
LC
400 #:env-vars `(("PATH" .
401 ,(string-append
59688fc4 402 (derivation->output-path %coreutils)
a987d2c0
LC
403 "/bin")))
404 #:inputs `((,builder)
405 (,%coreutils))))
de4c3f26 406 (succeeded?
59688fc4 407 (build-derivations %store (list drv))))
de4c3f26 408 (and succeeded?
59688fc4 409 (let ((p (derivation->output-path drv)))
31ef99a8
LC
410 (and (valid-path? %store p)
411 (file-exists? (string-append p "/good")))))))
de4c3f26 412
9c629a27 413(test-skip (if (%guile-for-build) 0 8))
9a20830e
LC
414
415(test-assert "build-expression->derivation and derivation-prerequisites"
dd1a5a15 416 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
417 (any (match-lambda
418 (($ <derivation-input> path)
59688fc4 419 (string=? path (derivation-file-name (%guile-for-build)))))
9a20830e 420 (derivation-prerequisites drv))))
d9085c23
LC
421
422(test-assert "build-expression->derivation without inputs"
423 (let* ((builder '(begin
424 (mkdir %output)
425 (call-with-output-file (string-append %output "/test")
426 (lambda (p)
427 (display '(hello guix) p)))))
dd1a5a15 428 (drv (build-expression->derivation %store "goo" builder))
59688fc4 429 (succeeded? (build-derivations %store (list drv))))
d9085c23 430 (and succeeded?
59688fc4 431 (let ((p (derivation->output-path drv)))
d9085c23
LC
432 (equal? '(hello guix)
433 (call-with-input-file (string-append p "/test") read))))))
434
969e678e
LC
435(test-assert "build-expression->derivation and max-silent-time"
436 (let* ((store (let ((s (open-connection)))
437 (set-build-options s #:max-silent-time 1)
438 s))
01e82af5 439 (builder '(begin (sleep 100) (mkdir %output) #t))
dd1a5a15 440 (drv (build-expression->derivation store "silent" builder))
59688fc4 441 (out-path (derivation->output-path drv)))
969e678e
LC
442 (guard (c ((nix-protocol-error? c)
443 (and (string-contains (nix-protocol-error-message c)
444 "failed")
445 (not (valid-path? store out-path)))))
01e82af5
LC
446 (build-derivations store (list drv))
447 #f)))
969e678e 448
9a20830e 449(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
dd1a5a15 450 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
451 ;; The only direct dependency is (%guile-for-build) and it's already
452 ;; built.
453 (null? (derivation-prerequisites-to-build %store drv))))
454
784bb1f3 455(test-assert "derivation-prerequisites-to-build when outputs already present"
59688fc4 456 (let* ((builder '(begin (mkdir %output) #t))
dd1a5a15 457 (input-drv (build-expression->derivation %store "input" builder))
59688fc4
LC
458 (input-path (derivation-output-path
459 (assoc-ref (derivation-outputs input-drv)
460 "out")))
dd1a5a15
LC
461 (drv (build-expression->derivation %store "something" builder
462 #:inputs
59688fc4
LC
463 `(("i" ,input-drv))))
464 (output (derivation->output-path drv)))
784bb1f3
LC
465 ;; Make sure these things are not already built.
466 (when (valid-path? %store input-path)
467 (delete-paths %store (list input-path)))
468 (when (valid-path? %store output)
469 (delete-paths %store (list output)))
470
471 (and (equal? (map derivation-input-path
472 (derivation-prerequisites-to-build %store drv))
59688fc4 473 (list (derivation-file-name input-drv)))
784bb1f3
LC
474
475 ;; Build DRV and delete its input.
59688fc4 476 (build-derivations %store (list drv))
784bb1f3
LC
477 (delete-paths %store (list input-path))
478 (not (valid-path? %store input-path))
479
480 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
481 ;; prerequisite to build because DRV itself is already built.
482 (null? (derivation-prerequisites-to-build %store drv)))))
483
dd36b51b
LC
484(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
485(test-assert "derivation-prerequisites-to-build and substitutes"
59688fc4
LC
486 (let* ((store (open-connection))
487 (drv (build-expression->derivation store "prereq-subst"
dd1a5a15 488 (random 1000)))
59688fc4
LC
489 (output (derivation->output-path drv))
490 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
dd36b51b
LC
491 (compose uri-path string->uri))))
492 ;; Create fake substituter data, to be read by `substitute-binary'.
493 (call-with-output-file (string-append dir "/nix-cache-info")
494 (lambda (p)
495 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
496 (%store-prefix))))
497 (call-with-output-file (string-append dir "/" (store-path-hash-part output)
498 ".narinfo")
499 (lambda (p)
500 (format p "StorePath: ~a
501URL: ~a
502Compression: none
503NarSize: 1234
504References:
505System: ~a
506Deriver: ~a~%"
507 output ; StorePath
508 (string-append dir "/example.nar") ; URL
509 (%current-system) ; System
59688fc4
LC
510 (basename
511 (derivation-file-name drv))))) ; Deriver
dd36b51b
LC
512
513 (let-values (((build download)
514 (derivation-prerequisites-to-build store drv))
515 ((build* download*)
516 (derivation-prerequisites-to-build store drv
517 #:use-substitutes? #f)))
518 (pk build download build* download*)
519 (and (null? build)
520 (equal? download (list output))
521 (null? download*)
522 (null? build*)))))
523
db393b33
LC
524(test-assert "build-expression->derivation with expression returning #f"
525 (let* ((builder '(begin
526 (mkdir %output)
527 #f)) ; fail!
dd1a5a15 528 (drv (build-expression->derivation %store "fail" builder))
59688fc4 529 (out-path (derivation->output-path drv)))
db393b33
LC
530 (guard (c ((nix-protocol-error? c)
531 ;; Note that the output path may exist at this point, but it
532 ;; is invalid.
31ef99a8
LC
533 (and (string-match "build .* failed"
534 (nix-protocol-error-message c))
535 (not (valid-path? %store out-path)))))
59688fc4 536 (build-derivations %store (list drv))
db393b33
LC
537 #f)))
538
9bc07f4d
LC
539(test-assert "build-expression->derivation with two outputs"
540 (let* ((builder '(begin
541 (call-with-output-file (assoc-ref %outputs "out")
542 (lambda (p)
543 (display '(hello) p)))
544 (call-with-output-file (assoc-ref %outputs "second")
545 (lambda (p)
546 (display '(world) p)))))
dd1a5a15 547 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
548 #:outputs '("out"
549 "second")))
59688fc4 550 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 551 (and succeeded?
59688fc4
LC
552 (let ((one (derivation->output-path drv))
553 (two (derivation->output-path drv "second")))
9bc07f4d
LC
554 (and (equal? '(hello) (call-with-input-file one read))
555 (equal? '(world) (call-with-input-file two read)))))))
556
ad1ebab3 557(test-skip (if %coreutils 0 1))
d9085c23
LC
558(test-assert "build-expression->derivation with one input"
559 (let* ((builder '(call-with-output-file %output
560 (lambda (p)
561 (let ((cu (assoc-ref %build-inputs "cu")))
562 (close 1)
563 (dup2 (port->fdes p) 1)
564 (execl (string-append cu "/bin/uname")
565 "uname" "-a")))))
dd1a5a15
LC
566 (drv (build-expression->derivation %store "uname" builder
567 #:inputs
2acb2cb6 568 `(("cu" ,%coreutils))))
59688fc4 569 (succeeded? (build-derivations %store (list drv))))
d9085c23 570 (and succeeded?
59688fc4 571 (let ((p (derivation->output-path drv)))
d9085c23
LC
572 (string-contains (call-with-input-file p read-line) "GNU")))))
573
99634e3f
LC
574(test-assert "imported-files"
575 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
576 ("a/b/c" . ,(search-path %load-path
577 "guix/derivations.scm"))
224f7ad6
LC
578 ("p/q" . ,(search-path %load-path "guix.scm"))
579 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
59688fc4
LC
580 (drv (imported-files %store files)))
581 (and (build-derivations %store (list drv))
582 (let ((dir (derivation->output-path drv)))
99634e3f
LC
583 (every (match-lambda
584 ((path . source)
585 (equal? (call-with-input-file (string-append dir "/" path)
586 get-bytevector-all)
587 (call-with-input-file source
588 get-bytevector-all))))
589 files)))))
590
d9024884
LC
591(test-assert "build-expression->derivation with modules"
592 (let* ((builder `(begin
593 (use-modules (guix build utils))
594 (let ((out (assoc-ref %outputs "out")))
595 (mkdir-p (string-append out "/guile/guix/nix"))
596 #t)))
59688fc4 597 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 598 builder
d9024884
LC
599 #:modules
600 '((guix build utils)))))
59688fc4
LC
601 (and (build-derivations %store (list drv))
602 (let* ((p (derivation->output-path drv))
d9024884
LC
603 (s (stat (string-append p "/guile/guix/nix"))))
604 (eq? (stat:type s) 'directory)))))
605
813986ac
LC
606(test-assert "build-expression->derivation: same fixed-output path"
607 (let* ((builder1 '(call-with-output-file %output
608 (lambda (p)
609 (write "hello" p))))
610 (builder2 '(call-with-output-file (pk 'difference-here! %output)
611 (lambda (p)
612 (write "hello" p))))
613 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 614 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
615 #:hash hash
616 #:hash-algo 'sha256))
dd1a5a15 617 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
618 #:hash hash
619 #:hash-algo 'sha256))
620 (succeeded? (build-derivations %store (list input1 input2))))
621 (and succeeded?
59688fc4
LC
622 (not (string=? (derivation-file-name input1)
623 (derivation-file-name input2)))
624 (string=? (derivation->output-path input1)
625 (derivation->output-path input2)))))
813986ac 626
7bdd1f0e
LC
627(test-assert "build-expression->derivation with a fixed-output input"
628 (let* ((builder1 '(call-with-output-file %output
629 (lambda (p)
630 (write "hello" p))))
631 (builder2 '(call-with-output-file (pk 'difference-here! %output)
632 (lambda (p)
633 (write "hello" p))))
634 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 635 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
636 #:hash hash
637 #:hash-algo 'sha256))
dd1a5a15 638 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
639 #:hash hash
640 #:hash-algo 'sha256))
641 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
642 (call-with-output-file %output
643 (lambda (out)
644 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
645 (final1 (build-expression->derivation %store "final" builder3
646 #:inputs
7bdd1f0e 647 `(("input" ,input1))))
dd1a5a15
LC
648 (final2 (build-expression->derivation %store "final" builder3
649 #:inputs
7bdd1f0e 650 `(("input" ,input2)))))
59688fc4
LC
651 (and (string=? (derivation->output-path final1)
652 (derivation->output-path final2))
653 (string=? (derivation->output-path final1)
654 (derivation-path->output-path
655 (derivation-file-name final1)))
7bdd1f0e
LC
656 (build-derivations %store (list final1 final2)))))
657
36bbbbd1
LC
658(test-assert "build-expression->derivation produces recursive fixed-output"
659 (let* ((builder '(begin
660 (use-modules (srfi srfi-26))
661 (mkdir %output)
662 (chdir %output)
663 (call-with-output-file "exe"
664 (cut display "executable" <>))
665 (chmod "exe" #o777)
666 (symlink "exe" "symlink")
667 (mkdir "subdir")))
668 (drv (build-expression->derivation %store "fixed-rec" builder
669 #:hash-algo 'sha256
670 #:hash (base32
671 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
672 #:recursive? #t)))
673 (and (build-derivations %store (list drv))
674 (let* ((dir (derivation->output-path drv))
675 (exe (string-append dir "/exe"))
676 (link (string-append dir "/symlink"))
677 (subdir (string-append dir "/subdir")))
678 (and (executable-file? exe)
679 (string=? "executable"
680 (call-with-input-file exe get-string-all))
681 (string=? "exe" (readlink link))
682 (file-is-directory? subdir))))))
683
684(test-assert "build-expression->derivation uses recursive fixed-output"
685 (let* ((builder '(call-with-output-file %output
686 (lambda (port)
687 (display "hello" port))))
688 (fixed (build-expression->derivation %store "small-fixed-rec"
689 builder
690 #:hash-algo 'sha256
691 #:hash (base32
692 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
693 #:recursive? #t))
694 (in (derivation->output-path fixed))
695 (builder `(begin
696 (mkdir %output)
697 (chdir %output)
698 (symlink ,in "symlink")))
699 (drv (build-expression->derivation %store "fixed-rec-user"
700 builder
701 #:inputs `(("fixed" ,fixed)))))
702 (and (build-derivations %store (list drv))
703 (let ((out (derivation->output-path drv)))
704 (string=? (readlink (string-append out "/symlink")) in)))))
705
858e9282 706(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
707 (let* ((input (add-text-to-store %store "foo" "hello"
708 (list %bash %mkdir)))
709 (builder '(copy-file "input" %output))
858e9282 710 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 711 builder
858e9282 712 #:references-graphs
9c629a27 713 `(("input" . ,input))))
59688fc4 714 (out (derivation->output-path drv)))
9c629a27
LC
715 (define (deps path . deps)
716 (let ((count (length deps)))
717 (string-append path "\n\n" (number->string count) "\n"
718 (string-join (sort deps string<?) "\n")
719 (if (zero? count) "" "\n"))))
720
721 (and (build-derivations %store (list drv))
722 (equal? (call-with-input-file out get-string-all)
723 (string-concatenate
724 (map cdr
725 (sort (map (lambda (p d)
726 (cons p (apply deps p d)))
727 (list input %bash %mkdir)
728 (list (list %bash %mkdir)
729 '() '()))
730 (lambda (x y)
731 (match x
732 ((p1 . _)
733 (match y
734 ((p2 . _)
735 (string<? p1 p2)))))))))))))
736
e387ab7c
LC
737
738(test-equal "map-derivation"
739 "hello"
740 (let* ((joke (package-derivation %store guile-1.8))
741 (good (package-derivation %store %bootstrap-guile))
742 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 743 #f ; systematically fail
e387ab7c
LC
744 #:guile-for-build joke))
745 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
746 '(call-with-output-file %output
747 (lambda (p)
dd1a5a15 748 (display "hello" p)))))
e387ab7c 749 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
750 '(let ((in (assoc-ref
751 %build-inputs "in")))
752 (copy-file in %output))
dd1a5a15 753 #:inputs `(("in" ,drv1))
e387ab7c
LC
754 #:guile-for-build joke))
755 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
756 (,joke . ,good))))
757 (out (derivation->output-path drv4)))
758 (and (build-derivations %store (list (pk 'remapped drv4)))
759 (call-with-input-file out get-string-all))))
760
a716e36d
LC
761(test-equal "map-derivation, sources"
762 "hello"
763 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
764 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
765 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
766 (drv1 (derivation %store "drv-to-remap"
767
768 ;; XXX: This wouldn't work in practice, but if
769 ;; we append "/bin/bash" then we can't replace
770 ;; it with the bootstrap bash, which is a
771 ;; single file.
772 (derivation->output-path bash-full)
773
774 `("-e" ,script1)
775 #:inputs `((,bash-full) (,script1))))
776 (drv2 (map-derivation %store drv1
777 `((,bash-full . ,%bash)
778 (,script1 . ,script2))))
779 (out (derivation->output-path drv2)))
780 (and (build-derivations %store (list (pk 'remapped* drv2)))
781 (call-with-input-file out get-string-all))))
782
341c6fdd
LC
783(test-end)
784
785\f
786(exit (= (test-runner-fail-count (test-runner-current)) 0))