Merge remote-tracking branch 'origin/master' into qt-updates
[jackhill/guix/guix.git] / tests / gexp.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
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 grafts)
24 #:use-module (guix derivations)
25 #:use-module (guix packages)
26 #:use-module (guix build-system trivial)
27 #:use-module (guix tests)
28 #:use-module ((guix build utils) #:select (with-directory-excursion))
29 #:use-module ((guix utils) #:select (call-with-temporary-directory))
30 #:use-module (gnu packages)
31 #:use-module (gnu packages base)
32 #:use-module (gnu packages bootstrap)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-64)
36 #:use-module (rnrs io ports)
37 #:use-module (ice-9 match)
38 #:use-module (ice-9 regex)
39 #:use-module (ice-9 popen)
40 #:use-module (ice-9 ftw))
41
42 ;; Test the (guix gexp) module.
43
44 (define %store
45 (open-connection-for-tests))
46
47 ;; Globally disable grafts because they can trigger early builds.
48 (%graft? #f)
49
50 ;; For white-box testing.
51 (define (gexp-inputs x)
52 ((@@ (guix gexp) gexp-inputs) x))
53 (define (gexp-native-inputs x)
54 ((@@ (guix gexp) gexp-native-inputs) x))
55 (define (gexp-outputs x)
56 ((@@ (guix gexp) gexp-outputs) x))
57 (define (gexp->sexp . x)
58 (apply (@@ (guix gexp) gexp->sexp) x))
59
60 (define* (gexp->sexp* exp #:optional target)
61 (run-with-store %store (gexp->sexp exp
62 #:target target)
63 #:guile-for-build (%guile-for-build)))
64
65 (define-syntax-rule (test-assertm name exp)
66 (test-assert name
67 (run-with-store %store exp
68 #:guile-for-build (%guile-for-build))))
69
70 (define %extension-package
71 ;; Example of a package to use when testing 'with-extensions'.
72 (dummy-package "extension"
73 (build-system trivial-build-system)
74 (arguments
75 `(#:guile ,%bootstrap-guile
76 #:modules ((guix build utils))
77 #:builder
78 (begin
79 (use-modules (guix build utils))
80 (let* ((out (string-append (assoc-ref %outputs "out")
81 "/share/guile/site/"
82 (effective-version))))
83 (mkdir-p out)
84 (call-with-output-file (string-append out "/hg2g.scm")
85 (lambda (port)
86 (write '(define-module (hg2g)
87 #:export (the-answer))
88 port)
89 (write '(define the-answer 42) port)))))))))
90
91 \f
92 (test-begin "gexp")
93
94 (test-equal "no refs"
95 '(display "hello!")
96 (let ((exp (gexp (display "hello!"))))
97 (and (gexp? exp)
98 (null? (gexp-inputs exp))
99 (gexp->sexp* exp))))
100
101 (test-equal "unquote"
102 '(display `(foo ,(+ 2 3)))
103 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
104 (and (gexp? exp)
105 (null? (gexp-inputs exp))
106 (gexp->sexp* exp))))
107
108 (test-assert "one input package"
109 (let ((exp (gexp (display (ungexp coreutils)))))
110 (and (gexp? exp)
111 (match (gexp-inputs exp)
112 (((p "out"))
113 (eq? p coreutils)))
114 (equal? `(display ,(derivation->output-path
115 (package-derivation %store coreutils)))
116 (gexp->sexp* exp)))))
117
118 (test-assert "one input package, dotted list"
119 (let ((exp (gexp (coreutils . (ungexp coreutils)))))
120 (and (gexp? exp)
121 (match (gexp-inputs exp)
122 (((p "out"))
123 (eq? p coreutils)))
124 (equal? `(coreutils . ,(derivation->output-path
125 (package-derivation %store coreutils)))
126 (gexp->sexp* exp)))))
127
128 (test-assert "one input origin"
129 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
130 (and (gexp? exp)
131 (match (gexp-inputs exp)
132 (((o "out"))
133 (eq? o (package-source coreutils))))
134 (equal? `(display ,(derivation->output-path
135 (package-source-derivation
136 %store (package-source coreutils))))
137 (gexp->sexp* exp)))))
138
139 (test-assert "one local file"
140 (let* ((file (search-path %load-path "guix.scm"))
141 (local (local-file file))
142 (exp (gexp (display (ungexp local))))
143 (intd (add-to-store %store (basename file) #f
144 "sha256" file)))
145 (and (gexp? exp)
146 (match (gexp-inputs exp)
147 (((x "out"))
148 (eq? x local)))
149 (equal? `(display ,intd) (gexp->sexp* exp)))))
150
151 (test-assert "one local file, symlink"
152 (let ((file (search-path %load-path "guix.scm"))
153 (link (tmpnam)))
154 (dynamic-wind
155 (const #t)
156 (lambda ()
157 (symlink (canonicalize-path file) link)
158 (let* ((local (local-file link "my-file" #:recursive? #f))
159 (exp (gexp (display (ungexp local))))
160 (intd (add-to-store %store "my-file" #f
161 "sha256" file)))
162 (and (gexp? exp)
163 (match (gexp-inputs exp)
164 (((x "out"))
165 (eq? x local)))
166 (equal? `(display ,intd) (gexp->sexp* exp)))))
167 (lambda ()
168 (false-if-exception (delete-file link))))))
169
170 (test-equal "local-file, relative file name"
171 (canonicalize-path (search-path %load-path "guix/base32.scm"))
172 (let ((directory (dirname (search-path %load-path
173 "guix/build-system/gnu.scm"))))
174 (with-directory-excursion directory
175 (let ((file (local-file "../guix/base32.scm")))
176 (local-file-absolute-file-name file)))))
177
178 (test-assertm "local-file, #:select?"
179 (mlet* %store-monad ((select? -> (lambda (file stat)
180 (member (basename file)
181 '("guix.scm" "tests"
182 "gexp.scm"))))
183 (file -> (local-file ".." "directory"
184 #:recursive? #t
185 #:select? select?))
186 (dir (lower-object file)))
187 (return (and (store-path? dir)
188 (equal? (scandir dir)
189 '("." ".." "guix.scm" "tests"))
190 (equal? (scandir (string-append dir "/tests"))
191 '("." ".." "gexp.scm"))))))
192
193 (test-assert "one plain file"
194 (let* ((file (plain-file "hi" "Hello, world!"))
195 (exp (gexp (display (ungexp file))))
196 (expected (add-text-to-store %store "hi" "Hello, world!")))
197 (and (gexp? exp)
198 (match (gexp-inputs exp)
199 (((x "out"))
200 (eq? x file)))
201 (equal? `(display ,expected) (gexp->sexp* exp)))))
202
203 (test-assert "same input twice"
204 (let ((exp (gexp (begin
205 (display (ungexp coreutils))
206 (display (ungexp coreutils))))))
207 (and (gexp? exp)
208 (match (gexp-inputs exp)
209 (((p "out"))
210 (eq? p coreutils)))
211 (let ((e `(display ,(derivation->output-path
212 (package-derivation %store coreutils)))))
213 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
214
215 (test-assert "two input packages, one derivation, one file"
216 (let* ((drv (build-expression->derivation
217 %store "foo" 'bar
218 #:guile-for-build (package-derivation %store %bootstrap-guile)))
219 (txt (add-text-to-store %store "foo" "Hello, world!"))
220 (exp (gexp (begin
221 (display (ungexp coreutils))
222 (display (ungexp %bootstrap-guile))
223 (display (ungexp drv))
224 (display (ungexp txt))))))
225 (define (match-input thing)
226 (match-lambda
227 ((drv-or-pkg _ ...)
228 (eq? thing drv-or-pkg))))
229
230 (and (gexp? exp)
231 (= 4 (length (gexp-inputs exp)))
232 (every (lambda (input)
233 (find (match-input input) (gexp-inputs exp)))
234 (list drv coreutils %bootstrap-guile txt))
235 (let ((e0 `(display ,(derivation->output-path
236 (package-derivation %store coreutils))))
237 (e1 `(display ,(derivation->output-path
238 (package-derivation %store %bootstrap-guile))))
239 (e2 `(display ,(derivation->output-path drv)))
240 (e3 `(display ,txt)))
241 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
242
243 (test-assert "file-append"
244 (let* ((drv (package-derivation %store %bootstrap-guile))
245 (fa (file-append %bootstrap-guile "/bin/guile"))
246 (exp #~(here we go #$fa)))
247 (and (match (gexp->sexp* exp)
248 (('here 'we 'go (? string? result))
249 (string=? result
250 (string-append (derivation->output-path drv)
251 "/bin/guile"))))
252 (match (gexp-inputs exp)
253 (((thing "out"))
254 (eq? thing fa))))))
255
256 (test-assert "file-append, output"
257 (let* ((drv (package-derivation %store glibc))
258 (fa (file-append glibc "/lib" "/debug"))
259 (exp #~(foo #$fa:debug)))
260 (and (match (gexp->sexp* exp)
261 (('foo (? string? result))
262 (string=? result
263 (string-append (derivation->output-path drv "debug")
264 "/lib/debug"))))
265 (match (gexp-inputs exp)
266 (((thing "debug"))
267 (eq? thing fa))))))
268
269 (test-assert "file-append, nested"
270 (let* ((drv (package-derivation %store glibc))
271 (dir (file-append glibc "/bin"))
272 (slash (file-append dir "/"))
273 (file (file-append slash "getent"))
274 (exp #~(foo #$file)))
275 (and (match (gexp->sexp* exp)
276 (('foo (? string? result))
277 (string=? result
278 (string-append (derivation->output-path drv)
279 "/bin/getent"))))
280 (match (gexp-inputs exp)
281 (((thing "out"))
282 (eq? thing file))))))
283
284 (test-assert "ungexp + ungexp-native"
285 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
286 (ungexp coreutils)
287 (ungexp-native glibc)
288 (ungexp binutils))))
289 (target "mips64el-linux")
290 (guile (derivation->output-path
291 (package-derivation %store %bootstrap-guile)))
292 (cu (derivation->output-path
293 (package-cross-derivation %store coreutils target)))
294 (libc (derivation->output-path
295 (package-derivation %store glibc)))
296 (bu (derivation->output-path
297 (package-cross-derivation %store binutils target))))
298 (and (lset= equal?
299 `((,%bootstrap-guile "out") (,glibc "out"))
300 (gexp-native-inputs exp))
301 (lset= equal?
302 `((,coreutils "out") (,binutils "out"))
303 (gexp-inputs exp))
304 (equal? `(list ,guile ,cu ,libc ,bu)
305 (gexp->sexp* exp target)))))
306
307 (test-equal "ungexp + ungexp-native, nested"
308 (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out")))
309 (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
310 (ungexp %bootstrap-guile)))))
311 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
312
313 (test-equal "ungexp + ungexp-native, nested, special mixture"
314 `(() <> ((,coreutils "out")))
315
316 ;; (gexp-native-inputs exp) used to return '(), wrongfully.
317 (let* ((foo (gexp (foo (ungexp-native coreutils))))
318 (exp (gexp (bar (ungexp foo)))))
319 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
320
321 (test-assert "input list"
322 (let ((exp (gexp (display
323 '(ungexp (list %bootstrap-guile coreutils)))))
324 (guile (derivation->output-path
325 (package-derivation %store %bootstrap-guile)))
326 (cu (derivation->output-path
327 (package-derivation %store coreutils))))
328 (and (lset= equal?
329 `((,%bootstrap-guile "out") (,coreutils "out"))
330 (gexp-inputs exp))
331 (equal? `(display '(,guile ,cu))
332 (gexp->sexp* exp)))))
333
334 (test-assert "input list + ungexp-native"
335 (let* ((target "mips64el-linux")
336 (exp (gexp (display
337 (cons '(ungexp-native (list %bootstrap-guile coreutils))
338 '(ungexp (list glibc binutils))))))
339 (guile (derivation->output-path
340 (package-derivation %store %bootstrap-guile)))
341 (cu (derivation->output-path
342 (package-derivation %store coreutils)))
343 (xlibc (derivation->output-path
344 (package-cross-derivation %store glibc target)))
345 (xbu (derivation->output-path
346 (package-cross-derivation %store binutils target))))
347 (and (lset= equal?
348 `((,%bootstrap-guile "out") (,coreutils "out"))
349 (gexp-native-inputs exp))
350 (lset= equal?
351 `((,glibc "out") (,binutils "out"))
352 (gexp-inputs exp))
353 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
354 (gexp->sexp* exp target)))))
355
356 (test-assert "input list splicing"
357 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
358 (outputs (list (derivation->output-path
359 (package-derivation %store glibc)
360 "debug")
361 (derivation->output-path
362 (package-derivation %store %bootstrap-guile))))
363 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
364 (and (lset= equal?
365 `((,glibc "debug") (,%bootstrap-guile "out"))
366 (gexp-inputs exp))
367 (equal? (gexp->sexp* exp)
368 `(list ,@(cons 5 outputs))))))
369
370 (test-assert "input list splicing + ungexp-native-splicing"
371 (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
372 %bootstrap-guile))
373 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
374 (and (lset= equal?
375 `((,glibc "debug") (,%bootstrap-guile "out"))
376 (gexp-native-inputs exp))
377 (null? (gexp-inputs exp))
378 (equal? (gexp->sexp* exp) ;native
379 (gexp->sexp* exp "mips64el-linux")))))
380
381 (test-assert "gexp list splicing + ungexp-splicing"
382 (let* ((inner (gexp (ungexp-native glibc)))
383 (exp (gexp (list (ungexp-splicing (list inner))))))
384 (and (equal? `((,glibc "out")) (gexp-native-inputs exp))
385 (null? (gexp-inputs exp))
386 (equal? (gexp->sexp* exp) ;native
387 (gexp->sexp* exp "mips64el-linux")))))
388
389 (test-equal "output list"
390 2
391 (let ((exp (gexp (begin (mkdir (ungexp output))
392 (mkdir (ungexp output "bar"))))))
393 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
394
395 (test-assert "output list, combined gexps"
396 (let* ((exp0 (gexp (mkdir (ungexp output))))
397 (exp1 (gexp (mkdir (ungexp output "foo"))))
398 (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
399 (and (lset= equal?
400 (append (gexp-outputs exp0) (gexp-outputs exp1))
401 (gexp-outputs exp2))
402 (= 2 (length (gexp-outputs exp2))))))
403
404 (test-equal "output list, combined gexps, duplicate output"
405 1
406 (let* ((exp0 (gexp (mkdir (ungexp output))))
407 (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
408 (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
409 (length (gexp-outputs exp2))))
410
411 (test-assert "output list + ungexp-splicing list, combined gexps"
412 (let* ((exp0 (gexp (mkdir (ungexp output))))
413 (exp1 (gexp (mkdir (ungexp output "foo"))))
414 (exp2 (gexp (begin (display "hi!")
415 (ungexp-splicing (list exp0 exp1))))))
416 (and (lset= equal?
417 (append (gexp-outputs exp0) (gexp-outputs exp1))
418 (gexp-outputs exp2))
419 (= 2 (length (gexp-outputs exp2))))))
420
421 (test-assertm "gexp->file"
422 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
423 (guile (package-file %bootstrap-guile))
424 (sexp (gexp->sexp exp))
425 (drv (gexp->file "foo" exp))
426 (out -> (derivation->output-path drv))
427 (done (built-derivations (list drv)))
428 (refs (references* out)))
429 (return (and (equal? sexp (call-with-input-file out read))
430 (equal? (list guile) refs)))))
431
432 (test-assertm "gexp->file + file-append"
433 (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
434 "/bin/guile"))
435 (guile (package-file %bootstrap-guile))
436 (drv (gexp->file "foo" exp))
437 (out -> (derivation->output-path drv))
438 (done (built-derivations (list drv)))
439 (refs (references* out)))
440 (return (and (equal? (string-append guile "/bin/guile")
441 (call-with-input-file out read))
442 (equal? (list guile) refs)))))
443
444 (test-assertm "gexp->file + #:splice?"
445 (mlet* %store-monad ((exp -> (list
446 #~(define foo 'bar)
447 #~(define guile #$%bootstrap-guile)))
448 (guile (package-file %bootstrap-guile))
449 (drv (gexp->file "splice" exp #:splice? #t))
450 (out -> (derivation->output-path drv))
451 (done (built-derivations (list drv)))
452 (refs (references* out)))
453 (pk 'splice out)
454 (return (and (equal? `((define foo 'bar)
455 (define guile ,guile)
456 ,(call-with-input-string "" read))
457 (call-with-input-file out
458 (lambda (port)
459 (list (read port) (read port) (read port)))))
460 (equal? (list guile) refs)))))
461
462 (test-assertm "gexp->derivation"
463 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
464 (exp -> (gexp
465 (begin
466 (mkdir (ungexp output))
467 (chdir (ungexp output))
468 (symlink
469 (string-append (ungexp %bootstrap-guile)
470 "/bin/guile")
471 "foo")
472 (symlink (ungexp file)
473 (ungexp output "2nd")))))
474 (drv (gexp->derivation "foo" exp))
475 (out -> (derivation->output-path drv))
476 (out2 -> (derivation->output-path drv "2nd"))
477 (done (built-derivations (list drv)))
478 (refs (references* out))
479 (refs2 (references* out2))
480 (guile (package-file %bootstrap-guile "bin/guile")))
481 (return (and (string=? (readlink (string-append out "/foo")) guile)
482 (string=? (readlink out2) file)
483 (equal? refs (list (dirname (dirname guile))))
484 (equal? refs2 (list file))))))
485
486 (test-assertm "gexp->derivation vs. grafts"
487 (mlet* %store-monad ((graft? (set-grafting #f))
488 (p0 -> (dummy-package "dummy"
489 (arguments
490 '(#:implicit-inputs? #f))))
491 (r -> (package (inherit p0) (name "DuMMY")))
492 (p1 -> (package (inherit p0) (replacement r)))
493 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
494 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
495 (void (set-guile-for-build %bootstrap-guile))
496 (drv0 (gexp->derivation "t" exp0 #:graft? #t))
497 (drv1 (gexp->derivation "t" exp1 #:graft? #t))
498 (drv1* (gexp->derivation "t" exp1 #:graft? #f))
499 (_ (set-grafting graft?)))
500 (return (and (not (string=? (derivation->output-path drv0)
501 (derivation->output-path drv1)))
502 (string=? (derivation->output-path drv0)
503 (derivation->output-path drv1*))))))
504
505 (test-assertm "gexp->derivation, composed gexps"
506 (mlet* %store-monad ((exp0 -> (gexp (begin
507 (mkdir (ungexp output))
508 (chdir (ungexp output)))))
509 (exp1 -> (gexp (symlink
510 (string-append (ungexp %bootstrap-guile)
511 "/bin/guile")
512 "foo")))
513 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
514 (drv (gexp->derivation "foo" exp))
515 (out -> (derivation->output-path drv))
516 (done (built-derivations (list drv)))
517 (guile (package-file %bootstrap-guile "bin/guile")))
518 (return (string=? (readlink (string-append out "/foo"))
519 guile))))
520
521 (test-assertm "gexp->derivation, default system"
522 ;; The default system should be the one at '>>=' time, not the one at
523 ;; invocation time. See <http://bugs.gnu.org/18002>.
524 (let ((system (%current-system))
525 (mdrv (parameterize ((%current-system "foobar64-linux"))
526 (gexp->derivation "foo"
527 (gexp
528 (mkdir (ungexp output)))))))
529 (mlet %store-monad ((drv mdrv))
530 (return (string=? system (derivation-system drv))))))
531
532 (test-assertm "gexp->derivation, local-file"
533 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
534 (intd (interned-file file #:recursive? #f))
535 (local -> (local-file file))
536 (exp -> (gexp (begin
537 (stat (ungexp local))
538 (symlink (ungexp local)
539 (ungexp output)))))
540 (drv (gexp->derivation "local-file" exp)))
541 (mbegin %store-monad
542 (built-derivations (list drv))
543 (return (string=? (readlink (derivation->output-path drv))
544 intd)))))
545
546 (test-assertm "gexp->derivation, cross-compilation"
547 (mlet* %store-monad ((target -> "mips64el-linux")
548 (exp -> (gexp (list (ungexp coreutils)
549 (ungexp output))))
550 (xdrv (gexp->derivation "foo" exp
551 #:target target))
552 (refs (references*
553 (derivation-file-name xdrv)))
554 (xcu (package->cross-derivation coreutils
555 target))
556 (cu (package->derivation coreutils)))
557 (return (and (member (derivation-file-name xcu) refs)
558 (not (member (derivation-file-name cu) refs))))))
559
560 (test-assertm "gexp->derivation, ungexp-native"
561 (mlet* %store-monad ((target -> "mips64el-linux")
562 (exp -> (gexp (list (ungexp-native coreutils)
563 (ungexp output))))
564 (xdrv (gexp->derivation "foo" exp
565 #:target target))
566 (drv (gexp->derivation "foo" exp)))
567 (return (string=? (derivation-file-name drv)
568 (derivation-file-name xdrv)))))
569
570 (test-assertm "gexp->derivation, ungexp + ungexp-native"
571 (mlet* %store-monad ((target -> "mips64el-linux")
572 (exp -> (gexp (list (ungexp-native coreutils)
573 (ungexp glibc)
574 (ungexp output))))
575 (xdrv (gexp->derivation "foo" exp
576 #:target target))
577 (refs (references*
578 (derivation-file-name xdrv)))
579 (xglibc (package->cross-derivation glibc target))
580 (cu (package->derivation coreutils)))
581 (return (and (member (derivation-file-name cu) refs)
582 (member (derivation-file-name xglibc) refs)))))
583
584 (test-assertm "gexp->derivation, ungexp-native + composed gexps"
585 (mlet* %store-monad ((target -> "mips64el-linux")
586 (exp0 -> (gexp (list 1 2
587 (ungexp coreutils))))
588 (exp -> (gexp (list 0 (ungexp-native exp0))))
589 (xdrv (gexp->derivation "foo" exp
590 #:target target))
591 (drv (gexp->derivation "foo" exp)))
592 (return (string=? (derivation-file-name drv)
593 (derivation-file-name xdrv)))))
594
595 (test-assertm "gexp->derivation, store copy"
596 (let ((build-one #~(call-with-output-file #$output
597 (lambda (port)
598 (display "This is the one." port))))
599 (build-two (lambda (one)
600 #~(begin
601 (mkdir #$output)
602 (symlink #$one (string-append #$output "/one"))
603 (call-with-output-file (string-append #$output "/two")
604 (lambda (port)
605 (display "This is the second one." port))))))
606 (build-drv #~(begin
607 (use-modules (guix build store-copy))
608
609 (mkdir #$output)
610 (populate-store '("graph") #$output))))
611 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
612 (two (gexp->derivation "two" (build-two one)))
613 (drv (gexp->derivation "store-copy" build-drv
614 #:references-graphs
615 `(("graph" ,two))
616 #:modules
617 '((guix build store-copy)
618 (guix sets)
619 (guix build utils))))
620 (ok? (built-derivations (list drv)))
621 (out -> (derivation->output-path drv)))
622 (let ((one (derivation->output-path one))
623 (two (derivation->output-path two)))
624 (return (and ok?
625 (file-exists? (string-append out "/" one))
626 (file-exists? (string-append out "/" two))
627 (file-exists? (string-append out "/" two "/two"))
628 (string=? (readlink (string-append out "/" two "/one"))
629 one)))))))
630
631 (test-assertm "imported-files"
632 (mlet* %store-monad
633 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
634 ("a/b/c" . ,(search-path %load-path
635 "guix/derivations.scm"))
636 ("p/q" . ,(search-path %load-path "guix.scm"))
637 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
638 (drv (imported-files files)))
639 (mbegin %store-monad
640 (built-derivations (list drv))
641 (let ((dir (derivation->output-path drv)))
642 (return
643 (every (match-lambda
644 ((path . source)
645 (equal? (call-with-input-file (string-append dir "/" path)
646 get-bytevector-all)
647 (call-with-input-file source
648 get-bytevector-all))))
649 files))))))
650
651 (test-assertm "imported-files with file-like objects"
652 (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
653 (q-scm -> (search-path %load-path "ice-9/q.scm"))
654 (files -> `(("a/b/c" . ,q-scm)
655 ("p/q" . ,plain)))
656 (drv (imported-files files)))
657 (mbegin %store-monad
658 (built-derivations (list drv))
659 (mlet %store-monad ((dir -> (derivation->output-path drv))
660 (plain* (text-file "foo" "bar!"))
661 (q-scm* (interned-file q-scm "c")))
662 (return
663 (and (string=? (readlink (string-append dir "/a/b/c"))
664 q-scm*)
665 (string=? (readlink (string-append dir "/p/q"))
666 plain*)))))))
667
668 (test-equal "gexp-modules & ungexp"
669 '((bar) (foo))
670 ((@@ (guix gexp) gexp-modules)
671 #~(foo #$(with-imported-modules '((foo)) #~+)
672 #+(with-imported-modules '((bar)) #~-))))
673
674 (test-equal "gexp-modules & ungexp-splicing"
675 '((foo) (bar))
676 ((@@ (guix gexp) gexp-modules)
677 #~(foo #$@(list (with-imported-modules '((foo)) #~+)
678 (with-imported-modules '((bar)) #~-)))))
679
680 (test-equal "gexp-modules and literal Scheme object"
681 '()
682 (gexp-modules #t))
683
684 (test-assertm "gexp->derivation #:modules"
685 (mlet* %store-monad
686 ((build -> #~(begin
687 (use-modules (guix build utils))
688 (mkdir-p (string-append #$output "/guile/guix/nix"))
689 #t))
690 (drv (gexp->derivation "test-with-modules" build
691 #:modules '((guix build utils)))))
692 (mbegin %store-monad
693 (built-derivations (list drv))
694 (let* ((p (derivation->output-path drv))
695 (s (stat (string-append p "/guile/guix/nix"))))
696 (return (eq? (stat:type s) 'directory))))))
697
698 (test-assertm "gexp->derivation & with-imported-modules"
699 ;; Same test as above, but using 'with-imported-modules'.
700 (mlet* %store-monad
701 ((build -> (with-imported-modules '((guix build utils))
702 #~(begin
703 (use-modules (guix build utils))
704 (mkdir-p (string-append #$output "/guile/guix/nix"))
705 #t)))
706 (drv (gexp->derivation "test-with-modules" build)))
707 (mbegin %store-monad
708 (built-derivations (list drv))
709 (let* ((p (derivation->output-path drv))
710 (s (stat (string-append p "/guile/guix/nix"))))
711 (return (eq? (stat:type s) 'directory))))))
712
713 (test-assertm "gexp->derivation & nested with-imported-modules"
714 (mlet* %store-monad
715 ((build1 -> (with-imported-modules '((guix build utils))
716 #~(begin
717 (use-modules (guix build utils))
718 (mkdir-p (string-append #$output "/guile/guix/nix"))
719 #t)))
720 (build2 -> (with-imported-modules '((guix build bournish))
721 #~(begin
722 (use-modules (guix build bournish)
723 (system base compile))
724 #+build1
725 (call-with-output-file (string-append #$output "/b")
726 (lambda (port)
727 (write
728 (read-and-compile (open-input-string "cd /foo")
729 #:from %bournish-language
730 #:to 'scheme)
731 port))))))
732 (drv (gexp->derivation "test-with-modules" build2)))
733 (mbegin %store-monad
734 (built-derivations (list drv))
735 (let* ((p (derivation->output-path drv))
736 (s (stat (string-append p "/guile/guix/nix")))
737 (b (string-append p "/b")))
738 (return (and (eq? (stat:type s) 'directory)
739 (equal? '(chdir "/foo")
740 (call-with-input-file b read))))))))
741
742 (test-assertm "gexp->derivation & with-imported-module & computed module"
743 (mlet* %store-monad
744 ((module -> (scheme-file "x" #~(;; splice!
745 (define-module (foo bar)
746 #:export (the-answer))
747
748 (define the-answer 42))
749 #:splice? #t))
750 (build -> (with-imported-modules `(((foo bar) => ,module)
751 (guix build utils))
752 #~(begin
753 (use-modules (guix build utils)
754 (foo bar))
755 mkdir-p
756 (call-with-output-file #$output
757 (lambda (port)
758 (write the-answer port))))))
759 (drv (gexp->derivation "thing" build))
760 (out -> (derivation->output-path drv)))
761 (mbegin %store-monad
762 (built-derivations (list drv))
763 (return (= 42 (call-with-input-file out read))))))
764
765 (test-equal "gexp-extensions & ungexp"
766 (list sed grep)
767 ((@@ (guix gexp) gexp-extensions)
768 #~(foo #$(with-extensions (list grep) #~+)
769 #+(with-extensions (list sed) #~-))))
770
771 (test-equal "gexp-extensions & ungexp-splicing"
772 (list grep sed)
773 ((@@ (guix gexp) gexp-extensions)
774 #~(foo #$@(list (with-extensions (list grep) #~+)
775 (with-imported-modules '((foo))
776 (with-extensions (list sed) #~-))))))
777
778 (test-equal "gexp-extensions and literal Scheme object"
779 '()
780 ((@@ (guix gexp) gexp-extensions) #t))
781
782 (test-assertm "gexp->derivation & with-extensions"
783 ;; Create a fake Guile extension and make sure it is accessible both to the
784 ;; imported modules and to the derivation build script.
785 (mlet* %store-monad
786 ((extension -> %extension-package)
787 (module -> (scheme-file "x" #~( ;; splice!
788 (define-module (foo)
789 #:use-module (hg2g)
790 #:export (multiply))
791
792 (define (multiply x)
793 (* the-answer x)))
794 #:splice? #t))
795 (build -> (with-extensions (list extension)
796 (with-imported-modules `((guix build utils)
797 ((foo) => ,module))
798 #~(begin
799 (use-modules (guix build utils)
800 (hg2g) (foo))
801 (call-with-output-file #$output
802 (lambda (port)
803 (write (list the-answer (multiply 2))
804 port)))))))
805 (drv (gexp->derivation "thingie" build
806 ;; %BOOTSTRAP-GUILE is 2.0.
807 #:effective-version "2.0"))
808 (out -> (derivation->output-path drv)))
809 (mbegin %store-monad
810 (built-derivations (list drv))
811 (return (equal? '(42 84) (call-with-input-file out read))))))
812
813 (test-assertm "gexp->derivation #:references-graphs"
814 (mlet* %store-monad
815 ((one (text-file "one" (random-text)))
816 (two (gexp->derivation "two"
817 #~(symlink #$one #$output:chbouib)))
818 (build -> (with-imported-modules '((guix build store-copy)
819 (guix sets)
820 (guix build utils))
821 #~(begin
822 (use-modules (guix build store-copy))
823 (with-output-to-file #$output
824 (lambda ()
825 (write (map store-info-item
826 (call-with-input-file "guile"
827 read-reference-graph)))))
828 (with-output-to-file #$output:one
829 (lambda ()
830 (write (map store-info-item
831 (call-with-input-file "one"
832 read-reference-graph)))))
833 (with-output-to-file #$output:two
834 (lambda ()
835 (write (map store-info-item
836 (call-with-input-file "two"
837 read-reference-graph))))))))
838 (drv (gexp->derivation "ref-graphs" build
839 #:references-graphs `(("one" ,one)
840 ("two" ,two "chbouib")
841 ("guile" ,%bootstrap-guile))))
842 (ok? (built-derivations (list drv)))
843 (guile-drv (package->derivation %bootstrap-guile))
844 (bash (interned-file (search-bootstrap-binary "bash"
845 (%current-system))
846 "bash" #:recursive? #t))
847 (g-one -> (derivation->output-path drv "one"))
848 (g-two -> (derivation->output-path drv "two"))
849 (g-guile -> (derivation->output-path drv)))
850 (return (and ok?
851 (equal? (call-with-input-file g-one read) (list one))
852 (lset= string=?
853 (call-with-input-file g-two read)
854 (list one (derivation->output-path two "chbouib")))
855
856 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
857 (lset= string=?
858 (call-with-input-file g-guile read)
859 (list (derivation->output-path guile-drv) bash))))))
860
861 (test-assertm "gexp->derivation #:allowed-references"
862 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
863 #~(begin
864 (mkdir #$output)
865 (chdir #$output)
866 (symlink #$output "self")
867 (symlink #$%bootstrap-guile
868 "guile"))
869 #:allowed-references
870 (list "out" %bootstrap-guile))))
871 (built-derivations (list drv))))
872
873 (test-assertm "gexp->derivation #:allowed-references, specific output"
874 (mlet* %store-monad ((in (gexp->derivation "thing"
875 #~(begin
876 (mkdir #$output:ok)
877 (mkdir #$output:not-ok))))
878 (drv (gexp->derivation "allowed-refs"
879 #~(begin
880 (pk #$in:not-ok)
881 (mkdir #$output)
882 (chdir #$output)
883 (symlink #$output "self")
884 (symlink #$in:ok "ok"))
885 #:allowed-references
886 (list "out"
887 (gexp-input in "ok")))))
888 (built-derivations (list drv))))
889
890 (test-assert "gexp->derivation #:allowed-references, disallowed"
891 (let ((drv (run-with-store %store
892 (gexp->derivation "allowed-refs"
893 #~(begin
894 (mkdir #$output)
895 (chdir #$output)
896 (symlink #$%bootstrap-guile "guile"))
897 #:allowed-references '()))))
898 (guard (c ((nix-protocol-error? c) #t))
899 (build-derivations %store (list drv))
900 #f)))
901
902 (test-assertm "gexp->derivation #:disallowed-references, allowed"
903 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
904 #~(begin
905 (mkdir #$output)
906 (chdir #$output)
907 (symlink #$output "self")
908 (symlink #$%bootstrap-guile
909 "guile"))
910 #:disallowed-references '())))
911 (built-derivations (list drv))))
912
913
914 (test-assert "gexp->derivation #:disallowed-references"
915 (let ((drv (run-with-store %store
916 (gexp->derivation "disallowed-refs"
917 #~(begin
918 (mkdir #$output)
919 (chdir #$output)
920 (symlink #$%bootstrap-guile "guile"))
921 #:disallowed-references (list %bootstrap-guile)))))
922 (guard (c ((nix-protocol-error? c) #t))
923 (build-derivations %store (list drv))
924 #f)))
925
926 (define shebang
927 (string-append "#!" (derivation->output-path (%guile-for-build))
928 "/bin/guile --no-auto-compile"))
929
930 ;; If we're going to hit the silly shebang limit (128 chars on Linux-based
931 ;; systems), then skip the following test.
932 (test-skip (if (> (string-length shebang) 127) 2 0))
933
934 (test-assertm "gexp->script"
935 (mlet* %store-monad ((n -> (random (expt 2 50)))
936 (exp -> (gexp
937 (system*
938 (string-append (ungexp %bootstrap-guile)
939 "/bin/guile")
940 "-c" (object->string
941 '(display (expt (ungexp n) 2))))))
942 (drv (gexp->script "guile-thing" exp
943 #:guile %bootstrap-guile))
944 (out -> (derivation->output-path drv))
945 (done (built-derivations (list drv))))
946 (let* ((pipe (open-input-pipe out))
947 (str (get-string-all pipe)))
948 (return (and (zero? (close-pipe pipe))
949 (= (expt n 2) (string->number str)))))))
950
951 (test-assert "gexp->script #:module-path"
952 (call-with-temporary-directory
953 (lambda (directory)
954 (define str
955 "Fake (guix base32) module!")
956
957 (mkdir (string-append directory "/guix"))
958 (call-with-output-file (string-append directory "/guix/base32.scm")
959 (lambda (port)
960 (write `(begin (define-module (guix base32))
961 (define-public %fake! ,str))
962 port)))
963
964 (run-with-store %store
965 (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
966 (gexp (begin
967 (use-modules (guix base32))
968 (write (list %load-path
969 %fake!))))))
970 (drv (gexp->script "guile-thing" exp
971 #:guile %bootstrap-guile
972 #:module-path (list directory)))
973 (out -> (derivation->output-path drv))
974 (done (built-derivations (list drv))))
975 (let* ((pipe (open-input-pipe out))
976 (data (read pipe)))
977 (return (and (zero? (close-pipe pipe))
978 (match data
979 ((load-path str*)
980 (and (string=? str* str)
981 (not (member directory load-path)))))))))))))
982
983 (test-assertm "program-file"
984 (let* ((n (random (expt 2 50)))
985 (exp (with-imported-modules '((guix build utils))
986 (gexp (begin
987 (use-modules (guix build utils))
988 (display (ungexp n))))))
989 (file (program-file "program" exp
990 #:guile %bootstrap-guile)))
991 (mlet* %store-monad ((drv (lower-object file))
992 (out -> (derivation->output-path drv)))
993 (mbegin %store-monad
994 (built-derivations (list drv))
995 (let* ((pipe (open-input-pipe out))
996 (str (get-string-all pipe)))
997 (return (and (zero? (close-pipe pipe))
998 (= n (string->number str)))))))))
999
1000 (test-assert "program-file #:module-path"
1001 (call-with-temporary-directory
1002 (lambda (directory)
1003 (define text (random-text))
1004
1005 (call-with-output-file (string-append directory "/stupid-module.scm")
1006 (lambda (port)
1007 (write `(begin (define-module (stupid-module))
1008 (define-public %stupid-thing ,text))
1009 port)))
1010
1011 (let* ((exp (with-imported-modules '((stupid-module))
1012 (gexp (begin
1013 (use-modules (stupid-module))
1014 (display %stupid-thing)))))
1015 (file (program-file "program" exp
1016 #:guile %bootstrap-guile
1017 #:module-path (list directory))))
1018 (run-with-store %store
1019 (mlet* %store-monad ((drv (lower-object file))
1020 (out -> (derivation->output-path drv)))
1021 (mbegin %store-monad
1022 (built-derivations (list drv))
1023 (let* ((pipe (open-input-pipe out))
1024 (str (get-string-all pipe)))
1025 (return (and (zero? (close-pipe pipe))
1026 (string=? text str)))))))))))
1027
1028 (test-assertm "program-file & with-extensions"
1029 (let* ((exp (with-extensions (list %extension-package)
1030 (gexp (begin
1031 (use-modules (hg2g))
1032 (display the-answer)))))
1033 (file (program-file "program" exp
1034 #:guile %bootstrap-guile)))
1035 (mlet* %store-monad ((drv (lower-object file))
1036 (out -> (derivation->output-path drv)))
1037 (mbegin %store-monad
1038 (built-derivations (list drv))
1039 (let* ((pipe (open-input-pipe out))
1040 (str (get-string-all pipe)))
1041 (return (and (zero? (close-pipe pipe))
1042 (= 42 (string->number str)))))))))
1043
1044 (test-assertm "scheme-file"
1045 (let* ((text (plain-file "foo" "Hello, world!"))
1046 (scheme (scheme-file "bar" #~(list "foo" #$text))))
1047 (mlet* %store-monad ((drv (lower-object scheme))
1048 (text (lower-object text))
1049 (out -> (derivation->output-path drv)))
1050 (mbegin %store-monad
1051 (built-derivations (list drv))
1052 (mlet %store-monad ((refs (references* out)))
1053 (return (and (equal? refs (list text))
1054 (equal? `(list "foo" ,text)
1055 (call-with-input-file out read)))))))))
1056
1057 (test-assert "text-file*"
1058 (run-with-store %store
1059 (mlet* %store-monad
1060 ((drv (package->derivation %bootstrap-guile))
1061 (guile -> (derivation->output-path drv))
1062 (file (text-file "bar" "This is bar."))
1063 (text (text-file* "foo"
1064 %bootstrap-guile "/bin/guile "
1065 (gexp-input %bootstrap-guile "out") "/bin/guile "
1066 drv "/bin/guile "
1067 file))
1068 (done (built-derivations (list text)))
1069 (out -> (derivation->output-path text))
1070 (refs (references* out)))
1071 ;; Make sure we get the right references and the right content.
1072 (return (and (lset= string=? refs (list guile file))
1073 (equal? (call-with-input-file out get-string-all)
1074 (string-append guile "/bin/guile "
1075 guile "/bin/guile "
1076 guile "/bin/guile "
1077 file)))))
1078 #:guile-for-build (package-derivation %store %bootstrap-guile)))
1079
1080 (test-assertm "mixed-text-file"
1081 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
1082 "export PATH="
1083 %bootstrap-guile "/bin"))
1084 (drv (lower-object file))
1085 (out -> (derivation->output-path drv))
1086 (guile-drv (package->derivation %bootstrap-guile))
1087 (guile -> (derivation->output-path guile-drv)))
1088 (mbegin %store-monad
1089 (built-derivations (list drv))
1090 (mlet %store-monad ((refs (references* out)))
1091 (return (and (string=? (string-append "export PATH=" guile "/bin")
1092 (call-with-input-file out get-string-all))
1093 (equal? refs (list guile))))))))
1094
1095 (test-assert "gexp->derivation vs. %current-target-system"
1096 (let ((mval (gexp->derivation "foo"
1097 #~(begin
1098 (mkdir #$output)
1099 (foo #+gnu-make))
1100 #:target #f)))
1101 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
1102 ;; influence.
1103 (parameterize ((%current-target-system "fooooo"))
1104 (derivation? (run-with-store %store mval)))))
1105
1106 (test-assertm "lower-object"
1107 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
1108 (drv2 (lower-object (package-source coreutils)))
1109 (item (lower-object (plain-file "foo" "Hello!"))))
1110 (return (and (derivation? drv1) (derivation? drv2)
1111 (store-path? item)))))
1112
1113 (test-assertm "lower-object, computed-file"
1114 (let* ((text (plain-file "foo" "Hello!"))
1115 (exp #~(begin
1116 (mkdir #$output)
1117 (symlink #$%bootstrap-guile
1118 (string-append #$output "/guile"))
1119 (symlink #$text (string-append #$output "/text"))))
1120 (computed (computed-file "computed" exp)))
1121 (mlet* %store-monad ((text (lower-object text))
1122 (guile-drv (lower-object %bootstrap-guile))
1123 (comp-drv (lower-object computed))
1124 (comp -> (derivation->output-path comp-drv)))
1125 (mbegin %store-monad
1126 (built-derivations (list comp-drv))
1127 (return (and (string=? (readlink (string-append comp "/guile"))
1128 (derivation->output-path guile-drv))
1129 (string=? (readlink (string-append comp "/text"))
1130 text)))))))
1131
1132 (test-assert "lower-object & gexp-input-error?"
1133 (guard (c ((gexp-input-error? c)
1134 (gexp-error-invalid-input c)))
1135 (run-with-store %store
1136 (lower-object (current-module))
1137 #:guile-for-build (%guile-for-build))))
1138
1139 (test-assert "printer"
1140 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
1141 \"/bin/uname\"\\) [[:xdigit:]]+>$"
1142 (with-output-to-string
1143 (lambda ()
1144 (write
1145 (gexp (string-append (ungexp coreutils)
1146 "/bin/uname")))))))
1147
1148 (test-assert "printer vs. ungexp-splicing"
1149 (string-match "^#<gexp .* [[:xdigit:]]+>$"
1150 (with-output-to-string
1151 (lambda ()
1152 ;; #~(begin #$@#~())
1153 (write
1154 (gexp (begin (ungexp-splicing (gexp ())))))))))
1155
1156 (test-equal "sugar"
1157 '(gexp (foo (ungexp bar) (ungexp baz "out")
1158 (ungexp (chbouib 42))
1159 (ungexp-splicing (list x y z))
1160 (ungexp-native foo) (ungexp-native foo "out")
1161 (ungexp-native (chbouib 42))
1162 (ungexp-native-splicing (list x y z))))
1163 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
1164 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
1165
1166 (test-end "gexp")
1167
1168 ;; Local Variables:
1169 ;; eval: (put 'test-assertm 'scheme-indent-function 1)
1170 ;; End: