store-copy: 'populate-store' resets timestamps.
[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)
7b8d239e
LC
739 (populate-store '("graph") #$output)
740
741 ;; Check whether 'populate-store' canonicalizes
742 ;; permissions and timestamps.
743 (unless (every canonical-file? (find-files #$output))
744 (error "not canonical!" #$output)))))
6fd1a796
LC
745 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
746 (two (gexp->derivation "two" (build-two one)))
b53833b2 747 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 748 #:references-graphs
b53833b2 749 `(("graph" ,two))
6fd1a796
LC
750 #:modules
751 '((guix build store-copy)
d4e9317b
LC
752 (guix progress)
753 (guix records)
6892f0a2 754 (guix sets)
6fd1a796
LC
755 (guix build utils))))
756 (ok? (built-derivations (list drv)))
757 (out -> (derivation->output-path drv)))
758 (let ((one (derivation->output-path one))
759 (two (derivation->output-path two)))
760 (return (and ok?
761 (file-exists? (string-append out "/" one))
762 (file-exists? (string-append out "/" two))
763 (file-exists? (string-append out "/" two "/two"))
764 (string=? (readlink (string-append out "/" two "/one"))
765 one)))))))
766
aa72d9af
LC
767(test-assertm "imported-files"
768 (mlet* %store-monad
769 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
770 ("a/b/c" . ,(search-path %load-path
771 "guix/derivations.scm"))
772 ("p/q" . ,(search-path %load-path "guix.scm"))
773 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
8df2eca6 774 (dir (imported-files files)))
aa72d9af 775 (mbegin %store-monad
8df2eca6
LC
776 (return
777 (every (match-lambda
778 ((path . source)
779 (equal? (call-with-input-file (string-append dir "/" path)
780 get-bytevector-all)
781 (call-with-input-file source
782 get-bytevector-all))))
783 files)))))
aa72d9af 784
d938a58b
LC
785(test-assertm "imported-files with file-like objects"
786 (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
787 (q-scm -> (search-path %load-path "ice-9/q.scm"))
788 (files -> `(("a/b/c" . ,q-scm)
789 ("p/q" . ,plain)))
790 (drv (imported-files files)))
e529d468
LC
791 (define (file=? file1 file2)
792 ;; Assume deduplication is in place.
8c7bebd6
LC
793 (= (stat:ino (stat file1))
794 (stat:ino (stat file2))))
e529d468 795
d938a58b 796 (mbegin %store-monad
8c7bebd6 797 (built-derivations (list (pk 'drv drv)))
d938a58b
LC
798 (mlet %store-monad ((dir -> (derivation->output-path drv))
799 (plain* (text-file "foo" "bar!"))
800 (q-scm* (interned-file q-scm "c")))
801 (return
e529d468
LC
802 (and (file=? (string-append dir "/a/b/c") q-scm*)
803 (file=? (string-append dir "/p/q") plain*)))))))
d938a58b 804
0bb9929e
LC
805(test-equal "gexp-modules & ungexp"
806 '((bar) (foo))
807 ((@@ (guix gexp) gexp-modules)
808 #~(foo #$(with-imported-modules '((foo)) #~+)
809 #+(with-imported-modules '((bar)) #~-))))
810
811(test-equal "gexp-modules & ungexp-splicing"
812 '((foo) (bar))
813 ((@@ (guix gexp) gexp-modules)
814 #~(foo #$@(list (with-imported-modules '((foo)) #~+)
815 (with-imported-modules '((bar)) #~-)))))
816
932d1600
LC
817(test-assert "gexp-modules deletes duplicates" ;<https://bugs.gnu.org/32966>
818 (let ((make-file (lambda ()
819 ;; Use 'eval' to make sure we get an object that's not
820 ;; 'eq?' nor 'equal?' due to the closures it embeds.
821 (eval '(scheme-file "bar.scm" #~(define-module (bar)))
822 (current-module)))))
823 (define result
824 ((@@ (guix gexp) gexp-modules)
825 (with-imported-modules `(((bar) => ,(make-file))
826 ((bar) => ,(make-file))
827 (foo) (foo))
828 #~+)))
829
830 (match result
831 (((('bar) '=> (? scheme-file?)) ('foo)) #t))))
832
2363bdd7
LC
833(test-equal "gexp-modules and literal Scheme object"
834 '()
835 (gexp-modules #t))
836
ca465a9c
LC
837(test-assert "gexp-modules, warning"
838 (string-match "tests/gexp.scm:[0-9]+:[0-9]+: warning: \
839importing.* \\(guix config\\) from the host"
840 (call-with-output-string
841 (lambda (port)
842 (parameterize ((guix-warning-port port))
843 (let* ((x (with-imported-modules '((guix config))
844 #~(+ 1 2 3)))
845 (y #~(+ 39 #$x)))
846 (gexp-modules y)))))))
847
aa72d9af
LC
848(test-assertm "gexp->derivation #:modules"
849 (mlet* %store-monad
850 ((build -> #~(begin
851 (use-modules (guix build utils))
852 (mkdir-p (string-append #$output "/guile/guix/nix"))
853 #t))
854 (drv (gexp->derivation "test-with-modules" build
855 #:modules '((guix build utils)))))
856 (mbegin %store-monad
857 (built-derivations (list drv))
858 (let* ((p (derivation->output-path drv))
859 (s (stat (string-append p "/guile/guix/nix"))))
860 (return (eq? (stat:type s) 'directory))))))
861
0bb9929e
LC
862(test-assertm "gexp->derivation & with-imported-modules"
863 ;; Same test as above, but using 'with-imported-modules'.
864 (mlet* %store-monad
865 ((build -> (with-imported-modules '((guix build utils))
866 #~(begin
867 (use-modules (guix build utils))
868 (mkdir-p (string-append #$output "/guile/guix/nix"))
869 #t)))
870 (drv (gexp->derivation "test-with-modules" build)))
871 (mbegin %store-monad
872 (built-derivations (list drv))
873 (let* ((p (derivation->output-path drv))
874 (s (stat (string-append p "/guile/guix/nix"))))
875 (return (eq? (stat:type s) 'directory))))))
876
877(test-assertm "gexp->derivation & nested with-imported-modules"
878 (mlet* %store-monad
879 ((build1 -> (with-imported-modules '((guix build utils))
880 #~(begin
881 (use-modules (guix build utils))
882 (mkdir-p (string-append #$output "/guile/guix/nix"))
883 #t)))
884 (build2 -> (with-imported-modules '((guix build bournish))
885 #~(begin
886 (use-modules (guix build bournish)
887 (system base compile))
888 #+build1
889 (call-with-output-file (string-append #$output "/b")
890 (lambda (port)
891 (write
892 (read-and-compile (open-input-string "cd /foo")
893 #:from %bournish-language
894 #:to 'scheme)
895 port))))))
896 (drv (gexp->derivation "test-with-modules" build2)))
897 (mbegin %store-monad
898 (built-derivations (list drv))
899 (let* ((p (derivation->output-path drv))
900 (s (stat (string-append p "/guile/guix/nix")))
901 (b (string-append p "/b")))
902 (return (and (eq? (stat:type s) 'directory)
903 (equal? '(chdir "/foo")
904 (call-with-input-file b read))))))))
905
d938a58b
LC
906(test-assertm "gexp->derivation & with-imported-module & computed module"
907 (mlet* %store-monad
4fbd1a2b 908 ((module -> (scheme-file "x" #~(;; splice!
d938a58b
LC
909 (define-module (foo bar)
910 #:export (the-answer))
911
4fbd1a2b
LC
912 (define the-answer 42))
913 #:splice? #t))
d938a58b
LC
914 (build -> (with-imported-modules `(((foo bar) => ,module)
915 (guix build utils))
916 #~(begin
917 (use-modules (guix build utils)
918 (foo bar))
919 mkdir-p
920 (call-with-output-file #$output
921 (lambda (port)
922 (write the-answer port))))))
923 (drv (gexp->derivation "thing" build))
924 (out -> (derivation->output-path drv)))
925 (mbegin %store-monad
926 (built-derivations (list drv))
927 (return (= 42 (call-with-input-file out read))))))
928
838e17d8
LC
929(test-equal "gexp-extensions & ungexp"
930 (list sed grep)
931 ((@@ (guix gexp) gexp-extensions)
932 #~(foo #$(with-extensions (list grep) #~+)
933 #+(with-extensions (list sed) #~-))))
934
935(test-equal "gexp-extensions & ungexp-splicing"
936 (list grep sed)
937 ((@@ (guix gexp) gexp-extensions)
938 #~(foo #$@(list (with-extensions (list grep) #~+)
939 (with-imported-modules '((foo))
940 (with-extensions (list sed) #~-))))))
941
942(test-equal "gexp-extensions and literal Scheme object"
943 '()
944 ((@@ (guix gexp) gexp-extensions) #t))
945
946(test-assertm "gexp->derivation & with-extensions"
947 ;; Create a fake Guile extension and make sure it is accessible both to the
948 ;; imported modules and to the derivation build script.
949 (mlet* %store-monad
950 ((extension -> %extension-package)
951 (module -> (scheme-file "x" #~( ;; splice!
952 (define-module (foo)
953 #:use-module (hg2g)
954 #:export (multiply))
955
956 (define (multiply x)
957 (* the-answer x)))
958 #:splice? #t))
959 (build -> (with-extensions (list extension)
960 (with-imported-modules `((guix build utils)
961 ((foo) => ,module))
962 #~(begin
963 (use-modules (guix build utils)
964 (hg2g) (foo))
965 (call-with-output-file #$output
966 (lambda (port)
967 (write (list the-answer (multiply 2))
968 port)))))))
969 (drv (gexp->derivation "thingie" build
970 ;; %BOOTSTRAP-GUILE is 2.0.
971 #:effective-version "2.0"))
972 (out -> (derivation->output-path drv)))
973 (mbegin %store-monad
974 (built-derivations (list drv))
975 (return (equal? '(42 84) (call-with-input-file out read))))))
976
2ca41030
LC
977(test-assertm "lower-gexp"
978 (mlet* %store-monad
979 ((extension -> %extension-package)
980 (extension-drv (package->derivation %extension-package))
981 (coreutils-drv (package->derivation coreutils))
982 (exp -> (with-extensions (list extension)
983 (with-imported-modules `((guix build utils))
984 #~(begin
985 (use-modules (guix build utils)
986 (hg2g))
987 #$coreutils:debug
988 mkdir-p
989 the-answer))))
990 (lexp (lower-gexp exp
991 #:effective-version "2.0")))
992 (define (matching-input drv output)
993 (lambda (input)
38685774
LC
994 (and (eq? (derivation-input-derivation input) drv)
995 (equal? (derivation-input-sub-derivations input)
996 (list output)))))
2ca41030
LC
997
998 (mbegin %store-monad
999 (return (and (find (matching-input extension-drv "out")
1000 (lowered-gexp-inputs (pk 'lexp lexp)))
1001 (find (matching-input coreutils-drv "debug")
1002 (lowered-gexp-inputs lexp))
1003 (member (string-append
1004 (derivation->output-path extension-drv)
1005 "/share/guile/site/2.0")
1006 (lowered-gexp-load-path lexp))
1007 (= 2 (length (lowered-gexp-load-path lexp)))
1008 (member (string-append
1009 (derivation->output-path extension-drv)
1010 "/lib/guile/2.0/site-ccache")
1011 (lowered-gexp-load-compiled-path lexp))
1012 (= 2 (length (lowered-gexp-load-compiled-path lexp)))
b9373e26
LC
1013 (eq? (derivation-input-derivation (lowered-gexp-guile lexp))
1014 (%guile-for-build)))))))
2ca41030 1015
d63ee94d
LC
1016(test-assertm "lower-gexp, raw-derivation-file"
1017 (mlet* %store-monad ((thing -> (program-file "prog" #~(display "hi!")))
1018 (exp -> #~(list #$(raw-derivation-file thing)))
1019 (drv (lower-object thing))
1020 (lexp (lower-gexp exp #:effective-version "2.0")))
1021 (return (and (equal? `(list ,(derivation-file-name drv))
1022 (lowered-gexp-sexp lexp))
1023 (equal? (list (derivation-file-name drv))
1024 (lowered-gexp-sources lexp))
1025 (null? (lowered-gexp-inputs lexp))))))
1026
24ab804c
LC
1027(test-eq "lower-gexp, non-self-quoting input"
1028 +
1029 (guard (c ((gexp-input-error? c)
1030 (gexp-error-invalid-input c)))
1031 (run-with-store %store
1032 (lower-gexp #~(foo #$+)))))
1033
ab7010af
MB
1034(test-equal "lower-gexp, character literal"
1035 '(#\+)
1036 (lowered-gexp-sexp
1037 (run-with-store %store
1038 (lower-gexp #~(#\+)))))
1039
b53833b2
LC
1040(test-assertm "gexp->derivation #:references-graphs"
1041 (mlet* %store-monad
72cd8ec0 1042 ((one (text-file "one" (random-text)))
b53833b2
LC
1043 (two (gexp->derivation "two"
1044 #~(symlink #$one #$output:chbouib)))
66a35ceb 1045 (build -> (with-imported-modules '((guix build store-copy)
d4e9317b
LC
1046 (guix progress)
1047 (guix records)
6892f0a2 1048 (guix sets)
66a35ceb
LC
1049 (guix build utils))
1050 #~(begin
1051 (use-modules (guix build store-copy))
1052 (with-output-to-file #$output
1053 (lambda ()
6892f0a2
LC
1054 (write (map store-info-item
1055 (call-with-input-file "guile"
1056 read-reference-graph)))))
66a35ceb
LC
1057 (with-output-to-file #$output:one
1058 (lambda ()
6892f0a2
LC
1059 (write (map store-info-item
1060 (call-with-input-file "one"
1061 read-reference-graph)))))
66a35ceb
LC
1062 (with-output-to-file #$output:two
1063 (lambda ()
6892f0a2
LC
1064 (write (map store-info-item
1065 (call-with-input-file "two"
1066 read-reference-graph))))))))
66a35ceb 1067 (drv (gexp->derivation "ref-graphs" build
b53833b2
LC
1068 #:references-graphs `(("one" ,one)
1069 ("two" ,two "chbouib")
66a35ceb 1070 ("guile" ,%bootstrap-guile))))
b53833b2
LC
1071 (ok? (built-derivations (list drv)))
1072 (guile-drv (package->derivation %bootstrap-guile))
686784d0
LC
1073 (bash (interned-file (search-bootstrap-binary "bash"
1074 (%current-system))
1075 "bash" #:recursive? #t))
b53833b2
LC
1076 (g-one -> (derivation->output-path drv "one"))
1077 (g-two -> (derivation->output-path drv "two"))
1078 (g-guile -> (derivation->output-path drv)))
1079 (return (and ok?
1080 (equal? (call-with-input-file g-one read) (list one))
72cd8ec0
LC
1081 (lset= string=?
1082 (call-with-input-file g-two read)
1083 (list one (derivation->output-path two "chbouib")))
686784d0
LC
1084
1085 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
72cd8ec0
LC
1086 (lset= string=?
1087 (call-with-input-file g-guile read)
1088 (list (derivation->output-path guile-drv) bash))))))
b53833b2 1089
c8351d9a
LC
1090(test-assertm "gexp->derivation #:allowed-references"
1091 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
1092 #~(begin
1093 (mkdir #$output)
1094 (chdir #$output)
1095 (symlink #$output "self")
1096 (symlink #$%bootstrap-guile
1097 "guile"))
1098 #:allowed-references
1099 (list "out" %bootstrap-guile))))
1100 (built-derivations (list drv))))
1101
accb682c
LC
1102(test-assertm "gexp->derivation #:allowed-references, specific output"
1103 (mlet* %store-monad ((in (gexp->derivation "thing"
1104 #~(begin
1105 (mkdir #$output:ok)
1106 (mkdir #$output:not-ok))))
1107 (drv (gexp->derivation "allowed-refs"
1108 #~(begin
1109 (pk #$in:not-ok)
1110 (mkdir #$output)
1111 (chdir #$output)
1112 (symlink #$output "self")
1113 (symlink #$in:ok "ok"))
1114 #:allowed-references
1115 (list "out"
1116 (gexp-input in "ok")))))
1117 (built-derivations (list drv))))
1118
c8351d9a
LC
1119(test-assert "gexp->derivation #:allowed-references, disallowed"
1120 (let ((drv (run-with-store %store
1121 (gexp->derivation "allowed-refs"
1122 #~(begin
1123 (mkdir #$output)
1124 (chdir #$output)
1125 (symlink #$%bootstrap-guile "guile"))
1126 #:allowed-references '()))))
f9e8a123 1127 (guard (c ((store-protocol-error? c) #t))
c8351d9a
LC
1128 (build-derivations %store (list drv))
1129 #f)))
1130
3f4ecf32
LC
1131(test-assertm "gexp->derivation #:disallowed-references, allowed"
1132 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
1133 #~(begin
1134 (mkdir #$output)
1135 (chdir #$output)
1136 (symlink #$output "self")
1137 (symlink #$%bootstrap-guile
1138 "guile"))
1139 #:disallowed-references '())))
1140 (built-derivations (list drv))))
1141
1142
1143(test-assert "gexp->derivation #:disallowed-references"
1144 (let ((drv (run-with-store %store
1145 (gexp->derivation "disallowed-refs"
1146 #~(begin
1147 (mkdir #$output)
1148 (chdir #$output)
1149 (symlink #$%bootstrap-guile "guile"))
1150 #:disallowed-references (list %bootstrap-guile)))))
f9e8a123 1151 (guard (c ((store-protocol-error? c) #t))
3f4ecf32
LC
1152 (build-derivations %store (list drv))
1153 #f)))
1154
c17b5ab4 1155(define shebang
c1bc358f 1156 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
1157 "/bin/guile --no-auto-compile"))
1158
1159;; If we're going to hit the silly shebang limit (128 chars on Linux-based
1160;; systems), then skip the following test.
47b3124a 1161(test-skip (if (> (string-length shebang) 127) 2 0))
c17b5ab4 1162
21b679f6
LC
1163(test-assertm "gexp->script"
1164 (mlet* %store-monad ((n -> (random (expt 2 50)))
1165 (exp -> (gexp
1166 (system*
1167 (string-append (ungexp %bootstrap-guile)
1168 "/bin/guile")
1169 "-c" (object->string
1170 '(display (expt (ungexp n) 2))))))
1171 (drv (gexp->script "guile-thing" exp
1172 #:guile %bootstrap-guile))
1173 (out -> (derivation->output-path drv))
1174 (done (built-derivations (list drv))))
1175 (let* ((pipe (open-input-pipe out))
1176 (str (get-string-all pipe)))
1177 (return (and (zero? (close-pipe pipe))
1178 (= (expt n 2) (string->number str)))))))
1179
92bcccc5 1180(test-assert "gexp->script #:module-path"
1ae16033
LC
1181 (call-with-temporary-directory
1182 (lambda (directory)
1183 (define str
1184 "Fake (guix base32) module!")
1185
1186 (mkdir (string-append directory "/guix"))
1187 (call-with-output-file (string-append directory "/guix/base32.scm")
1188 (lambda (port)
1189 (write `(begin (define-module (guix base32))
1190 (define-public %fake! ,str))
1191 port)))
1192
92bcccc5
LF
1193 (run-with-store %store
1194 (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
1195 (gexp (begin
1196 (use-modules (guix base32))
1197 (write (list %load-path
1198 %fake!))))))
1199 (drv (gexp->script "guile-thing" exp
1200 #:guile %bootstrap-guile
1201 #:module-path (list directory)))
1202 (out -> (derivation->output-path drv))
1203 (done (built-derivations (list drv))))
1204 (let* ((pipe (open-input-pipe out))
1205 (data (read pipe)))
1206 (return (and (zero? (close-pipe pipe))
1207 (match data
1208 ((load-path str*)
1209 (and (string=? str* str)
1210 (not (member directory load-path)))))))))))))
1ae16033 1211
15a01c72
LC
1212(test-assertm "program-file"
1213 (let* ((n (random (expt 2 50)))
0bb9929e
LC
1214 (exp (with-imported-modules '((guix build utils))
1215 (gexp (begin
1216 (use-modules (guix build utils))
1217 (display (ungexp n))))))
15a01c72 1218 (file (program-file "program" exp
15a01c72
LC
1219 #:guile %bootstrap-guile)))
1220 (mlet* %store-monad ((drv (lower-object file))
1221 (out -> (derivation->output-path drv)))
1222 (mbegin %store-monad
1223 (built-derivations (list drv))
1224 (let* ((pipe (open-input-pipe out))
1225 (str (get-string-all pipe)))
1226 (return (and (zero? (close-pipe pipe))
1227 (= n (string->number str)))))))))
1228
92bcccc5 1229(test-assert "program-file #:module-path"
427ec19e
LC
1230 (call-with-temporary-directory
1231 (lambda (directory)
1232 (define text (random-text))
1233
1234 (call-with-output-file (string-append directory "/stupid-module.scm")
1235 (lambda (port)
1236 (write `(begin (define-module (stupid-module))
1237 (define-public %stupid-thing ,text))
1238 port)))
1239
1240 (let* ((exp (with-imported-modules '((stupid-module))
1241 (gexp (begin
1242 (use-modules (stupid-module))
1243 (display %stupid-thing)))))
1244 (file (program-file "program" exp
1245 #:guile %bootstrap-guile
1246 #:module-path (list directory))))
92bcccc5
LF
1247 (run-with-store %store
1248 (mlet* %store-monad ((drv (lower-object file))
1249 (out -> (derivation->output-path drv)))
1250 (mbegin %store-monad
1251 (built-derivations (list drv))
1252 (let* ((pipe (open-input-pipe out))
1253 (str (get-string-all pipe)))
1254 (return (and (zero? (close-pipe pipe))
1255 (string=? text str)))))))))))
427ec19e 1256
838e17d8
LC
1257(test-assertm "program-file & with-extensions"
1258 (let* ((exp (with-extensions (list %extension-package)
1259 (gexp (begin
1260 (use-modules (hg2g))
1261 (display the-answer)))))
1262 (file (program-file "program" exp
1263 #:guile %bootstrap-guile)))
1264 (mlet* %store-monad ((drv (lower-object file))
1265 (out -> (derivation->output-path drv)))
1266 (mbegin %store-monad
1267 (built-derivations (list drv))
1268 (let* ((pipe (open-input-pipe out))
1269 (str (get-string-all pipe)))
1270 (return (and (zero? (close-pipe pipe))
1271 (= 42 (string->number str)))))))))
1272
2e8cabb8
LC
1273(test-assertm "program-file #:system"
1274 (let* ((exp (with-imported-modules '((guix build utils))
1275 (gexp (begin
1276 (use-modules (guix build utils))
1277 (display "hi!")))))
1278 (system (if (string=? (%current-system) "x86_64-linux")
1279 "armhf-linux"
1280 "x86_64-linux"))
1281 (file (program-file "program" exp)))
1282 (mlet %store-monad ((drv (lower-object file system)))
1283 (return (and (string=? (derivation-system drv) system)
1284 (find (lambda (input)
1285 (let ((drv (pk (derivation-input-derivation input))))
1286 (and (string=? (derivation-name drv)
1287 "module-import-compiled")
1288 (string=? (derivation-system drv)
1289 system))))
1290 (derivation-inputs drv)))))))
1291
e1c153e0
LC
1292(test-assertm "scheme-file"
1293 (let* ((text (plain-file "foo" "Hello, world!"))
1294 (scheme (scheme-file "bar" #~(list "foo" #$text))))
1295 (mlet* %store-monad ((drv (lower-object scheme))
1296 (text (lower-object text))
1297 (out -> (derivation->output-path drv)))
1298 (mbegin %store-monad
1299 (built-derivations (list drv))
e74f64b9 1300 (mlet %store-monad ((refs (references* out)))
e1c153e0
LC
1301 (return (and (equal? refs (list text))
1302 (equal? `(list "foo" ,text)
1303 (call-with-input-file out read)))))))))
1304
d63ee94d
LC
1305(test-assertm "raw-derivation-file"
1306 (let* ((exp #~(let ((drv #$(raw-derivation-file coreutils)))
1307 (when (file-exists? drv)
1308 (symlink drv #$output)))))
1309 (mlet* %store-monad ((dep (lower-object coreutils))
1310 (drv (gexp->derivation "drv-ref" exp))
1311 (out -> (derivation->output-path drv)))
1312 (mbegin %store-monad
1313 (built-derivations (list drv))
1314 (mlet %store-monad ((refs (references* out)))
1315 (return (and (member (derivation-file-name dep)
1316 (derivation-sources drv))
1317 (not (member (derivation-file-name dep)
1318 (map derivation-input-path
1319 (derivation-inputs drv))))
1320 (equal? (readlink out) (derivation-file-name dep))
1321 (equal? refs (list (derivation-file-name dep))))))))))
1322
462a3fa3 1323(test-assert "text-file*"
e74f64b9
LC
1324 (run-with-store %store
1325 (mlet* %store-monad
1326 ((drv (package->derivation %bootstrap-guile))
1327 (guile -> (derivation->output-path drv))
1328 (file (text-file "bar" "This is bar."))
1329 (text (text-file* "foo"
1330 %bootstrap-guile "/bin/guile "
1331 (gexp-input %bootstrap-guile "out") "/bin/guile "
1332 drv "/bin/guile "
1333 file))
1334 (done (built-derivations (list text)))
1335 (out -> (derivation->output-path text))
1336 (refs (references* out)))
1337 ;; Make sure we get the right references and the right content.
1338 (return (and (lset= string=? refs (list guile file))
1339 (equal? (call-with-input-file out get-string-all)
1340 (string-append guile "/bin/guile "
1341 guile "/bin/guile "
1342 guile "/bin/guile "
1343 file)))))
1344 #:guile-for-build (package-derivation %store %bootstrap-guile)))
462a3fa3 1345
b751cde3
LC
1346(test-assertm "mixed-text-file"
1347 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
1348 "export PATH="
1349 %bootstrap-guile "/bin"))
1350 (drv (lower-object file))
1351 (out -> (derivation->output-path drv))
1352 (guile-drv (package->derivation %bootstrap-guile))
1353 (guile -> (derivation->output-path guile-drv)))
1354 (mbegin %store-monad
1355 (built-derivations (list drv))
e74f64b9 1356 (mlet %store-monad ((refs (references* out)))
b751cde3
LC
1357 (return (and (string=? (string-append "export PATH=" guile "/bin")
1358 (call-with-input-file out get-string-all))
1359 (equal? refs (list guile))))))))
1360
5dec93bb
LC
1361(test-assertm "file-union"
1362 (mlet* %store-monad ((union -> (file-union "union"
1363 `(("a" ,(plain-file "a" "1"))
1364 ("b/c/d" ,(plain-file "d" "2"))
1365 ("e" ,(plain-file "e" "3")))))
1366 (drv (lower-object union))
1367 (out -> (derivation->output-path drv)))
1368 (define (contents=? file str)
1369 (string=? (call-with-input-file (string-append out "/" file)
1370 get-string-all)
1371 str))
1372
1373 (mbegin %store-monad
1374 (built-derivations (list drv))
1375 (return (and (contents=? "a" "1")
1376 (contents=? "b/c/d" "2")
1377 (contents=? "e" "3"))))))
1378
a8afb9ae
LC
1379(test-assert "gexp->derivation vs. %current-target-system"
1380 (let ((mval (gexp->derivation "foo"
1381 #~(begin
1382 (mkdir #$output)
1383 (foo #+gnu-make))
1384 #:target #f)))
1385 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
1386 ;; influence.
1387 (parameterize ((%current-target-system "fooooo"))
1388 (derivation? (run-with-store %store mval)))))
1389
c2b84676
LC
1390(test-assertm "lower-object"
1391 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
1392 (drv2 (lower-object (package-source coreutils)))
1393 (item (lower-object (plain-file "foo" "Hello!"))))
1394 (return (and (derivation? drv1) (derivation? drv2)
1395 (store-path? item)))))
1396
91937029
LC
1397(test-assertm "lower-object, computed-file"
1398 (let* ((text (plain-file "foo" "Hello!"))
1399 (exp #~(begin
1400 (mkdir #$output)
1401 (symlink #$%bootstrap-guile
1402 (string-append #$output "/guile"))
1403 (symlink #$text (string-append #$output "/text"))))
1404 (computed (computed-file "computed" exp)))
1405 (mlet* %store-monad ((text (lower-object text))
1406 (guile-drv (lower-object %bootstrap-guile))
1407 (comp-drv (lower-object computed))
1408 (comp -> (derivation->output-path comp-drv)))
1409 (mbegin %store-monad
1410 (built-derivations (list comp-drv))
1411 (return (and (string=? (readlink (string-append comp "/guile"))
1412 (derivation->output-path guile-drv))
1413 (string=? (readlink (string-append comp "/text"))
1414 text)))))))
1415
9ec154f5
LC
1416(test-equal "lower-object, computed-file, #:system"
1417 '("mips64el-linux")
1418 (run-with-store %store
1419 (let* ((exp #~(symlink #$coreutils #$output))
1420 (computed (computed-file "computed" exp
1421 #:guile %bootstrap-guile)))
1422 ;; Make sure that the SYSTEM argument to 'lower-object' is honored.
1423 (mlet* %store-monad ((drv (lower-object computed "mips64el-linux"))
1424 (refs (references* (derivation-file-name drv))))
1425 (return (delete-duplicates
1426 (filter-map (lambda (file)
1427 (and (string-suffix? ".drv" file)
1428 (let ((drv (read-derivation-from-file
1429 file)))
1430 (derivation-system drv))))
1431 (cons (derivation-file-name drv)
1432 refs))))))))
1433
3e43166f
LC
1434(test-assert "lower-object & gexp-input-error?"
1435 (guard (c ((gexp-input-error? c)
1436 (gexp-error-invalid-input c)))
1437 (run-with-store %store
1438 (lower-object (current-module))
1439 #:guile-for-build (%guile-for-build))))
1440
2cf0ea0d
LC
1441(test-assert "printer"
1442 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
18fc84bc 1443 \"/bin/uname\"\\) [[:graph:]]+tests/gexp\\.scm:[0-9]+:[0-9]+ [[:xdigit:]]+>$"
2cf0ea0d
LC
1444 (with-output-to-string
1445 (lambda ()
1446 (write
1447 (gexp (string-append (ungexp coreutils)
1448 "/bin/uname")))))))
1449
1450(test-assert "printer vs. ungexp-splicing"
1451 (string-match "^#<gexp .* [[:xdigit:]]+>$"
1452 (with-output-to-string
1453 (lambda ()
1454 ;; #~(begin #$@#~())
1455 (write
1456 (gexp (begin (ungexp-splicing (gexp ())))))))))
1457
21b679f6
LC
1458(test-equal "sugar"
1459 '(gexp (foo (ungexp bar) (ungexp baz "out")
1460 (ungexp (chbouib 42))
667b2508
LC
1461 (ungexp-splicing (list x y z))
1462 (ungexp-native foo) (ungexp-native foo "out")
1463 (ungexp-native (chbouib 42))
1464 (ungexp-native-splicing (list x y z))))
1465 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
1466 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6 1467
9a2f99f4
MO
1468(test-assertm "gexp->file, cross-compilation"
1469 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1470 (exp -> (gexp (list (ungexp coreutils))))
1471 (xdrv (gexp->file "foo" exp #:target target))
1472 (refs (references*
1473 (derivation-file-name xdrv)))
1474 (xcu (package->cross-derivation coreutils
1475 target))
1476 (cu (package->derivation coreutils)))
1477 (return (and (member (derivation-file-name xcu) refs)
1478 (not (member (derivation-file-name cu) refs))))))
1479
1480(test-assertm "gexp->file, cross-compilation with default target"
1481 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1482 (_ (set-current-target target))
1483 (exp -> (gexp (list (ungexp coreutils))))
1484 (xdrv (gexp->file "foo" exp))
1485 (refs (references*
1486 (derivation-file-name xdrv)))
1487 (xcu (package->cross-derivation coreutils
1488 target))
1489 (cu (package->derivation coreutils)))
1490 (return (and (member (derivation-file-name xcu) refs)
1491 (not (member (derivation-file-name cu) refs))))))
1492
1493(test-assertm "gexp->script, cross-compilation"
1494 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1495 (exp -> (gexp (list (ungexp coreutils))))
1496 (xdrv (gexp->script "foo" exp #:target target))
1497 (refs (references*
1498 (derivation-file-name xdrv)))
1499 (xcu (package->cross-derivation coreutils
1500 target))
1501 (cu (package->derivation coreutils)))
1502 (return (and (member (derivation-file-name xcu) refs)
1503 (not (member (derivation-file-name cu) refs))))))
1504
1505(test-assertm "gexp->script, cross-compilation with default target"
1506 (mlet* %store-monad ((target -> "aarch64-linux-gnu")
1507 (_ (set-current-target target))
1508 (exp -> (gexp (list (ungexp coreutils))))
1509 (xdrv (gexp->script "foo" exp))
1510 (refs (references*
1511 (derivation-file-name xdrv)))
1512 (xcu (package->cross-derivation coreutils
1513 target))
1514 (cu (package->derivation coreutils)))
1515 (return (and (member (derivation-file-name xcu) refs)
1516 (not (member (derivation-file-name cu) refs))))))
1517
21b679f6
LC
1518(test-end "gexp")
1519
21b679f6
LC
1520;; Local Variables:
1521;; eval: (put 'test-assertm 'scheme-indent-function 1)
1522;; End: