download: Comment on lack of progress report with chunked encoding.
[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
ce45eb4c
LC
252(test-assertm "gexp->derivation vs. grafts"
253 (mlet* %store-monad ((p0 -> (dummy-package "dummy"
254 (arguments
255 '(#:implicit-inputs? #f))))
256 (r -> (package (inherit p0) (name "DuMMY")))
257 (p1 -> (package (inherit p0) (replacement r)))
258 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
259 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
260 (void (set-guile-for-build %bootstrap-guile))
261 (drv0 (gexp->derivation "t" exp0))
262 (drv1 (gexp->derivation "t" exp1))
263 (drv1* (gexp->derivation "t" exp1 #:graft? #f)))
264 (return (and (not (string=? (derivation->output-path drv0)
265 (derivation->output-path drv1)))
266 (string=? (derivation->output-path drv0)
267 (derivation->output-path drv1*))))))
268
21b679f6
LC
269(test-assertm "gexp->derivation, composed gexps"
270 (mlet* %store-monad ((exp0 -> (gexp (begin
271 (mkdir (ungexp output))
272 (chdir (ungexp output)))))
273 (exp1 -> (gexp (symlink
274 (string-append (ungexp %bootstrap-guile)
275 "/bin/guile")
276 "foo")))
277 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
278 (drv (gexp->derivation "foo" exp))
279 (out -> (derivation->output-path drv))
280 (done (built-derivations (list drv)))
281 (guile (package-file %bootstrap-guile "bin/guile")))
282 (return (string=? (readlink (string-append out "/foo"))
283 guile))))
284
5d098459
LC
285(test-assertm "gexp->derivation, default system"
286 ;; The default system should be the one at '>>=' time, not the one at
287 ;; invocation time. See <http://bugs.gnu.org/18002>.
288 (let ((system (%current-system))
289 (mdrv (parameterize ((%current-system "foobar64-linux"))
290 (gexp->derivation "foo"
291 (gexp
292 (mkdir (ungexp output)))))))
293 (mlet %store-monad ((drv mdrv))
294 (return (string=? system (derivation-system drv))))))
295
68a61e9f
LC
296(test-assertm "gexp->derivation, cross-compilation"
297 (mlet* %store-monad ((target -> "mips64el-linux")
298 (exp -> (gexp (list (ungexp coreutils)
299 (ungexp output))))
300 (xdrv (gexp->derivation "foo" exp
301 #:target target))
302 (refs ((store-lift references)
303 (derivation-file-name xdrv)))
304 (xcu (package->cross-derivation coreutils
305 target))
306 (cu (package->derivation coreutils)))
307 (return (and (member (derivation-file-name xcu) refs)
308 (not (member (derivation-file-name cu) refs))))))
309
667b2508
LC
310(test-assertm "gexp->derivation, ungexp-native"
311 (mlet* %store-monad ((target -> "mips64el-linux")
312 (exp -> (gexp (list (ungexp-native coreutils)
313 (ungexp output))))
314 (xdrv (gexp->derivation "foo" exp
315 #:target target))
316 (drv (gexp->derivation "foo" exp)))
317 (return (string=? (derivation-file-name drv)
318 (derivation-file-name xdrv)))))
319
320(test-assertm "gexp->derivation, ungexp + ungexp-native"
321 (mlet* %store-monad ((target -> "mips64el-linux")
322 (exp -> (gexp (list (ungexp-native coreutils)
323 (ungexp glibc)
324 (ungexp output))))
325 (xdrv (gexp->derivation "foo" exp
326 #:target target))
327 (refs ((store-lift references)
328 (derivation-file-name xdrv)))
329 (xglibc (package->cross-derivation glibc target))
330 (cu (package->derivation coreutils)))
331 (return (and (member (derivation-file-name cu) refs)
332 (member (derivation-file-name xglibc) refs)))))
333
334(test-assertm "gexp->derivation, ungexp-native + composed gexps"
335 (mlet* %store-monad ((target -> "mips64el-linux")
336 (exp0 -> (gexp (list 1 2
337 (ungexp coreutils))))
338 (exp -> (gexp (list 0 (ungexp-native exp0))))
339 (xdrv (gexp->derivation "foo" exp
340 #:target target))
341 (drv (gexp->derivation "foo" exp)))
342 (return (string=? (derivation-file-name drv)
343 (derivation-file-name xdrv)))))
344
6fd1a796
LC
345(test-assertm "gexp->derivation, store copy"
346 (let ((build-one #~(call-with-output-file #$output
347 (lambda (port)
348 (display "This is the one." port))))
349 (build-two (lambda (one)
350 #~(begin
351 (mkdir #$output)
352 (symlink #$one (string-append #$output "/one"))
353 (call-with-output-file (string-append #$output "/two")
354 (lambda (port)
355 (display "This is the second one." port))))))
b53833b2
LC
356 (build-drv #~(begin
357 (use-modules (guix build store-copy))
6fd1a796 358
b53833b2
LC
359 (mkdir #$output)
360 (populate-store '("graph") #$output))))
6fd1a796
LC
361 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
362 (two (gexp->derivation "two" (build-two one)))
b53833b2 363 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 364 #:references-graphs
b53833b2 365 `(("graph" ,two))
6fd1a796
LC
366 #:modules
367 '((guix build store-copy)
368 (guix build utils))))
369 (ok? (built-derivations (list drv)))
370 (out -> (derivation->output-path drv)))
371 (let ((one (derivation->output-path one))
372 (two (derivation->output-path two)))
373 (return (and ok?
374 (file-exists? (string-append out "/" one))
375 (file-exists? (string-append out "/" two))
376 (file-exists? (string-append out "/" two "/two"))
377 (string=? (readlink (string-append out "/" two "/one"))
378 one)))))))
379
aa72d9af
LC
380(test-assertm "imported-files"
381 (mlet* %store-monad
382 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
383 ("a/b/c" . ,(search-path %load-path
384 "guix/derivations.scm"))
385 ("p/q" . ,(search-path %load-path "guix.scm"))
386 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
387 (drv (imported-files files)))
388 (mbegin %store-monad
389 (built-derivations (list drv))
390 (let ((dir (derivation->output-path drv)))
391 (return
392 (every (match-lambda
393 ((path . source)
394 (equal? (call-with-input-file (string-append dir "/" path)
395 get-bytevector-all)
396 (call-with-input-file source
397 get-bytevector-all))))
398 files))))))
399
400(test-assertm "gexp->derivation #:modules"
401 (mlet* %store-monad
402 ((build -> #~(begin
403 (use-modules (guix build utils))
404 (mkdir-p (string-append #$output "/guile/guix/nix"))
405 #t))
406 (drv (gexp->derivation "test-with-modules" build
407 #:modules '((guix build utils)))))
408 (mbegin %store-monad
409 (built-derivations (list drv))
410 (let* ((p (derivation->output-path drv))
411 (s (stat (string-append p "/guile/guix/nix"))))
412 (return (eq? (stat:type s) 'directory))))))
413
b53833b2
LC
414(test-assertm "gexp->derivation #:references-graphs"
415 (mlet* %store-monad
416 ((one (text-file "one" "hello, world"))
417 (two (gexp->derivation "two"
418 #~(symlink #$one #$output:chbouib)))
419 (drv (gexp->derivation "ref-graphs"
420 #~(begin
421 (use-modules (guix build store-copy))
422 (with-output-to-file #$output
423 (lambda ()
424 (write (call-with-input-file "guile"
425 read-reference-graph))))
426 (with-output-to-file #$output:one
427 (lambda ()
428 (write (call-with-input-file "one"
429 read-reference-graph))))
430 (with-output-to-file #$output:two
431 (lambda ()
432 (write (call-with-input-file "two"
433 read-reference-graph)))))
434 #:references-graphs `(("one" ,one)
435 ("two" ,two "chbouib")
436 ("guile" ,%bootstrap-guile))
437 #:modules '((guix build store-copy)
438 (guix build utils))))
439 (ok? (built-derivations (list drv)))
440 (guile-drv (package->derivation %bootstrap-guile))
441 (g-one -> (derivation->output-path drv "one"))
442 (g-two -> (derivation->output-path drv "two"))
443 (g-guile -> (derivation->output-path drv)))
444 (return (and ok?
445 (equal? (call-with-input-file g-one read) (list one))
446 (equal? (call-with-input-file g-two read)
447 (list one (derivation->output-path two "chbouib")))
448 (equal? (call-with-input-file g-guile read)
449 (list (derivation->output-path guile-drv)))))))
450
c8351d9a
LC
451(test-assertm "gexp->derivation #:allowed-references"
452 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
453 #~(begin
454 (mkdir #$output)
455 (chdir #$output)
456 (symlink #$output "self")
457 (symlink #$%bootstrap-guile
458 "guile"))
459 #:allowed-references
460 (list "out" %bootstrap-guile))))
461 (built-derivations (list drv))))
462
463(test-assert "gexp->derivation #:allowed-references, disallowed"
464 (let ((drv (run-with-store %store
465 (gexp->derivation "allowed-refs"
466 #~(begin
467 (mkdir #$output)
468 (chdir #$output)
469 (symlink #$%bootstrap-guile "guile"))
470 #:allowed-references '()))))
471 (guard (c ((nix-protocol-error? c) #t))
472 (build-derivations %store (list drv))
473 #f)))
474
c17b5ab4 475(define shebang
c1bc358f 476 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
477 "/bin/guile --no-auto-compile"))
478
479;; If we're going to hit the silly shebang limit (128 chars on Linux-based
480;; systems), then skip the following test.
481(test-skip (if (> (string-length shebang) 127) 1 0))
482
21b679f6
LC
483(test-assertm "gexp->script"
484 (mlet* %store-monad ((n -> (random (expt 2 50)))
485 (exp -> (gexp
486 (system*
487 (string-append (ungexp %bootstrap-guile)
488 "/bin/guile")
489 "-c" (object->string
490 '(display (expt (ungexp n) 2))))))
491 (drv (gexp->script "guile-thing" exp
492 #:guile %bootstrap-guile))
493 (out -> (derivation->output-path drv))
494 (done (built-derivations (list drv))))
495 (let* ((pipe (open-input-pipe out))
496 (str (get-string-all pipe)))
497 (return (and (zero? (close-pipe pipe))
498 (= (expt n 2) (string->number str)))))))
499
462a3fa3
LC
500(test-assert "text-file*"
501 (let ((references (store-lift references)))
502 (run-with-store %store
503 (mlet* %store-monad
504 ((drv (package->derivation %bootstrap-guile))
505 (guile -> (derivation->output-path drv))
506 (file (text-file "bar" "This is bar."))
507 (text (text-file* "foo"
508 %bootstrap-guile "/bin/guile "
509 `(,%bootstrap-guile "out") "/bin/guile "
510 drv "/bin/guile "
511 file))
512 (done (built-derivations (list text)))
513 (out -> (derivation->output-path text))
514 (refs (references out)))
515 ;; Make sure we get the right references and the right content.
516 (return (and (lset= string=? refs (list guile file))
517 (equal? (call-with-input-file out get-string-all)
518 (string-append guile "/bin/guile "
519 guile "/bin/guile "
520 guile "/bin/guile "
521 file)))))
522 #:guile-for-build (package-derivation %store %bootstrap-guile))))
523
2cf0ea0d
LC
524(test-assert "printer"
525 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
526 \"/bin/uname\"\\) [[:xdigit:]]+>$"
527 (with-output-to-string
528 (lambda ()
529 (write
530 (gexp (string-append (ungexp coreutils)
531 "/bin/uname")))))))
532
533(test-assert "printer vs. ungexp-splicing"
534 (string-match "^#<gexp .* [[:xdigit:]]+>$"
535 (with-output-to-string
536 (lambda ()
537 ;; #~(begin #$@#~())
538 (write
539 (gexp (begin (ungexp-splicing (gexp ())))))))))
540
21b679f6
LC
541(test-equal "sugar"
542 '(gexp (foo (ungexp bar) (ungexp baz "out")
543 (ungexp (chbouib 42))
667b2508
LC
544 (ungexp-splicing (list x y z))
545 (ungexp-native foo) (ungexp-native foo "out")
546 (ungexp-native (chbouib 42))
547 (ungexp-native-splicing (list x y z))))
548 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
549 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
550
551(test-end "gexp")
552
553\f
554(exit (= (test-runner-fail-count (test-runner-current)) 0))
555
556;; Local Variables:
557;; eval: (put 'test-assertm 'scheme-indent-function 1)
558;; End: