derivations: Add #:disallowed-references.
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
ef8de985 2;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
21b679f6
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
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;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
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
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (test-gexp)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix gexp)
ef8de985 23 #:use-module (guix grafts)
21b679f6 24 #:use-module (guix derivations)
79c0c8cd 25 #:use-module (guix packages)
c1bc358f 26 #:use-module (guix tests)
21b679f6
LC
27 #:use-module (gnu packages)
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages bootstrap)
30 #:use-module (srfi srfi-1)
c8351d9a 31 #:use-module (srfi srfi-34)
21b679f6
LC
32 #:use-module (srfi srfi-64)
33 #:use-module (rnrs io ports)
34 #:use-module (ice-9 match)
2cf0ea0d 35 #:use-module (ice-9 regex)
21b679f6
LC
36 #:use-module (ice-9 popen))
37
38;; Test the (guix gexp) module.
39
40(define %store
c1bc358f 41 (open-connection-for-tests))
21b679f6 42
ef8de985
LC
43;; Globally disable grafts because they can trigger early builds.
44(%graft? #f)
45
21b679f6 46;; For white-box testing.
1f976033
LC
47(define (gexp-inputs x)
48 ((@@ (guix gexp) gexp-inputs) x))
49(define (gexp-native-inputs x)
50 ((@@ (guix gexp) gexp-native-inputs) x))
51(define (gexp-outputs x)
52 ((@@ (guix gexp) gexp-outputs) x))
53(define (gexp->sexp . x)
54 (apply (@@ (guix gexp) gexp->sexp) x))
21b679f6 55
667b2508 56(define* (gexp->sexp* exp #:optional target)
68a61e9f 57 (run-with-store %store (gexp->sexp exp
68a61e9f 58 #:target target)
c1bc358f 59 #:guile-for-build (%guile-for-build)))
21b679f6
LC
60
61(define-syntax-rule (test-assertm name exp)
62 (test-assert name
63 (run-with-store %store exp
c1bc358f 64 #:guile-for-build (%guile-for-build))))
21b679f6
LC
65
66\f
67(test-begin "gexp")
68
69(test-equal "no refs"
70 '(display "hello!")
71 (let ((exp (gexp (display "hello!"))))
72 (and (gexp? exp)
73 (null? (gexp-inputs exp))
74 (gexp->sexp* exp))))
75
76(test-equal "unquote"
77 '(display `(foo ,(+ 2 3)))
78 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
79 (and (gexp? exp)
80 (null? (gexp-inputs exp))
81 (gexp->sexp* exp))))
82
83(test-assert "one input package"
84 (let ((exp (gexp (display (ungexp coreutils)))))
85 (and (gexp? exp)
86 (match (gexp-inputs exp)
87 (((p "out"))
88 (eq? p coreutils)))
89 (equal? `(display ,(derivation->output-path
90 (package-derivation %store coreutils)))
91 (gexp->sexp* exp)))))
92
79c0c8cd
LC
93(test-assert "one input origin"
94 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
95 (and (gexp? exp)
96 (match (gexp-inputs exp)
97 (((o "out"))
98 (eq? o (package-source coreutils))))
99 (equal? `(display ,(derivation->output-path
100 (package-source-derivation
101 %store (package-source coreutils))))
102 (gexp->sexp* exp)))))
103
d9ae938f
LC
104(test-assert "one local file"
105 (let* ((file (search-path %load-path "guix.scm"))
106 (local (local-file file))
107 (exp (gexp (display (ungexp local))))
020f3e41 108 (intd (add-to-store %store (basename file) #f
d9ae938f
LC
109 "sha256" file)))
110 (and (gexp? exp)
111 (match (gexp-inputs exp)
112 (((x "out"))
113 (eq? x local)))
114 (equal? `(display ,intd) (gexp->sexp* exp)))))
115
7833db1f
LC
116(test-assert "one local file, symlink"
117 (let ((file (search-path %load-path "guix.scm"))
118 (link (tmpnam)))
119 (dynamic-wind
120 (const #t)
121 (lambda ()
122 (symlink (canonicalize-path file) link)
123 (let* ((local (local-file link "my-file" #:recursive? #f))
124 (exp (gexp (display (ungexp local))))
125 (intd (add-to-store %store "my-file" #f
126 "sha256" file)))
127 (and (gexp? exp)
128 (match (gexp-inputs exp)
129 (((x "out"))
130 (eq? x local)))
131 (equal? `(display ,intd) (gexp->sexp* exp)))))
132 (lambda ()
133 (false-if-exception (delete-file link))))))
134
558e8b11
LC
135(test-assert "one plain file"
136 (let* ((file (plain-file "hi" "Hello, world!"))
137 (exp (gexp (display (ungexp file))))
138 (expected (add-text-to-store %store "hi" "Hello, world!")))
139 (and (gexp? exp)
140 (match (gexp-inputs exp)
141 (((x "out"))
142 (eq? x file)))
143 (equal? `(display ,expected) (gexp->sexp* exp)))))
144
21b679f6
LC
145(test-assert "same input twice"
146 (let ((exp (gexp (begin
147 (display (ungexp coreutils))
148 (display (ungexp coreutils))))))
149 (and (gexp? exp)
150 (match (gexp-inputs exp)
151 (((p "out"))
152 (eq? p coreutils)))
153 (let ((e `(display ,(derivation->output-path
154 (package-derivation %store coreutils)))))
155 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
156
157(test-assert "two input packages, one derivation, one file"
158 (let* ((drv (build-expression->derivation
159 %store "foo" 'bar
160 #:guile-for-build (package-derivation %store %bootstrap-guile)))
161 (txt (add-text-to-store %store "foo" "Hello, world!"))
162 (exp (gexp (begin
163 (display (ungexp coreutils))
164 (display (ungexp %bootstrap-guile))
165 (display (ungexp drv))
166 (display (ungexp txt))))))
167 (define (match-input thing)
168 (match-lambda
169 ((drv-or-pkg _ ...)
170 (eq? thing drv-or-pkg))))
171
172 (and (gexp? exp)
173 (= 4 (length (gexp-inputs exp)))
174 (every (lambda (input)
175 (find (match-input input) (gexp-inputs exp)))
176 (list drv coreutils %bootstrap-guile txt))
177 (let ((e0 `(display ,(derivation->output-path
178 (package-derivation %store coreutils))))
179 (e1 `(display ,(derivation->output-path
180 (package-derivation %store %bootstrap-guile))))
181 (e2 `(display ,(derivation->output-path drv)))
182 (e3 `(display ,txt)))
183 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
184
667b2508
LC
185(test-assert "ungexp + ungexp-native"
186 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
187 (ungexp coreutils)
188 (ungexp-native glibc)
189 (ungexp binutils))))
190 (target "mips64el-linux")
191 (guile (derivation->output-path
192 (package-derivation %store %bootstrap-guile)))
193 (cu (derivation->output-path
194 (package-cross-derivation %store coreutils target)))
195 (libc (derivation->output-path
196 (package-derivation %store glibc)))
197 (bu (derivation->output-path
198 (package-cross-derivation %store binutils target))))
199 (and (lset= equal?
200 `((,%bootstrap-guile "out") (,glibc "out"))
201 (gexp-native-inputs exp))
202 (lset= equal?
203 `((,coreutils "out") (,binutils "out"))
204 (gexp-inputs exp))
205 (equal? `(list ,guile ,cu ,libc ,bu)
206 (gexp->sexp* exp target)))))
207
1123759b
LC
208(test-equal "ungexp + ungexp-native, nested"
209 (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out")))
210 (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
211 (ungexp %bootstrap-guile)))))
212 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
213
21b679f6
LC
214(test-assert "input list"
215 (let ((exp (gexp (display
216 '(ungexp (list %bootstrap-guile coreutils)))))
217 (guile (derivation->output-path
218 (package-derivation %store %bootstrap-guile)))
219 (cu (derivation->output-path
220 (package-derivation %store coreutils))))
221 (and (lset= equal?
222 `((,%bootstrap-guile "out") (,coreutils "out"))
223 (gexp-inputs exp))
224 (equal? `(display '(,guile ,cu))
225 (gexp->sexp* exp)))))
226
667b2508
LC
227(test-assert "input list + ungexp-native"
228 (let* ((target "mips64el-linux")
229 (exp (gexp (display
230 (cons '(ungexp-native (list %bootstrap-guile coreutils))
231 '(ungexp (list glibc binutils))))))
232 (guile (derivation->output-path
233 (package-derivation %store %bootstrap-guile)))
234 (cu (derivation->output-path
235 (package-derivation %store coreutils)))
236 (xlibc (derivation->output-path
237 (package-cross-derivation %store glibc target)))
238 (xbu (derivation->output-path
239 (package-cross-derivation %store binutils target))))
240 (and (lset= equal?
241 `((,%bootstrap-guile "out") (,coreutils "out"))
242 (gexp-native-inputs exp))
243 (lset= equal?
244 `((,glibc "out") (,binutils "out"))
245 (gexp-inputs exp))
246 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
247 (gexp->sexp* exp target)))))
248
21b679f6 249(test-assert "input list splicing"
a482cfdc 250 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
21b679f6
LC
251 (outputs (list (derivation->output-path
252 (package-derivation %store glibc)
253 "debug")
254 (derivation->output-path
255 (package-derivation %store %bootstrap-guile))))
256 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
257 (and (lset= equal?
258 `((,glibc "debug") (,%bootstrap-guile "out"))
259 (gexp-inputs exp))
260 (equal? (gexp->sexp* exp)
261 `(list ,@(cons 5 outputs))))))
262
667b2508 263(test-assert "input list splicing + ungexp-native-splicing"
0dbea56b
LC
264 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
265 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
266 (and (lset= equal?
267 `((,glibc "debug") (,%bootstrap-guile "out"))
268 (gexp-native-inputs exp))
269 (null? (gexp-inputs exp))
270 (equal? (gexp->sexp* exp) ;native
271 (gexp->sexp* exp "mips64el-linux")))))
272
4b23c466
LC
273(test-equal "output list"
274 2
275 (let ((exp (gexp (begin (mkdir (ungexp output))
276 (mkdir (ungexp output "bar"))))))
277 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
278
279(test-assert "output list, combined gexps"
280 (let* ((exp0 (gexp (mkdir (ungexp output))))
281 (exp1 (gexp (mkdir (ungexp output "foo"))))
282 (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
283 (and (lset= equal?
284 (append (gexp-outputs exp0) (gexp-outputs exp1))
285 (gexp-outputs exp2))
286 (= 2 (length (gexp-outputs exp2))))))
287
7e75a673
LC
288(test-equal "output list, combined gexps, duplicate output"
289 1
290 (let* ((exp0 (gexp (mkdir (ungexp output))))
291 (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
292 (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
293 (length (gexp-outputs exp2))))
294
f9efe568
LC
295(test-assert "output list + ungexp-splicing list, combined gexps"
296 (let* ((exp0 (gexp (mkdir (ungexp output))))
297 (exp1 (gexp (mkdir (ungexp output "foo"))))
298 (exp2 (gexp (begin (display "hi!")
299 (ungexp-splicing (list exp0 exp1))))))
300 (and (lset= equal?
301 (append (gexp-outputs exp0) (gexp-outputs exp1))
302 (gexp-outputs exp2))
303 (= 2 (length (gexp-outputs exp2))))))
304
21b679f6
LC
305(test-assertm "gexp->file"
306 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
307 (guile (package-file %bootstrap-guile))
308 (sexp (gexp->sexp exp))
309 (drv (gexp->file "foo" exp))
310 (out -> (derivation->output-path drv))
311 (done (built-derivations (list drv)))
312 (refs ((store-lift references) out)))
313 (return (and (equal? sexp (call-with-input-file out read))
314 (equal? (list guile) refs)))))
315
316(test-assertm "gexp->derivation"
317 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
318 (exp -> (gexp
319 (begin
320 (mkdir (ungexp output))
321 (chdir (ungexp output))
322 (symlink
323 (string-append (ungexp %bootstrap-guile)
324 "/bin/guile")
325 "foo")
326 (symlink (ungexp file)
327 (ungexp output "2nd")))))
328 (drv (gexp->derivation "foo" exp))
329 (out -> (derivation->output-path drv))
330 (out2 -> (derivation->output-path drv "2nd"))
331 (done (built-derivations (list drv)))
332 (refs ((store-lift references) out))
333 (refs2 ((store-lift references) out2))
334 (guile (package-file %bootstrap-guile "bin/guile")))
335 (return (and (string=? (readlink (string-append out "/foo")) guile)
336 (string=? (readlink out2) file)
337 (equal? refs (list (dirname (dirname guile))))
338 (equal? refs2 (list file))))))
339
ce45eb4c 340(test-assertm "gexp->derivation vs. grafts"
ef8de985
LC
341 (mlet* %store-monad ((graft? (set-grafting #f))
342 (p0 -> (dummy-package "dummy"
ce45eb4c
LC
343 (arguments
344 '(#:implicit-inputs? #f))))
345 (r -> (package (inherit p0) (name "DuMMY")))
346 (p1 -> (package (inherit p0) (replacement r)))
347 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
348 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
349 (void (set-guile-for-build %bootstrap-guile))
ef8de985
LC
350 (drv0 (gexp->derivation "t" exp0 #:graft? #t))
351 (drv1 (gexp->derivation "t" exp1 #:graft? #t))
352 (drv1* (gexp->derivation "t" exp1 #:graft? #f))
353 (_ (set-grafting graft?)))
ce45eb4c
LC
354 (return (and (not (string=? (derivation->output-path drv0)
355 (derivation->output-path drv1)))
356 (string=? (derivation->output-path drv0)
357 (derivation->output-path drv1*))))))
358
21b679f6
LC
359(test-assertm "gexp->derivation, composed gexps"
360 (mlet* %store-monad ((exp0 -> (gexp (begin
361 (mkdir (ungexp output))
362 (chdir (ungexp output)))))
363 (exp1 -> (gexp (symlink
364 (string-append (ungexp %bootstrap-guile)
365 "/bin/guile")
366 "foo")))
367 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
368 (drv (gexp->derivation "foo" exp))
369 (out -> (derivation->output-path drv))
370 (done (built-derivations (list drv)))
371 (guile (package-file %bootstrap-guile "bin/guile")))
372 (return (string=? (readlink (string-append out "/foo"))
373 guile))))
374
5d098459
LC
375(test-assertm "gexp->derivation, default system"
376 ;; The default system should be the one at '>>=' time, not the one at
377 ;; invocation time. See <http://bugs.gnu.org/18002>.
378 (let ((system (%current-system))
379 (mdrv (parameterize ((%current-system "foobar64-linux"))
380 (gexp->derivation "foo"
381 (gexp
382 (mkdir (ungexp output)))))))
383 (mlet %store-monad ((drv mdrv))
384 (return (string=? system (derivation-system drv))))))
385
d9ae938f
LC
386(test-assertm "gexp->derivation, local-file"
387 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
020f3e41 388 (intd (interned-file file #:recursive? #f))
d9ae938f
LC
389 (local -> (local-file file))
390 (exp -> (gexp (begin
391 (stat (ungexp local))
392 (symlink (ungexp local)
393 (ungexp output)))))
394 (drv (gexp->derivation "local-file" exp)))
395 (mbegin %store-monad
396 (built-derivations (list drv))
397 (return (string=? (readlink (derivation->output-path drv))
398 intd)))))
399
68a61e9f
LC
400(test-assertm "gexp->derivation, cross-compilation"
401 (mlet* %store-monad ((target -> "mips64el-linux")
402 (exp -> (gexp (list (ungexp coreutils)
403 (ungexp output))))
404 (xdrv (gexp->derivation "foo" exp
405 #:target target))
406 (refs ((store-lift references)
407 (derivation-file-name xdrv)))
408 (xcu (package->cross-derivation coreutils
409 target))
410 (cu (package->derivation coreutils)))
411 (return (and (member (derivation-file-name xcu) refs)
412 (not (member (derivation-file-name cu) refs))))))
413
667b2508
LC
414(test-assertm "gexp->derivation, ungexp-native"
415 (mlet* %store-monad ((target -> "mips64el-linux")
416 (exp -> (gexp (list (ungexp-native coreutils)
417 (ungexp output))))
418 (xdrv (gexp->derivation "foo" exp
419 #:target target))
420 (drv (gexp->derivation "foo" exp)))
421 (return (string=? (derivation-file-name drv)
422 (derivation-file-name xdrv)))))
423
424(test-assertm "gexp->derivation, ungexp + ungexp-native"
425 (mlet* %store-monad ((target -> "mips64el-linux")
426 (exp -> (gexp (list (ungexp-native coreutils)
427 (ungexp glibc)
428 (ungexp output))))
429 (xdrv (gexp->derivation "foo" exp
430 #:target target))
431 (refs ((store-lift references)
432 (derivation-file-name xdrv)))
433 (xglibc (package->cross-derivation glibc target))
434 (cu (package->derivation coreutils)))
435 (return (and (member (derivation-file-name cu) refs)
436 (member (derivation-file-name xglibc) refs)))))
437
438(test-assertm "gexp->derivation, ungexp-native + composed gexps"
439 (mlet* %store-monad ((target -> "mips64el-linux")
440 (exp0 -> (gexp (list 1 2
441 (ungexp coreutils))))
442 (exp -> (gexp (list 0 (ungexp-native exp0))))
443 (xdrv (gexp->derivation "foo" exp
444 #:target target))
445 (drv (gexp->derivation "foo" exp)))
446 (return (string=? (derivation-file-name drv)
447 (derivation-file-name xdrv)))))
448
6fd1a796
LC
449(test-assertm "gexp->derivation, store copy"
450 (let ((build-one #~(call-with-output-file #$output
451 (lambda (port)
452 (display "This is the one." port))))
453 (build-two (lambda (one)
454 #~(begin
455 (mkdir #$output)
456 (symlink #$one (string-append #$output "/one"))
457 (call-with-output-file (string-append #$output "/two")
458 (lambda (port)
459 (display "This is the second one." port))))))
b53833b2
LC
460 (build-drv #~(begin
461 (use-modules (guix build store-copy))
6fd1a796 462
b53833b2
LC
463 (mkdir #$output)
464 (populate-store '("graph") #$output))))
6fd1a796
LC
465 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
466 (two (gexp->derivation "two" (build-two one)))
b53833b2 467 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 468 #:references-graphs
b53833b2 469 `(("graph" ,two))
6fd1a796
LC
470 #:modules
471 '((guix build store-copy)
472 (guix build utils))))
473 (ok? (built-derivations (list drv)))
474 (out -> (derivation->output-path drv)))
475 (let ((one (derivation->output-path one))
476 (two (derivation->output-path two)))
477 (return (and ok?
478 (file-exists? (string-append out "/" one))
479 (file-exists? (string-append out "/" two))
480 (file-exists? (string-append out "/" two "/two"))
481 (string=? (readlink (string-append out "/" two "/one"))
482 one)))))))
483
aa72d9af
LC
484(test-assertm "imported-files"
485 (mlet* %store-monad
486 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
487 ("a/b/c" . ,(search-path %load-path
488 "guix/derivations.scm"))
489 ("p/q" . ,(search-path %load-path "guix.scm"))
490 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
491 (drv (imported-files files)))
492 (mbegin %store-monad
493 (built-derivations (list drv))
494 (let ((dir (derivation->output-path drv)))
495 (return
496 (every (match-lambda
497 ((path . source)
498 (equal? (call-with-input-file (string-append dir "/" path)
499 get-bytevector-all)
500 (call-with-input-file source
501 get-bytevector-all))))
502 files))))))
503
504(test-assertm "gexp->derivation #:modules"
505 (mlet* %store-monad
506 ((build -> #~(begin
507 (use-modules (guix build utils))
508 (mkdir-p (string-append #$output "/guile/guix/nix"))
509 #t))
510 (drv (gexp->derivation "test-with-modules" build
511 #:modules '((guix build utils)))))
512 (mbegin %store-monad
513 (built-derivations (list drv))
514 (let* ((p (derivation->output-path drv))
515 (s (stat (string-append p "/guile/guix/nix"))))
516 (return (eq? (stat:type s) 'directory))))))
517
b53833b2
LC
518(test-assertm "gexp->derivation #:references-graphs"
519 (mlet* %store-monad
72cd8ec0 520 ((one (text-file "one" (random-text)))
b53833b2
LC
521 (two (gexp->derivation "two"
522 #~(symlink #$one #$output:chbouib)))
523 (drv (gexp->derivation "ref-graphs"
524 #~(begin
525 (use-modules (guix build store-copy))
526 (with-output-to-file #$output
527 (lambda ()
528 (write (call-with-input-file "guile"
529 read-reference-graph))))
530 (with-output-to-file #$output:one
531 (lambda ()
532 (write (call-with-input-file "one"
533 read-reference-graph))))
534 (with-output-to-file #$output:two
535 (lambda ()
536 (write (call-with-input-file "two"
537 read-reference-graph)))))
538 #:references-graphs `(("one" ,one)
539 ("two" ,two "chbouib")
540 ("guile" ,%bootstrap-guile))
541 #:modules '((guix build store-copy)
542 (guix build utils))))
543 (ok? (built-derivations (list drv)))
544 (guile-drv (package->derivation %bootstrap-guile))
686784d0
LC
545 (bash (interned-file (search-bootstrap-binary "bash"
546 (%current-system))
547 "bash" #:recursive? #t))
b53833b2
LC
548 (g-one -> (derivation->output-path drv "one"))
549 (g-two -> (derivation->output-path drv "two"))
550 (g-guile -> (derivation->output-path drv)))
551 (return (and ok?
552 (equal? (call-with-input-file g-one read) (list one))
72cd8ec0
LC
553 (lset= string=?
554 (call-with-input-file g-two read)
555 (list one (derivation->output-path two "chbouib")))
686784d0
LC
556
557 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
72cd8ec0
LC
558 (lset= string=?
559 (call-with-input-file g-guile read)
560 (list (derivation->output-path guile-drv) bash))))))
b53833b2 561
c8351d9a
LC
562(test-assertm "gexp->derivation #:allowed-references"
563 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
564 #~(begin
565 (mkdir #$output)
566 (chdir #$output)
567 (symlink #$output "self")
568 (symlink #$%bootstrap-guile
569 "guile"))
570 #:allowed-references
571 (list "out" %bootstrap-guile))))
572 (built-derivations (list drv))))
573
accb682c
LC
574(test-assertm "gexp->derivation #:allowed-references, specific output"
575 (mlet* %store-monad ((in (gexp->derivation "thing"
576 #~(begin
577 (mkdir #$output:ok)
578 (mkdir #$output:not-ok))))
579 (drv (gexp->derivation "allowed-refs"
580 #~(begin
581 (pk #$in:not-ok)
582 (mkdir #$output)
583 (chdir #$output)
584 (symlink #$output "self")
585 (symlink #$in:ok "ok"))
586 #:allowed-references
587 (list "out"
588 (gexp-input in "ok")))))
589 (built-derivations (list drv))))
590
c8351d9a
LC
591(test-assert "gexp->derivation #:allowed-references, disallowed"
592 (let ((drv (run-with-store %store
593 (gexp->derivation "allowed-refs"
594 #~(begin
595 (mkdir #$output)
596 (chdir #$output)
597 (symlink #$%bootstrap-guile "guile"))
598 #:allowed-references '()))))
599 (guard (c ((nix-protocol-error? c) #t))
600 (build-derivations %store (list drv))
601 #f)))
602
c17b5ab4 603(define shebang
c1bc358f 604 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
605 "/bin/guile --no-auto-compile"))
606
607;; If we're going to hit the silly shebang limit (128 chars on Linux-based
608;; systems), then skip the following test.
47b3124a 609(test-skip (if (> (string-length shebang) 127) 2 0))
c17b5ab4 610
21b679f6
LC
611(test-assertm "gexp->script"
612 (mlet* %store-monad ((n -> (random (expt 2 50)))
613 (exp -> (gexp
614 (system*
615 (string-append (ungexp %bootstrap-guile)
616 "/bin/guile")
617 "-c" (object->string
618 '(display (expt (ungexp n) 2))))))
619 (drv (gexp->script "guile-thing" exp
620 #:guile %bootstrap-guile))
621 (out -> (derivation->output-path drv))
622 (done (built-derivations (list drv))))
623 (let* ((pipe (open-input-pipe out))
624 (str (get-string-all pipe)))
625 (return (and (zero? (close-pipe pipe))
626 (= (expt n 2) (string->number str)))))))
627
15a01c72
LC
628(test-assertm "program-file"
629 (let* ((n (random (expt 2 50)))
630 (exp (gexp (begin
631 (use-modules (guix build utils))
632 (display (ungexp n)))))
633 (file (program-file "program" exp
634 #:modules '((guix build utils))
635 #:guile %bootstrap-guile)))
636 (mlet* %store-monad ((drv (lower-object file))
637 (out -> (derivation->output-path drv)))
638 (mbegin %store-monad
639 (built-derivations (list drv))
640 (let* ((pipe (open-input-pipe out))
641 (str (get-string-all pipe)))
642 (return (and (zero? (close-pipe pipe))
643 (= n (string->number str)))))))))
644
e1c153e0
LC
645(test-assertm "scheme-file"
646 (let* ((text (plain-file "foo" "Hello, world!"))
647 (scheme (scheme-file "bar" #~(list "foo" #$text))))
648 (mlet* %store-monad ((drv (lower-object scheme))
649 (text (lower-object text))
650 (out -> (derivation->output-path drv)))
651 (mbegin %store-monad
652 (built-derivations (list drv))
653 (mlet %store-monad ((refs ((store-lift references) out)))
654 (return (and (equal? refs (list text))
655 (equal? `(list "foo" ,text)
656 (call-with-input-file out read)))))))))
657
462a3fa3
LC
658(test-assert "text-file*"
659 (let ((references (store-lift references)))
660 (run-with-store %store
661 (mlet* %store-monad
662 ((drv (package->derivation %bootstrap-guile))
663 (guile -> (derivation->output-path drv))
664 (file (text-file "bar" "This is bar."))
665 (text (text-file* "foo"
666 %bootstrap-guile "/bin/guile "
a482cfdc 667 (gexp-input %bootstrap-guile "out") "/bin/guile "
462a3fa3
LC
668 drv "/bin/guile "
669 file))
670 (done (built-derivations (list text)))
671 (out -> (derivation->output-path text))
672 (refs (references out)))
673 ;; Make sure we get the right references and the right content.
674 (return (and (lset= string=? refs (list guile file))
675 (equal? (call-with-input-file out get-string-all)
676 (string-append guile "/bin/guile "
677 guile "/bin/guile "
678 guile "/bin/guile "
679 file)))))
680 #:guile-for-build (package-derivation %store %bootstrap-guile))))
681
b751cde3
LC
682(test-assertm "mixed-text-file"
683 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
684 "export PATH="
685 %bootstrap-guile "/bin"))
686 (drv (lower-object file))
687 (out -> (derivation->output-path drv))
688 (guile-drv (package->derivation %bootstrap-guile))
689 (guile -> (derivation->output-path guile-drv)))
690 (mbegin %store-monad
691 (built-derivations (list drv))
692 (mlet %store-monad ((refs ((store-lift references) out)))
693 (return (and (string=? (string-append "export PATH=" guile "/bin")
694 (call-with-input-file out get-string-all))
695 (equal? refs (list guile))))))))
696
a8afb9ae
LC
697(test-assert "gexp->derivation vs. %current-target-system"
698 (let ((mval (gexp->derivation "foo"
699 #~(begin
700 (mkdir #$output)
701 (foo #+gnu-make))
702 #:target #f)))
703 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
704 ;; influence.
705 (parameterize ((%current-target-system "fooooo"))
706 (derivation? (run-with-store %store mval)))))
707
c2b84676
LC
708(test-assertm "lower-object"
709 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
710 (drv2 (lower-object (package-source coreutils)))
711 (item (lower-object (plain-file "foo" "Hello!"))))
712 (return (and (derivation? drv1) (derivation? drv2)
713 (store-path? item)))))
714
91937029
LC
715(test-assertm "lower-object, computed-file"
716 (let* ((text (plain-file "foo" "Hello!"))
717 (exp #~(begin
718 (mkdir #$output)
719 (symlink #$%bootstrap-guile
720 (string-append #$output "/guile"))
721 (symlink #$text (string-append #$output "/text"))))
722 (computed (computed-file "computed" exp)))
723 (mlet* %store-monad ((text (lower-object text))
724 (guile-drv (lower-object %bootstrap-guile))
725 (comp-drv (lower-object computed))
726 (comp -> (derivation->output-path comp-drv)))
727 (mbegin %store-monad
728 (built-derivations (list comp-drv))
729 (return (and (string=? (readlink (string-append comp "/guile"))
730 (derivation->output-path guile-drv))
731 (string=? (readlink (string-append comp "/text"))
732 text)))))))
733
2cf0ea0d
LC
734(test-assert "printer"
735 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
736 \"/bin/uname\"\\) [[:xdigit:]]+>$"
737 (with-output-to-string
738 (lambda ()
739 (write
740 (gexp (string-append (ungexp coreutils)
741 "/bin/uname")))))))
742
743(test-assert "printer vs. ungexp-splicing"
744 (string-match "^#<gexp .* [[:xdigit:]]+>$"
745 (with-output-to-string
746 (lambda ()
747 ;; #~(begin #$@#~())
748 (write
749 (gexp (begin (ungexp-splicing (gexp ())))))))))
750
21b679f6
LC
751(test-equal "sugar"
752 '(gexp (foo (ungexp bar) (ungexp baz "out")
753 (ungexp (chbouib 42))
667b2508
LC
754 (ungexp-splicing (list x y z))
755 (ungexp-native foo) (ungexp-native foo "out")
756 (ungexp-native (chbouib 42))
757 (ungexp-native-splicing (list x y z))))
758 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
759 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
760
761(test-end "gexp")
762
763\f
764(exit (= (test-runner-fail-count (test-runner-current)) 0))
765
766;; Local Variables:
767;; eval: (put 'test-assertm 'scheme-indent-function 1)
768;; End: