packages: Add 'set-grafting' procedure.
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
462a3fa3 2;;; Copyright © 2014, 2015 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)
23 #:use-module (guix derivations)
79c0c8cd 24 #:use-module (guix packages)
c1bc358f 25 #:use-module (guix tests)
21b679f6
LC
26 #:use-module (gnu packages)
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-1)
c8351d9a 30 #:use-module (srfi srfi-34)
21b679f6
LC
31 #:use-module (srfi srfi-64)
32 #:use-module (rnrs io ports)
33 #:use-module (ice-9 match)
2cf0ea0d 34 #:use-module (ice-9 regex)
21b679f6
LC
35 #:use-module (ice-9 popen))
36
37;; Test the (guix gexp) module.
38
39(define %store
c1bc358f 40 (open-connection-for-tests))
21b679f6
LC
41
42;; For white-box testing.
43(define gexp-inputs (@@ (guix gexp) gexp-inputs))
667b2508 44(define gexp-native-inputs (@@ (guix gexp) gexp-native-inputs))
21b679f6
LC
45(define gexp->sexp (@@ (guix gexp) gexp->sexp))
46
667b2508 47(define* (gexp->sexp* exp #:optional target)
68a61e9f 48 (run-with-store %store (gexp->sexp exp
68a61e9f 49 #:target target)
c1bc358f 50 #:guile-for-build (%guile-for-build)))
21b679f6
LC
51
52(define-syntax-rule (test-assertm name exp)
53 (test-assert name
54 (run-with-store %store exp
c1bc358f 55 #:guile-for-build (%guile-for-build))))
21b679f6
LC
56
57\f
58(test-begin "gexp")
59
60(test-equal "no refs"
61 '(display "hello!")
62 (let ((exp (gexp (display "hello!"))))
63 (and (gexp? exp)
64 (null? (gexp-inputs exp))
65 (gexp->sexp* exp))))
66
67(test-equal "unquote"
68 '(display `(foo ,(+ 2 3)))
69 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
70 (and (gexp? exp)
71 (null? (gexp-inputs exp))
72 (gexp->sexp* exp))))
73
74(test-assert "one input package"
75 (let ((exp (gexp (display (ungexp coreutils)))))
76 (and (gexp? exp)
77 (match (gexp-inputs exp)
78 (((p "out"))
79 (eq? p coreutils)))
80 (equal? `(display ,(derivation->output-path
81 (package-derivation %store coreutils)))
82 (gexp->sexp* exp)))))
83
79c0c8cd
LC
84(test-assert "one input origin"
85 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
86 (and (gexp? exp)
87 (match (gexp-inputs exp)
88 (((o "out"))
89 (eq? o (package-source coreutils))))
90 (equal? `(display ,(derivation->output-path
91 (package-source-derivation
92 %store (package-source coreutils))))
93 (gexp->sexp* exp)))))
94
21b679f6
LC
95(test-assert "same input twice"
96 (let ((exp (gexp (begin
97 (display (ungexp coreutils))
98 (display (ungexp coreutils))))))
99 (and (gexp? exp)
100 (match (gexp-inputs exp)
101 (((p "out"))
102 (eq? p coreutils)))
103 (let ((e `(display ,(derivation->output-path
104 (package-derivation %store coreutils)))))
105 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
106
107(test-assert "two input packages, one derivation, one file"
108 (let* ((drv (build-expression->derivation
109 %store "foo" 'bar
110 #:guile-for-build (package-derivation %store %bootstrap-guile)))
111 (txt (add-text-to-store %store "foo" "Hello, world!"))
112 (exp (gexp (begin
113 (display (ungexp coreutils))
114 (display (ungexp %bootstrap-guile))
115 (display (ungexp drv))
116 (display (ungexp txt))))))
117 (define (match-input thing)
118 (match-lambda
119 ((drv-or-pkg _ ...)
120 (eq? thing drv-or-pkg))))
121
122 (and (gexp? exp)
123 (= 4 (length (gexp-inputs exp)))
124 (every (lambda (input)
125 (find (match-input input) (gexp-inputs exp)))
126 (list drv coreutils %bootstrap-guile txt))
127 (let ((e0 `(display ,(derivation->output-path
128 (package-derivation %store coreutils))))
129 (e1 `(display ,(derivation->output-path
130 (package-derivation %store %bootstrap-guile))))
131 (e2 `(display ,(derivation->output-path drv)))
132 (e3 `(display ,txt)))
133 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
134
667b2508
LC
135(test-assert "ungexp + ungexp-native"
136 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
137 (ungexp coreutils)
138 (ungexp-native glibc)
139 (ungexp binutils))))
140 (target "mips64el-linux")
141 (guile (derivation->output-path
142 (package-derivation %store %bootstrap-guile)))
143 (cu (derivation->output-path
144 (package-cross-derivation %store coreutils target)))
145 (libc (derivation->output-path
146 (package-derivation %store glibc)))
147 (bu (derivation->output-path
148 (package-cross-derivation %store binutils target))))
149 (and (lset= equal?
150 `((,%bootstrap-guile "out") (,glibc "out"))
151 (gexp-native-inputs exp))
152 (lset= equal?
153 `((,coreutils "out") (,binutils "out"))
154 (gexp-inputs exp))
155 (equal? `(list ,guile ,cu ,libc ,bu)
156 (gexp->sexp* exp target)))))
157
21b679f6
LC
158(test-assert "input list"
159 (let ((exp (gexp (display
160 '(ungexp (list %bootstrap-guile coreutils)))))
161 (guile (derivation->output-path
162 (package-derivation %store %bootstrap-guile)))
163 (cu (derivation->output-path
164 (package-derivation %store coreutils))))
165 (and (lset= equal?
166 `((,%bootstrap-guile "out") (,coreutils "out"))
167 (gexp-inputs exp))
168 (equal? `(display '(,guile ,cu))
169 (gexp->sexp* exp)))))
170
667b2508
LC
171(test-assert "input list + ungexp-native"
172 (let* ((target "mips64el-linux")
173 (exp (gexp (display
174 (cons '(ungexp-native (list %bootstrap-guile coreutils))
175 '(ungexp (list glibc binutils))))))
176 (guile (derivation->output-path
177 (package-derivation %store %bootstrap-guile)))
178 (cu (derivation->output-path
179 (package-derivation %store coreutils)))
180 (xlibc (derivation->output-path
181 (package-cross-derivation %store glibc target)))
182 (xbu (derivation->output-path
183 (package-cross-derivation %store binutils target))))
184 (and (lset= equal?
185 `((,%bootstrap-guile "out") (,coreutils "out"))
186 (gexp-native-inputs exp))
187 (lset= equal?
188 `((,glibc "out") (,binutils "out"))
189 (gexp-inputs exp))
190 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
191 (gexp->sexp* exp target)))))
192
21b679f6
LC
193(test-assert "input list splicing"
194 (let* ((inputs (list (list glibc "debug") %bootstrap-guile))
195 (outputs (list (derivation->output-path
196 (package-derivation %store glibc)
197 "debug")
198 (derivation->output-path
199 (package-derivation %store %bootstrap-guile))))
200 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
201 (and (lset= equal?
202 `((,glibc "debug") (,%bootstrap-guile "out"))
203 (gexp-inputs exp))
204 (equal? (gexp->sexp* exp)
205 `(list ,@(cons 5 outputs))))))
206
667b2508
LC
207(test-assert "input list splicing + ungexp-native-splicing"
208 (let* ((inputs (list (list glibc "debug") %bootstrap-guile))
209 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
210 (and (lset= equal?
211 `((,glibc "debug") (,%bootstrap-guile "out"))
212 (gexp-native-inputs exp))
213 (null? (gexp-inputs exp))
214 (equal? (gexp->sexp* exp) ;native
215 (gexp->sexp* exp "mips64el-linux")))))
216
21b679f6
LC
217(test-assertm "gexp->file"
218 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
219 (guile (package-file %bootstrap-guile))
220 (sexp (gexp->sexp exp))
221 (drv (gexp->file "foo" exp))
222 (out -> (derivation->output-path drv))
223 (done (built-derivations (list drv)))
224 (refs ((store-lift references) out)))
225 (return (and (equal? sexp (call-with-input-file out read))
226 (equal? (list guile) refs)))))
227
228(test-assertm "gexp->derivation"
229 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
230 (exp -> (gexp
231 (begin
232 (mkdir (ungexp output))
233 (chdir (ungexp output))
234 (symlink
235 (string-append (ungexp %bootstrap-guile)
236 "/bin/guile")
237 "foo")
238 (symlink (ungexp file)
239 (ungexp output "2nd")))))
240 (drv (gexp->derivation "foo" exp))
241 (out -> (derivation->output-path drv))
242 (out2 -> (derivation->output-path drv "2nd"))
243 (done (built-derivations (list drv)))
244 (refs ((store-lift references) out))
245 (refs2 ((store-lift references) out2))
246 (guile (package-file %bootstrap-guile "bin/guile")))
247 (return (and (string=? (readlink (string-append out "/foo")) guile)
248 (string=? (readlink out2) file)
249 (equal? refs (list (dirname (dirname guile))))
250 (equal? refs2 (list file))))))
251
252(test-assertm "gexp->derivation, composed gexps"
253 (mlet* %store-monad ((exp0 -> (gexp (begin
254 (mkdir (ungexp output))
255 (chdir (ungexp output)))))
256 (exp1 -> (gexp (symlink
257 (string-append (ungexp %bootstrap-guile)
258 "/bin/guile")
259 "foo")))
260 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
261 (drv (gexp->derivation "foo" exp))
262 (out -> (derivation->output-path drv))
263 (done (built-derivations (list drv)))
264 (guile (package-file %bootstrap-guile "bin/guile")))
265 (return (string=? (readlink (string-append out "/foo"))
266 guile))))
267
5d098459
LC
268(test-assertm "gexp->derivation, default system"
269 ;; The default system should be the one at '>>=' time, not the one at
270 ;; invocation time. See <http://bugs.gnu.org/18002>.
271 (let ((system (%current-system))
272 (mdrv (parameterize ((%current-system "foobar64-linux"))
273 (gexp->derivation "foo"
274 (gexp
275 (mkdir (ungexp output)))))))
276 (mlet %store-monad ((drv mdrv))
277 (return (string=? system (derivation-system drv))))))
278
68a61e9f
LC
279(test-assertm "gexp->derivation, cross-compilation"
280 (mlet* %store-monad ((target -> "mips64el-linux")
281 (exp -> (gexp (list (ungexp coreutils)
282 (ungexp output))))
283 (xdrv (gexp->derivation "foo" exp
284 #:target target))
285 (refs ((store-lift references)
286 (derivation-file-name xdrv)))
287 (xcu (package->cross-derivation coreutils
288 target))
289 (cu (package->derivation coreutils)))
290 (return (and (member (derivation-file-name xcu) refs)
291 (not (member (derivation-file-name cu) refs))))))
292
667b2508
LC
293(test-assertm "gexp->derivation, ungexp-native"
294 (mlet* %store-monad ((target -> "mips64el-linux")
295 (exp -> (gexp (list (ungexp-native coreutils)
296 (ungexp output))))
297 (xdrv (gexp->derivation "foo" exp
298 #:target target))
299 (drv (gexp->derivation "foo" exp)))
300 (return (string=? (derivation-file-name drv)
301 (derivation-file-name xdrv)))))
302
303(test-assertm "gexp->derivation, ungexp + ungexp-native"
304 (mlet* %store-monad ((target -> "mips64el-linux")
305 (exp -> (gexp (list (ungexp-native coreutils)
306 (ungexp glibc)
307 (ungexp output))))
308 (xdrv (gexp->derivation "foo" exp
309 #:target target))
310 (refs ((store-lift references)
311 (derivation-file-name xdrv)))
312 (xglibc (package->cross-derivation glibc target))
313 (cu (package->derivation coreutils)))
314 (return (and (member (derivation-file-name cu) refs)
315 (member (derivation-file-name xglibc) refs)))))
316
317(test-assertm "gexp->derivation, ungexp-native + composed gexps"
318 (mlet* %store-monad ((target -> "mips64el-linux")
319 (exp0 -> (gexp (list 1 2
320 (ungexp coreutils))))
321 (exp -> (gexp (list 0 (ungexp-native exp0))))
322 (xdrv (gexp->derivation "foo" exp
323 #:target target))
324 (drv (gexp->derivation "foo" exp)))
325 (return (string=? (derivation-file-name drv)
326 (derivation-file-name xdrv)))))
327
6fd1a796
LC
328(test-assertm "gexp->derivation, store copy"
329 (let ((build-one #~(call-with-output-file #$output
330 (lambda (port)
331 (display "This is the one." port))))
332 (build-two (lambda (one)
333 #~(begin
334 (mkdir #$output)
335 (symlink #$one (string-append #$output "/one"))
336 (call-with-output-file (string-append #$output "/two")
337 (lambda (port)
338 (display "This is the second one." port))))))
b53833b2
LC
339 (build-drv #~(begin
340 (use-modules (guix build store-copy))
6fd1a796 341
b53833b2
LC
342 (mkdir #$output)
343 (populate-store '("graph") #$output))))
6fd1a796
LC
344 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
345 (two (gexp->derivation "two" (build-two one)))
b53833b2 346 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 347 #:references-graphs
b53833b2 348 `(("graph" ,two))
6fd1a796
LC
349 #:modules
350 '((guix build store-copy)
351 (guix build utils))))
352 (ok? (built-derivations (list drv)))
353 (out -> (derivation->output-path drv)))
354 (let ((one (derivation->output-path one))
355 (two (derivation->output-path two)))
356 (return (and ok?
357 (file-exists? (string-append out "/" one))
358 (file-exists? (string-append out "/" two))
359 (file-exists? (string-append out "/" two "/two"))
360 (string=? (readlink (string-append out "/" two "/one"))
361 one)))))))
362
aa72d9af
LC
363(test-assertm "imported-files"
364 (mlet* %store-monad
365 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
366 ("a/b/c" . ,(search-path %load-path
367 "guix/derivations.scm"))
368 ("p/q" . ,(search-path %load-path "guix.scm"))
369 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
370 (drv (imported-files files)))
371 (mbegin %store-monad
372 (built-derivations (list drv))
373 (let ((dir (derivation->output-path drv)))
374 (return
375 (every (match-lambda
376 ((path . source)
377 (equal? (call-with-input-file (string-append dir "/" path)
378 get-bytevector-all)
379 (call-with-input-file source
380 get-bytevector-all))))
381 files))))))
382
383(test-assertm "gexp->derivation #:modules"
384 (mlet* %store-monad
385 ((build -> #~(begin
386 (use-modules (guix build utils))
387 (mkdir-p (string-append #$output "/guile/guix/nix"))
388 #t))
389 (drv (gexp->derivation "test-with-modules" build
390 #:modules '((guix build utils)))))
391 (mbegin %store-monad
392 (built-derivations (list drv))
393 (let* ((p (derivation->output-path drv))
394 (s (stat (string-append p "/guile/guix/nix"))))
395 (return (eq? (stat:type s) 'directory))))))
396
b53833b2
LC
397(test-assertm "gexp->derivation #:references-graphs"
398 (mlet* %store-monad
399 ((one (text-file "one" "hello, world"))
400 (two (gexp->derivation "two"
401 #~(symlink #$one #$output:chbouib)))
402 (drv (gexp->derivation "ref-graphs"
403 #~(begin
404 (use-modules (guix build store-copy))
405 (with-output-to-file #$output
406 (lambda ()
407 (write (call-with-input-file "guile"
408 read-reference-graph))))
409 (with-output-to-file #$output:one
410 (lambda ()
411 (write (call-with-input-file "one"
412 read-reference-graph))))
413 (with-output-to-file #$output:two
414 (lambda ()
415 (write (call-with-input-file "two"
416 read-reference-graph)))))
417 #:references-graphs `(("one" ,one)
418 ("two" ,two "chbouib")
419 ("guile" ,%bootstrap-guile))
420 #:modules '((guix build store-copy)
421 (guix build utils))))
422 (ok? (built-derivations (list drv)))
423 (guile-drv (package->derivation %bootstrap-guile))
424 (g-one -> (derivation->output-path drv "one"))
425 (g-two -> (derivation->output-path drv "two"))
426 (g-guile -> (derivation->output-path drv)))
427 (return (and ok?
428 (equal? (call-with-input-file g-one read) (list one))
429 (equal? (call-with-input-file g-two read)
430 (list one (derivation->output-path two "chbouib")))
431 (equal? (call-with-input-file g-guile read)
432 (list (derivation->output-path guile-drv)))))))
433
c8351d9a
LC
434(test-assertm "gexp->derivation #:allowed-references"
435 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
436 #~(begin
437 (mkdir #$output)
438 (chdir #$output)
439 (symlink #$output "self")
440 (symlink #$%bootstrap-guile
441 "guile"))
442 #:allowed-references
443 (list "out" %bootstrap-guile))))
444 (built-derivations (list drv))))
445
446(test-assert "gexp->derivation #:allowed-references, disallowed"
447 (let ((drv (run-with-store %store
448 (gexp->derivation "allowed-refs"
449 #~(begin
450 (mkdir #$output)
451 (chdir #$output)
452 (symlink #$%bootstrap-guile "guile"))
453 #:allowed-references '()))))
454 (guard (c ((nix-protocol-error? c) #t))
455 (build-derivations %store (list drv))
456 #f)))
457
c17b5ab4 458(define shebang
c1bc358f 459 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
460 "/bin/guile --no-auto-compile"))
461
462;; If we're going to hit the silly shebang limit (128 chars on Linux-based
463;; systems), then skip the following test.
464(test-skip (if (> (string-length shebang) 127) 1 0))
465
21b679f6
LC
466(test-assertm "gexp->script"
467 (mlet* %store-monad ((n -> (random (expt 2 50)))
468 (exp -> (gexp
469 (system*
470 (string-append (ungexp %bootstrap-guile)
471 "/bin/guile")
472 "-c" (object->string
473 '(display (expt (ungexp n) 2))))))
474 (drv (gexp->script "guile-thing" exp
475 #:guile %bootstrap-guile))
476 (out -> (derivation->output-path drv))
477 (done (built-derivations (list drv))))
478 (let* ((pipe (open-input-pipe out))
479 (str (get-string-all pipe)))
480 (return (and (zero? (close-pipe pipe))
481 (= (expt n 2) (string->number str)))))))
482
462a3fa3
LC
483(test-assert "text-file*"
484 (let ((references (store-lift references)))
485 (run-with-store %store
486 (mlet* %store-monad
487 ((drv (package->derivation %bootstrap-guile))
488 (guile -> (derivation->output-path drv))
489 (file (text-file "bar" "This is bar."))
490 (text (text-file* "foo"
491 %bootstrap-guile "/bin/guile "
492 `(,%bootstrap-guile "out") "/bin/guile "
493 drv "/bin/guile "
494 file))
495 (done (built-derivations (list text)))
496 (out -> (derivation->output-path text))
497 (refs (references out)))
498 ;; Make sure we get the right references and the right content.
499 (return (and (lset= string=? refs (list guile file))
500 (equal? (call-with-input-file out get-string-all)
501 (string-append guile "/bin/guile "
502 guile "/bin/guile "
503 guile "/bin/guile "
504 file)))))
505 #:guile-for-build (package-derivation %store %bootstrap-guile))))
506
2cf0ea0d
LC
507(test-assert "printer"
508 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
509 \"/bin/uname\"\\) [[:xdigit:]]+>$"
510 (with-output-to-string
511 (lambda ()
512 (write
513 (gexp (string-append (ungexp coreutils)
514 "/bin/uname")))))))
515
516(test-assert "printer vs. ungexp-splicing"
517 (string-match "^#<gexp .* [[:xdigit:]]+>$"
518 (with-output-to-string
519 (lambda ()
520 ;; #~(begin #$@#~())
521 (write
522 (gexp (begin (ungexp-splicing (gexp ())))))))))
523
21b679f6
LC
524(test-equal "sugar"
525 '(gexp (foo (ungexp bar) (ungexp baz "out")
526 (ungexp (chbouib 42))
667b2508
LC
527 (ungexp-splicing (list x y z))
528 (ungexp-native foo) (ungexp-native foo "out")
529 (ungexp-native (chbouib 42))
530 (ungexp-native-splicing (list x y z))))
531 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
532 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
533
534(test-end "gexp")
535
536\f
537(exit (= (test-runner-fail-count (test-runner-current)) 0))
538
539;; Local Variables:
540;; eval: (put 'test-assertm 'scheme-indent-function 1)
541;; End: