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