gnu: emacs-geiser-guile: Depend on Guile 3.0.
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
fc6d6aee 2;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
d9e0ae07 3;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
21b679f6
LC
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (test-gexp)
21 #:use-module (guix store)
22 #:use-module (guix monads)
23 #:use-module (guix gexp)
ef8de985 24 #:use-module (guix grafts)
21b679f6 25 #:use-module (guix derivations)
79c0c8cd 26 #:use-module (guix packages)
838e17d8 27 #:use-module (guix build-system trivial)
c1bc358f 28 #:use-module (guix tests)
4ff76a0a 29 #:use-module ((guix build utils) #:select (with-directory-excursion))
1ae16033 30 #:use-module ((guix utils) #:select (call-with-temporary-directory))
21b679f6
LC
31 #:use-module (gnu packages)
32 #:use-module (gnu packages base)
33 #:use-module (gnu packages bootstrap)
ca465a9c 34 #:use-module ((guix diagnostics) #:select (guix-warning-port))
21b679f6 35 #:use-module (srfi srfi-1)
c8351d9a 36 #:use-module (srfi srfi-34)
21b679f6
LC
37 #:use-module (srfi srfi-64)
38 #:use-module (rnrs io ports)
39 #:use-module (ice-9 match)
2cf0ea0d 40 #:use-module (ice-9 regex)
0687fc9c
LC
41 #:use-module (ice-9 popen)
42 #:use-module (ice-9 ftw))
21b679f6
LC
43
44;; Test the (guix gexp) module.
45
46(define %store
c1bc358f 47 (open-connection-for-tests))
21b679f6 48
ef8de985
LC
49;; Globally disable grafts because they can trigger early builds.
50(%graft? #f)
51
21b679f6 52;; For white-box testing.
1f976033
LC
53(define (gexp-inputs x)
54 ((@@ (guix gexp) gexp-inputs) x))
1f976033
LC
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)
b57de6fe 61 (run-with-store %store (gexp->sexp exp (%current-system) target)
c1bc358f 62 #:guile-for-build (%guile-for-build)))
21b679f6 63
fc6d6aee 64(define (gexp-input->tuple input)
4fa9d48f
LC
65 (list (gexp-input-thing input) (gexp-input-output input)
66 (gexp-input-native? input)))
fc6d6aee 67
838e17d8
LC
68(define %extension-package
69 ;; Example of a package to use when testing 'with-extensions'.
70 (dummy-package "extension"
71 (build-system trivial-build-system)
72 (arguments
73 `(#:guile ,%bootstrap-guile
74 #:modules ((guix build utils))
75 #:builder
76 (begin
77 (use-modules (guix build utils))
78 (let* ((out (string-append (assoc-ref %outputs "out")
79 "/share/guile/site/"
80 (effective-version))))
81 (mkdir-p out)
82 (call-with-output-file (string-append out "/hg2g.scm")
83 (lambda (port)
e033700f
LC
84 (define defmod 'define-module) ;fool Geiser
85 (write `(,defmod (hg2g)
838e17d8
LC
86 #:export (the-answer))
87 port)
88 (write '(define the-answer 42) port)))))))))
89
21b679f6
LC
90\f
91(test-begin "gexp")
92
d9e0ae07
MD
93(test-equal "no references"
94 '(display "hello gexp->approximate-sexp!")
95 (gexp->approximate-sexp #~(display "hello gexp->approximate-sexp!")))
96
97(test-equal "unquoted gexp"
98 '(display "hello")
99 (let ((inside #~"hello"))
100 (gexp->approximate-sexp #~(display #$inside))))
101
102(test-equal "unquoted gexp (native)"
103 '(display "hello")
104 (let ((inside #~"hello"))
105 (gexp->approximate-sexp #~(display #+inside))))
106
107(test-equal "spliced gexp"
108 '(display '(fresh vegetables))
109 (let ((inside #~(fresh vegetables)))
110 (gexp->approximate-sexp #~(display '(#$@inside)))))
111
112(test-equal "unspliced gexp, approximated"
113 ;; (*approximate*) is really an implementation detail
114 '(display '(*approximate*))
115 (let ((inside (file-append coreutils "/bin/hello")))
116 (gexp->approximate-sexp #~(display '(#$@inside)))))
117
118(test-equal "unquoted gexp, approximated"
119 '(display '(*approximate*))
120 (let ((inside (file-append coreutils "/bin/hello")))
121 (gexp->approximate-sexp #~(display '#$inside))))
122
21b679f6
LC
123(test-equal "no refs"
124 '(display "hello!")
125 (let ((exp (gexp (display "hello!"))))
126 (and (gexp? exp)
127 (null? (gexp-inputs exp))
128 (gexp->sexp* exp))))
129
da86e90e
LC
130(test-equal "sexp->gexp"
131 '(a b (c d) e)
132 (let ((exp (sexp->gexp '(a b (c d) e))))
133 (and (gexp? exp)
134 (null? (gexp-inputs exp))
135 (gexp->sexp* exp))))
136
21b679f6
LC
137(test-equal "unquote"
138 '(display `(foo ,(+ 2 3)))
139 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
140 (and (gexp? exp)
141 (null? (gexp-inputs exp))
142 (gexp->sexp* exp))))
143
144(test-assert "one input package"
145 (let ((exp (gexp (display (ungexp coreutils)))))
146 (and (gexp? exp)
147 (match (gexp-inputs exp)
fc6d6aee
LC
148 ((input)
149 (eq? (gexp-input-thing input) coreutils)))
21b679f6
LC
150 (equal? `(display ,(derivation->output-path
151 (package-derivation %store coreutils)))
152 (gexp->sexp* exp)))))
5e2e4a51
LC
153
154(test-assert "one input package, dotted list"
155 (let ((exp (gexp (coreutils . (ungexp coreutils)))))
156 (and (gexp? exp)
157 (match (gexp-inputs exp)
fc6d6aee
LC
158 ((input)
159 (eq? (gexp-input-thing input) coreutils)))
5e2e4a51
LC
160 (equal? `(coreutils . ,(derivation->output-path
161 (package-derivation %store coreutils)))
162 (gexp->sexp* exp)))))
21b679f6 163
79c0c8cd
LC
164(test-assert "one input origin"
165 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
166 (and (gexp? exp)
167 (match (gexp-inputs exp)
fc6d6aee
LC
168 ((input)
169 (and (eq? (gexp-input-thing input) (package-source coreutils))
170 (string=? (gexp-input-output input) "out"))))
79c0c8cd
LC
171 (equal? `(display ,(derivation->output-path
172 (package-source-derivation
173 %store (package-source coreutils))))
174 (gexp->sexp* exp)))))
175
d9ae938f
LC
176(test-assert "one local file"
177 (let* ((file (search-path %load-path "guix.scm"))
178 (local (local-file file))
179 (exp (gexp (display (ungexp local))))
020f3e41 180 (intd (add-to-store %store (basename file) #f
d9ae938f
LC
181 "sha256" file)))
182 (and (gexp? exp)
183 (match (gexp-inputs exp)
fc6d6aee
LC
184 ((input)
185 (and (eq? (gexp-input-thing input) local)
186 (string=? (gexp-input-output input) "out"))))
d9ae938f
LC
187 (equal? `(display ,intd) (gexp->sexp* exp)))))
188
7833db1f
LC
189(test-assert "one local file, symlink"
190 (let ((file (search-path %load-path "guix.scm"))
191 (link (tmpnam)))
192 (dynamic-wind
193 (const #t)
194 (lambda ()
195 (symlink (canonicalize-path file) link)
196 (let* ((local (local-file link "my-file" #:recursive? #f))
197 (exp (gexp (display (ungexp local))))
198 (intd (add-to-store %store "my-file" #f
199 "sha256" file)))
200 (and (gexp? exp)
201 (match (gexp-inputs exp)
fc6d6aee
LC
202 ((input)
203 (and (eq? (gexp-input-thing input) local)
204 (string=? (gexp-input-output input) "out"))))
7833db1f
LC
205 (equal? `(display ,intd) (gexp->sexp* exp)))))
206 (lambda ()
207 (false-if-exception (delete-file link))))))
208
4ff76a0a
LC
209(test-equal "local-file, relative file name"
210 (canonicalize-path (search-path %load-path "guix/base32.scm"))
211 (let ((directory (dirname (search-path %load-path
212 "guix/build-system/gnu.scm"))))
213 (with-directory-excursion directory
214 (let ((file (local-file "../guix/base32.scm")))
215 (local-file-absolute-file-name file)))))
216
99c45877
LC
217(test-equal "local-file, non-literal relative file name"
218 (canonicalize-path (search-path %load-path "guix/base32.scm"))
219 (let ((directory (dirname (search-path %load-path
220 "guix/build-system/gnu.scm"))))
221 (with-directory-excursion directory
222 (let ((file (local-file (string-copy "../base32.scm"))))
223 (local-file-absolute-file-name file)))))
224
0687fc9c
LC
225(test-assertm "local-file, #:select?"
226 (mlet* %store-monad ((select? -> (lambda (file stat)
227 (member (basename file)
228 '("guix.scm" "tests"
229 "gexp.scm"))))
230 (file -> (local-file ".." "directory"
231 #:recursive? #t
232 #:select? select?))
233 (dir (lower-object file)))
234 (return (and (store-path? dir)
235 (equal? (scandir dir)
236 '("." ".." "guix.scm" "tests"))
237 (equal? (scandir (string-append dir "/tests"))
238 '("." ".." "gexp.scm"))))))
239
558e8b11
LC
240(test-assert "one plain file"
241 (let* ((file (plain-file "hi" "Hello, world!"))
242 (exp (gexp (display (ungexp file))))
243 (expected (add-text-to-store %store "hi" "Hello, world!")))
244 (and (gexp? exp)
245 (match (gexp-inputs exp)
fc6d6aee
LC
246 ((input)
247 (and (eq? (gexp-input-thing input) file)
248 (string=? (gexp-input-output input) "out"))))
558e8b11
LC
249 (equal? `(display ,expected) (gexp->sexp* exp)))))
250
21b679f6
LC
251(test-assert "same input twice"
252 (let ((exp (gexp (begin
253 (display (ungexp coreutils))
254 (display (ungexp coreutils))))))
255 (and (gexp? exp)
256 (match (gexp-inputs exp)
fc6d6aee
LC
257 ((input)
258 (and (eq? (gexp-input-thing input) coreutils)
259 (string=? (gexp-input-output input) "out"))))
21b679f6
LC
260 (let ((e `(display ,(derivation->output-path
261 (package-derivation %store coreutils)))))
262 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
263
264(test-assert "two input packages, one derivation, one file"
265 (let* ((drv (build-expression->derivation
266 %store "foo" 'bar
267 #:guile-for-build (package-derivation %store %bootstrap-guile)))
268 (txt (add-text-to-store %store "foo" "Hello, world!"))
269 (exp (gexp (begin
270 (display (ungexp coreutils))
271 (display (ungexp %bootstrap-guile))
272 (display (ungexp drv))
273 (display (ungexp txt))))))
274 (define (match-input thing)
fc6d6aee
LC
275 (lambda (input)
276 (eq? (gexp-input-thing input) thing)))
21b679f6
LC
277
278 (and (gexp? exp)
279 (= 4 (length (gexp-inputs exp)))
280 (every (lambda (input)
281 (find (match-input input) (gexp-inputs exp)))
282 (list drv coreutils %bootstrap-guile txt))
283 (let ((e0 `(display ,(derivation->output-path
284 (package-derivation %store coreutils))))
285 (e1 `(display ,(derivation->output-path
286 (package-derivation %store %bootstrap-guile))))
287 (e2 `(display ,(derivation->output-path drv)))
288 (e3 `(display ,txt)))
289 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
290
a9e5e92f
LC
291(test-assert "file-append"
292 (let* ((drv (package-derivation %store %bootstrap-guile))
293 (fa (file-append %bootstrap-guile "/bin/guile"))
294 (exp #~(here we go #$fa)))
295 (and (match (gexp->sexp* exp)
296 (('here 'we 'go (? string? result))
297 (string=? result
298 (string-append (derivation->output-path drv)
299 "/bin/guile"))))
300 (match (gexp-inputs exp)
fc6d6aee
LC
301 ((input)
302 (and (eq? (gexp-input-thing input) fa)
303 (string=? (gexp-input-output input) "out")))))))
a9e5e92f
LC
304
305(test-assert "file-append, output"
306 (let* ((drv (package-derivation %store glibc))
307 (fa (file-append glibc "/lib" "/debug"))
308 (exp #~(foo #$fa:debug)))
309 (and (match (gexp->sexp* exp)
310 (('foo (? string? result))
311 (string=? result
312 (string-append (derivation->output-path drv "debug")
313 "/lib/debug"))))
314 (match (gexp-inputs exp)
fc6d6aee
LC
315 ((input)
316 (and (eq? (gexp-input-thing input) fa)
317 (string=? (gexp-input-output input) "debug")))))))
a9e5e92f
LC
318
319(test-assert "file-append, nested"
320 (let* ((drv (package-derivation %store glibc))
321 (dir (file-append glibc "/bin"))
322 (slash (file-append dir "/"))
323 (file (file-append slash "getent"))
324 (exp #~(foo #$file)))
325 (and (match (gexp->sexp* exp)
326 (('foo (? string? result))
327 (string=? result
328 (string-append (derivation->output-path drv)
329 "/bin/getent"))))
330 (match (gexp-inputs exp)
fc6d6aee
LC
331 ((input)
332 (eq? (gexp-input-thing input) file))))))
a9e5e92f 333
abf43d45
LC
334(test-assert "file-append, raw store item"
335 (let* ((obj (plain-file "example.txt" "Hello!"))
336 (a (file-append obj "/a"))
337 (b (file-append a "/b"))
338 (c (file-append b "/c"))
339 (exp #~(list #$c))
340 (item (run-with-store %store (lower-object obj)))
341 (lexp (run-with-store %store (lower-gexp exp))))
342 (and (equal? (lowered-gexp-sexp lexp)
343 `(list ,(string-append item "/a/b/c")))
344 (equal? (lowered-gexp-sources lexp)
345 (list item))
346 (null? (lowered-gexp-inputs lexp)))))
347
cf2ac04f
LC
348(test-assertm "with-parameters for %current-system"
349 (mlet* %store-monad ((system -> (match (%current-system)
350 ("aarch64-linux" "x86_64-linux")
351 (_ "aarch64-linux")))
352 (drv (package->derivation coreutils system))
353 (obj -> (with-parameters ((%current-system system))
354 coreutils))
355 (result (lower-object obj)))
356 (return (string=? (derivation-file-name drv)
357 (derivation-file-name result)))))
358
359(test-assertm "with-parameters for %current-target-system"
360 (mlet* %store-monad ((target -> "riscv64-linux-gnu")
361 (drv (package->cross-derivation coreutils target))
362 (obj -> (with-parameters
363 ((%current-target-system target))
364 coreutils))
365 (result (lower-object obj)))
366 (return (string=? (derivation-file-name drv)
367 (derivation-file-name result)))))
368
369(test-assert "with-parameters + file-append"
370 (let* ((system (match (%current-system)
371 ("aarch64-linux" "x86_64-linux")
372 (_ "aarch64-linux")))
373 (drv (package-derivation %store coreutils system))
374 (param (make-parameter 7))
375 (exp #~(here we go #$(with-parameters ((%current-system system)
376 (param 42))
377 (if (= (param) 42)
378 (file-append coreutils "/bin/touch")
379 %bootstrap-guile)))))
380 (match (gexp->sexp* exp)
381 (('here 'we 'go (? string? result))
382 (string=? result
383 (string-append (derivation->output-path drv)
384 "/bin/touch"))))))
644cb40c 385(test-equal "let-system"
4fa9d48f 386 (list `(begin ,(%current-system) #t) '(system-binding)
644cb40c
LC
387 'low '() '())
388 (let* ((exp #~(begin
389 #$(let-system system system)
390 #t))
391 (low (run-with-store %store (lower-gexp exp))))
392 (list (lowered-gexp-sexp low)
393 (match (gexp-inputs exp)
fc6d6aee
LC
394 ((input)
395 (and (eq? (struct-vtable (gexp-input-thing input))
396 (@@ (guix gexp) <system-binding>))
397 (string=? (gexp-input-output input) "out")
398 '(system-binding)))
644cb40c 399 (x x))
644cb40c
LC
400 'low
401 (lowered-gexp-inputs low)
402 (lowered-gexp-sources low))))
403
404(test-equal "let-system, target"
405 (list `(list ,(%current-system) #f)
406 `(list ,(%current-system) "aarch64-linux-gnu"))
407 (let ((exp #~(list #$@(let-system (system target)
408 (list system target)))))
409 (list (gexp->sexp* exp)
410 (gexp->sexp* exp "aarch64-linux-gnu"))))
411
412(test-equal "let-system, ungexp-native, target"
413 `(here it is: ,(%current-system) #f)
414 (let ((exp #~(here it is: #+@(let-system (system target)
415 (list system target)))))
416 (gexp->sexp* exp "aarch64-linux-gnu")))
417
418(test-equal "let-system, nested"
419 (list `(system* ,(string-append "qemu-system-" (%current-system))
420 "-m" "256")
644cb40c
LC
421 '(system-binding))
422 (let ((exp #~(system*
423 #+(let-system (system target)
424 (file-append (@@ (gnu packages virtualization)
425 qemu)
426 "/bin/qemu-system-"
427 system))
428 "-m" "256")))
429 (list (match (gexp->sexp* exp)
430 (('system* command rest ...)
431 `(system* ,(and (string-prefix? (%store-prefix) command)
432 (basename command))
433 ,@rest))
434 (x x))
4fa9d48f 435 (match (gexp-inputs exp)
fc6d6aee
LC
436 ((input)
437 (and (eq? (struct-vtable (gexp-input-thing input))
438 (@@ (guix gexp) <system-binding>))
439 (string=? (gexp-input-output input) "out")
4fa9d48f 440 (gexp-input-native? input)
fc6d6aee 441 '(system-binding)))
644cb40c 442 (x x)))))
cf2ac04f 443
667b2508
LC
444(test-assert "ungexp + ungexp-native"
445 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
446 (ungexp coreutils)
447 (ungexp-native glibc)
448 (ungexp binutils))))
449 (target "mips64el-linux")
450 (guile (derivation->output-path
451 (package-derivation %store %bootstrap-guile)))
452 (cu (derivation->output-path
453 (package-cross-derivation %store coreutils target)))
454 (libc (derivation->output-path
455 (package-derivation %store glibc)))
456 (bu (derivation->output-path
457 (package-cross-derivation %store binutils target))))
458 (and (lset= equal?
4fa9d48f
LC
459 `((,%bootstrap-guile "out" #t)
460 (,coreutils "out" #f)
461 (,glibc "out" #t)
462 (,binutils "out" #f))
fc6d6aee 463 (map gexp-input->tuple (gexp-inputs exp)))
667b2508
LC
464 (equal? `(list ,guile ,cu ,libc ,bu)
465 (gexp->sexp* exp target)))))
466
1123759b 467(test-equal "ungexp + ungexp-native, nested"
4fa9d48f 468 `((,%bootstrap-guile "out" #f) (,coreutils "out" #t))
1123759b
LC
469 (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
470 (ungexp %bootstrap-guile)))))
4fa9d48f 471 (map gexp-input->tuple (gexp-inputs exp))))
1123759b 472
5b14a790 473(test-equal "ungexp + ungexp-native, nested, special mixture"
4fa9d48f 474 `((,coreutils "out" #t))
5b14a790 475
5b14a790
LC
476 (let* ((foo (gexp (foo (ungexp-native coreutils))))
477 (exp (gexp (bar (ungexp foo)))))
4fa9d48f 478 (map gexp-input->tuple (gexp-inputs exp))))
5b14a790 479
21b679f6
LC
480(test-assert "input list"
481 (let ((exp (gexp (display
482 '(ungexp (list %bootstrap-guile coreutils)))))
483 (guile (derivation->output-path
484 (package-derivation %store %bootstrap-guile)))
485 (cu (derivation->output-path
486 (package-derivation %store coreutils))))
487 (and (lset= equal?
4fa9d48f 488 `((,%bootstrap-guile "out" #f) (,coreutils "out" #f))
fc6d6aee 489 (map gexp-input->tuple (gexp-inputs exp)))
21b679f6
LC
490 (equal? `(display '(,guile ,cu))
491 (gexp->sexp* exp)))))
492
667b2508
LC
493(test-assert "input list + ungexp-native"
494 (let* ((target "mips64el-linux")
495 (exp (gexp (display
496 (cons '(ungexp-native (list %bootstrap-guile coreutils))
497 '(ungexp (list glibc binutils))))))
498 (guile (derivation->output-path
499 (package-derivation %store %bootstrap-guile)))
500 (cu (derivation->output-path
501 (package-derivation %store coreutils)))
502 (xlibc (derivation->output-path
503 (package-cross-derivation %store glibc target)))
504 (xbu (derivation->output-path
505 (package-cross-derivation %store binutils target))))
506 (and (lset= equal?
4fa9d48f
LC
507 `((,%bootstrap-guile "out" #t) (,coreutils "out" #t)
508 (,glibc "out" #f) (,binutils "out" #f))
fc6d6aee 509 (map gexp-input->tuple (gexp-inputs exp)))
667b2508
LC
510 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
511 (gexp->sexp* exp target)))))
512
21b679f6 513(test-assert "input list splicing"
a482cfdc 514 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
21b679f6
LC
515 (outputs (list (derivation->output-path
516 (package-derivation %store glibc)
517 "debug")
518 (derivation->output-path
519 (package-derivation %store %bootstrap-guile))))
520 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
521 (and (lset= equal?
4fa9d48f 522 `((,glibc "debug" #f) (,%bootstrap-guile "out" #f))
fc6d6aee 523 (map gexp-input->tuple (gexp-inputs exp)))
21b679f6
LC
524 (equal? (gexp->sexp* exp)
525 `(list ,@(cons 5 outputs))))))
526
667b2508 527(test-assert "input list splicing + ungexp-native-splicing"
5b14a790
LC
528 (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
529 %bootstrap-guile))
0dbea56b
LC
530 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
531 (and (lset= equal?
4fa9d48f
LC
532 `((,glibc "debug" #t) (,%bootstrap-guile "out" #t))
533 (map gexp-input->tuple (gexp-inputs exp)))
0dbea56b
LC
534 (equal? (gexp->sexp* exp) ;native
535 (gexp->sexp* exp "mips64el-linux")))))
536
578dfbe0
LC
537(test-assert "gexp list splicing + ungexp-splicing"
538 (let* ((inner (gexp (ungexp-native glibc)))
539 (exp (gexp (list (ungexp-splicing (list inner))))))
4fa9d48f
LC
540 (and (equal? `((,glibc "out" #t))
541 (map gexp-input->tuple (gexp-inputs exp)))
578dfbe0
LC
542 (equal? (gexp->sexp* exp) ;native
543 (gexp->sexp* exp "mips64el-linux")))))
544
4b23c466
LC
545(test-equal "output list"
546 2
547 (let ((exp (gexp (begin (mkdir (ungexp output))
548 (mkdir (ungexp output "bar"))))))
549 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
550
551(test-assert "output list, combined gexps"
552 (let* ((exp0 (gexp (mkdir (ungexp output))))
553 (exp1 (gexp (mkdir (ungexp output "foo"))))
554 (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
555 (and (lset= equal?
556 (append (gexp-outputs exp0) (gexp-outputs exp1))
557 (gexp-outputs exp2))
558 (= 2 (length (gexp-outputs exp2))))))
559
7e75a673
LC
560(test-equal "output list, combined gexps, duplicate output"
561 1
562 (let* ((exp0 (gexp (mkdir (ungexp output))))
563 (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
564 (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
565 (length (gexp-outputs exp2))))
566
f9efe568
LC
567(test-assert "output list + ungexp-splicing list, combined gexps"
568 (let* ((exp0 (gexp (mkdir (ungexp output))))
569 (exp1 (gexp (mkdir (ungexp output "foo"))))
570 (exp2 (gexp (begin (display "hi!")
571 (ungexp-splicing (list exp0 exp1))))))
572 (and (lset= equal?
573 (append (gexp-outputs exp0) (gexp-outputs exp1))
574 (gexp-outputs exp2))
575 (= 2 (length (gexp-outputs exp2))))))
576
21b679f6
LC
577(test-assertm "gexp->file"
578 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
579 (guile (package-file %bootstrap-guile))
b57de6fe 580 (sexp (gexp->sexp exp (%current-system) #f))
21b679f6
LC
581 (drv (gexp->file "foo" exp))
582 (out -> (derivation->output-path drv))
583 (done (built-derivations (list drv)))
e74f64b9 584 (refs (references* out)))
21b679f6
LC
585 (return (and (equal? sexp (call-with-input-file out read))
586 (equal? (list guile) refs)))))
587
a9e5e92f
LC
588(test-assertm "gexp->file + file-append"
589 (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
590 "/bin/guile"))
591 (guile (package-file %bootstrap-guile))
592 (drv (gexp->file "foo" exp))
593 (out -> (derivation->output-path drv))
594 (done (built-derivations (list drv)))
e74f64b9 595 (refs (references* out)))
a9e5e92f
LC
596 (return (and (equal? (string-append guile "/bin/guile")
597 (call-with-input-file out read))
598 (equal? (list guile) refs)))))
599
4fbd1a2b
LC
600(test-assertm "gexp->file + #:splice?"
601 (mlet* %store-monad ((exp -> (list
602 #~(define foo 'bar)
603 #~(define guile #$%bootstrap-guile)))
604 (guile (package-file %bootstrap-guile))
605 (drv (gexp->file "splice" exp #:splice? #t))
606 (out -> (derivation->output-path drv))
607 (done (built-derivations (list drv)))
608 (refs (references* out)))
609 (pk 'splice out)
610 (return (and (equal? `((define foo 'bar)
611 (define guile ,guile)
612 ,(call-with-input-string "" read))
613 (call-with-input-file out
614 (lambda (port)
615 (list (read port) (read port) (read port)))))
616 (equal? (list guile) refs)))))
617
21b679f6
LC
618(test-assertm "gexp->derivation"
619 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
620 (exp -> (gexp
621 (begin
622 (mkdir (ungexp output))
623 (chdir (ungexp output))
624 (symlink
625 (string-append (ungexp %bootstrap-guile)
626 "/bin/guile")
627 "foo")
628 (symlink (ungexp file)
629 (ungexp output "2nd")))))
630 (drv (gexp->derivation "foo" exp))
631 (out -> (derivation->output-path drv))
632 (out2 -> (derivation->output-path drv "2nd"))
633 (done (built-derivations (list drv)))
e74f64b9
LC
634 (refs (references* out))
635 (refs2 (references* out2))
21b679f6
LC
636 (guile (package-file %bootstrap-guile "bin/guile")))
637 (return (and (string=? (readlink (string-append out "/foo")) guile)
638 (string=? (readlink out2) file)
639 (equal? refs (list (dirname (dirname guile))))
8856f409
LC
640 (equal? refs2 (list file))
641 (null? (derivation-properties drv))))))
642
643(test-assertm "gexp->derivation properties"
644 (mlet %store-monad ((drv (gexp->derivation "foo"
645 #~(mkdir #$output)
646 #:properties '((type . test)))))
647 (return (equal? '((type . test))
648 (derivation-properties drv)))))
21b679f6 649
ce45eb4c 650(test-assertm "gexp->derivation vs. grafts"
ef8de985
LC
651 (mlet* %store-monad ((graft? (set-grafting #f))
652 (p0 -> (dummy-package "dummy"
ce45eb4c
LC
653 (arguments
654 '(#:implicit-inputs? #f))))
655 (r -> (package (inherit p0) (name "DuMMY")))
656 (p1 -> (package (inherit p0) (replacement r)))
657 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
658 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
659 (void (set-guile-for-build %bootstrap-guile))
ef8de985
LC
660 (drv0 (gexp->derivation "t" exp0 #:graft? #t))
661 (drv1 (gexp->derivation "t" exp1 #:graft? #t))
662 (drv1* (gexp->derivation "t" exp1 #:graft? #f))
663 (_ (set-grafting graft?)))
ce45eb4c
LC
664 (return (and (not (string=? (derivation->output-path drv0)
665 (derivation->output-path drv1)))
666 (string=? (derivation->output-path drv0)
667 (derivation->output-path drv1*))))))
668
21b679f6
LC
669(test-assertm "gexp->derivation, composed gexps"
670 (mlet* %store-monad ((exp0 -> (gexp (begin
671 (mkdir (ungexp output))
672 (chdir (ungexp output)))))
673 (exp1 -> (gexp (symlink
674 (string-append (ungexp %bootstrap-guile)
675 "/bin/guile")
676 "foo")))
677 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
678 (drv (gexp->derivation "foo" exp))
679 (out -> (derivation->output-path drv))
680 (done (built-derivations (list drv)))
681 (guile (package-file %bootstrap-guile "bin/guile")))
682 (return (string=? (readlink (string-append out "/foo"))
683 guile))))
684
5d098459
LC
685(test-assertm "gexp->derivation, default system"
686 ;; The default system should be the one at '>>=' time, not the one at
687 ;; invocation time. See <http://bugs.gnu.org/18002>.
688 (let ((system (%current-system))
689 (mdrv (parameterize ((%current-system "foobar64-linux"))
690 (gexp->derivation "foo"
691 (gexp
692 (mkdir (ungexp output)))))))
693 (mlet %store-monad ((drv mdrv))
694 (return (string=? system (derivation-system drv))))))
695
d9ae938f
LC
696(test-assertm "gexp->derivation, local-file"
697 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
020f3e41 698 (intd (interned-file file #:recursive? #f))
d9ae938f
LC
699 (local -> (local-file file))
700 (exp -> (gexp (begin
701 (stat (ungexp local))
702 (symlink (ungexp local)
703 (ungexp output)))))
704 (drv (gexp->derivation "local-file" exp)))
705 (mbegin %store-monad
706 (built-derivations (list drv))
707 (return (string=? (readlink (derivation->output-path drv))
708 intd)))))
709
68a61e9f
LC
710(test-assertm "gexp->derivation, cross-compilation"
711 (mlet* %store-monad ((target -> "mips64el-linux")
712 (exp -> (gexp (list (ungexp coreutils)
713 (ungexp output))))
714 (xdrv (gexp->derivation "foo" exp
715 #:target target))
e74f64b9 716 (refs (references*
68a61e9f
LC
717 (derivation-file-name xdrv)))
718 (xcu (package->cross-derivation coreutils
719 target))
720 (cu (package->derivation coreutils)))
721 (return (and (member (derivation-file-name xcu) refs)
722 (not (member (derivation-file-name cu) refs))))))
723
667b2508
LC
724(test-assertm "gexp->derivation, ungexp-native"
725 (mlet* %store-monad ((target -> "mips64el-linux")
726 (exp -> (gexp (list (ungexp-native coreutils)
727 (ungexp output))))
728 (xdrv (gexp->derivation "foo" exp
729 #:target target))
730 (drv (gexp->derivation "foo" exp)))
731 (return (string=? (derivation-file-name drv)
732 (derivation-file-name xdrv)))))
733
734(test-assertm "gexp->derivation, ungexp + ungexp-native"
735 (mlet* %store-monad ((target -> "mips64el-linux")
736 (exp -> (gexp (list (ungexp-native coreutils)
737 (ungexp glibc)
738 (ungexp output))))
739 (xdrv (gexp->derivation "foo" exp
740 #:target target))
e74f64b9 741 (refs (references*
667b2508
LC
742 (derivation-file-name xdrv)))
743 (xglibc (package->cross-derivation glibc target))
744 (cu (package->derivation coreutils)))
745 (return (and (member (derivation-file-name cu) refs)
746 (member (derivation-file-name xglibc) refs)))))
747
748(test-assertm "gexp->derivation, ungexp-native + composed gexps"
749 (mlet* %store-monad ((target -> "mips64el-linux")
750 (exp0 -> (gexp (list 1 2
751 (ungexp coreutils))))
752 (exp -> (gexp (list 0 (ungexp-native exp0))))
753 (xdrv (gexp->derivation "foo" exp
754 #:target target))
755 (drv (gexp->derivation "foo" exp)))
756 (return (string=? (derivation-file-name drv)
757 (derivation-file-name xdrv)))))
758
6fd1a796
LC
759(test-assertm "gexp->derivation, store copy"
760 (let ((build-one #~(call-with-output-file #$output
761 (lambda (port)
762 (display "This is the one." port))))
763 (build-two (lambda (one)
764 #~(begin
765 (mkdir #$output)
766 (symlink #$one (string-append #$output "/one"))
767 (call-with-output-file (string-append #$output "/two")
768 (lambda (port)
769 (display "This is the second one." port))))))
b53833b2 770 (build-drv #~(begin
7b8d239e
LC
771 (use-modules (guix build store-copy)
772 (guix build utils)
773 (srfi srfi-1))
774
775 (define (canonical-file? file)
776 ;; Copied from (guix tests).
777 (let ((st (lstat file)))
778 (or (not (string-prefix? (%store-directory) file))
779 (eq? 'symlink (stat:type st))
780 (and (= 1 (stat:mtime st))
781 (zero? (logand #o222 (stat:mode st)))))))
6fd1a796 782
b53833b2 783 (mkdir #$output)
6a060ff2
LC
784 (populate-store '("graph") #$output
785 #:deduplicate? #f)
7b8d239e
LC
786
787 ;; Check whether 'populate-store' canonicalizes
788 ;; permissions and timestamps.
789 (unless (every canonical-file? (find-files #$output))
790 (error "not canonical!" #$output)))))
6fd1a796
LC
791 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
792 (two (gexp->derivation "two" (build-two one)))
b53833b2 793 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 794 #:references-graphs
b53833b2 795 `(("graph" ,two))
6fd1a796
LC
796 #:modules
797 '((guix build store-copy)
d4e9317b
LC
798 (guix progress)
799 (guix records)
6892f0a2 800 (guix sets)
6fd1a796
LC
801 (guix build utils))))
802 (ok? (built-derivations (list drv)))
803 (out -> (derivation->output-path drv)))
804 (let ((one (derivation->output-path one))
805 (two (derivation->output-path two)))
806 (return (and ok?
807 (file-exists? (string-append out "/" one))
808 (file-exists? (string-append out "/" two))
809 (file-exists? (string-append out "/" two "/two"))
810 (string=? (readlink (string-append out "/" two "/one"))
811 one)))))))
812
aa72d9af
LC
813(test-assertm "imported-files"
814 (mlet* %store-monad
815 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
816 ("a/b/c" . ,(search-path %load-path
817 "guix/derivations.scm"))
818 ("p/q" . ,(search-path %load-path "guix.scm"))
819 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
8df2eca6 820 (dir (imported-files files)))
aa72d9af 821 (mbegin %store-monad
8df2eca6
LC
822 (return
823 (every (match-lambda
824 ((path . source)
825 (equal? (call-with-input-file (string-append dir "/" path)
826 get-bytevector-all)
827 (call-with-input-file source
828 get-bytevector-all))))
829 files)))))
aa72d9af 830
d938a58b
LC
831(test-assertm "imported-files with file-like objects"
832 (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
833 (q-scm -> (search-path %load-path "ice-9/q.scm"))
834 (files -> `(("a/b/c" . ,q-scm)
835 ("p/q" . ,plain)))
836 (drv (imported-files files)))
e529d468
LC
837 (define (file=? file1 file2)
838 ;; Assume deduplication is in place.
8c7bebd6
LC
839 (= (stat:ino (stat file1))
840 (stat:ino (stat file2))))
e529d468 841
d938a58b 842 (mbegin %store-monad
8c7bebd6 843 (built-derivations (list (pk 'drv drv)))
d938a58b
LC
844 (mlet %store-monad ((dir -> (derivation->output-path drv))
845 (plain* (text-file "foo" "bar!"))
846 (q-scm* (interned-file q-scm "c")))
847 (return
e529d468
LC
848 (and (file=? (string-append dir "/a/b/c") q-scm*)
849 (file=? (string-append dir "/p/q") plain*)))))))
d938a58b 850
0bb9929e
LC
851(test-equal "gexp-modules & ungexp"
852 '((bar) (foo))
853 ((@@ (guix gexp) gexp-modules)
854 #~(foo #$(with-imported-modules '((foo)) #~+)
855 #+(with-imported-modules '((bar)) #~-))))
856
857(test-equal "gexp-modules & ungexp-splicing"
858 '((foo) (bar))
859 ((@@ (guix gexp) gexp-modules)
860 #~(foo #$@(list (with-imported-modules '((foo)) #~+)
861 (with-imported-modules '((bar)) #~-)))))
862
932d1600
LC
863(test-assert "gexp-modules deletes duplicates" ;<https://bugs.gnu.org/32966>
864 (let ((make-file (lambda ()
865 ;; Use 'eval' to make sure we get an object that's not
866 ;; 'eq?' nor 'equal?' due to the closures it embeds.
867 (eval '(scheme-file "bar.scm" #~(define-module (bar)))
868 (current-module)))))
869 (define result
870 ((@@ (guix gexp) gexp-modules)
871 (with-imported-modules `(((bar) => ,(make-file))
872 ((bar) => ,(make-file))
873 (foo) (foo))
874 #~+)))
875
876 (match result
877 (((('bar) '=> (? scheme-file?)) ('foo)) #t))))
878
2363bdd7
LC
879(test-equal "gexp-modules and literal Scheme object"
880 '()
881 (gexp-modules #t))
882
ca465a9c
LC
883(test-assert "gexp-modules, warning"
884 (string-match "tests/gexp.scm:[0-9]+:[0-9]+: warning: \
885importing.* \\(guix config\\) from the host"
886 (call-with-output-string
887 (lambda (port)
888 (parameterize ((guix-warning-port port))
889 (let* ((x (with-imported-modules '((guix config))
890 #~(+ 1 2 3)))
891 (y #~(+ 39 #$x)))
892 (gexp-modules y)))))))
893
aa72d9af
LC
894(test-assertm "gexp->derivation #:modules"
895 (mlet* %store-monad
896 ((build -> #~(begin
897 (use-modules (guix build utils))
898 (mkdir-p (string-append #$output "/guile/guix/nix"))
899 #t))
900 (drv (gexp->derivation "test-with-modules" build
901 #:modules '((guix build utils)))))
902 (mbegin %store-monad
903 (built-derivations (list drv))
904 (let* ((p (derivation->output-path drv))
905 (s (stat (string-append p "/guile/guix/nix"))))
906 (return (eq? (stat:type s) 'directory))))))
907
0bb9929e
LC
908(test-assertm "gexp->derivation & with-imported-modules"
909 ;; Same test as above, but using 'with-imported-modules'.
910 (mlet* %store-monad
911 ((build -> (with-imported-modules '((guix build utils))
912 #~(begin
913 (use-modules (guix build utils))
914 (mkdir-p (string-append #$output "/guile/guix/nix"))
915 #t)))
916 (drv (gexp->derivation "test-with-modules" build)))
917 (mbegin %store-monad
918 (built-derivations (list drv))
919 (let* ((p (derivation->output-path drv))
920 (s (stat (string-append p "/guile/guix/nix"))))
921 (return (eq? (stat:type s) 'directory))))))
922
923(test-assertm "gexp->derivation & nested with-imported-modules"
924 (mlet* %store-monad
925 ((build1 -> (with-imported-modules '((guix build utils))
926 #~(begin
927 (use-modules (guix build utils))
928 (mkdir-p (string-append #$output "/guile/guix/nix"))
929 #t)))
930 (build2 -> (with-imported-modules '((guix build bournish))
931 #~(begin
932 (use-modules (guix build bournish)
933 (system base compile))
934 #+build1
935 (call-with-output-file (string-append #$output "/b")
936 (lambda (port)
937 (write
938 (read-and-compile (open-input-string "cd /foo")
939 #:from %bournish-language
940 #:to 'scheme)
941 port))))))
942 (drv (gexp->derivation "test-with-modules" build2)))
943 (mbegin %store-monad
944 (built-derivations (list drv))
945 (let* ((p (derivation->output-path drv))
946 (s (stat (string-append p "/guile/guix/nix")))
947 (b (string-append p "/b")))
948 (return (and (eq? (stat:type s) 'directory)
949 (equal? '(chdir "/foo")
950 (call-with-input-file b read))))))))
951
d938a58b
LC
952(test-assertm "gexp->derivation & with-imported-module & computed module"
953 (mlet* %store-monad
4fbd1a2b 954 ((module -> (scheme-file "x" #~(;; splice!
d938a58b
LC
955 (define-module (foo bar)
956 #:export (the-answer))
957
4fbd1a2b
LC
958 (define the-answer 42))
959 #:splice? #t))
d938a58b
LC
960 (build -> (with-imported-modules `(((foo bar) => ,module)
961 (guix build utils))
962 #~(begin
963 (use-modules (guix build utils)
964 (foo bar))
965 mkdir-p
966 (call-with-output-file #$output
967 (lambda (port)
968 (write the-answer port))))))
969 (drv (gexp->derivation "thing" build))
970 (out -> (derivation->output-path drv)))
971 (mbegin %store-monad
972 (built-derivations (list drv))
973 (return (= 42 (call-with-input-file out read))))))
974
838e17d8
LC
975(test-equal "gexp-extensions & ungexp"
976 (list sed grep)
977 ((@@ (guix gexp) gexp-extensions)
978 #~(foo #$(with-extensions (list grep) #~+)
979 #+(with-extensions (list sed) #~-))))
980
981(test-equal "gexp-extensions & ungexp-splicing"
982 (list grep sed)
983 ((@@ (guix gexp) gexp-extensions)
984 #~(foo #$@(list (with-extensions (list grep) #~+)
985 (with-imported-modules '((foo))
986 (with-extensions (list sed) #~-))))))
987
988(test-equal "gexp-extensions and literal Scheme object"
989 '()
990 ((@@ (guix gexp) gexp-extensions) #t))
991
992(test-assertm "gexp->derivation & with-extensions"
993 ;; Create a fake Guile extension and make sure it is accessible both to the
994 ;; imported modules and to the derivation build script.
995 (mlet* %store-monad
996 ((extension -> %extension-package)
997 (module -> (scheme-file "x" #~( ;; splice!
998 (define-module (foo)
999 #:use-module (hg2g)
1000 #:export (multiply))
1001
1002 (define (multiply x)
1003 (* the-answer x)))
1004 #:splice? #t))
1005 (build -> (with-extensions (list extension)
1006 (with-imported-modules `((guix build utils)
1007 ((foo) => ,module))
1008 #~(begin
1009 (use-modules (guix build utils)
1010 (hg2g) (foo))
1011 (call-with-output-file #$output
1012 (lambda (port)
1013 (write (list the-answer (multiply 2))
1014 port)))))))
1015 (drv (gexp->derivation "thingie" build
1016 ;; %BOOTSTRAP-GUILE is 2.0.
1017 #:effective-version "2.0"))
1018 (out -> (derivation->output-path drv)))
1019 (mbegin %store-monad
1020 (built-derivations (list drv))
1021 (return (equal? '(42 84) (call-with-input-file out read))))))
1022
2ca41030
LC
1023(test-assertm "lower-gexp"
1024 (mlet* %store-monad
1025 ((extension -> %extension-package)
1026 (extension-drv (package->derivation %extension-package))
1027 (coreutils-drv (package->derivation coreutils))
1028 (exp -> (with-extensions (list extension)
1029 (with-imported-modules `((guix build utils))
1030 #~(begin
1031 (use-modules (guix build utils)
1032 (hg2g))
1033 #$coreutils:debug
1034 mkdir-p
1035 the-answer))))
1036 (lexp (lower-gexp exp
1037 #:effective-version "2.0")))
1038 (define (matching-input drv output)
1039 (lambda (input)
38685774
LC
1040 (and (eq? (derivation-input-derivation input) drv)
1041 (equal? (derivation-input-sub-derivations input)
1042 (list output)))))
2ca41030
LC
1043
1044 (mbegin %store-monad
1045 (return (and (find (matching-input extension-drv "out")
1046 (lowered-gexp-inputs (pk 'lexp lexp)))
1047 (find (matching-input coreutils-drv "debug")
1048 (lowered-gexp-inputs lexp))
1049 (member (string-append
1050 (derivation->output-path extension-drv)
1051 "/share/guile/site/2.0")
1052 (lowered-gexp-load-path lexp))
1053 (= 2 (length (lowered-gexp-load-path lexp)))
1054 (member (string-append
1055 (derivation->output-path extension-drv)
1056 "/lib/guile/2.0/site-ccache")
1057 (lowered-gexp-load-compiled-path lexp))
1058 (= 2 (length (lowered-gexp-load-compiled-path lexp)))
b9373e26
LC
1059 (eq? (derivation-input-derivation (lowered-gexp-guile lexp))
1060 (%guile-for-build)))))))
2ca41030 1061
d63ee94d
LC
1062(test-assertm "lower-gexp, raw-derivation-file"
1063 (mlet* %store-monad ((thing -> (program-file "prog" #~(display "hi!")))
1064 (exp -> #~(list #$(raw-derivation-file thing)))
1065 (drv (lower-object thing))
1066 (lexp (lower-gexp exp #:effective-version "2.0")))
1067 (return (and (equal? `(list ,(derivation-file-name drv))
1068 (lowered-gexp-sexp lexp))
1069 (equal? (list (derivation-file-name drv))
1070 (lowered-gexp-sources lexp))
1071 (null? (lowered-gexp-inputs lexp))))))
1072
24ab804c
LC
1073(test-eq "lower-gexp, non-self-quoting input"
1074 +
1075 (guard (c ((gexp-input-error? c)
1076 (gexp-error-invalid-input c)))
1077 (run-with-store %store
1078 (lower-gexp #~(foo #$+)))))
1079
ab7010af
MB
1080(test-equal "lower-gexp, character literal"
1081 '(#\+)
1082 (lowered-gexp-sexp
1083 (run-with-store %store
1084 (lower-gexp #~(#\+)))))
1085
b53833b2
LC
1086(test-assertm "gexp->derivation #:references-graphs"
1087 (mlet* %store-monad
72cd8ec0 1088 ((one (text-file "one" (random-text)))
b53833b2
LC
1089 (two (gexp->derivation "two"
1090 #~(symlink #$one #$output:chbouib)))
66a35ceb 1091 (build -> (with-imported-modules '((guix build store-copy)
d4e9317b
LC
1092 (guix progress)
1093 (guix records)
6892f0a2 1094 (guix sets)
66a35ceb
LC
1095 (guix build utils))
1096 #~(begin
1097 (use-modules (guix build store-copy))
1098 (with-output-to-file #$output
1099 (lambda ()
6892f0a2
LC
1100 (write (map store-info-item
1101 (call-with-input-file "guile"
1102 read-reference-graph)))))
66a35ceb
LC
1103 (with-output-to-file #$output:one
1104 (lambda ()
6892f0a2
LC
1105 (write (map store-info-item
1106 (call-with-input-file "one"
1107 read-reference-graph)))))
66a35ceb
LC
1108 (with-output-to-file #$output:two
1109 (lambda ()
6892f0a2
LC
1110 (write (map store-info-item
1111 (call-with-input-file "two"
1112 read-reference-graph))))))))
66a35ceb 1113 (drv (gexp->derivation "ref-graphs" build
b53833b2
LC
1114 #:references-graphs `(("one" ,one)
1115 ("two" ,two "chbouib")
66a35ceb 1116 ("guile" ,%bootstrap-guile))))
b53833b2
LC
1117 (ok? (built-derivations (list drv)))
1118 (guile-drv (package->derivation %bootstrap-guile))
686784d0
LC
1119 (bash (interned-file (search-bootstrap-binary "bash"
1120 (%current-system))
1121 "bash" #:recursive? #t))
b53833b2
LC
1122 (g-one -> (derivation->output-path drv "one"))
1123 (g-two -> (derivation->output-path drv "two"))
1124 (g-guile -> (derivation->output-path drv)))
1125 (return (and ok?
1126 (equal? (call-with-input-file g-one read) (list one))
72cd8ec0
LC
1127 (lset= string=?
1128 (call-with-input-file g-two read)
1129 (list one (derivation->output-path two "chbouib")))
686784d0
LC
1130
1131 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
72cd8ec0
LC
1132 (lset= string=?
1133 (call-with-input-file g-guile read)
1134 (list (derivation->output-path guile-drv) bash))))))
b53833b2 1135
9fc4e949
LC
1136(test-assertm "gexp->derivation #:references-graphs cross-compilation"
1137 ;; The objects passed in #:references-graphs implicitly refer to
1138 ;; cross-compiled derivations. Make sure this is the case.
1139 (mlet* %store-monad ((drv1 (lower-object coreutils (%current-system)
1140 #:target "i586-pc-gnu"))
1141 (drv2 (lower-object coreutils (%current-system)
1142 #:target #f))
1143 (drv3 (gexp->derivation "three"
1144 #~(symlink #$coreutils #$output)
1145 #:target "i586-pc-gnu"
1146 #:references-graphs
1147 `(("coreutils" ,coreutils))))
1148 (refs (references* (derivation-file-name drv3))))
1149 (return (and (member (derivation-file-name drv1) refs)
1150 (not (member (derivation-file-name drv2) refs))))))
1151
c8351d9a
LC
1152(test-assertm "gexp->derivation #:allowed-references"
1153 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
1154 #~(begin
1155 (mkdir #$output)
1156 (chdir #$output)
1157 (symlink #$output "self")
1158 (symlink #$%bootstrap-guile
1159 "guile"))
1160 #:allowed-references
1161 (list "out" %bootstrap-guile))))
1162 (built-derivations (list drv))))
1163
accb682c
LC
1164(test-assertm "gexp->derivation #:allowed-references, specific output"
1165 (mlet* %store-monad ((in (gexp->derivation "thing"
1166 #~(begin
1167 (mkdir #$output:ok)
1168 (mkdir #$output:not-ok))))
1169 (drv (gexp->derivation "allowed-refs"
1170 #~(begin
1171 (pk #$in:not-ok)
1172 (mkdir #$output)
1173 (chdir #$output)
1174 (symlink #$output "self")
1175 (symlink #$in:ok "ok"))
1176 #:allowed-references
1177 (list "out"
1178 (gexp-input in "ok")))))
1179 (built-derivations (list drv))))
1180
c8351d9a
LC
1181(test-assert "gexp->derivation #:allowed-references, disallowed"
1182 (let ((drv (run-with-store %store
1183 (gexp->derivation "allowed-refs"
1184 #~(begin
1185 (mkdir #$output)
1186 (chdir #$output)
1187 (symlink #$%bootstrap-guile "guile"))
1188 #:allowed-references '()))))
f9e8a123 1189 (guard (c ((store-protocol-error? c) #t))
c8351d9a
LC
1190 (build-derivations %store (list drv))
1191 #f)))
1192
3f4ecf32
LC
1193(test-assertm "gexp->derivation #:disallowed-references, allowed"
1194 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
1195 #~(begin
1196 (mkdir #$output)
1197 (chdir #$output)
1198 (symlink #$output "self")
1199 (symlink #$%bootstrap-guile
1200 "guile"))
1201 #:disallowed-references '())))
1202 (built-derivations (list drv))))
1203
1204
1205(test-assert "gexp->derivation #:disallowed-references"
1206 (let ((drv (run-with-store %store
1207 (gexp->derivation "disallowed-refs"
1208 #~(begin
1209 (mkdir #$output)
1210 (chdir #$output)
1211 (symlink #$%bootstrap-guile "guile"))
1212 #:disallowed-references (list %bootstrap-guile)))))
f9e8a123 1213 (guard (c ((store-protocol-error? c) #t))
3f4ecf32
LC
1214 (build-derivations %store (list drv))
1215 #f)))
1216
c17b5ab4 1217(define shebang
c1bc358f 1218 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
1219 "/bin/guile --no-auto-compile"))
1220
1221;; If we're going to hit the silly shebang limit (128 chars on Linux-based
1222;; systems), then skip the following test.
47b3124a 1223(test-skip (if (> (string-length shebang) 127) 2 0))
c17b5ab4 1224
21b679f6
LC
1225(test-assertm "gexp->script"
1226 (mlet* %store-monad ((n -> (random (expt 2 50)))
1227 (exp -> (gexp
1228 (system*
1229 (string-append (ungexp %bootstrap-guile)
1230 "/bin/guile")
1231 "-c" (object->string
1232 '(display (expt (ungexp n) 2))))))
1233 (drv (gexp->script "guile-thing" exp
1234 #:guile %bootstrap-guile))
1235 (out -> (derivation->output-path drv))
1236 (done (built-derivations (list drv))))
1237 (let* ((pipe (open-input-pipe out))
1238 (str (get-string-all pipe)))
1239 (return (and (zero? (close-pipe pipe))
1240 (= (expt n 2) (string->number str)))))))
1241
92bcccc5 1242(test-assert "gexp->script #:module-path"
1ae16033
LC
1243 (call-with-temporary-directory
1244 (lambda (directory)
1245 (define str
1246 "Fake (guix base32) module!")
1247
1248 (mkdir (string-append directory "/guix"))
1249 (call-with-output-file (string-append directory "/guix/base32.scm")
1250 (lambda (port)
1251 (write `(begin (define-module (guix base32))
1252 (define-public %fake! ,str))
1253 port)))
1254
92bcccc5
LF
1255 (run-with-store %store
1256 (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
1257 (gexp (begin
1258 (use-modules (guix base32))
1259 (write (list %load-path
1260 %fake!))))))
1261 (drv (gexp->script "guile-thing" exp
1262 #:guile %bootstrap-guile
1263 #:module-path (list directory)))
1264 (out -> (derivation->output-path drv))
1265 (done (built-derivations (list drv))))
1266 (let* ((pipe (open-input-pipe out))
1267 (data (read pipe)))
1268 (return (and (zero? (close-pipe pipe))
1269 (match data
1270 ((load-path str*)
1271 (and (string=? str* str)
1272 (not (member directory load-path)))))))))))))
1ae16033 1273
15a01c72
LC
1274(test-assertm "program-file"
1275 (let* ((n (random (expt 2 50)))
0bb9929e
LC
1276 (exp (with-imported-modules '((guix build utils))
1277 (gexp (begin
1278 (use-modules (guix build utils))
1279 (display (ungexp n))))))
15a01c72 1280 (file (program-file "program" exp
15a01c72
LC
1281 #:guile %bootstrap-guile)))
1282 (mlet* %store-monad ((drv (lower-object file))
1283 (out -> (derivation->output-path drv)))
1284 (mbegin %store-monad
1285 (built-derivations (list drv))
1286 (let* ((pipe (open-input-pipe out))
1287 (str (get-string-all pipe)))
1288 (return (and (zero? (close-pipe pipe))
1289 (= n (string->number str)))))))))
1290
92bcccc5 1291(test-assert "program-file #:module-path"
427ec19e
LC
1292 (call-with-temporary-directory
1293 (lambda (directory)
1294 (define text (random-text))
1295
1296 (call-with-output-file (string-append directory "/stupid-module.scm")
1297 (lambda (port)
1298 (write `(begin (define-module (stupid-module))
1299 (define-public %stupid-thing ,text))
1300 port)))
1301
1302 (let* ((exp (with-imported-modules '((stupid-module))
1303 (gexp (begin
1304 (use-modules (stupid-module))
1305 (display %stupid-thing)))))
1306 (file (program-file "program" exp
1307 #:guile %bootstrap-guile
1308 #:module-path (list directory))))
92bcccc5
LF
1309 (run-with-store %store
1310 (mlet* %store-monad ((drv (lower-object file))
1311 (out -> (derivation->output-path drv)))
1312 (mbegin %store-monad
1313 (built-derivations (list drv))
1314 (let* ((pipe (open-input-pipe out))
1315 (str (get-string-all pipe)))
1316 (return (and (zero? (close-pipe pipe))
1317 (string=? text str)))))))))))
427ec19e 1318
838e17d8
LC
1319(test-assertm "program-file & with-extensions"
1320 (let* ((exp (with-extensions (list %extension-package)
1321 (gexp (begin
1322 (use-modules (hg2g))
1323 (display the-answer)))))
1324 (file (program-file "program" exp
1325 #:guile %bootstrap-guile)))
1326 (mlet* %store-monad ((drv (lower-object file))
1327 (out -> (derivation->output-path drv)))
1328 (mbegin %store-monad
1329 (built-derivations (list drv))
1330 (let* ((pipe (open-input-pipe out))
1331 (str (get-string-all pipe)))
1332 (return (and (zero? (close-pipe pipe))
1333 (= 42 (string->number str)))))))))
1334
2e8cabb8
LC
1335(test-assertm "program-file #:system"
1336 (let* ((exp (with-imported-modules '((guix build utils))
1337 (gexp (begin
1338 (use-modules (guix build utils))
1339 (display "hi!")))))
1340 (system (if (string=? (%current-system) "x86_64-linux")
1341 "armhf-linux"
1342 "x86_64-linux"))
1343 (file (program-file "program" exp)))
1344 (mlet %store-monad ((drv (lower-object file system)))
1345 (return (and (string=? (derivation-system drv) system)
1346 (find (lambda (input)
1347 (let ((drv (pk (derivation-input-derivation input))))
1348 (and (string=? (derivation-name drv)
1349 "module-import-compiled")
1350 (string=? (derivation-system drv)
1351 system))))
1352 (derivation-inputs drv)))))))
1353
e1c153e0
LC
1354(test-assertm "scheme-file"
1355 (let* ((text (plain-file "foo" "Hello, world!"))
1356 (scheme (scheme-file "bar" #~(list "foo" #$text))))
1357 (mlet* %store-monad ((drv (lower-object scheme))
1358 (text (lower-object text))
1359 (out -> (derivation->output-path drv)))
1360 (mbegin %store-monad
1361 (built-derivations (list drv))
e74f64b9 1362 (mlet %store-monad ((refs (references* out)))
e1c153e0
LC
1363 (return (and (equal? refs (list text))
1364 (equal? `(list "foo" ,text)
1365 (call-with-input-file out read)))))))))
1366
d63ee94d
LC
1367(test-assertm "raw-derivation-file"
1368 (let* ((exp #~(let ((drv #$(raw-derivation-file coreutils)))
1369 (when (file-exists? drv)
1370 (symlink drv #$output)))))
1371 (mlet* %store-monad ((dep (lower-object coreutils))
1372 (drv (gexp->derivation "drv-ref" exp))
1373 (out -> (derivation->output-path drv)))
1374 (mbegin %store-monad
1375 (built-derivations (list drv))
1376 (mlet %store-monad ((refs (references* out)))
1377 (return (and (member (derivation-file-name dep)
1378 (derivation-sources drv))
1379 (not (member (derivation-file-name dep)
1380 (map derivation-input-path
1381 (derivation-inputs drv))))
1382 (equal? (readlink out) (derivation-file-name dep))
1383 (equal? refs (list (derivation-file-name dep))))))))))
1384
462a3fa3 1385(test-assert "text-file*"
e74f64b9
LC
1386 (run-with-store %store
1387 (mlet* %store-monad
1388 ((drv (package->derivation %bootstrap-guile))
1389 (guile -> (derivation->output-path drv))
1390 (file (text-file "bar" "This is bar."))
1391 (text (text-file* "foo"
1392 %bootstrap-guile "/bin/guile "
1393 (gexp-input %bootstrap-guile "out") "/bin/guile "
1394 drv "/bin/guile "
1395 file))
1396 (done (built-derivations (list text)))
1397 (out -> (derivation->output-path text))
1398 (refs (references* out)))
1399 ;; Make sure we get the right references and the right content.
1400 (return (and (lset= string=? refs (list guile file))
1401 (equal? (call-with-input-file out get-string-all)
1402 (string-append guile "/bin/guile "
1403 guile "/bin/guile "
1404 guile "/bin/guile "
1405 file)))))
1406 #:guile-for-build (package-derivation %store %bootstrap-guile)))
462a3fa3 1407
b751cde3
LC
1408(test-assertm "mixed-text-file"
1409 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
1410 "export PATH="
1411 %bootstrap-guile "/bin"))
1412 (drv (lower-object file))
1413 (out -> (derivation->output-path drv))
1414 (guile-drv (package->derivation %bootstrap-guile))
1415 (guile -> (derivation->output-path guile-drv)))
1416 (mbegin %store-monad
1417 (built-derivations (list drv))
e74f64b9 1418 (mlet %store-monad ((refs (references* out)))
b751cde3
LC
1419 (return (and (string=? (string-append "export PATH=" guile "/bin")
1420 (call-with-input-file out get-string-all))
1421 (equal? refs (list guile))))))))
1422
5dec93bb
LC
1423(test-assertm "file-union"
1424 (mlet* %store-monad ((union -> (file-union "union"
1425 `(("a" ,(plain-file "a" "1"))
1426 ("b/c/d" ,(plain-file "d" "2"))
1427 ("e" ,(plain-file "e" "3")))))
1428 (drv (lower-object union))
1429 (out -> (derivation->output-path drv)))
1430 (define (contents=? file str)
1431 (string=? (call-with-input-file (string-append out "/" file)
1432 get-string-all)
1433 str))
1434
1435 (mbegin %store-monad
1436 (built-derivations (list drv))
1437 (return (and (contents=? "a" "1")
1438 (contents=? "b/c/d" "2")
1439 (contents=? "e" "3"))))))
1440
a8afb9ae
LC
1441(test-assert "gexp->derivation vs. %current-target-system"
1442 (let ((mval (gexp->derivation "foo"
1443 #~(begin
1444 (mkdir #$output)
1445 (foo #+gnu-make))
1446 #:target #f)))
1447 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
1448 ;; influence.
1449 (parameterize ((%current-target-system "fooooo"))
1450 (derivation? (run-with-store %store mval)))))
1451
c2b84676
LC
1452(test-assertm "lower-object"
1453 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
1454 (drv2 (lower-object (package-source coreutils)))
1455 (item (lower-object (plain-file "foo" "Hello!"))))
1456 (return (and (derivation? drv1) (derivation? drv2)
1457 (store-path? item)))))
1458
91937029
LC
1459(test-assertm "lower-object, computed-file"
1460 (let* ((text (plain-file "foo" "Hello!"))
1461 (exp #~(begin
1462 (mkdir #$output)
1463 (symlink #$%bootstrap-guile
1464 (string-append #$output "/guile"))
1465 (symlink #$text (string-append #$output "/text"))))
1466 (computed (computed-file "computed" exp)))
1467 (mlet* %store-monad ((text (lower-object text))
1468 (guile-drv (lower-object %bootstrap-guile))
1469 (comp-drv (lower-object computed))
1470 (comp -> (derivation->output-path comp-drv)))
1471 (mbegin %store-monad
1472 (built-derivations (list comp-drv))
1473 (return (and (string=? (readlink (string-append comp "/guile"))
1474 (derivation->output-path guile-drv))
1475 (string=? (readlink (string-append comp "/text"))
1476 text)))))))
1477
9ec154f5
LC
1478(test-equal "lower-object, computed-file, #:system"
1479 '("mips64el-linux")
1480 (run-with-store %store
1481 (let* ((exp #~(symlink #$coreutils #$output))
1482 (computed (computed-file "computed" exp
1483 #:guile %bootstrap-guile)))
1484 ;; Make sure that the SYSTEM argument to 'lower-object' is honored.
1485 (mlet* %store-monad ((drv (lower-object computed "mips64el-linux"))
1486 (refs (references* (derivation-file-name drv))))
1487 (return (delete-duplicates
1488 (filter-map (lambda (file)
1489 (and (string-suffix? ".drv" file)
1490 (let ((drv (read-derivation-from-file
1491 file)))
1492 (derivation-system drv))))
1493 (cons (derivation-file-name drv)
1494 refs))))))))
1495
3e43166f
LC
1496(test-assert "lower-object & gexp-input-error?"
1497 (guard (c ((gexp-input-error? c)
1498 (gexp-error-invalid-input c)))
1499 (run-with-store %store
1500 (lower-object (current-module))
1501 #:guile-for-build (%guile-for-build))))
1502
2cf0ea0d
LC
1503(test-assert "printer"
1504 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
18fc84bc 1505 \"/bin/uname\"\\) [[:graph:]]+tests/gexp\\.scm:[0-9]+:[0-9]+ [[:xdigit:]]+>$"
2cf0ea0d
LC
1506 (with-output-to-string
1507 (lambda ()
1508 (write
1509 (gexp (string-append (ungexp coreutils)
1510 "/bin/uname")))))))
1511
1512(test-assert "printer vs. ungexp-splicing"
1513 (string-match "^#<gexp .* [[:xdigit:]]+>$"
1514 (with-output-to-string
1515 (lambda ()
1516 ;; #~(begin #$@#~())
1517 (write
1518 (gexp (begin (ungexp-splicing (gexp ())))))))))
1519
21b679f6
LC
1520(test-equal "sugar"
1521 '(gexp (foo (ungexp bar) (ungexp baz "out")
1522 (ungexp (chbouib 42))
667b2508
LC
1523 (ungexp-splicing (list x y z))
1524 (ungexp-native foo) (ungexp-native foo "out")
1525 (ungexp-native (chbouib 42))
1526 (ungexp-native-splicing (list x y z))))
1527 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
1528 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6 1529
9a2f99f4
MO
1530(test-assertm "gexp->file, cross-compilation"
1531 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1532 (exp -> (gexp (list (ungexp coreutils))))
1533 (xdrv (gexp->file "foo" exp #:target target))
1534 (refs (references*
1535 (derivation-file-name xdrv)))
1536 (xcu (package->cross-derivation coreutils
1537 target))
1538 (cu (package->derivation coreutils)))
1539 (return (and (member (derivation-file-name xcu) refs)
1540 (not (member (derivation-file-name cu) refs))))))
1541
1542(test-assertm "gexp->file, cross-compilation with default target"
1543 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1544 (_ (set-current-target target))
1545 (exp -> (gexp (list (ungexp coreutils))))
1546 (xdrv (gexp->file "foo" exp))
1547 (refs (references*
1548 (derivation-file-name xdrv)))
1549 (xcu (package->cross-derivation coreutils
1550 target))
1551 (cu (package->derivation coreutils)))
1552 (return (and (member (derivation-file-name xcu) refs)
1553 (not (member (derivation-file-name cu) refs))))))
1554
1555(test-assertm "gexp->script, cross-compilation"
1556 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1557 (exp -> (gexp (list (ungexp coreutils))))
1558 (xdrv (gexp->script "foo" exp #:target target))
1559 (refs (references*
1560 (derivation-file-name xdrv)))
1561 (xcu (package->cross-derivation coreutils
1562 target))
1563 (cu (package->derivation coreutils)))
1564 (return (and (member (derivation-file-name xcu) refs)
1565 (not (member (derivation-file-name cu) refs))))))
1566
1567(test-assertm "gexp->script, cross-compilation with default target"
1568 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1569 (_ (set-current-target target))
1570 (exp -> (gexp (list (ungexp coreutils))))
1571 (xdrv (gexp->script "foo" exp))
1572 (refs (references*
1573 (derivation-file-name xdrv)))
1574 (xcu (package->cross-derivation coreutils
1575 target))
1576 (cu (package->derivation coreutils)))
1577 (return (and (member (derivation-file-name xcu) refs)
1578 (not (member (derivation-file-name cu) refs))))))
1579
21b679f6
LC
1580(test-end "gexp")
1581
21b679f6
LC
1582;; Local Variables:
1583;; eval: (put 'test-assertm 'scheme-indent-function 1)
1584;; End: