utils: Move base16 procedures to (guix base16).
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
5e2e4a51 2;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
21b679f6
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (test-gexp)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix gexp)
ef8de985 23 #:use-module (guix grafts)
21b679f6 24 #:use-module (guix derivations)
79c0c8cd 25 #:use-module (guix packages)
c1bc358f 26 #:use-module (guix tests)
4ff76a0a 27 #:use-module ((guix build utils) #:select (with-directory-excursion))
21b679f6
LC
28 #:use-module (gnu packages)
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages bootstrap)
31 #:use-module (srfi srfi-1)
c8351d9a 32 #:use-module (srfi srfi-34)
21b679f6
LC
33 #:use-module (srfi srfi-64)
34 #:use-module (rnrs io ports)
35 #:use-module (ice-9 match)
2cf0ea0d 36 #:use-module (ice-9 regex)
0687fc9c
LC
37 #:use-module (ice-9 popen)
38 #:use-module (ice-9 ftw))
21b679f6
LC
39
40;; Test the (guix gexp) module.
41
42(define %store
c1bc358f 43 (open-connection-for-tests))
21b679f6 44
ef8de985
LC
45;; Globally disable grafts because they can trigger early builds.
46(%graft? #f)
47
21b679f6 48;; For white-box testing.
1f976033
LC
49(define (gexp-inputs x)
50 ((@@ (guix gexp) gexp-inputs) x))
51(define (gexp-native-inputs x)
52 ((@@ (guix gexp) gexp-native-inputs) x))
53(define (gexp-outputs x)
54 ((@@ (guix gexp) gexp-outputs) x))
55(define (gexp->sexp . x)
56 (apply (@@ (guix gexp) gexp->sexp) x))
21b679f6 57
667b2508 58(define* (gexp->sexp* exp #:optional target)
68a61e9f 59 (run-with-store %store (gexp->sexp exp
68a61e9f 60 #:target target)
c1bc358f 61 #:guile-for-build (%guile-for-build)))
21b679f6
LC
62
63(define-syntax-rule (test-assertm name exp)
64 (test-assert name
65 (run-with-store %store exp
c1bc358f 66 #:guile-for-build (%guile-for-build))))
21b679f6
LC
67
68\f
69(test-begin "gexp")
70
71(test-equal "no refs"
72 '(display "hello!")
73 (let ((exp (gexp (display "hello!"))))
74 (and (gexp? exp)
75 (null? (gexp-inputs exp))
76 (gexp->sexp* exp))))
77
78(test-equal "unquote"
79 '(display `(foo ,(+ 2 3)))
80 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
81 (and (gexp? exp)
82 (null? (gexp-inputs exp))
83 (gexp->sexp* exp))))
84
85(test-assert "one input package"
86 (let ((exp (gexp (display (ungexp coreutils)))))
87 (and (gexp? exp)
88 (match (gexp-inputs exp)
89 (((p "out"))
90 (eq? p coreutils)))
91 (equal? `(display ,(derivation->output-path
92 (package-derivation %store coreutils)))
93 (gexp->sexp* exp)))))
5e2e4a51
LC
94
95(test-assert "one input package, dotted list"
96 (let ((exp (gexp (coreutils . (ungexp coreutils)))))
97 (and (gexp? exp)
98 (match (gexp-inputs exp)
99 (((p "out"))
100 (eq? p coreutils)))
101 (equal? `(coreutils . ,(derivation->output-path
102 (package-derivation %store coreutils)))
103 (gexp->sexp* exp)))))
21b679f6 104
79c0c8cd
LC
105(test-assert "one input origin"
106 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
107 (and (gexp? exp)
108 (match (gexp-inputs exp)
109 (((o "out"))
110 (eq? o (package-source coreutils))))
111 (equal? `(display ,(derivation->output-path
112 (package-source-derivation
113 %store (package-source coreutils))))
114 (gexp->sexp* exp)))))
115
d9ae938f
LC
116(test-assert "one local file"
117 (let* ((file (search-path %load-path "guix.scm"))
118 (local (local-file file))
119 (exp (gexp (display (ungexp local))))
020f3e41 120 (intd (add-to-store %store (basename file) #f
d9ae938f
LC
121 "sha256" file)))
122 (and (gexp? exp)
123 (match (gexp-inputs exp)
124 (((x "out"))
125 (eq? x local)))
126 (equal? `(display ,intd) (gexp->sexp* exp)))))
127
7833db1f
LC
128(test-assert "one local file, symlink"
129 (let ((file (search-path %load-path "guix.scm"))
130 (link (tmpnam)))
131 (dynamic-wind
132 (const #t)
133 (lambda ()
134 (symlink (canonicalize-path file) link)
135 (let* ((local (local-file link "my-file" #:recursive? #f))
136 (exp (gexp (display (ungexp local))))
137 (intd (add-to-store %store "my-file" #f
138 "sha256" file)))
139 (and (gexp? exp)
140 (match (gexp-inputs exp)
141 (((x "out"))
142 (eq? x local)))
143 (equal? `(display ,intd) (gexp->sexp* exp)))))
144 (lambda ()
145 (false-if-exception (delete-file link))))))
146
4ff76a0a
LC
147(test-equal "local-file, relative file name"
148 (canonicalize-path (search-path %load-path "guix/base32.scm"))
149 (let ((directory (dirname (search-path %load-path
150 "guix/build-system/gnu.scm"))))
151 (with-directory-excursion directory
152 (let ((file (local-file "../guix/base32.scm")))
153 (local-file-absolute-file-name file)))))
154
0687fc9c
LC
155(test-assertm "local-file, #:select?"
156 (mlet* %store-monad ((select? -> (lambda (file stat)
157 (member (basename file)
158 '("guix.scm" "tests"
159 "gexp.scm"))))
160 (file -> (local-file ".." "directory"
161 #:recursive? #t
162 #:select? select?))
163 (dir (lower-object file)))
164 (return (and (store-path? dir)
165 (equal? (scandir dir)
166 '("." ".." "guix.scm" "tests"))
167 (equal? (scandir (string-append dir "/tests"))
168 '("." ".." "gexp.scm"))))))
169
558e8b11
LC
170(test-assert "one plain file"
171 (let* ((file (plain-file "hi" "Hello, world!"))
172 (exp (gexp (display (ungexp file))))
173 (expected (add-text-to-store %store "hi" "Hello, world!")))
174 (and (gexp? exp)
175 (match (gexp-inputs exp)
176 (((x "out"))
177 (eq? x file)))
178 (equal? `(display ,expected) (gexp->sexp* exp)))))
179
21b679f6
LC
180(test-assert "same input twice"
181 (let ((exp (gexp (begin
182 (display (ungexp coreutils))
183 (display (ungexp coreutils))))))
184 (and (gexp? exp)
185 (match (gexp-inputs exp)
186 (((p "out"))
187 (eq? p coreutils)))
188 (let ((e `(display ,(derivation->output-path
189 (package-derivation %store coreutils)))))
190 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
191
192(test-assert "two input packages, one derivation, one file"
193 (let* ((drv (build-expression->derivation
194 %store "foo" 'bar
195 #:guile-for-build (package-derivation %store %bootstrap-guile)))
196 (txt (add-text-to-store %store "foo" "Hello, world!"))
197 (exp (gexp (begin
198 (display (ungexp coreutils))
199 (display (ungexp %bootstrap-guile))
200 (display (ungexp drv))
201 (display (ungexp txt))))))
202 (define (match-input thing)
203 (match-lambda
204 ((drv-or-pkg _ ...)
205 (eq? thing drv-or-pkg))))
206
207 (and (gexp? exp)
208 (= 4 (length (gexp-inputs exp)))
209 (every (lambda (input)
210 (find (match-input input) (gexp-inputs exp)))
211 (list drv coreutils %bootstrap-guile txt))
212 (let ((e0 `(display ,(derivation->output-path
213 (package-derivation %store coreutils))))
214 (e1 `(display ,(derivation->output-path
215 (package-derivation %store %bootstrap-guile))))
216 (e2 `(display ,(derivation->output-path drv)))
217 (e3 `(display ,txt)))
218 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
219
a9e5e92f
LC
220(test-assert "file-append"
221 (let* ((drv (package-derivation %store %bootstrap-guile))
222 (fa (file-append %bootstrap-guile "/bin/guile"))
223 (exp #~(here we go #$fa)))
224 (and (match (gexp->sexp* exp)
225 (('here 'we 'go (? string? result))
226 (string=? result
227 (string-append (derivation->output-path drv)
228 "/bin/guile"))))
229 (match (gexp-inputs exp)
230 (((thing "out"))
231 (eq? thing fa))))))
232
233(test-assert "file-append, output"
234 (let* ((drv (package-derivation %store glibc))
235 (fa (file-append glibc "/lib" "/debug"))
236 (exp #~(foo #$fa:debug)))
237 (and (match (gexp->sexp* exp)
238 (('foo (? string? result))
239 (string=? result
240 (string-append (derivation->output-path drv "debug")
241 "/lib/debug"))))
242 (match (gexp-inputs exp)
243 (((thing "debug"))
244 (eq? thing fa))))))
245
246(test-assert "file-append, nested"
247 (let* ((drv (package-derivation %store glibc))
248 (dir (file-append glibc "/bin"))
249 (slash (file-append dir "/"))
250 (file (file-append slash "getent"))
251 (exp #~(foo #$file)))
252 (and (match (gexp->sexp* exp)
253 (('foo (? string? result))
254 (string=? result
255 (string-append (derivation->output-path drv)
256 "/bin/getent"))))
257 (match (gexp-inputs exp)
258 (((thing "out"))
259 (eq? thing file))))))
260
667b2508
LC
261(test-assert "ungexp + ungexp-native"
262 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
263 (ungexp coreutils)
264 (ungexp-native glibc)
265 (ungexp binutils))))
266 (target "mips64el-linux")
267 (guile (derivation->output-path
268 (package-derivation %store %bootstrap-guile)))
269 (cu (derivation->output-path
270 (package-cross-derivation %store coreutils target)))
271 (libc (derivation->output-path
272 (package-derivation %store glibc)))
273 (bu (derivation->output-path
274 (package-cross-derivation %store binutils target))))
275 (and (lset= equal?
276 `((,%bootstrap-guile "out") (,glibc "out"))
277 (gexp-native-inputs exp))
278 (lset= equal?
279 `((,coreutils "out") (,binutils "out"))
280 (gexp-inputs exp))
281 (equal? `(list ,guile ,cu ,libc ,bu)
282 (gexp->sexp* exp target)))))
283
1123759b
LC
284(test-equal "ungexp + ungexp-native, nested"
285 (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out")))
286 (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
287 (ungexp %bootstrap-guile)))))
288 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
289
5b14a790
LC
290(test-equal "ungexp + ungexp-native, nested, special mixture"
291 `(() <> ((,coreutils "out")))
292
293 ;; (gexp-native-inputs exp) used to return '(), wrongfully.
294 (let* ((foo (gexp (foo (ungexp-native coreutils))))
295 (exp (gexp (bar (ungexp foo)))))
296 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
297
21b679f6
LC
298(test-assert "input list"
299 (let ((exp (gexp (display
300 '(ungexp (list %bootstrap-guile coreutils)))))
301 (guile (derivation->output-path
302 (package-derivation %store %bootstrap-guile)))
303 (cu (derivation->output-path
304 (package-derivation %store coreutils))))
305 (and (lset= equal?
306 `((,%bootstrap-guile "out") (,coreutils "out"))
307 (gexp-inputs exp))
308 (equal? `(display '(,guile ,cu))
309 (gexp->sexp* exp)))))
310
667b2508
LC
311(test-assert "input list + ungexp-native"
312 (let* ((target "mips64el-linux")
313 (exp (gexp (display
314 (cons '(ungexp-native (list %bootstrap-guile coreutils))
315 '(ungexp (list glibc binutils))))))
316 (guile (derivation->output-path
317 (package-derivation %store %bootstrap-guile)))
318 (cu (derivation->output-path
319 (package-derivation %store coreutils)))
320 (xlibc (derivation->output-path
321 (package-cross-derivation %store glibc target)))
322 (xbu (derivation->output-path
323 (package-cross-derivation %store binutils target))))
324 (and (lset= equal?
325 `((,%bootstrap-guile "out") (,coreutils "out"))
326 (gexp-native-inputs exp))
327 (lset= equal?
328 `((,glibc "out") (,binutils "out"))
329 (gexp-inputs exp))
330 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
331 (gexp->sexp* exp target)))))
332
21b679f6 333(test-assert "input list splicing"
a482cfdc 334 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
21b679f6
LC
335 (outputs (list (derivation->output-path
336 (package-derivation %store glibc)
337 "debug")
338 (derivation->output-path
339 (package-derivation %store %bootstrap-guile))))
340 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
341 (and (lset= equal?
342 `((,glibc "debug") (,%bootstrap-guile "out"))
343 (gexp-inputs exp))
344 (equal? (gexp->sexp* exp)
345 `(list ,@(cons 5 outputs))))))
346
667b2508 347(test-assert "input list splicing + ungexp-native-splicing"
5b14a790
LC
348 (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
349 %bootstrap-guile))
0dbea56b
LC
350 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
351 (and (lset= equal?
352 `((,glibc "debug") (,%bootstrap-guile "out"))
353 (gexp-native-inputs exp))
354 (null? (gexp-inputs exp))
355 (equal? (gexp->sexp* exp) ;native
356 (gexp->sexp* exp "mips64el-linux")))))
357
4b23c466
LC
358(test-equal "output list"
359 2
360 (let ((exp (gexp (begin (mkdir (ungexp output))
361 (mkdir (ungexp output "bar"))))))
362 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
363
364(test-assert "output list, combined gexps"
365 (let* ((exp0 (gexp (mkdir (ungexp output))))
366 (exp1 (gexp (mkdir (ungexp output "foo"))))
367 (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
368 (and (lset= equal?
369 (append (gexp-outputs exp0) (gexp-outputs exp1))
370 (gexp-outputs exp2))
371 (= 2 (length (gexp-outputs exp2))))))
372
7e75a673
LC
373(test-equal "output list, combined gexps, duplicate output"
374 1
375 (let* ((exp0 (gexp (mkdir (ungexp output))))
376 (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
377 (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
378 (length (gexp-outputs exp2))))
379
f9efe568
LC
380(test-assert "output list + ungexp-splicing list, combined gexps"
381 (let* ((exp0 (gexp (mkdir (ungexp output))))
382 (exp1 (gexp (mkdir (ungexp output "foo"))))
383 (exp2 (gexp (begin (display "hi!")
384 (ungexp-splicing (list exp0 exp1))))))
385 (and (lset= equal?
386 (append (gexp-outputs exp0) (gexp-outputs exp1))
387 (gexp-outputs exp2))
388 (= 2 (length (gexp-outputs exp2))))))
389
21b679f6
LC
390(test-assertm "gexp->file"
391 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
392 (guile (package-file %bootstrap-guile))
393 (sexp (gexp->sexp exp))
394 (drv (gexp->file "foo" exp))
395 (out -> (derivation->output-path drv))
396 (done (built-derivations (list drv)))
e74f64b9 397 (refs (references* out)))
21b679f6
LC
398 (return (and (equal? sexp (call-with-input-file out read))
399 (equal? (list guile) refs)))))
400
a9e5e92f
LC
401(test-assertm "gexp->file + file-append"
402 (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
403 "/bin/guile"))
404 (guile (package-file %bootstrap-guile))
405 (drv (gexp->file "foo" exp))
406 (out -> (derivation->output-path drv))
407 (done (built-derivations (list drv)))
e74f64b9 408 (refs (references* out)))
a9e5e92f
LC
409 (return (and (equal? (string-append guile "/bin/guile")
410 (call-with-input-file out read))
411 (equal? (list guile) refs)))))
412
21b679f6
LC
413(test-assertm "gexp->derivation"
414 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
415 (exp -> (gexp
416 (begin
417 (mkdir (ungexp output))
418 (chdir (ungexp output))
419 (symlink
420 (string-append (ungexp %bootstrap-guile)
421 "/bin/guile")
422 "foo")
423 (symlink (ungexp file)
424 (ungexp output "2nd")))))
425 (drv (gexp->derivation "foo" exp))
426 (out -> (derivation->output-path drv))
427 (out2 -> (derivation->output-path drv "2nd"))
428 (done (built-derivations (list drv)))
e74f64b9
LC
429 (refs (references* out))
430 (refs2 (references* out2))
21b679f6
LC
431 (guile (package-file %bootstrap-guile "bin/guile")))
432 (return (and (string=? (readlink (string-append out "/foo")) guile)
433 (string=? (readlink out2) file)
434 (equal? refs (list (dirname (dirname guile))))
435 (equal? refs2 (list file))))))
436
ce45eb4c 437(test-assertm "gexp->derivation vs. grafts"
ef8de985
LC
438 (mlet* %store-monad ((graft? (set-grafting #f))
439 (p0 -> (dummy-package "dummy"
ce45eb4c
LC
440 (arguments
441 '(#:implicit-inputs? #f))))
442 (r -> (package (inherit p0) (name "DuMMY")))
443 (p1 -> (package (inherit p0) (replacement r)))
444 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
445 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
446 (void (set-guile-for-build %bootstrap-guile))
ef8de985
LC
447 (drv0 (gexp->derivation "t" exp0 #:graft? #t))
448 (drv1 (gexp->derivation "t" exp1 #:graft? #t))
449 (drv1* (gexp->derivation "t" exp1 #:graft? #f))
450 (_ (set-grafting graft?)))
ce45eb4c
LC
451 (return (and (not (string=? (derivation->output-path drv0)
452 (derivation->output-path drv1)))
453 (string=? (derivation->output-path drv0)
454 (derivation->output-path drv1*))))))
455
21b679f6
LC
456(test-assertm "gexp->derivation, composed gexps"
457 (mlet* %store-monad ((exp0 -> (gexp (begin
458 (mkdir (ungexp output))
459 (chdir (ungexp output)))))
460 (exp1 -> (gexp (symlink
461 (string-append (ungexp %bootstrap-guile)
462 "/bin/guile")
463 "foo")))
464 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
465 (drv (gexp->derivation "foo" exp))
466 (out -> (derivation->output-path drv))
467 (done (built-derivations (list drv)))
468 (guile (package-file %bootstrap-guile "bin/guile")))
469 (return (string=? (readlink (string-append out "/foo"))
470 guile))))
471
5d098459
LC
472(test-assertm "gexp->derivation, default system"
473 ;; The default system should be the one at '>>=' time, not the one at
474 ;; invocation time. See <http://bugs.gnu.org/18002>.
475 (let ((system (%current-system))
476 (mdrv (parameterize ((%current-system "foobar64-linux"))
477 (gexp->derivation "foo"
478 (gexp
479 (mkdir (ungexp output)))))))
480 (mlet %store-monad ((drv mdrv))
481 (return (string=? system (derivation-system drv))))))
482
d9ae938f
LC
483(test-assertm "gexp->derivation, local-file"
484 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
020f3e41 485 (intd (interned-file file #:recursive? #f))
d9ae938f
LC
486 (local -> (local-file file))
487 (exp -> (gexp (begin
488 (stat (ungexp local))
489 (symlink (ungexp local)
490 (ungexp output)))))
491 (drv (gexp->derivation "local-file" exp)))
492 (mbegin %store-monad
493 (built-derivations (list drv))
494 (return (string=? (readlink (derivation->output-path drv))
495 intd)))))
496
68a61e9f
LC
497(test-assertm "gexp->derivation, cross-compilation"
498 (mlet* %store-monad ((target -> "mips64el-linux")
499 (exp -> (gexp (list (ungexp coreutils)
500 (ungexp output))))
501 (xdrv (gexp->derivation "foo" exp
502 #:target target))
e74f64b9 503 (refs (references*
68a61e9f
LC
504 (derivation-file-name xdrv)))
505 (xcu (package->cross-derivation coreutils
506 target))
507 (cu (package->derivation coreutils)))
508 (return (and (member (derivation-file-name xcu) refs)
509 (not (member (derivation-file-name cu) refs))))))
510
667b2508
LC
511(test-assertm "gexp->derivation, ungexp-native"
512 (mlet* %store-monad ((target -> "mips64el-linux")
513 (exp -> (gexp (list (ungexp-native coreutils)
514 (ungexp output))))
515 (xdrv (gexp->derivation "foo" exp
516 #:target target))
517 (drv (gexp->derivation "foo" exp)))
518 (return (string=? (derivation-file-name drv)
519 (derivation-file-name xdrv)))))
520
521(test-assertm "gexp->derivation, ungexp + ungexp-native"
522 (mlet* %store-monad ((target -> "mips64el-linux")
523 (exp -> (gexp (list (ungexp-native coreutils)
524 (ungexp glibc)
525 (ungexp output))))
526 (xdrv (gexp->derivation "foo" exp
527 #:target target))
e74f64b9 528 (refs (references*
667b2508
LC
529 (derivation-file-name xdrv)))
530 (xglibc (package->cross-derivation glibc target))
531 (cu (package->derivation coreutils)))
532 (return (and (member (derivation-file-name cu) refs)
533 (member (derivation-file-name xglibc) refs)))))
534
535(test-assertm "gexp->derivation, ungexp-native + composed gexps"
536 (mlet* %store-monad ((target -> "mips64el-linux")
537 (exp0 -> (gexp (list 1 2
538 (ungexp coreutils))))
539 (exp -> (gexp (list 0 (ungexp-native exp0))))
540 (xdrv (gexp->derivation "foo" exp
541 #:target target))
542 (drv (gexp->derivation "foo" exp)))
543 (return (string=? (derivation-file-name drv)
544 (derivation-file-name xdrv)))))
545
6fd1a796
LC
546(test-assertm "gexp->derivation, store copy"
547 (let ((build-one #~(call-with-output-file #$output
548 (lambda (port)
549 (display "This is the one." port))))
550 (build-two (lambda (one)
551 #~(begin
552 (mkdir #$output)
553 (symlink #$one (string-append #$output "/one"))
554 (call-with-output-file (string-append #$output "/two")
555 (lambda (port)
556 (display "This is the second one." port))))))
b53833b2
LC
557 (build-drv #~(begin
558 (use-modules (guix build store-copy))
6fd1a796 559
b53833b2
LC
560 (mkdir #$output)
561 (populate-store '("graph") #$output))))
6fd1a796
LC
562 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
563 (two (gexp->derivation "two" (build-two one)))
b53833b2 564 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 565 #:references-graphs
b53833b2 566 `(("graph" ,two))
6fd1a796
LC
567 #:modules
568 '((guix build store-copy)
569 (guix build utils))))
570 (ok? (built-derivations (list drv)))
571 (out -> (derivation->output-path drv)))
572 (let ((one (derivation->output-path one))
573 (two (derivation->output-path two)))
574 (return (and ok?
575 (file-exists? (string-append out "/" one))
576 (file-exists? (string-append out "/" two))
577 (file-exists? (string-append out "/" two "/two"))
578 (string=? (readlink (string-append out "/" two "/one"))
579 one)))))))
580
aa72d9af
LC
581(test-assertm "imported-files"
582 (mlet* %store-monad
583 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
584 ("a/b/c" . ,(search-path %load-path
585 "guix/derivations.scm"))
586 ("p/q" . ,(search-path %load-path "guix.scm"))
587 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
588 (drv (imported-files files)))
589 (mbegin %store-monad
590 (built-derivations (list drv))
591 (let ((dir (derivation->output-path drv)))
592 (return
593 (every (match-lambda
594 ((path . source)
595 (equal? (call-with-input-file (string-append dir "/" path)
596 get-bytevector-all)
597 (call-with-input-file source
598 get-bytevector-all))))
599 files))))))
600
0bb9929e
LC
601(test-equal "gexp-modules & ungexp"
602 '((bar) (foo))
603 ((@@ (guix gexp) gexp-modules)
604 #~(foo #$(with-imported-modules '((foo)) #~+)
605 #+(with-imported-modules '((bar)) #~-))))
606
607(test-equal "gexp-modules & ungexp-splicing"
608 '((foo) (bar))
609 ((@@ (guix gexp) gexp-modules)
610 #~(foo #$@(list (with-imported-modules '((foo)) #~+)
611 (with-imported-modules '((bar)) #~-)))))
612
aa72d9af
LC
613(test-assertm "gexp->derivation #:modules"
614 (mlet* %store-monad
615 ((build -> #~(begin
616 (use-modules (guix build utils))
617 (mkdir-p (string-append #$output "/guile/guix/nix"))
618 #t))
619 (drv (gexp->derivation "test-with-modules" build
620 #:modules '((guix build utils)))))
621 (mbegin %store-monad
622 (built-derivations (list drv))
623 (let* ((p (derivation->output-path drv))
624 (s (stat (string-append p "/guile/guix/nix"))))
625 (return (eq? (stat:type s) 'directory))))))
626
0bb9929e
LC
627(test-assertm "gexp->derivation & with-imported-modules"
628 ;; Same test as above, but using 'with-imported-modules'.
629 (mlet* %store-monad
630 ((build -> (with-imported-modules '((guix build utils))
631 #~(begin
632 (use-modules (guix build utils))
633 (mkdir-p (string-append #$output "/guile/guix/nix"))
634 #t)))
635 (drv (gexp->derivation "test-with-modules" build)))
636 (mbegin %store-monad
637 (built-derivations (list drv))
638 (let* ((p (derivation->output-path drv))
639 (s (stat (string-append p "/guile/guix/nix"))))
640 (return (eq? (stat:type s) 'directory))))))
641
642(test-assertm "gexp->derivation & nested with-imported-modules"
643 (mlet* %store-monad
644 ((build1 -> (with-imported-modules '((guix build utils))
645 #~(begin
646 (use-modules (guix build utils))
647 (mkdir-p (string-append #$output "/guile/guix/nix"))
648 #t)))
649 (build2 -> (with-imported-modules '((guix build bournish))
650 #~(begin
651 (use-modules (guix build bournish)
652 (system base compile))
653 #+build1
654 (call-with-output-file (string-append #$output "/b")
655 (lambda (port)
656 (write
657 (read-and-compile (open-input-string "cd /foo")
658 #:from %bournish-language
659 #:to 'scheme)
660 port))))))
661 (drv (gexp->derivation "test-with-modules" build2)))
662 (mbegin %store-monad
663 (built-derivations (list drv))
664 (let* ((p (derivation->output-path drv))
665 (s (stat (string-append p "/guile/guix/nix")))
666 (b (string-append p "/b")))
667 (return (and (eq? (stat:type s) 'directory)
668 (equal? '(chdir "/foo")
669 (call-with-input-file b read))))))))
670
b53833b2
LC
671(test-assertm "gexp->derivation #:references-graphs"
672 (mlet* %store-monad
72cd8ec0 673 ((one (text-file "one" (random-text)))
b53833b2
LC
674 (two (gexp->derivation "two"
675 #~(symlink #$one #$output:chbouib)))
66a35ceb
LC
676 (build -> (with-imported-modules '((guix build store-copy)
677 (guix build utils))
678 #~(begin
679 (use-modules (guix build store-copy))
680 (with-output-to-file #$output
681 (lambda ()
682 (write (call-with-input-file "guile"
683 read-reference-graph))))
684 (with-output-to-file #$output:one
685 (lambda ()
686 (write (call-with-input-file "one"
687 read-reference-graph))))
688 (with-output-to-file #$output:two
689 (lambda ()
690 (write (call-with-input-file "two"
691 read-reference-graph)))))))
692 (drv (gexp->derivation "ref-graphs" build
b53833b2
LC
693 #:references-graphs `(("one" ,one)
694 ("two" ,two "chbouib")
66a35ceb 695 ("guile" ,%bootstrap-guile))))
b53833b2
LC
696 (ok? (built-derivations (list drv)))
697 (guile-drv (package->derivation %bootstrap-guile))
686784d0
LC
698 (bash (interned-file (search-bootstrap-binary "bash"
699 (%current-system))
700 "bash" #:recursive? #t))
b53833b2
LC
701 (g-one -> (derivation->output-path drv "one"))
702 (g-two -> (derivation->output-path drv "two"))
703 (g-guile -> (derivation->output-path drv)))
704 (return (and ok?
705 (equal? (call-with-input-file g-one read) (list one))
72cd8ec0
LC
706 (lset= string=?
707 (call-with-input-file g-two read)
708 (list one (derivation->output-path two "chbouib")))
686784d0
LC
709
710 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
72cd8ec0
LC
711 (lset= string=?
712 (call-with-input-file g-guile read)
713 (list (derivation->output-path guile-drv) bash))))))
b53833b2 714
c8351d9a
LC
715(test-assertm "gexp->derivation #:allowed-references"
716 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
717 #~(begin
718 (mkdir #$output)
719 (chdir #$output)
720 (symlink #$output "self")
721 (symlink #$%bootstrap-guile
722 "guile"))
723 #:allowed-references
724 (list "out" %bootstrap-guile))))
725 (built-derivations (list drv))))
726
accb682c
LC
727(test-assertm "gexp->derivation #:allowed-references, specific output"
728 (mlet* %store-monad ((in (gexp->derivation "thing"
729 #~(begin
730 (mkdir #$output:ok)
731 (mkdir #$output:not-ok))))
732 (drv (gexp->derivation "allowed-refs"
733 #~(begin
734 (pk #$in:not-ok)
735 (mkdir #$output)
736 (chdir #$output)
737 (symlink #$output "self")
738 (symlink #$in:ok "ok"))
739 #:allowed-references
740 (list "out"
741 (gexp-input in "ok")))))
742 (built-derivations (list drv))))
743
c8351d9a
LC
744(test-assert "gexp->derivation #:allowed-references, disallowed"
745 (let ((drv (run-with-store %store
746 (gexp->derivation "allowed-refs"
747 #~(begin
748 (mkdir #$output)
749 (chdir #$output)
750 (symlink #$%bootstrap-guile "guile"))
751 #:allowed-references '()))))
752 (guard (c ((nix-protocol-error? c) #t))
753 (build-derivations %store (list drv))
754 #f)))
755
3f4ecf32
LC
756(test-assertm "gexp->derivation #:disallowed-references, allowed"
757 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
758 #~(begin
759 (mkdir #$output)
760 (chdir #$output)
761 (symlink #$output "self")
762 (symlink #$%bootstrap-guile
763 "guile"))
764 #:disallowed-references '())))
765 (built-derivations (list drv))))
766
767
768(test-assert "gexp->derivation #:disallowed-references"
769 (let ((drv (run-with-store %store
770 (gexp->derivation "disallowed-refs"
771 #~(begin
772 (mkdir #$output)
773 (chdir #$output)
774 (symlink #$%bootstrap-guile "guile"))
775 #:disallowed-references (list %bootstrap-guile)))))
776 (guard (c ((nix-protocol-error? c) #t))
777 (build-derivations %store (list drv))
778 #f)))
779
c17b5ab4 780(define shebang
c1bc358f 781 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
782 "/bin/guile --no-auto-compile"))
783
784;; If we're going to hit the silly shebang limit (128 chars on Linux-based
785;; systems), then skip the following test.
47b3124a 786(test-skip (if (> (string-length shebang) 127) 2 0))
c17b5ab4 787
21b679f6
LC
788(test-assertm "gexp->script"
789 (mlet* %store-monad ((n -> (random (expt 2 50)))
790 (exp -> (gexp
791 (system*
792 (string-append (ungexp %bootstrap-guile)
793 "/bin/guile")
794 "-c" (object->string
795 '(display (expt (ungexp n) 2))))))
796 (drv (gexp->script "guile-thing" exp
797 #:guile %bootstrap-guile))
798 (out -> (derivation->output-path drv))
799 (done (built-derivations (list drv))))
800 (let* ((pipe (open-input-pipe out))
801 (str (get-string-all pipe)))
802 (return (and (zero? (close-pipe pipe))
803 (= (expt n 2) (string->number str)))))))
804
15a01c72
LC
805(test-assertm "program-file"
806 (let* ((n (random (expt 2 50)))
0bb9929e
LC
807 (exp (with-imported-modules '((guix build utils))
808 (gexp (begin
809 (use-modules (guix build utils))
810 (display (ungexp n))))))
15a01c72 811 (file (program-file "program" exp
15a01c72
LC
812 #:guile %bootstrap-guile)))
813 (mlet* %store-monad ((drv (lower-object file))
814 (out -> (derivation->output-path drv)))
815 (mbegin %store-monad
816 (built-derivations (list drv))
817 (let* ((pipe (open-input-pipe out))
818 (str (get-string-all pipe)))
819 (return (and (zero? (close-pipe pipe))
820 (= n (string->number str)))))))))
821
e1c153e0
LC
822(test-assertm "scheme-file"
823 (let* ((text (plain-file "foo" "Hello, world!"))
824 (scheme (scheme-file "bar" #~(list "foo" #$text))))
825 (mlet* %store-monad ((drv (lower-object scheme))
826 (text (lower-object text))
827 (out -> (derivation->output-path drv)))
828 (mbegin %store-monad
829 (built-derivations (list drv))
e74f64b9 830 (mlet %store-monad ((refs (references* out)))
e1c153e0
LC
831 (return (and (equal? refs (list text))
832 (equal? `(list "foo" ,text)
833 (call-with-input-file out read)))))))))
834
462a3fa3 835(test-assert "text-file*"
e74f64b9
LC
836 (run-with-store %store
837 (mlet* %store-monad
838 ((drv (package->derivation %bootstrap-guile))
839 (guile -> (derivation->output-path drv))
840 (file (text-file "bar" "This is bar."))
841 (text (text-file* "foo"
842 %bootstrap-guile "/bin/guile "
843 (gexp-input %bootstrap-guile "out") "/bin/guile "
844 drv "/bin/guile "
845 file))
846 (done (built-derivations (list text)))
847 (out -> (derivation->output-path text))
848 (refs (references* out)))
849 ;; Make sure we get the right references and the right content.
850 (return (and (lset= string=? refs (list guile file))
851 (equal? (call-with-input-file out get-string-all)
852 (string-append guile "/bin/guile "
853 guile "/bin/guile "
854 guile "/bin/guile "
855 file)))))
856 #:guile-for-build (package-derivation %store %bootstrap-guile)))
462a3fa3 857
b751cde3
LC
858(test-assertm "mixed-text-file"
859 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
860 "export PATH="
861 %bootstrap-guile "/bin"))
862 (drv (lower-object file))
863 (out -> (derivation->output-path drv))
864 (guile-drv (package->derivation %bootstrap-guile))
865 (guile -> (derivation->output-path guile-drv)))
866 (mbegin %store-monad
867 (built-derivations (list drv))
e74f64b9 868 (mlet %store-monad ((refs (references* out)))
b751cde3
LC
869 (return (and (string=? (string-append "export PATH=" guile "/bin")
870 (call-with-input-file out get-string-all))
871 (equal? refs (list guile))))))))
872
a8afb9ae
LC
873(test-assert "gexp->derivation vs. %current-target-system"
874 (let ((mval (gexp->derivation "foo"
875 #~(begin
876 (mkdir #$output)
877 (foo #+gnu-make))
878 #:target #f)))
879 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
880 ;; influence.
881 (parameterize ((%current-target-system "fooooo"))
882 (derivation? (run-with-store %store mval)))))
883
c2b84676
LC
884(test-assertm "lower-object"
885 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
886 (drv2 (lower-object (package-source coreutils)))
887 (item (lower-object (plain-file "foo" "Hello!"))))
888 (return (and (derivation? drv1) (derivation? drv2)
889 (store-path? item)))))
890
91937029
LC
891(test-assertm "lower-object, computed-file"
892 (let* ((text (plain-file "foo" "Hello!"))
893 (exp #~(begin
894 (mkdir #$output)
895 (symlink #$%bootstrap-guile
896 (string-append #$output "/guile"))
897 (symlink #$text (string-append #$output "/text"))))
898 (computed (computed-file "computed" exp)))
899 (mlet* %store-monad ((text (lower-object text))
900 (guile-drv (lower-object %bootstrap-guile))
901 (comp-drv (lower-object computed))
902 (comp -> (derivation->output-path comp-drv)))
903 (mbegin %store-monad
904 (built-derivations (list comp-drv))
905 (return (and (string=? (readlink (string-append comp "/guile"))
906 (derivation->output-path guile-drv))
907 (string=? (readlink (string-append comp "/text"))
908 text)))))))
909
2cf0ea0d
LC
910(test-assert "printer"
911 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
912 \"/bin/uname\"\\) [[:xdigit:]]+>$"
913 (with-output-to-string
914 (lambda ()
915 (write
916 (gexp (string-append (ungexp coreutils)
917 "/bin/uname")))))))
918
919(test-assert "printer vs. ungexp-splicing"
920 (string-match "^#<gexp .* [[:xdigit:]]+>$"
921 (with-output-to-string
922 (lambda ()
923 ;; #~(begin #$@#~())
924 (write
925 (gexp (begin (ungexp-splicing (gexp ())))))))))
926
21b679f6
LC
927(test-equal "sugar"
928 '(gexp (foo (ungexp bar) (ungexp baz "out")
929 (ungexp (chbouib 42))
667b2508
LC
930 (ungexp-splicing (list x y z))
931 (ungexp-native foo) (ungexp-native foo "out")
932 (ungexp-native (chbouib 42))
933 (ungexp-native-splicing (list x y z))))
934 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
935 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
936
937(test-end "gexp")
938
21b679f6
LC
939;; Local Variables:
940;; eval: (put 'test-assertm 'scheme-indent-function 1)
941;; End: