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