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