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