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