1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (test-gexp)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix gexp)
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
25 #:use-module (guix tests)
26 #:use-module (gnu packages)
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-34)
31 #:use-module (srfi srfi-64)
32 #:use-module (rnrs io ports)
33 #:use-module (ice-9 match)
34 #:use-module (ice-9 regex)
35 #:use-module (ice-9 popen))
37 ;; Test the (guix gexp) module.
40 (open-connection-for-tests))
42 ;; For white-box testing.
43 (define (gexp-inputs x)
44 ((@@ (guix gexp) gexp-inputs) x))
45 (define (gexp-native-inputs x)
46 ((@@ (guix gexp) gexp-native-inputs) x))
47 (define (gexp-outputs x)
48 ((@@ (guix gexp) gexp-outputs) x))
49 (define (gexp->sexp . x)
50 (apply (@@ (guix gexp) gexp->sexp) x))
52 (define* (gexp->sexp* exp #:optional target)
53 (run-with-store %store (gexp->sexp exp
55 #:guile-for-build (%guile-for-build)))
57 (define-syntax-rule (test-assertm name exp)
59 (run-with-store %store exp
60 #:guile-for-build (%guile-for-build))))
67 (let ((exp (gexp (display "hello!"))))
69 (null? (gexp-inputs exp))
73 '(display `(foo ,(+ 2 3)))
74 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
76 (null? (gexp-inputs exp))
79 (test-assert "one input package"
80 (let ((exp (gexp (display (ungexp coreutils)))))
82 (match (gexp-inputs exp)
85 (equal? `(display ,(derivation->output-path
86 (package-derivation %store coreutils)))
89 (test-assert "one input origin"
90 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
92 (match (gexp-inputs exp)
94 (eq? o (package-source coreutils))))
95 (equal? `(display ,(derivation->output-path
96 (package-source-derivation
97 %store (package-source coreutils))))
100 (test-assert "one local file"
101 (let* ((file (search-path %load-path "guix.scm"))
102 (local (local-file file))
103 (exp (gexp (display (ungexp local))))
104 (intd (add-to-store %store (basename file) #t
107 (match (gexp-inputs exp)
110 (equal? `(display ,intd) (gexp->sexp* exp)))))
112 (test-assert "same input twice"
113 (let ((exp (gexp (begin
114 (display (ungexp coreutils))
115 (display (ungexp coreutils))))))
117 (match (gexp-inputs exp)
120 (let ((e `(display ,(derivation->output-path
121 (package-derivation %store coreutils)))))
122 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
124 (test-assert "two input packages, one derivation, one file"
125 (let* ((drv (build-expression->derivation
127 #:guile-for-build (package-derivation %store %bootstrap-guile)))
128 (txt (add-text-to-store %store "foo" "Hello, world!"))
130 (display (ungexp coreutils))
131 (display (ungexp %bootstrap-guile))
132 (display (ungexp drv))
133 (display (ungexp txt))))))
134 (define (match-input thing)
137 (eq? thing drv-or-pkg))))
140 (= 4 (length (gexp-inputs exp)))
141 (every (lambda (input)
142 (find (match-input input) (gexp-inputs exp)))
143 (list drv coreutils %bootstrap-guile txt))
144 (let ((e0 `(display ,(derivation->output-path
145 (package-derivation %store coreutils))))
146 (e1 `(display ,(derivation->output-path
147 (package-derivation %store %bootstrap-guile))))
148 (e2 `(display ,(derivation->output-path drv)))
149 (e3 `(display ,txt)))
150 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
152 (test-assert "ungexp + ungexp-native"
153 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
155 (ungexp-native glibc)
157 (target "mips64el-linux")
158 (guile (derivation->output-path
159 (package-derivation %store %bootstrap-guile)))
160 (cu (derivation->output-path
161 (package-cross-derivation %store coreutils target)))
162 (libc (derivation->output-path
163 (package-derivation %store glibc)))
164 (bu (derivation->output-path
165 (package-cross-derivation %store binutils target))))
167 `((,%bootstrap-guile "out") (,glibc "out"))
168 (gexp-native-inputs exp))
170 `((,coreutils "out") (,binutils "out"))
172 (equal? `(list ,guile ,cu ,libc ,bu)
173 (gexp->sexp* exp target)))))
175 (test-equal "ungexp + ungexp-native, nested"
176 (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out")))
177 (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
178 (ungexp %bootstrap-guile)))))
179 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
181 (test-assert "input list"
182 (let ((exp (gexp (display
183 '(ungexp (list %bootstrap-guile coreutils)))))
184 (guile (derivation->output-path
185 (package-derivation %store %bootstrap-guile)))
186 (cu (derivation->output-path
187 (package-derivation %store coreutils))))
189 `((,%bootstrap-guile "out") (,coreutils "out"))
191 (equal? `(display '(,guile ,cu))
192 (gexp->sexp* exp)))))
194 (test-assert "input list + ungexp-native"
195 (let* ((target "mips64el-linux")
197 (cons '(ungexp-native (list %bootstrap-guile coreutils))
198 '(ungexp (list glibc binutils))))))
199 (guile (derivation->output-path
200 (package-derivation %store %bootstrap-guile)))
201 (cu (derivation->output-path
202 (package-derivation %store coreutils)))
203 (xlibc (derivation->output-path
204 (package-cross-derivation %store glibc target)))
205 (xbu (derivation->output-path
206 (package-cross-derivation %store binutils target))))
208 `((,%bootstrap-guile "out") (,coreutils "out"))
209 (gexp-native-inputs exp))
211 `((,glibc "out") (,binutils "out"))
213 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
214 (gexp->sexp* exp target)))))
216 (test-assert "input list splicing"
217 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
218 (outputs (list (derivation->output-path
219 (package-derivation %store glibc)
221 (derivation->output-path
222 (package-derivation %store %bootstrap-guile))))
223 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
225 `((,glibc "debug") (,%bootstrap-guile "out"))
227 (equal? (gexp->sexp* exp)
228 `(list ,@(cons 5 outputs))))))
230 (test-assert "input list splicing + ungexp-native-splicing"
231 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
232 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
234 `((,glibc "debug") (,%bootstrap-guile "out"))
235 (gexp-native-inputs exp))
236 (null? (gexp-inputs exp))
237 (equal? (gexp->sexp* exp) ;native
238 (gexp->sexp* exp "mips64el-linux")))))
240 (test-equal "output list"
242 (let ((exp (gexp (begin (mkdir (ungexp output))
243 (mkdir (ungexp output "bar"))))))
244 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
246 (test-assert "output list, combined gexps"
247 (let* ((exp0 (gexp (mkdir (ungexp output))))
248 (exp1 (gexp (mkdir (ungexp output "foo"))))
249 (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
251 (append (gexp-outputs exp0) (gexp-outputs exp1))
253 (= 2 (length (gexp-outputs exp2))))))
255 (test-equal "output list, combined gexps, duplicate output"
257 (let* ((exp0 (gexp (mkdir (ungexp output))))
258 (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
259 (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
260 (length (gexp-outputs exp2))))
262 (test-assert "output list + ungexp-splicing list, combined gexps"
263 (let* ((exp0 (gexp (mkdir (ungexp output))))
264 (exp1 (gexp (mkdir (ungexp output "foo"))))
265 (exp2 (gexp (begin (display "hi!")
266 (ungexp-splicing (list exp0 exp1))))))
268 (append (gexp-outputs exp0) (gexp-outputs exp1))
270 (= 2 (length (gexp-outputs exp2))))))
272 (test-assertm "gexp->file"
273 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
274 (guile (package-file %bootstrap-guile))
275 (sexp (gexp->sexp exp))
276 (drv (gexp->file "foo" exp))
277 (out -> (derivation->output-path drv))
278 (done (built-derivations (list drv)))
279 (refs ((store-lift references) out)))
280 (return (and (equal? sexp (call-with-input-file out read))
281 (equal? (list guile) refs)))))
283 (test-assertm "gexp->derivation"
284 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
287 (mkdir (ungexp output))
288 (chdir (ungexp output))
290 (string-append (ungexp %bootstrap-guile)
293 (symlink (ungexp file)
294 (ungexp output "2nd")))))
295 (drv (gexp->derivation "foo" exp))
296 (out -> (derivation->output-path drv))
297 (out2 -> (derivation->output-path drv "2nd"))
298 (done (built-derivations (list drv)))
299 (refs ((store-lift references) out))
300 (refs2 ((store-lift references) out2))
301 (guile (package-file %bootstrap-guile "bin/guile")))
302 (return (and (string=? (readlink (string-append out "/foo")) guile)
303 (string=? (readlink out2) file)
304 (equal? refs (list (dirname (dirname guile))))
305 (equal? refs2 (list file))))))
307 (test-assertm "gexp->derivation vs. grafts"
308 (mlet* %store-monad ((p0 -> (dummy-package "dummy"
310 '(#:implicit-inputs? #f))))
311 (r -> (package (inherit p0) (name "DuMMY")))
312 (p1 -> (package (inherit p0) (replacement r)))
313 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
314 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
315 (void (set-guile-for-build %bootstrap-guile))
316 (drv0 (gexp->derivation "t" exp0))
317 (drv1 (gexp->derivation "t" exp1))
318 (drv1* (gexp->derivation "t" exp1 #:graft? #f)))
319 (return (and (not (string=? (derivation->output-path drv0)
320 (derivation->output-path drv1)))
321 (string=? (derivation->output-path drv0)
322 (derivation->output-path drv1*))))))
324 (test-assertm "gexp->derivation, composed gexps"
325 (mlet* %store-monad ((exp0 -> (gexp (begin
326 (mkdir (ungexp output))
327 (chdir (ungexp output)))))
328 (exp1 -> (gexp (symlink
329 (string-append (ungexp %bootstrap-guile)
332 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
333 (drv (gexp->derivation "foo" exp))
334 (out -> (derivation->output-path drv))
335 (done (built-derivations (list drv)))
336 (guile (package-file %bootstrap-guile "bin/guile")))
337 (return (string=? (readlink (string-append out "/foo"))
340 (test-assertm "gexp->derivation, default system"
341 ;; The default system should be the one at '>>=' time, not the one at
342 ;; invocation time. See <http://bugs.gnu.org/18002>.
343 (let ((system (%current-system))
344 (mdrv (parameterize ((%current-system "foobar64-linux"))
345 (gexp->derivation "foo"
347 (mkdir (ungexp output)))))))
348 (mlet %store-monad ((drv mdrv))
349 (return (string=? system (derivation-system drv))))))
351 (test-assertm "gexp->derivation, local-file"
352 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
353 (intd (interned-file file))
354 (local -> (local-file file))
356 (stat (ungexp local))
357 (symlink (ungexp local)
359 (drv (gexp->derivation "local-file" exp)))
361 (built-derivations (list drv))
362 (return (string=? (readlink (derivation->output-path drv))
365 (test-assertm "gexp->derivation, cross-compilation"
366 (mlet* %store-monad ((target -> "mips64el-linux")
367 (exp -> (gexp (list (ungexp coreutils)
369 (xdrv (gexp->derivation "foo" exp
371 (refs ((store-lift references)
372 (derivation-file-name xdrv)))
373 (xcu (package->cross-derivation coreutils
375 (cu (package->derivation coreutils)))
376 (return (and (member (derivation-file-name xcu) refs)
377 (not (member (derivation-file-name cu) refs))))))
379 (test-assertm "gexp->derivation, ungexp-native"
380 (mlet* %store-monad ((target -> "mips64el-linux")
381 (exp -> (gexp (list (ungexp-native coreutils)
383 (xdrv (gexp->derivation "foo" exp
385 (drv (gexp->derivation "foo" exp)))
386 (return (string=? (derivation-file-name drv)
387 (derivation-file-name xdrv)))))
389 (test-assertm "gexp->derivation, ungexp + ungexp-native"
390 (mlet* %store-monad ((target -> "mips64el-linux")
391 (exp -> (gexp (list (ungexp-native coreutils)
394 (xdrv (gexp->derivation "foo" exp
396 (refs ((store-lift references)
397 (derivation-file-name xdrv)))
398 (xglibc (package->cross-derivation glibc target))
399 (cu (package->derivation coreutils)))
400 (return (and (member (derivation-file-name cu) refs)
401 (member (derivation-file-name xglibc) refs)))))
403 (test-assertm "gexp->derivation, ungexp-native + composed gexps"
404 (mlet* %store-monad ((target -> "mips64el-linux")
405 (exp0 -> (gexp (list 1 2
406 (ungexp coreutils))))
407 (exp -> (gexp (list 0 (ungexp-native exp0))))
408 (xdrv (gexp->derivation "foo" exp
410 (drv (gexp->derivation "foo" exp)))
411 (return (string=? (derivation-file-name drv)
412 (derivation-file-name xdrv)))))
414 (test-assertm "gexp->derivation, store copy"
415 (let ((build-one #~(call-with-output-file #$output
417 (display "This is the one." port))))
418 (build-two (lambda (one)
421 (symlink #$one (string-append #$output "/one"))
422 (call-with-output-file (string-append #$output "/two")
424 (display "This is the second one." port))))))
426 (use-modules (guix build store-copy))
429 (populate-store '("graph") #$output))))
430 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
431 (two (gexp->derivation "two" (build-two one)))
432 (drv (gexp->derivation "store-copy" build-drv
436 '((guix build store-copy)
437 (guix build utils))))
438 (ok? (built-derivations (list drv)))
439 (out -> (derivation->output-path drv)))
440 (let ((one (derivation->output-path one))
441 (two (derivation->output-path two)))
443 (file-exists? (string-append out "/" one))
444 (file-exists? (string-append out "/" two))
445 (file-exists? (string-append out "/" two "/two"))
446 (string=? (readlink (string-append out "/" two "/one"))
449 (test-assertm "imported-files"
451 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
452 ("a/b/c" . ,(search-path %load-path
453 "guix/derivations.scm"))
454 ("p/q" . ,(search-path %load-path "guix.scm"))
455 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
456 (drv (imported-files files)))
458 (built-derivations (list drv))
459 (let ((dir (derivation->output-path drv)))
463 (equal? (call-with-input-file (string-append dir "/" path)
465 (call-with-input-file source
466 get-bytevector-all))))
469 (test-assertm "gexp->derivation #:modules"
472 (use-modules (guix build utils))
473 (mkdir-p (string-append #$output "/guile/guix/nix"))
475 (drv (gexp->derivation "test-with-modules" build
476 #:modules '((guix build utils)))))
478 (built-derivations (list drv))
479 (let* ((p (derivation->output-path drv))
480 (s (stat (string-append p "/guile/guix/nix"))))
481 (return (eq? (stat:type s) 'directory))))))
483 (test-assertm "gexp->derivation #:references-graphs"
485 ((one (text-file "one" "hello, world"))
486 (two (gexp->derivation "two"
487 #~(symlink #$one #$output:chbouib)))
488 (drv (gexp->derivation "ref-graphs"
490 (use-modules (guix build store-copy))
491 (with-output-to-file #$output
493 (write (call-with-input-file "guile"
494 read-reference-graph))))
495 (with-output-to-file #$output:one
497 (write (call-with-input-file "one"
498 read-reference-graph))))
499 (with-output-to-file #$output:two
501 (write (call-with-input-file "two"
502 read-reference-graph)))))
503 #:references-graphs `(("one" ,one)
504 ("two" ,two "chbouib")
505 ("guile" ,%bootstrap-guile))
506 #:modules '((guix build store-copy)
507 (guix build utils))))
508 (ok? (built-derivations (list drv)))
509 (guile-drv (package->derivation %bootstrap-guile))
510 (g-one -> (derivation->output-path drv "one"))
511 (g-two -> (derivation->output-path drv "two"))
512 (g-guile -> (derivation->output-path drv)))
514 (equal? (call-with-input-file g-one read) (list one))
515 (equal? (call-with-input-file g-two read)
516 (list one (derivation->output-path two "chbouib")))
517 (equal? (call-with-input-file g-guile read)
518 (list (derivation->output-path guile-drv)))))))
520 (test-assertm "gexp->derivation #:allowed-references"
521 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
525 (symlink #$output "self")
526 (symlink #$%bootstrap-guile
529 (list "out" %bootstrap-guile))))
530 (built-derivations (list drv))))
532 (test-assertm "gexp->derivation #:allowed-references, specific output"
533 (mlet* %store-monad ((in (gexp->derivation "thing"
536 (mkdir #$output:not-ok))))
537 (drv (gexp->derivation "allowed-refs"
542 (symlink #$output "self")
543 (symlink #$in:ok "ok"))
546 (gexp-input in "ok")))))
547 (built-derivations (list drv))))
549 (test-assert "gexp->derivation #:allowed-references, disallowed"
550 (let ((drv (run-with-store %store
551 (gexp->derivation "allowed-refs"
555 (symlink #$%bootstrap-guile "guile"))
556 #:allowed-references '()))))
557 (guard (c ((nix-protocol-error? c) #t))
558 (build-derivations %store (list drv))
562 (string-append "#!" (derivation->output-path (%guile-for-build))
563 "/bin/guile --no-auto-compile"))
565 ;; If we're going to hit the silly shebang limit (128 chars on Linux-based
566 ;; systems), then skip the following test.
567 (test-skip (if (> (string-length shebang) 127) 1 0))
569 (test-assertm "gexp->script"
570 (mlet* %store-monad ((n -> (random (expt 2 50)))
573 (string-append (ungexp %bootstrap-guile)
576 '(display (expt (ungexp n) 2))))))
577 (drv (gexp->script "guile-thing" exp
578 #:guile %bootstrap-guile))
579 (out -> (derivation->output-path drv))
580 (done (built-derivations (list drv))))
581 (let* ((pipe (open-input-pipe out))
582 (str (get-string-all pipe)))
583 (return (and (zero? (close-pipe pipe))
584 (= (expt n 2) (string->number str)))))))
586 (test-assert "text-file*"
587 (let ((references (store-lift references)))
588 (run-with-store %store
590 ((drv (package->derivation %bootstrap-guile))
591 (guile -> (derivation->output-path drv))
592 (file (text-file "bar" "This is bar."))
593 (text (text-file* "foo"
594 %bootstrap-guile "/bin/guile "
595 (gexp-input %bootstrap-guile "out") "/bin/guile "
598 (done (built-derivations (list text)))
599 (out -> (derivation->output-path text))
600 (refs (references out)))
601 ;; Make sure we get the right references and the right content.
602 (return (and (lset= string=? refs (list guile file))
603 (equal? (call-with-input-file out get-string-all)
604 (string-append guile "/bin/guile "
608 #:guile-for-build (package-derivation %store %bootstrap-guile))))
610 (test-assert "printer"
611 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
612 \"/bin/uname\"\\) [[:xdigit:]]+>$"
613 (with-output-to-string
616 (gexp (string-append (ungexp coreutils)
619 (test-assert "printer vs. ungexp-splicing"
620 (string-match "^#<gexp .* [[:xdigit:]]+>$"
621 (with-output-to-string
625 (gexp (begin (ungexp-splicing (gexp ())))))))))
628 '(gexp (foo (ungexp bar) (ungexp baz "out")
629 (ungexp (chbouib 42))
630 (ungexp-splicing (list x y z))
631 (ungexp-native foo) (ungexp-native foo "out")
632 (ungexp-native (chbouib 42))
633 (ungexp-native-splicing (list x y z))))
634 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
635 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
640 (exit (= (test-runner-fail-count (test-runner-current)) 0))
643 ;; eval: (put 'test-assertm 'scheme-indent-function 1)