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