gnu: xf86-video-nouveau: Update to 1.0.15.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
9b5364a3 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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 18
94d92c77
LC
19(unsetenv "http_proxy")
20
341c6fdd
LC
21(define-module (test-derivations)
22 #:use-module (guix derivations)
ef8de985 23 #:use-module (guix grafts)
26bbbb95 24 #:use-module (guix store)
de4c3f26 25 #:use-module (guix utils)
72626a71 26 #:use-module (guix hash)
ddc29a78 27 #:use-module (guix base32)
c1bc358f 28 #:use-module (guix tests)
94d92c77 29 #:use-module (guix tests http)
36bbbbd1
LC
30 #:use-module ((guix packages) #:select (package-derivation base32))
31 #:use-module ((guix build utils) #:select (executable-file?))
59a43334 32 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
1ffa7090 33 #:use-module (gnu packages bootstrap)
e387ab7c 34 #:use-module ((gnu packages guile) #:select (guile-1.8))
b37eb5ed 35 #:use-module (srfi srfi-1)
fb3eec83 36 #:use-module (srfi srfi-11)
341c6fdd 37 #:use-module (srfi srfi-26)
99634e3f 38 #:use-module (srfi srfi-34)
341c6fdd 39 #:use-module (srfi srfi-64)
fb3eec83 40 #:use-module (rnrs io ports)
749c6567 41 #:use-module (rnrs bytevectors)
dd36b51b 42 #:use-module (web uri)
b37eb5ed 43 #:use-module (ice-9 rdelim)
db393b33 44 #:use-module (ice-9 regex)
99634e3f
LC
45 #:use-module (ice-9 ftw)
46 #:use-module (ice-9 match))
341c6fdd 47
26bbbb95 48(define %store
c1bc358f 49 (open-connection-for-tests))
b272c474 50
ef8de985
LC
51;; Globally disable grafts because they can trigger early builds.
52(%graft? #f)
53
5b0c9d16
LC
54(define (bootstrap-binary name)
55 (let ((bin (search-bootstrap-binary name (%current-system))))
97d3998e 56 (and %store
5b0c9d16
LC
57 (add-to-store %store name #t "sha256" bin))))
58
59(define %bash
60 (bootstrap-binary "bash"))
61(define %mkdir
62 (bootstrap-binary "mkdir"))
97d3998e 63
5b0c9d16 64(define* (directory-contents dir #:optional (slurp get-bytevector-all))
b37eb5ed
LC
65 "Return an alist representing the contents of DIR."
66 (define prefix-len (string-length dir))
67 (sort (file-system-fold (const #t) ; enter?
68 (lambda (path stat result) ; leaf
69 (alist-cons (string-drop path prefix-len)
5b0c9d16 70 (call-with-input-file path slurp)
b37eb5ed
LC
71 result))
72 (lambda (path stat result) result) ; down
73 (lambda (path stat result) result) ; up
74 (lambda (path stat result) result) ; skip
75 (lambda (path stat errno result) result) ; error
76 '()
77 dir)
78 (lambda (e1 e2)
79 (string<? (car e1) (car e2)))))
80
94d92c77
LC
81;; Avoid collisions with other tests.
82(%http-server-port 10500)
83
ef8de985 84\f
341c6fdd
LC
85(test-begin "derivations")
86
87(test-assert "parse & export"
33594aa4
LC
88 (let* ((f (search-path %load-path "tests/test.drv"))
89 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
90 (d1 (read-derivation (open-bytevector-input-port b1)))
91 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
92 (d2 (read-derivation (open-bytevector-input-port b2))))
93 (and (equal? b1 b2)
94 (equal? d1 d2))))
95
5b0c9d16 96(test-skip (if %store 0 12))
b37eb5ed 97
d1b1c424
LC
98(test-assert "add-to-store, flat"
99 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
a9ebd9ef 100 (drv (add-to-store %store "flat-test" #f "sha256" file)))
d1b1c424 101 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 102 (valid-path? %store drv)
d1b1c424
LC
103 (equal? (call-with-input-file file get-bytevector-all)
104 (call-with-input-file drv get-bytevector-all)))))
105
b37eb5ed
LC
106(test-assert "add-to-store, recursive"
107 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
a9ebd9ef 108 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
b37eb5ed 109 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 110 (valid-path? %store drv)
b37eb5ed
LC
111 (equal? (directory-contents dir)
112 (directory-contents drv)))))
26bbbb95
LC
113
114(test-assert "derivation with no inputs"
31ef99a8 115 (let* ((builder (add-text-to-store %store "my-builder.sh"
97d3998e 116 "echo hello, world\n"
31ef99a8 117 '()))
59688fc4 118 (drv (derivation %store "foo"
97d3998e 119 %bash `("-e" ,builder)
a987d2c0 120 #:env-vars '(("HOME" . "/homeless")))))
59688fc4
LC
121 (and (store-path? (derivation-file-name drv))
122 (valid-path? %store (derivation-file-name drv)))))
26bbbb95 123
fb3eec83 124(test-assert "build derivation with 1 source"
59688fc4
LC
125 (let* ((builder (add-text-to-store %store "my-builder.sh"
126 "echo hello, world > \"$out\"\n"
127 '()))
128 (drv (derivation %store "foo"
129 %bash `(,builder)
130 #:env-vars '(("HOME" . "/homeless")
131 ("zzz" . "Z!")
132 ("AAA" . "A!"))
bde2d9cf 133 #:inputs `((,%bash) (,builder))))
59688fc4
LC
134 (succeeded?
135 (build-derivations %store (list drv))))
fb3eec83 136 (and succeeded?
59688fc4 137 (let ((path (derivation->output-path drv)))
31ef99a8
LC
138 (and (valid-path? %store path)
139 (string=? (call-with-input-file path read-line)
140 "hello, world"))))))
fb3eec83 141
860a6f1a
LC
142(test-assert "derivation with local file as input"
143 (let* ((builder (add-text-to-store
144 %store "my-builder.sh"
bbb76f6f 145 "(while read line ; do echo \"$line\" ; done) < $in > $out"
860a6f1a
LC
146 '()))
147 (input (search-path %load-path "ice-9/boot-9.scm"))
a987d2c0
LC
148 (input* (add-to-store %store (basename input)
149 #t "sha256" input))
59688fc4 150 (drv (derivation %store "derivation-with-input-file"
97d3998e 151 %bash `(,builder)
a987d2c0
LC
152
153 ;; Cheat to pass the actual file name to the
154 ;; builder.
155 #:env-vars `(("in" . ,input*))
156
bde2d9cf
LC
157 #:inputs `((,%bash)
158 (,builder)
a987d2c0 159 (,input))))) ; ← local file name
59688fc4 160 (and (build-derivations %store (list drv))
bbb76f6f
LC
161 ;; Note: we can't compare the files because the above trick alters
162 ;; the contents.
59688fc4 163 (valid-path? %store (derivation->output-path drv)))))
860a6f1a 164
0e510971
LC
165(test-assert "derivation fails but keep going"
166 ;; In keep-going mode, 'build-derivations' should fail because of D1, but it
167 ;; must return only after D2 has succeeded.
168 (with-store store
169 (let* ((d1 (derivation %store "fails"
170 %bash `("-c" "false")
171 #:inputs `((,%bash))))
172 (d2 (build-expression->derivation %store "sleep-then-succeed"
173 `(begin
174 ,(random-text)
175 ;; XXX: Hopefully that's long
176 ;; enough that D1 has already
177 ;; failed.
178 (sleep 2)
179 (mkdir %output)))))
180 (set-build-options %store
181 #:use-substitutes? #f
182 #:keep-going? #t)
183 (guard (c ((nix-protocol-error? c)
184 (and (= 100 (nix-protocol-error-status c))
185 (string-contains (nix-protocol-error-message c)
186 (derivation-file-name d1))
187 (not (valid-path? %store (derivation->output-path d1)))
188 (valid-path? %store (derivation->output-path d2)))))
189 (build-derivations %store (list d1 d2))
190 #f))))
191
44d43c7a
LC
192(test-assert "identical files are deduplicated"
193 (let* ((build1 (add-text-to-store %store "one.sh"
194 "echo hello, world > \"$out\"\n"
195 '()))
196 (build2 (add-text-to-store %store "two.sh"
197 "# Hey!\necho hello, world > \"$out\"\n"
198 '()))
199 (drv1 (derivation %store "foo"
200 %bash `(,build1)
201 #:inputs `((,%bash) (,build1))))
202 (drv2 (derivation %store "bar"
203 %bash `(,build2)
204 #:inputs `((,%bash) (,build2)))))
205 (and (build-derivations %store (list drv1 drv2))
206 (let ((file1 (derivation->output-path drv1))
207 (file2 (derivation->output-path drv2)))
208 (and (valid-path? %store file1) (valid-path? %store file2)
209 (string=? (call-with-input-file file1 get-string-all)
210 "hello, world\n")
211 (= (stat:ino (lstat file1))
212 (stat:ino (lstat file2))))))))
213
f9aefa2d
LC
214(test-equal "built-in-builders"
215 '("download")
216 (built-in-builders %store))
217
94d92c77
LC
218(test-assert "unknown built-in builder"
219 (let ((drv (derivation %store "ohoh" "builtin:does-not-exist" '())))
220 (guard (c ((nix-protocol-error? c)
221 (string-contains (nix-protocol-error-message c) "failed")))
222 (build-derivations %store (list drv))
223 #f)))
224
225(unless (force %http-server-socket)
226 (test-skip 1))
227(test-assert "'download' built-in builder"
228 (let ((text (random-text)))
229 (with-http-server 200 text
230 (let* ((drv (derivation %store "world"
231 "builtin:download" '()
232 #:env-vars `(("url"
233 . ,(object->string (%local-url))))
234 #:hash-algo 'sha256
235 #:hash (sha256 (string->utf8 text)))))
236 (and (build-derivations %store (list drv))
237 (string=? (call-with-input-file (derivation->output-path drv)
238 get-string-all)
239 text))))))
240
241(unless (force %http-server-socket)
242 (test-skip 1))
243(test-assert "'download' built-in builder, invalid hash"
244 (with-http-server 200 "hello, world!"
245 (let* ((drv (derivation %store "world"
246 "builtin:download" '()
247 #:env-vars `(("url"
248 . ,(object->string (%local-url))))
249 #:hash-algo 'sha256
250 #:hash (sha256 (random-bytevector 100))))) ;wrong
251 (guard (c ((nix-protocol-error? c)
252 (string-contains (nix-protocol-error-message c) "failed")))
253 (build-derivations %store (list drv))
254 #f))))
255
256(unless (force %http-server-socket)
257 (test-skip 1))
258(test-assert "'download' built-in builder, not found"
259 (with-http-server 404 "not found"
260 (let* ((drv (derivation %store "will-never-be-found"
261 "builtin:download" '()
262 #:env-vars `(("url"
263 . ,(object->string (%local-url))))
264 #:hash-algo 'sha256
265 #:hash (sha256 (random-bytevector 100)))))
266 (guard (c ((nix-protocol-error? c)
267 (string-contains (nix-protocol-error-message (pk c)) "failed")))
268 (build-derivations %store (list drv))
269 #f))))
270
271(test-assert "'download' built-in builder, not fixed-output"
272 (let* ((source (add-text-to-store %store "hello" "hi!"))
273 (url (string-append "file://" source))
274 (drv (derivation %store "world"
275 "builtin:download" '()
276 #:env-vars `(("url" . ,(object->string url))))))
277 (guard (c ((nix-protocol-error? c)
278 (string-contains (nix-protocol-error-message c) "failed")))
279 (build-derivations %store (list drv))
280 #f)))
281
9b5364a3
LC
282(unless (force %http-server-socket)
283 (test-skip 1))
284(test-assert "'download' built-in builder, check mode"
285 ;; Make sure rebuilding the 'builtin:download' derivation in check mode
286 ;; works. See <http://bugs.gnu.org/25089>.
287 (let* ((text (random-text))
288 (drv (derivation %store "world"
289 "builtin:download" '()
290 #:env-vars `(("url"
291 . ,(object->string (%local-url))))
292 #:hash-algo 'sha256
293 #:hash (sha256 (string->utf8 text)))))
294 (and (with-http-server 200 text
295 (build-derivations %store (list drv)))
296 (with-http-server 200 text
297 (build-derivations %store (list drv)
298 (build-mode check)))
299 (string=? (call-with-input-file (derivation->output-path drv)
300 get-string-all)
301 text))))
302
e786293e
LC
303(test-equal "derivation-name"
304 "foo-0.0"
305 (let ((drv (derivation %store "foo-0.0" %bash '())))
306 (derivation-name drv)))
307
0b6af195
LC
308(test-equal "derivation-output-names"
309 '(("out") ("bar" "chbouib"))
310 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
311 (drv2 (derivation %store "foo-0.0" %bash '()
312 #:outputs '("bar" "chbouib"))))
313 (list (derivation-output-names drv1)
314 (derivation-output-names drv2))))
315
fc93e309
LC
316(test-assert "offloadable-derivation?"
317 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
4a6aeb67
LC
318 (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
319 (derivation %store "foo" %bash '()
320 #:substitutable? #f))
fc93e309
LC
321 (not (offloadable-derivation?
322 (derivation %store "foo" %bash '()
323 #:local-build? #t)))))
324
4a6aeb67
LC
325(test-assert "substitutable-derivation?"
326 (and (substitutable-derivation? (derivation %store "foo" %bash '()))
327 (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
328 (derivation %store "foo" %bash '()
cfc5d398 329 #:local-build? #t))
4a6aeb67
LC
330 (not (substitutable-derivation?
331 (derivation %store "foo" %bash '()
332 #:substitutable? #f)))))
333
99e17dc9
LC
334(test-assert "fixed-output-derivation?"
335 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
336 "echo -n hello > $out" '()))
337 (hash (sha256 (string->utf8 "hello")))
338 (drv (derivation %store "fixed"
339 %bash `(,builder)
340 #:inputs `((,builder))
341 #:hash hash #:hash-algo 'sha256)))
342 (fixed-output-derivation? drv)))
343
749c6567
LC
344(test-assert "fixed-output derivation"
345 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
346 "echo -n hello > $out" '()))
347 (hash (sha256 (string->utf8 "hello")))
59688fc4 348 (drv (derivation %store "fixed"
97d3998e 349 %bash `(,builder)
a987d2c0 350 #:inputs `((,builder)) ; optional
749c6567 351 #:hash hash #:hash-algo 'sha256))
59688fc4 352 (succeeded? (build-derivations %store (list drv))))
749c6567 353 (and succeeded?
59688fc4 354 (let ((p (derivation->output-path drv)))
82058eff
LC
355 (and (equal? (string->utf8 "hello")
356 (call-with-input-file p get-bytevector-all))
357 (bytevector? (query-path-hash %store p)))))))
749c6567 358
813986ac
LC
359(test-assert "fixed-output derivation: output paths are equal"
360 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
361 "echo -n hello > $out" '()))
362 (builder2 (add-text-to-store %store "fixed-builder2.sh"
363 "echo hey; echo -n hello > $out" '()))
364 (hash (sha256 (string->utf8 "hello")))
59688fc4 365 (drv1 (derivation %store "fixed"
97d3998e 366 %bash `(,builder1)
813986ac 367 #:hash hash #:hash-algo 'sha256))
59688fc4 368 (drv2 (derivation %store "fixed"
97d3998e 369 %bash `(,builder2)
813986ac 370 #:hash hash #:hash-algo 'sha256))
59688fc4 371 (succeeded? (build-derivations %store (list drv1 drv2))))
813986ac 372 (and succeeded?
59688fc4
LC
373 (equal? (derivation->output-path drv1)
374 (derivation->output-path drv2)))))
813986ac 375
36bbbbd1
LC
376(test-assert "fixed-output derivation, recursive"
377 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
378 "echo -n hello > $out" '()))
379 (hash (sha256 (string->utf8 "hello")))
380 (drv (derivation %store "fixed-rec"
381 %bash `(,builder)
382 #:inputs `((,builder))
383 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
384 #:hash-algo 'sha256
385 #:recursive? #t))
386 (succeeded? (build-derivations %store (list drv))))
387 (and succeeded?
388 (let ((p (derivation->output-path drv)))
389 (and (equal? (string->utf8 "hello")
390 (call-with-input-file p get-bytevector-all))
391 (bytevector? (query-path-hash %store p)))))))
392
813986ac
LC
393(test-assert "derivation with a fixed-output input"
394 ;; A derivation D using a fixed-output derivation F doesn't has the same
395 ;; output path when passed F or F', as long as F and F' have the same output
396 ;; path.
397 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
398 "echo -n hello > $out" '()))
399 (builder2 (add-text-to-store %store "fixed-builder2.sh"
400 "echo hey; echo -n hello > $out" '()))
401 (hash (sha256 (string->utf8 "hello")))
a987d2c0 402 (fixed1 (derivation %store "fixed"
97d3998e 403 %bash `(,builder1)
813986ac 404 #:hash hash #:hash-algo 'sha256))
a987d2c0 405 (fixed2 (derivation %store "fixed"
97d3998e 406 %bash `(,builder2)
813986ac 407 #:hash hash #:hash-algo 'sha256))
59688fc4 408 (fixed-out (derivation->output-path fixed1))
813986ac
LC
409 (builder3 (add-text-to-store
410 %store "final-builder.sh"
411 ;; Use Bash hackery to avoid Coreutils.
412 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
a987d2c0 413 (final1 (derivation %store "final"
97d3998e 414 %bash `(,builder3)
a987d2c0 415 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 416 #:inputs `((,%bash) (,builder3) (,fixed1))))
a987d2c0 417 (final2 (derivation %store "final"
97d3998e 418 %bash `(,builder3)
a987d2c0 419 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 420 #:inputs `((,%bash) (,builder3) (,fixed2))))
813986ac
LC
421 (succeeded? (build-derivations %store
422 (list final1 final2))))
423 (and succeeded?
59688fc4
LC
424 (equal? (derivation->output-path final1)
425 (derivation->output-path final2)))))
813986ac 426
7946c4e7
LC
427(test-assert "multiple-output derivation"
428 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
429 "echo one > $out ; echo two > $second"
430 '()))
59688fc4 431 (drv (derivation %store "fixed"
97d3998e 432 %bash `(,builder)
a987d2c0
LC
433 #:env-vars '(("HOME" . "/homeless")
434 ("zzz" . "Z!")
435 ("AAA" . "A!"))
bde2d9cf 436 #:inputs `((,%bash) (,builder))
7946c4e7 437 #:outputs '("out" "second")))
59688fc4 438 (succeeded? (build-derivations %store (list drv))))
7946c4e7 439 (and succeeded?
59688fc4
LC
440 (let ((one (derivation->output-path drv "out"))
441 (two (derivation->output-path drv "second")))
7244a5f7 442 (and (lset= equal?
59688fc4 443 (derivation->output-paths drv)
7244a5f7
LC
444 `(("out" . ,one) ("second" . ,two)))
445 (eq? 'one (call-with-input-file one read))
7946c4e7
LC
446 (eq? 'two (call-with-input-file two read)))))))
447
4b1786aa
LC
448(test-assert "multiple-output derivation, non-alphabetic order"
449 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
450 ;; path computation must reorder them first.
451 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
452 "echo one > $out ; echo two > $AAA"
453 '()))
59688fc4 454 (drv (derivation %store "fixed"
97d3998e 455 %bash `(,builder)
bde2d9cf 456 #:inputs `((,%bash) (,builder))
4b1786aa 457 #:outputs '("out" "AAA")))
59688fc4 458 (succeeded? (build-derivations %store (list drv))))
4b1786aa 459 (and succeeded?
59688fc4
LC
460 (let ((one (derivation->output-path drv "out"))
461 (two (derivation->output-path drv "AAA")))
4b1786aa
LC
462 (and (eq? 'one (call-with-input-file one read))
463 (eq? 'two (call-with-input-file two read)))))))
464
97507ebe
LC
465(test-assert "read-derivation vs. derivation"
466 ;; Make sure 'derivation' and 'read-derivation' return objects that are
467 ;; identical.
468 (let* ((sources (unfold (cut >= <> 10)
469 (lambda (n)
470 (add-text-to-store %store
471 (format #f "input~a" n)
472 (random-text)))
473 1+
474 0))
475 (inputs (map (lambda (file)
476 (derivation %store "derivation-input"
477 %bash '()
478 #:inputs `((,%bash) (,file))))
479 sources))
480 (builder (add-text-to-store %store "builder.sh"
481 "echo one > $one ; echo two > $two"
482 '()))
483 (drv (derivation %store "derivation"
484 %bash `(,builder)
485 #:inputs `((,%bash) (,builder)
486 ,@(map list (append sources inputs)))
487 #:outputs '("two" "one")))
488 (drv* (call-with-input-file (derivation-file-name drv)
489 read-derivation)))
490 (equal? drv* drv)))
491
d0dc4907
LC
492(test-assert "multiple-output derivation, derivation-path->output-path"
493 (let* ((builder (add-text-to-store %store "builder.sh"
494 "echo one > $out ; echo two > $second"
495 '()))
496 (drv (derivation %store "multiple"
497 %bash `(,builder)
498 #:outputs '("out" "second")))
499 (drv-file (derivation-file-name drv))
500 (one (derivation->output-path drv "out"))
501 (two (derivation->output-path drv "second"))
502 (first (derivation-path->output-path drv-file "out"))
503 (second (derivation-path->output-path drv-file "second")))
504 (and (not (string=? one two))
505 (string-suffix? "-second" two)
506 (string=? first one)
507 (string=? second two))))
508
d66ac374
LC
509(test-assert "user of multiple-output derivation"
510 ;; Check whether specifying several inputs coming from the same
511 ;; multiple-output derivation works.
512 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
513 "echo one > $out ; echo two > $two"
514 '()))
a987d2c0 515 (mdrv (derivation %store "multiple-output"
97d3998e 516 %bash `(,builder1)
bde2d9cf 517 #:inputs `((,%bash) (,builder1))
d66ac374
LC
518 #:outputs '("out" "two")))
519 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
520 "read x < $one;
521 read y < $two;
522 echo \"($x $y)\" > $out"
523 '()))
524 (udrv (derivation %store "multiple-output-user"
97d3998e 525 %bash `(,builder2)
a987d2c0 526 #:env-vars `(("one"
59688fc4 527 . ,(derivation->output-path
a987d2c0
LC
528 mdrv "out"))
529 ("two"
59688fc4 530 . ,(derivation->output-path
a987d2c0 531 mdrv "two")))
bde2d9cf
LC
532 #:inputs `((,%bash)
533 (,builder2)
a987d2c0
LC
534 ;; two occurrences of MDRV:
535 (,mdrv)
536 (,mdrv "two")))))
d66ac374 537 (and (build-derivations %store (list (pk 'udrv udrv)))
59688fc4 538 (let ((p (derivation->output-path udrv)))
d66ac374
LC
539 (and (valid-path? %store p)
540 (equal? '(one two) (call-with-input-file p read)))))))
541
858e9282 542(test-assert "derivation with #:references-graphs"
5b0c9d16
LC
543 (let* ((input1 (add-text-to-store %store "foo" "hello"
544 (list %bash)))
545 (input2 (add-text-to-store %store "bar"
546 (number->string (random 7777))
547 (list input1)))
548 (builder (add-text-to-store %store "build-graph"
549 (format #f "
550~a $out
551 (while read l ; do echo $l ; done) < bash > $out/bash
552 (while read l ; do echo $l ; done) < input1 > $out/input1
553 (while read l ; do echo $l ; done) < input2 > $out/input2"
554 %mkdir)
555 (list %mkdir)))
556 (drv (derivation %store "closure-graphs"
557 %bash `(,builder)
858e9282 558 #:references-graphs
5b0c9d16
LC
559 `(("bash" . ,%bash)
560 ("input1" . ,input1)
561 ("input2" . ,input2))
562 #:inputs `((,%bash) (,builder))))
59688fc4 563 (out (derivation->output-path drv)))
5b0c9d16
LC
564 (define (deps path . deps)
565 (let ((count (length deps)))
566 (string-append path "\n\n" (number->string count) "\n"
567 (string-join (sort deps string<?) "\n")
568 (if (zero? count) "" "\n"))))
569
570 (and (build-derivations %store (list drv))
571 (equal? (directory-contents out get-string-all)
572 `(("/bash" . ,(string-append %bash "\n\n0\n"))
573 ("/input1" . ,(if (string>? input1 %bash)
574 (string-append (deps %bash)
575 (deps input1 %bash))
576 (string-append (deps input1 %bash)
577 (deps %bash))))
578 ("/input2" . ,(string-concatenate
579 (map cdr
580 (sort
581 (map (lambda (p d)
582 (cons p (apply deps p d)))
583 (list %bash input1 input2)
584 (list '() (list %bash) (list input1)))
585 (lambda (x y)
586 (match x
587 ((p1 . _)
588 (match y
589 ((p2 . _)
590 (string<? p1 p2)))))))))))))))
591
b53be755
LC
592(test-assert "derivation #:allowed-references, ok"
593 (let ((drv (derivation %store "allowed" %bash
594 '("-c" "echo hello > $out")
595 #:inputs `((,%bash))
596 #:allowed-references '())))
597 (build-derivations %store (list drv))))
598
599(test-assert "derivation #:allowed-references, not allowed"
600 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
601 (drv (derivation %store "disallowed" %bash
602 `("-c" ,(string-append "echo " txt "> $out"))
603 #:inputs `((,%bash) (,txt))
604 #:allowed-references '())))
605 (guard (c ((nix-protocol-error? c)
606 ;; There's no specific error message to check for.
607 #t))
608 (build-derivations %store (list drv))
609 #f)))
610
611(test-assert "derivation #:allowed-references, self allowed"
612 (let ((drv (derivation %store "allowed" %bash
613 '("-c" "echo $out > $out")
614 #:inputs `((,%bash))
615 #:allowed-references '("out"))))
616 (build-derivations %store (list drv))))
617
618(test-assert "derivation #:allowed-references, self not allowed"
619 (let ((drv (derivation %store "disallowed" %bash
620 `("-c" ,"echo $out > $out")
621 #:inputs `((,%bash))
622 #:allowed-references '())))
623 (guard (c ((nix-protocol-error? c)
624 ;; There's no specific error message to check for.
625 #t))
626 (build-derivations %store (list drv))
627 #f)))
628
35b5ca78
LC
629(test-assert "derivation #:disallowed-references, ok"
630 (let ((drv (derivation %store "disallowed" %bash
631 '("-c" "echo hello > $out")
632 #:inputs `((,%bash))
633 #:disallowed-references '("out"))))
634 (build-derivations %store (list drv))))
635
636(test-assert "derivation #:disallowed-references, not ok"
637 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
638 (drv (derivation %store "disdisallowed" %bash
639 `("-c" ,(string-append "echo " txt "> $out"))
640 #:inputs `((,%bash) (,txt))
641 #:disallowed-references (list txt))))
642 (guard (c ((nix-protocol-error? c)
643 ;; There's no specific error message to check for.
644 #t))
645 (build-derivations %store (list drv))
646 #f)))
647
44ad3384
LC
648;; Here we should get the value of $NIX_STATE_DIR that the daemon sees, which
649;; is a unique value for each test process; this value is the same as the one
650;; we see in the process executing this file since it is set by 'test-env'.
651(test-equal "derivation #:leaked-env-vars"
652 (getenv "NIX_STATE_DIR")
653 (let* ((value (getenv "NIX_STATE_DIR"))
654 (drv (derivation %store "leaked-env-vars" %bash
655 '("-c" "echo -n $NIX_STATE_DIR > $out")
656 #:hash (sha256 (string->utf8 value))
657 #:hash-algo 'sha256
658 #:inputs `((,%bash))
659 #:leaked-env-vars '("NIX_STATE_DIR"))))
660 (and (build-derivations %store (list drv))
661 (call-with-input-file (derivation->output-path drv)
662 get-string-all))))
663
de4c3f26
LC
664\f
665(define %coreutils
8f3ecbd7 666 (false-if-exception
12d720fd 667 (and (network-reachable?)
f073e523 668 (package-derivation %store %bootstrap-coreutils&co))))
de4c3f26
LC
669
670(test-skip (if %coreutils 0 1))
671
672(test-assert "build derivation with coreutils"
673 (let* ((builder
674 (add-text-to-store %store "build-with-coreutils.sh"
675 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
676 '()))
59688fc4 677 (drv
a987d2c0 678 (derivation %store "foo"
97d3998e 679 %bash `(,builder)
a987d2c0
LC
680 #:env-vars `(("PATH" .
681 ,(string-append
59688fc4 682 (derivation->output-path %coreutils)
a987d2c0
LC
683 "/bin")))
684 #:inputs `((,builder)
685 (,%coreutils))))
de4c3f26 686 (succeeded?
59688fc4 687 (build-derivations %store (list drv))))
de4c3f26 688 (and succeeded?
59688fc4 689 (let ((p (derivation->output-path drv)))
31ef99a8
LC
690 (and (valid-path? %store p)
691 (file-exists? (string-append p "/good")))))))
de4c3f26 692
9c629a27 693(test-skip (if (%guile-for-build) 0 8))
9a20830e 694
d26e1967
LC
695(test-equal "build-expression->derivation and invalid module name"
696 '(file-search-error "guix/module/that/does/not/exist.scm")
697 (guard (c ((file-search-error? c)
698 (list 'file-search-error
699 (file-search-error-file-name c))))
700 (build-expression->derivation %store "foo" #t
701 #:modules '((guix module that
702 does not exist)))))
703
9231ef12
LC
704(test-equal "build-expression->derivation and builder encoding"
705 '("UTF-8" #t)
706 (let* ((exp '(λ (α) (+ α 1)))
707 (drv (build-expression->derivation %store "foo" exp)))
708 (match (derivation-builder-arguments drv)
709 ((... builder)
710 (call-with-input-file builder
711 (lambda (port)
712 (list (port-encoding port)
713 (->bool
714 (string-contains (get-string-all port)
715 "(λ (α) (+ α 1))")))))))))
716
9a20830e 717(test-assert "build-expression->derivation and derivation-prerequisites"
dd1a5a15 718 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
719 (any (match-lambda
720 (($ <derivation-input> path)
59688fc4 721 (string=? path (derivation-file-name (%guile-for-build)))))
9a20830e 722 (derivation-prerequisites drv))))
d9085c23 723
49c0a8d6 724(test-assert "derivation-prerequisites and valid-derivation-input?"
3681db5d
LC
725 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
726 (b (build-expression->derivation %store "b" `(list ,(random-text))))
727 (c (build-expression->derivation %store "c" `(mkdir %output)
728 #:inputs `(("a" ,a) ("b" ,b)))))
49c0a8d6
LC
729 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
730 ;; be removed by tests/guix-gc.sh.)
731 (build-derivations %store
732 (list a (package-derivation %store %bootstrap-guile)))
733
3681db5d
LC
734 (match (derivation-prerequisites c
735 (cut valid-derivation-input? %store
736 <>))
737 ((($ <derivation-input> file ("out")))
738 (string=? file (derivation-file-name b)))
739 (x
740 (pk 'fail x #f)))))
741
d9085c23
LC
742(test-assert "build-expression->derivation without inputs"
743 (let* ((builder '(begin
744 (mkdir %output)
745 (call-with-output-file (string-append %output "/test")
746 (lambda (p)
747 (display '(hello guix) p)))))
dd1a5a15 748 (drv (build-expression->derivation %store "goo" builder))
59688fc4 749 (succeeded? (build-derivations %store (list drv))))
d9085c23 750 (and succeeded?
59688fc4 751 (let ((p (derivation->output-path drv)))
d9085c23
LC
752 (equal? '(hello guix)
753 (call-with-input-file (string-append p "/test") read))))))
754
969e678e
LC
755(test-assert "build-expression->derivation and max-silent-time"
756 (let* ((store (let ((s (open-connection)))
757 (set-build-options s #:max-silent-time 1)
758 s))
01e82af5 759 (builder '(begin (sleep 100) (mkdir %output) #t))
dd1a5a15 760 (drv (build-expression->derivation store "silent" builder))
59688fc4 761 (out-path (derivation->output-path drv)))
6c20d1d0
LC
762 (guard (c ((nix-protocol-error? c)
763 (and (string-contains (nix-protocol-error-message c)
764 "failed")
765 (not (valid-path? store out-path)))))
766 (build-derivations store (list drv))
767 #f)))
768
769(test-assert "build-expression->derivation and timeout"
770 (let* ((store (let ((s (open-connection)))
771 (set-build-options s #:timeout 1)
772 s))
773 (builder '(begin (sleep 100) (mkdir %output) #t))
774 (drv (build-expression->derivation store "slow" builder))
775 (out-path (derivation->output-path drv)))
969e678e
LC
776 (guard (c ((nix-protocol-error? c)
777 (and (string-contains (nix-protocol-error-message c)
778 "failed")
779 (not (valid-path? store out-path)))))
01e82af5
LC
780 (build-derivations store (list drv))
781 #f)))
969e678e 782
9a20830e 783(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
dd1a5a15 784 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
785 ;; The only direct dependency is (%guile-for-build) and it's already
786 ;; built.
787 (null? (derivation-prerequisites-to-build %store drv))))
788
784bb1f3 789(test-assert "derivation-prerequisites-to-build when outputs already present"
59688fc4 790 (let* ((builder '(begin (mkdir %output) #t))
dd1a5a15 791 (input-drv (build-expression->derivation %store "input" builder))
59688fc4
LC
792 (input-path (derivation-output-path
793 (assoc-ref (derivation-outputs input-drv)
794 "out")))
dd1a5a15
LC
795 (drv (build-expression->derivation %store "something" builder
796 #:inputs
59688fc4
LC
797 `(("i" ,input-drv))))
798 (output (derivation->output-path drv)))
784bb1f3
LC
799 ;; Make sure these things are not already built.
800 (when (valid-path? %store input-path)
801 (delete-paths %store (list input-path)))
802 (when (valid-path? %store output)
803 (delete-paths %store (list output)))
804
805 (and (equal? (map derivation-input-path
806 (derivation-prerequisites-to-build %store drv))
59688fc4 807 (list (derivation-file-name input-drv)))
784bb1f3
LC
808
809 ;; Build DRV and delete its input.
59688fc4 810 (build-derivations %store (list drv))
784bb1f3
LC
811 (delete-paths %store (list input-path))
812 (not (valid-path? %store input-path))
813
814 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
815 ;; prerequisite to build because DRV itself is already built.
816 (null? (derivation-prerequisites-to-build %store drv)))))
817
dd36b51b 818(test-assert "derivation-prerequisites-to-build and substitutes"
59688fc4
LC
819 (let* ((store (open-connection))
820 (drv (build-expression->derivation store "prereq-subst"
dd1a5a15 821 (random 1000)))
e6740741 822 (output (derivation->output-path drv)))
dd36b51b 823
1950bf56 824 ;; Make sure substitutes are usable.
24f5aaaf
LC
825 (set-build-options store #:use-substitutes? #t
826 #:substitute-urls (%test-substitute-urls))
1950bf56 827
e6740741
LC
828 (with-derivation-narinfo drv
829 (let-values (((build download)
830 (derivation-prerequisites-to-build store drv))
831 ((build* download*)
832 (derivation-prerequisites-to-build store drv
e9651e39
LC
833 #:substitutable?
834 (const #f))))
e6740741
LC
835 (and (null? build)
836 (equal? download (list output))
837 (null? download*)
838 (null? build*))))))
dd36b51b 839
4a6aeb67 840(test-assert "derivation-prerequisites-to-build and substitutes, non-substitutable build"
d2d0514b 841 (let* ((store (open-connection))
4a6aeb67 842 (drv (build-expression->derivation store "prereq-no-subst"
d2d0514b 843 (random 1000)
4a6aeb67 844 #:substitutable? #f))
d2d0514b
LC
845 (output (derivation->output-path drv)))
846
847 ;; Make sure substitutes are usable.
24f5aaaf
LC
848 (set-build-options store #:use-substitutes? #t
849 #:substitute-urls (%test-substitute-urls))
d2d0514b
LC
850
851 (with-derivation-narinfo drv
852 (let-values (((build download)
853 (derivation-prerequisites-to-build store drv)))
854 ;; Despite being available as a substitute, DRV will be built locally
4a6aeb67 855 ;; due to #:substitutable? #f.
d2d0514b
LC
856 (and (null? download)
857 (match build
858 (((? derivation-input? input))
859 (string=? (derivation-input-path input)
860 (derivation-file-name drv)))))))))
861
4a6aeb67
LC
862(test-assert "derivation-prerequisites-to-build and substitutes, local build"
863 (with-store store
864 (let* ((drv (build-expression->derivation store "prereq-subst-local"
865 (random 1000)
866 #:local-build? #t))
867 (output (derivation->output-path drv)))
868
869 ;; Make sure substitutes are usable.
24f5aaaf
LC
870 (set-build-options store #:use-substitutes? #t
871 #:substitute-urls (%test-substitute-urls))
4a6aeb67
LC
872
873 (with-derivation-narinfo drv
874 (let-values (((build download)
875 (derivation-prerequisites-to-build store drv)))
cfc5d398 876 ;; #:local-build? is *not* synonymous with #:substitutable?, so we
4a6aeb67
LC
877 ;; must be able to substitute DRV's output.
878 ;; See <http://bugs.gnu.org/18747>.
879 (and (null? build)
880 (match download
881 (((? string? item))
882 (string=? item (derivation->output-path drv))))))))))
883
58c08df0
LC
884(test-assert "derivation-prerequisites-to-build in 'check' mode"
885 (with-store store
886 (let* ((dep (build-expression->derivation store "dep"
887 `(begin ,(random-text)
888 (mkdir %output))))
889 (drv (build-expression->derivation store "to-check"
890 '(mkdir %output)
891 #:inputs `(("dep" ,dep)))))
892 (build-derivations store (list drv))
893 (delete-paths store (list (derivation->output-path dep)))
894
895 ;; In 'check' mode, DEP must be rebuilt.
896 (and (null? (derivation-prerequisites-to-build store drv))
897 (match (derivation-prerequisites-to-build store drv
898 #:mode (build-mode
899 check))
900 ((input)
901 (string=? (derivation-input-path input)
902 (derivation-file-name dep))))))))
903
bdb59b33
LC
904(test-assert "substitution-oracle and #:substitute? #f"
905 (with-store store
906 (let* ((dep (build-expression->derivation store "dep"
907 `(begin ,(random-text)
908 (mkdir %output))))
909 (drv (build-expression->derivation store "not-subst"
910 `(begin ,(random-text)
911 (mkdir %output))
912 #:substitutable? #f
913 #:inputs `(("dep" ,dep))))
914 (query #f))
915 (define (record-substitutable-path-query store paths)
916 (when query
917 (error "already called!" query))
918 (set! query paths)
919 '())
920
921 (mock ((guix store) substitutable-paths
922 record-substitutable-path-query)
923
924 (let ((pred (substitution-oracle store (list drv))))
925 (pred (derivation->output-path drv))))
926
927 ;; Make sure the oracle didn't try to get substitute info for DRV since
928 ;; DRV is mark as non-substitutable. Assume that GUILE-FOR-BUILD is
929 ;; already in store and thus not part of QUERY.
930 (equal? (pk 'query query)
931 (list (derivation->output-path dep))))))
932
db393b33
LC
933(test-assert "build-expression->derivation with expression returning #f"
934 (let* ((builder '(begin
935 (mkdir %output)
936 #f)) ; fail!
dd1a5a15 937 (drv (build-expression->derivation %store "fail" builder))
59688fc4 938 (out-path (derivation->output-path drv)))
db393b33
LC
939 (guard (c ((nix-protocol-error? c)
940 ;; Note that the output path may exist at this point, but it
941 ;; is invalid.
31ef99a8
LC
942 (and (string-match "build .* failed"
943 (nix-protocol-error-message c))
944 (not (valid-path? %store out-path)))))
59688fc4 945 (build-derivations %store (list drv))
db393b33
LC
946 #f)))
947
9bc07f4d
LC
948(test-assert "build-expression->derivation with two outputs"
949 (let* ((builder '(begin
950 (call-with-output-file (assoc-ref %outputs "out")
951 (lambda (p)
952 (display '(hello) p)))
953 (call-with-output-file (assoc-ref %outputs "second")
954 (lambda (p)
955 (display '(world) p)))))
dd1a5a15 956 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
957 #:outputs '("out"
958 "second")))
59688fc4 959 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 960 (and succeeded?
59688fc4
LC
961 (let ((one (derivation->output-path drv))
962 (two (derivation->output-path drv "second")))
9bc07f4d
LC
963 (and (equal? '(hello) (call-with-input-file one read))
964 (equal? '(world) (call-with-input-file two read)))))))
965
ad1ebab3 966(test-skip (if %coreutils 0 1))
d9085c23
LC
967(test-assert "build-expression->derivation with one input"
968 (let* ((builder '(call-with-output-file %output
969 (lambda (p)
970 (let ((cu (assoc-ref %build-inputs "cu")))
971 (close 1)
972 (dup2 (port->fdes p) 1)
973 (execl (string-append cu "/bin/uname")
974 "uname" "-a")))))
dd1a5a15
LC
975 (drv (build-expression->derivation %store "uname" builder
976 #:inputs
2acb2cb6 977 `(("cu" ,%coreutils))))
59688fc4 978 (succeeded? (build-derivations %store (list drv))))
d9085c23 979 (and succeeded?
59688fc4 980 (let ((p (derivation->output-path drv)))
d9085c23
LC
981 (string-contains (call-with-input-file p read-line) "GNU")))))
982
d9024884
LC
983(test-assert "build-expression->derivation with modules"
984 (let* ((builder `(begin
985 (use-modules (guix build utils))
986 (let ((out (assoc-ref %outputs "out")))
987 (mkdir-p (string-append out "/guile/guix/nix"))
988 #t)))
59688fc4 989 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 990 builder
d9024884
LC
991 #:modules
992 '((guix build utils)))))
59688fc4
LC
993 (and (build-derivations %store (list drv))
994 (let* ((p (derivation->output-path drv))
d9024884
LC
995 (s (stat (string-append p "/guile/guix/nix"))))
996 (eq? (stat:type s) 'directory)))))
997
813986ac
LC
998(test-assert "build-expression->derivation: same fixed-output path"
999 (let* ((builder1 '(call-with-output-file %output
1000 (lambda (p)
1001 (write "hello" p))))
1002 (builder2 '(call-with-output-file (pk 'difference-here! %output)
1003 (lambda (p)
1004 (write "hello" p))))
1005 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 1006 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
1007 #:hash hash
1008 #:hash-algo 'sha256))
dd1a5a15 1009 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
1010 #:hash hash
1011 #:hash-algo 'sha256))
1012 (succeeded? (build-derivations %store (list input1 input2))))
1013 (and succeeded?
59688fc4
LC
1014 (not (string=? (derivation-file-name input1)
1015 (derivation-file-name input2)))
1016 (string=? (derivation->output-path input1)
1017 (derivation->output-path input2)))))
813986ac 1018
7bdd1f0e
LC
1019(test-assert "build-expression->derivation with a fixed-output input"
1020 (let* ((builder1 '(call-with-output-file %output
1021 (lambda (p)
1022 (write "hello" p))))
1023 (builder2 '(call-with-output-file (pk 'difference-here! %output)
1024 (lambda (p)
1025 (write "hello" p))))
1026 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 1027 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
1028 #:hash hash
1029 #:hash-algo 'sha256))
dd1a5a15 1030 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
1031 #:hash hash
1032 #:hash-algo 'sha256))
1033 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
1034 (call-with-output-file %output
1035 (lambda (out)
1036 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
1037 (final1 (build-expression->derivation %store "final" builder3
1038 #:inputs
7bdd1f0e 1039 `(("input" ,input1))))
dd1a5a15
LC
1040 (final2 (build-expression->derivation %store "final" builder3
1041 #:inputs
7bdd1f0e 1042 `(("input" ,input2)))))
59688fc4
LC
1043 (and (string=? (derivation->output-path final1)
1044 (derivation->output-path final2))
1045 (string=? (derivation->output-path final1)
1046 (derivation-path->output-path
1047 (derivation-file-name final1)))
7bdd1f0e
LC
1048 (build-derivations %store (list final1 final2)))))
1049
36bbbbd1
LC
1050(test-assert "build-expression->derivation produces recursive fixed-output"
1051 (let* ((builder '(begin
1052 (use-modules (srfi srfi-26))
1053 (mkdir %output)
1054 (chdir %output)
1055 (call-with-output-file "exe"
1056 (cut display "executable" <>))
1057 (chmod "exe" #o777)
1058 (symlink "exe" "symlink")
1059 (mkdir "subdir")))
1060 (drv (build-expression->derivation %store "fixed-rec" builder
1061 #:hash-algo 'sha256
1062 #:hash (base32
1063 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
1064 #:recursive? #t)))
1065 (and (build-derivations %store (list drv))
1066 (let* ((dir (derivation->output-path drv))
1067 (exe (string-append dir "/exe"))
1068 (link (string-append dir "/symlink"))
1069 (subdir (string-append dir "/subdir")))
1070 (and (executable-file? exe)
1071 (string=? "executable"
1072 (call-with-input-file exe get-string-all))
1073 (string=? "exe" (readlink link))
1074 (file-is-directory? subdir))))))
1075
1076(test-assert "build-expression->derivation uses recursive fixed-output"
1077 (let* ((builder '(call-with-output-file %output
1078 (lambda (port)
1079 (display "hello" port))))
1080 (fixed (build-expression->derivation %store "small-fixed-rec"
1081 builder
1082 #:hash-algo 'sha256
1083 #:hash (base32
1084 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
1085 #:recursive? #t))
1086 (in (derivation->output-path fixed))
1087 (builder `(begin
1088 (mkdir %output)
1089 (chdir %output)
1090 (symlink ,in "symlink")))
1091 (drv (build-expression->derivation %store "fixed-rec-user"
1092 builder
1093 #:inputs `(("fixed" ,fixed)))))
1094 (and (build-derivations %store (list drv))
1095 (let ((out (derivation->output-path drv)))
1096 (string=? (readlink (string-append out "/symlink")) in)))))
1097
858e9282 1098(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
1099 (let* ((input (add-text-to-store %store "foo" "hello"
1100 (list %bash %mkdir)))
1101 (builder '(copy-file "input" %output))
858e9282 1102 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 1103 builder
858e9282 1104 #:references-graphs
9c629a27 1105 `(("input" . ,input))))
59688fc4 1106 (out (derivation->output-path drv)))
9c629a27
LC
1107 (define (deps path . deps)
1108 (let ((count (length deps)))
1109 (string-append path "\n\n" (number->string count) "\n"
1110 (string-join (sort deps string<?) "\n")
1111 (if (zero? count) "" "\n"))))
1112
1113 (and (build-derivations %store (list drv))
1114 (equal? (call-with-input-file out get-string-all)
1115 (string-concatenate
1116 (map cdr
1117 (sort (map (lambda (p d)
1118 (cons p (apply deps p d)))
1119 (list input %bash %mkdir)
1120 (list (list %bash %mkdir)
1121 '() '()))
1122 (lambda (x y)
1123 (match x
1124 ((p1 . _)
1125 (match y
1126 ((p2 . _)
1127 (string<? p1 p2)))))))))))))
1128
e387ab7c
LC
1129(test-equal "map-derivation"
1130 "hello"
1131 (let* ((joke (package-derivation %store guile-1.8))
1132 (good (package-derivation %store %bootstrap-guile))
1133 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 1134 #f ; systematically fail
e387ab7c
LC
1135 #:guile-for-build joke))
1136 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
1137 '(call-with-output-file %output
1138 (lambda (p)
dd1a5a15 1139 (display "hello" p)))))
e387ab7c 1140 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
1141 '(let ((in (assoc-ref
1142 %build-inputs "in")))
1143 (copy-file in %output))
dd1a5a15 1144 #:inputs `(("in" ,drv1))
e387ab7c
LC
1145 #:guile-for-build joke))
1146 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
1147 (,joke . ,good))))
1148 (out (derivation->output-path drv4)))
1149 (and (build-derivations %store (list (pk 'remapped drv4)))
1150 (call-with-input-file out get-string-all))))
1151
a716e36d
LC
1152(test-equal "map-derivation, sources"
1153 "hello"
1154 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
1155 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
1156 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
1157 (drv1 (derivation %store "drv-to-remap"
1158
1159 ;; XXX: This wouldn't work in practice, but if
1160 ;; we append "/bin/bash" then we can't replace
1161 ;; it with the bootstrap bash, which is a
1162 ;; single file.
1163 (derivation->output-path bash-full)
1164
1165 `("-e" ,script1)
1166 #:inputs `((,bash-full) (,script1))))
1167 (drv2 (map-derivation %store drv1
1168 `((,bash-full . ,%bash)
1169 (,script1 . ,script2))))
1170 (out (derivation->output-path drv2)))
1171 (and (build-derivations %store (list (pk 'remapped* drv2)))
1172 (call-with-input-file out get-string-all))))
1173
341c6fdd 1174(test-end)
9b5364a3
LC
1175
1176;; Local Variables:
1177;; eval: (put 'with-http-server 'scheme-indent-function 2)
1178;; End: