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