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