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