gexp: 'compiled-modules' can cross-compile.
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
9ec154f5 2;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 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)
838e17d8 26 #:use-module (guix build-system trivial)
c1bc358f 27 #:use-module (guix tests)
4ff76a0a 28 #:use-module ((guix build utils) #:select (with-directory-excursion))
1ae16033 29 #:use-module ((guix utils) #:select (call-with-temporary-directory))
21b679f6
LC
30 #:use-module (gnu packages)
31 #:use-module (gnu packages base)
32 #:use-module (gnu packages bootstrap)
33 #:use-module (srfi srfi-1)
c8351d9a 34 #:use-module (srfi srfi-34)
21b679f6
LC
35 #:use-module (srfi srfi-64)
36 #:use-module (rnrs io ports)
37 #:use-module (ice-9 match)
2cf0ea0d 38 #:use-module (ice-9 regex)
0687fc9c
LC
39 #:use-module (ice-9 popen)
40 #:use-module (ice-9 ftw))
21b679f6
LC
41
42;; Test the (guix gexp) module.
43
44(define %store
c1bc358f 45 (open-connection-for-tests))
21b679f6 46
ef8de985
LC
47;; Globally disable grafts because they can trigger early builds.
48(%graft? #f)
49
21b679f6 50;; For white-box testing.
1f976033
LC
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))
21b679f6 59
667b2508 60(define* (gexp->sexp* exp #:optional target)
68a61e9f 61 (run-with-store %store (gexp->sexp exp
68a61e9f 62 #:target target)
c1bc358f 63 #:guile-for-build (%guile-for-build)))
21b679f6 64
838e17d8
LC
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
21b679f6
LC
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)))))
5e2e4a51
LC
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)))))
21b679f6 122
79c0c8cd
LC
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
d9ae938f
LC
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))))
020f3e41 138 (intd (add-to-store %store (basename file) #f
d9ae938f
LC
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
7833db1f
LC
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
4ff76a0a
LC
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
0687fc9c
LC
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
558e8b11
LC
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
21b679f6
LC
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
a9e5e92f
LC
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
667b2508
LC
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
1123759b
LC
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
5b14a790
LC
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
21b679f6
LC
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
667b2508
LC
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
21b679f6 351(test-assert "input list splicing"
a482cfdc 352 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
21b679f6
LC
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
667b2508 365(test-assert "input list splicing + ungexp-native-splicing"
5b14a790
LC
366 (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
367 %bootstrap-guile))
0dbea56b
LC
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
578dfbe0
LC
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
4b23c466
LC
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
7e75a673
LC
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
f9efe568
LC
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
21b679f6
LC
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)))
e74f64b9 423 (refs (references* out)))
21b679f6
LC
424 (return (and (equal? sexp (call-with-input-file out read))
425 (equal? (list guile) refs)))))
426
a9e5e92f
LC
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)))
e74f64b9 434 (refs (references* out)))
a9e5e92f
LC
435 (return (and (equal? (string-append guile "/bin/guile")
436 (call-with-input-file out read))
437 (equal? (list guile) refs)))))
438
4fbd1a2b
LC
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
21b679f6
LC
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)))
e74f64b9
LC
473 (refs (references* out))
474 (refs2 (references* out2))
21b679f6
LC
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))))
8856f409
LC
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)))))
21b679f6 488
ce45eb4c 489(test-assertm "gexp->derivation vs. grafts"
ef8de985
LC
490 (mlet* %store-monad ((graft? (set-grafting #f))
491 (p0 -> (dummy-package "dummy"
ce45eb4c
LC
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))
ef8de985
LC
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?)))
ce45eb4c
LC
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
21b679f6
LC
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
5d098459
LC
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
d9ae938f
LC
535(test-assertm "gexp->derivation, local-file"
536 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
020f3e41 537 (intd (interned-file file #:recursive? #f))
d9ae938f
LC
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
68a61e9f
LC
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))
e74f64b9 555 (refs (references*
68a61e9f
LC
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
667b2508
LC
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))
e74f64b9 580 (refs (references*
667b2508
LC
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
6fd1a796
LC
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))))))
b53833b2
LC
609 (build-drv #~(begin
610 (use-modules (guix build store-copy))
6fd1a796 611
b53833b2
LC
612 (mkdir #$output)
613 (populate-store '("graph") #$output))))
6fd1a796
LC
614 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
615 (two (gexp->derivation "two" (build-two one)))
b53833b2 616 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 617 #:references-graphs
b53833b2 618 `(("graph" ,two))
6fd1a796
LC
619 #:modules
620 '((guix build store-copy)
d4e9317b
LC
621 (guix progress)
622 (guix records)
6892f0a2 623 (guix sets)
6fd1a796
LC
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
aa72d9af
LC
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"))))
8df2eca6 643 (dir (imported-files files)))
aa72d9af 644 (mbegin %store-monad
8df2eca6
LC
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)))))
aa72d9af 653
d938a58b
LC
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)))
e529d468
LC
660 (define (file=? file1 file2)
661 ;; Assume deduplication is in place.
8c7bebd6
LC
662 (= (stat:ino (stat file1))
663 (stat:ino (stat file2))))
e529d468 664
d938a58b 665 (mbegin %store-monad
8c7bebd6 666 (built-derivations (list (pk 'drv drv)))
d938a58b
LC
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
e529d468
LC
671 (and (file=? (string-append dir "/a/b/c") q-scm*)
672 (file=? (string-append dir "/p/q") plain*)))))))
d938a58b 673
0bb9929e
LC
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
932d1600
LC
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
2363bdd7
LC
702(test-equal "gexp-modules and literal Scheme object"
703 '()
704 (gexp-modules #t))
705
aa72d9af
LC
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
0bb9929e
LC
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
d938a58b
LC
764(test-assertm "gexp->derivation & with-imported-module & computed module"
765 (mlet* %store-monad
4fbd1a2b 766 ((module -> (scheme-file "x" #~(;; splice!
d938a58b
LC
767 (define-module (foo bar)
768 #:export (the-answer))
769
4fbd1a2b
LC
770 (define the-answer 42))
771 #:splice? #t))
d938a58b
LC
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
838e17d8
LC
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
2ca41030
LC
835(test-assertm "lower-gexp"
836 (mlet* %store-monad
837 ((extension -> %extension-package)
838 (extension-drv (package->derivation %extension-package))
839 (coreutils-drv (package->derivation coreutils))
840 (exp -> (with-extensions (list extension)
841 (with-imported-modules `((guix build utils))
842 #~(begin
843 (use-modules (guix build utils)
844 (hg2g))
845 #$coreutils:debug
846 mkdir-p
847 the-answer))))
848 (lexp (lower-gexp exp
849 #:effective-version "2.0")))
850 (define (matching-input drv output)
851 (lambda (input)
38685774
LC
852 (and (eq? (derivation-input-derivation input) drv)
853 (equal? (derivation-input-sub-derivations input)
854 (list output)))))
2ca41030
LC
855
856 (mbegin %store-monad
857 (return (and (find (matching-input extension-drv "out")
858 (lowered-gexp-inputs (pk 'lexp lexp)))
859 (find (matching-input coreutils-drv "debug")
860 (lowered-gexp-inputs lexp))
861 (member (string-append
862 (derivation->output-path extension-drv)
863 "/share/guile/site/2.0")
864 (lowered-gexp-load-path lexp))
865 (= 2 (length (lowered-gexp-load-path lexp)))
866 (member (string-append
867 (derivation->output-path extension-drv)
868 "/lib/guile/2.0/site-ccache")
869 (lowered-gexp-load-compiled-path lexp))
870 (= 2 (length (lowered-gexp-load-compiled-path lexp)))
b9373e26
LC
871 (eq? (derivation-input-derivation (lowered-gexp-guile lexp))
872 (%guile-for-build)))))))
2ca41030 873
b53833b2
LC
874(test-assertm "gexp->derivation #:references-graphs"
875 (mlet* %store-monad
72cd8ec0 876 ((one (text-file "one" (random-text)))
b53833b2
LC
877 (two (gexp->derivation "two"
878 #~(symlink #$one #$output:chbouib)))
66a35ceb 879 (build -> (with-imported-modules '((guix build store-copy)
d4e9317b
LC
880 (guix progress)
881 (guix records)
6892f0a2 882 (guix sets)
66a35ceb
LC
883 (guix build utils))
884 #~(begin
885 (use-modules (guix build store-copy))
886 (with-output-to-file #$output
887 (lambda ()
6892f0a2
LC
888 (write (map store-info-item
889 (call-with-input-file "guile"
890 read-reference-graph)))))
66a35ceb
LC
891 (with-output-to-file #$output:one
892 (lambda ()
6892f0a2
LC
893 (write (map store-info-item
894 (call-with-input-file "one"
895 read-reference-graph)))))
66a35ceb
LC
896 (with-output-to-file #$output:two
897 (lambda ()
6892f0a2
LC
898 (write (map store-info-item
899 (call-with-input-file "two"
900 read-reference-graph))))))))
66a35ceb 901 (drv (gexp->derivation "ref-graphs" build
b53833b2
LC
902 #:references-graphs `(("one" ,one)
903 ("two" ,two "chbouib")
66a35ceb 904 ("guile" ,%bootstrap-guile))))
b53833b2
LC
905 (ok? (built-derivations (list drv)))
906 (guile-drv (package->derivation %bootstrap-guile))
686784d0
LC
907 (bash (interned-file (search-bootstrap-binary "bash"
908 (%current-system))
909 "bash" #:recursive? #t))
b53833b2
LC
910 (g-one -> (derivation->output-path drv "one"))
911 (g-two -> (derivation->output-path drv "two"))
912 (g-guile -> (derivation->output-path drv)))
913 (return (and ok?
914 (equal? (call-with-input-file g-one read) (list one))
72cd8ec0
LC
915 (lset= string=?
916 (call-with-input-file g-two read)
917 (list one (derivation->output-path two "chbouib")))
686784d0
LC
918
919 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
72cd8ec0
LC
920 (lset= string=?
921 (call-with-input-file g-guile read)
922 (list (derivation->output-path guile-drv) bash))))))
b53833b2 923
c8351d9a
LC
924(test-assertm "gexp->derivation #:allowed-references"
925 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
926 #~(begin
927 (mkdir #$output)
928 (chdir #$output)
929 (symlink #$output "self")
930 (symlink #$%bootstrap-guile
931 "guile"))
932 #:allowed-references
933 (list "out" %bootstrap-guile))))
934 (built-derivations (list drv))))
935
accb682c
LC
936(test-assertm "gexp->derivation #:allowed-references, specific output"
937 (mlet* %store-monad ((in (gexp->derivation "thing"
938 #~(begin
939 (mkdir #$output:ok)
940 (mkdir #$output:not-ok))))
941 (drv (gexp->derivation "allowed-refs"
942 #~(begin
943 (pk #$in:not-ok)
944 (mkdir #$output)
945 (chdir #$output)
946 (symlink #$output "self")
947 (symlink #$in:ok "ok"))
948 #:allowed-references
949 (list "out"
950 (gexp-input in "ok")))))
951 (built-derivations (list drv))))
952
c8351d9a
LC
953(test-assert "gexp->derivation #:allowed-references, disallowed"
954 (let ((drv (run-with-store %store
955 (gexp->derivation "allowed-refs"
956 #~(begin
957 (mkdir #$output)
958 (chdir #$output)
959 (symlink #$%bootstrap-guile "guile"))
960 #:allowed-references '()))))
f9e8a123 961 (guard (c ((store-protocol-error? c) #t))
c8351d9a
LC
962 (build-derivations %store (list drv))
963 #f)))
964
3f4ecf32
LC
965(test-assertm "gexp->derivation #:disallowed-references, allowed"
966 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
967 #~(begin
968 (mkdir #$output)
969 (chdir #$output)
970 (symlink #$output "self")
971 (symlink #$%bootstrap-guile
972 "guile"))
973 #:disallowed-references '())))
974 (built-derivations (list drv))))
975
976
977(test-assert "gexp->derivation #:disallowed-references"
978 (let ((drv (run-with-store %store
979 (gexp->derivation "disallowed-refs"
980 #~(begin
981 (mkdir #$output)
982 (chdir #$output)
983 (symlink #$%bootstrap-guile "guile"))
984 #:disallowed-references (list %bootstrap-guile)))))
f9e8a123 985 (guard (c ((store-protocol-error? c) #t))
3f4ecf32
LC
986 (build-derivations %store (list drv))
987 #f)))
988
c17b5ab4 989(define shebang
c1bc358f 990 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
991 "/bin/guile --no-auto-compile"))
992
993;; If we're going to hit the silly shebang limit (128 chars on Linux-based
994;; systems), then skip the following test.
47b3124a 995(test-skip (if (> (string-length shebang) 127) 2 0))
c17b5ab4 996
21b679f6
LC
997(test-assertm "gexp->script"
998 (mlet* %store-monad ((n -> (random (expt 2 50)))
999 (exp -> (gexp
1000 (system*
1001 (string-append (ungexp %bootstrap-guile)
1002 "/bin/guile")
1003 "-c" (object->string
1004 '(display (expt (ungexp n) 2))))))
1005 (drv (gexp->script "guile-thing" exp
1006 #:guile %bootstrap-guile))
1007 (out -> (derivation->output-path drv))
1008 (done (built-derivations (list drv))))
1009 (let* ((pipe (open-input-pipe out))
1010 (str (get-string-all pipe)))
1011 (return (and (zero? (close-pipe pipe))
1012 (= (expt n 2) (string->number str)))))))
1013
92bcccc5 1014(test-assert "gexp->script #:module-path"
1ae16033
LC
1015 (call-with-temporary-directory
1016 (lambda (directory)
1017 (define str
1018 "Fake (guix base32) module!")
1019
1020 (mkdir (string-append directory "/guix"))
1021 (call-with-output-file (string-append directory "/guix/base32.scm")
1022 (lambda (port)
1023 (write `(begin (define-module (guix base32))
1024 (define-public %fake! ,str))
1025 port)))
1026
92bcccc5
LF
1027 (run-with-store %store
1028 (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
1029 (gexp (begin
1030 (use-modules (guix base32))
1031 (write (list %load-path
1032 %fake!))))))
1033 (drv (gexp->script "guile-thing" exp
1034 #:guile %bootstrap-guile
1035 #:module-path (list directory)))
1036 (out -> (derivation->output-path drv))
1037 (done (built-derivations (list drv))))
1038 (let* ((pipe (open-input-pipe out))
1039 (data (read pipe)))
1040 (return (and (zero? (close-pipe pipe))
1041 (match data
1042 ((load-path str*)
1043 (and (string=? str* str)
1044 (not (member directory load-path)))))))))))))
1ae16033 1045
15a01c72
LC
1046(test-assertm "program-file"
1047 (let* ((n (random (expt 2 50)))
0bb9929e
LC
1048 (exp (with-imported-modules '((guix build utils))
1049 (gexp (begin
1050 (use-modules (guix build utils))
1051 (display (ungexp n))))))
15a01c72 1052 (file (program-file "program" exp
15a01c72
LC
1053 #:guile %bootstrap-guile)))
1054 (mlet* %store-monad ((drv (lower-object file))
1055 (out -> (derivation->output-path drv)))
1056 (mbegin %store-monad
1057 (built-derivations (list drv))
1058 (let* ((pipe (open-input-pipe out))
1059 (str (get-string-all pipe)))
1060 (return (and (zero? (close-pipe pipe))
1061 (= n (string->number str)))))))))
1062
92bcccc5 1063(test-assert "program-file #:module-path"
427ec19e
LC
1064 (call-with-temporary-directory
1065 (lambda (directory)
1066 (define text (random-text))
1067
1068 (call-with-output-file (string-append directory "/stupid-module.scm")
1069 (lambda (port)
1070 (write `(begin (define-module (stupid-module))
1071 (define-public %stupid-thing ,text))
1072 port)))
1073
1074 (let* ((exp (with-imported-modules '((stupid-module))
1075 (gexp (begin
1076 (use-modules (stupid-module))
1077 (display %stupid-thing)))))
1078 (file (program-file "program" exp
1079 #:guile %bootstrap-guile
1080 #:module-path (list directory))))
92bcccc5
LF
1081 (run-with-store %store
1082 (mlet* %store-monad ((drv (lower-object file))
1083 (out -> (derivation->output-path drv)))
1084 (mbegin %store-monad
1085 (built-derivations (list drv))
1086 (let* ((pipe (open-input-pipe out))
1087 (str (get-string-all pipe)))
1088 (return (and (zero? (close-pipe pipe))
1089 (string=? text str)))))))))))
427ec19e 1090
838e17d8
LC
1091(test-assertm "program-file & with-extensions"
1092 (let* ((exp (with-extensions (list %extension-package)
1093 (gexp (begin
1094 (use-modules (hg2g))
1095 (display the-answer)))))
1096 (file (program-file "program" exp
1097 #:guile %bootstrap-guile)))
1098 (mlet* %store-monad ((drv (lower-object file))
1099 (out -> (derivation->output-path drv)))
1100 (mbegin %store-monad
1101 (built-derivations (list drv))
1102 (let* ((pipe (open-input-pipe out))
1103 (str (get-string-all pipe)))
1104 (return (and (zero? (close-pipe pipe))
1105 (= 42 (string->number str)))))))))
1106
e1c153e0
LC
1107(test-assertm "scheme-file"
1108 (let* ((text (plain-file "foo" "Hello, world!"))
1109 (scheme (scheme-file "bar" #~(list "foo" #$text))))
1110 (mlet* %store-monad ((drv (lower-object scheme))
1111 (text (lower-object text))
1112 (out -> (derivation->output-path drv)))
1113 (mbegin %store-monad
1114 (built-derivations (list drv))
e74f64b9 1115 (mlet %store-monad ((refs (references* out)))
e1c153e0
LC
1116 (return (and (equal? refs (list text))
1117 (equal? `(list "foo" ,text)
1118 (call-with-input-file out read)))))))))
1119
462a3fa3 1120(test-assert "text-file*"
e74f64b9
LC
1121 (run-with-store %store
1122 (mlet* %store-monad
1123 ((drv (package->derivation %bootstrap-guile))
1124 (guile -> (derivation->output-path drv))
1125 (file (text-file "bar" "This is bar."))
1126 (text (text-file* "foo"
1127 %bootstrap-guile "/bin/guile "
1128 (gexp-input %bootstrap-guile "out") "/bin/guile "
1129 drv "/bin/guile "
1130 file))
1131 (done (built-derivations (list text)))
1132 (out -> (derivation->output-path text))
1133 (refs (references* out)))
1134 ;; Make sure we get the right references and the right content.
1135 (return (and (lset= string=? refs (list guile file))
1136 (equal? (call-with-input-file out get-string-all)
1137 (string-append guile "/bin/guile "
1138 guile "/bin/guile "
1139 guile "/bin/guile "
1140 file)))))
1141 #:guile-for-build (package-derivation %store %bootstrap-guile)))
462a3fa3 1142
b751cde3
LC
1143(test-assertm "mixed-text-file"
1144 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
1145 "export PATH="
1146 %bootstrap-guile "/bin"))
1147 (drv (lower-object file))
1148 (out -> (derivation->output-path drv))
1149 (guile-drv (package->derivation %bootstrap-guile))
1150 (guile -> (derivation->output-path guile-drv)))
1151 (mbegin %store-monad
1152 (built-derivations (list drv))
e74f64b9 1153 (mlet %store-monad ((refs (references* out)))
b751cde3
LC
1154 (return (and (string=? (string-append "export PATH=" guile "/bin")
1155 (call-with-input-file out get-string-all))
1156 (equal? refs (list guile))))))))
1157
5dec93bb
LC
1158(test-assertm "file-union"
1159 (mlet* %store-monad ((union -> (file-union "union"
1160 `(("a" ,(plain-file "a" "1"))
1161 ("b/c/d" ,(plain-file "d" "2"))
1162 ("e" ,(plain-file "e" "3")))))
1163 (drv (lower-object union))
1164 (out -> (derivation->output-path drv)))
1165 (define (contents=? file str)
1166 (string=? (call-with-input-file (string-append out "/" file)
1167 get-string-all)
1168 str))
1169
1170 (mbegin %store-monad
1171 (built-derivations (list drv))
1172 (return (and (contents=? "a" "1")
1173 (contents=? "b/c/d" "2")
1174 (contents=? "e" "3"))))))
1175
a8afb9ae
LC
1176(test-assert "gexp->derivation vs. %current-target-system"
1177 (let ((mval (gexp->derivation "foo"
1178 #~(begin
1179 (mkdir #$output)
1180 (foo #+gnu-make))
1181 #:target #f)))
1182 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
1183 ;; influence.
1184 (parameterize ((%current-target-system "fooooo"))
1185 (derivation? (run-with-store %store mval)))))
1186
c2b84676
LC
1187(test-assertm "lower-object"
1188 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
1189 (drv2 (lower-object (package-source coreutils)))
1190 (item (lower-object (plain-file "foo" "Hello!"))))
1191 (return (and (derivation? drv1) (derivation? drv2)
1192 (store-path? item)))))
1193
91937029
LC
1194(test-assertm "lower-object, computed-file"
1195 (let* ((text (plain-file "foo" "Hello!"))
1196 (exp #~(begin
1197 (mkdir #$output)
1198 (symlink #$%bootstrap-guile
1199 (string-append #$output "/guile"))
1200 (symlink #$text (string-append #$output "/text"))))
1201 (computed (computed-file "computed" exp)))
1202 (mlet* %store-monad ((text (lower-object text))
1203 (guile-drv (lower-object %bootstrap-guile))
1204 (comp-drv (lower-object computed))
1205 (comp -> (derivation->output-path comp-drv)))
1206 (mbegin %store-monad
1207 (built-derivations (list comp-drv))
1208 (return (and (string=? (readlink (string-append comp "/guile"))
1209 (derivation->output-path guile-drv))
1210 (string=? (readlink (string-append comp "/text"))
1211 text)))))))
1212
9ec154f5
LC
1213(test-equal "lower-object, computed-file, #:system"
1214 '("mips64el-linux")
1215 (run-with-store %store
1216 (let* ((exp #~(symlink #$coreutils #$output))
1217 (computed (computed-file "computed" exp
1218 #:guile %bootstrap-guile)))
1219 ;; Make sure that the SYSTEM argument to 'lower-object' is honored.
1220 (mlet* %store-monad ((drv (lower-object computed "mips64el-linux"))
1221 (refs (references* (derivation-file-name drv))))
1222 (return (delete-duplicates
1223 (filter-map (lambda (file)
1224 (and (string-suffix? ".drv" file)
1225 (let ((drv (read-derivation-from-file
1226 file)))
1227 (derivation-system drv))))
1228 (cons (derivation-file-name drv)
1229 refs))))))))
1230
3e43166f
LC
1231(test-assert "lower-object & gexp-input-error?"
1232 (guard (c ((gexp-input-error? c)
1233 (gexp-error-invalid-input c)))
1234 (run-with-store %store
1235 (lower-object (current-module))
1236 #:guile-for-build (%guile-for-build))))
1237
2cf0ea0d
LC
1238(test-assert "printer"
1239 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
1240 \"/bin/uname\"\\) [[:xdigit:]]+>$"
1241 (with-output-to-string
1242 (lambda ()
1243 (write
1244 (gexp (string-append (ungexp coreutils)
1245 "/bin/uname")))))))
1246
1247(test-assert "printer vs. ungexp-splicing"
1248 (string-match "^#<gexp .* [[:xdigit:]]+>$"
1249 (with-output-to-string
1250 (lambda ()
1251 ;; #~(begin #$@#~())
1252 (write
1253 (gexp (begin (ungexp-splicing (gexp ())))))))))
1254
21b679f6
LC
1255(test-equal "sugar"
1256 '(gexp (foo (ungexp bar) (ungexp baz "out")
1257 (ungexp (chbouib 42))
667b2508
LC
1258 (ungexp-splicing (list x y z))
1259 (ungexp-native foo) (ungexp-native foo "out")
1260 (ungexp-native (chbouib 42))
1261 (ungexp-native-splicing (list x y z))))
1262 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
1263 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
1264
1265(test-end "gexp")
1266
21b679f6
LC
1267;; Local Variables:
1268;; eval: (put 'test-assertm 'scheme-indent-function 1)
1269;; End: