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