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