Commit | Line | Data |
---|---|---|
21b679f6 | 1 | ;;; GNU Guix --- Functional package management for GNU |
1ae16033 | 2 | ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org> |
21b679f6 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (test-gexp) | |
20 | #:use-module (guix store) | |
21 | #:use-module (guix monads) | |
22 | #:use-module (guix gexp) | |
ef8de985 | 23 | #:use-module (guix grafts) |
21b679f6 | 24 | #:use-module (guix derivations) |
79c0c8cd | 25 | #:use-module (guix packages) |
838e17d8 | 26 | #:use-module (guix build-system trivial) |
c1bc358f | 27 | #:use-module (guix tests) |
4ff76a0a | 28 | #:use-module ((guix build utils) #:select (with-directory-excursion)) |
1ae16033 | 29 | #:use-module ((guix utils) #:select (call-with-temporary-directory)) |
21b679f6 LC |
30 | #:use-module (gnu packages) |
31 | #:use-module (gnu packages base) | |
32 | #:use-module (gnu packages bootstrap) | |
33 | #:use-module (srfi srfi-1) | |
c8351d9a | 34 | #:use-module (srfi srfi-34) |
21b679f6 LC |
35 | #:use-module (srfi srfi-64) |
36 | #:use-module (rnrs io ports) | |
37 | #:use-module (ice-9 match) | |
2cf0ea0d | 38 | #:use-module (ice-9 regex) |
0687fc9c LC |
39 | #:use-module (ice-9 popen) |
40 | #:use-module (ice-9 ftw)) | |
21b679f6 LC |
41 | |
42 | ;; Test the (guix gexp) module. | |
43 | ||
44 | (define %store | |
c1bc358f | 45 | (open-connection-for-tests)) |
21b679f6 | 46 | |
ef8de985 LC |
47 | ;; Globally disable grafts because they can trigger early builds. |
48 | (%graft? #f) | |
49 | ||
21b679f6 | 50 | ;; For white-box testing. |
1f976033 LC |
51 | (define (gexp-inputs x) |
52 | ((@@ (guix gexp) gexp-inputs) x)) | |
53 | (define (gexp-native-inputs x) | |
54 | ((@@ (guix gexp) gexp-native-inputs) x)) | |
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) |
68a61e9f | 61 | (run-with-store %store (gexp->sexp exp |
68a61e9f | 62 | #:target target) |
c1bc358f | 63 | #:guile-for-build (%guile-for-build))) |
21b679f6 LC |
64 | |
65 | (define-syntax-rule (test-assertm name exp) | |
66 | (test-assert name | |
67 | (run-with-store %store exp | |
c1bc358f | 68 | #:guile-for-build (%guile-for-build)))) |
21b679f6 | 69 | |
838e17d8 LC |
70 | (define %extension-package |
71 | ;; Example of a package to use when testing 'with-extensions'. | |
72 | (dummy-package "extension" | |
73 | (build-system trivial-build-system) | |
74 | (arguments | |
75 | `(#:guile ,%bootstrap-guile | |
76 | #:modules ((guix build utils)) | |
77 | #:builder | |
78 | (begin | |
79 | (use-modules (guix build utils)) | |
80 | (let* ((out (string-append (assoc-ref %outputs "out") | |
81 | "/share/guile/site/" | |
82 | (effective-version)))) | |
83 | (mkdir-p out) | |
84 | (call-with-output-file (string-append out "/hg2g.scm") | |
85 | (lambda (port) | |
86 | (write '(define-module (hg2g) | |
87 | #:export (the-answer)) | |
88 | port) | |
89 | (write '(define the-answer 42) port))))))))) | |
90 | ||
21b679f6 LC |
91 | \f |
92 | (test-begin "gexp") | |
93 | ||
94 | (test-equal "no refs" | |
95 | '(display "hello!") | |
96 | (let ((exp (gexp (display "hello!")))) | |
97 | (and (gexp? exp) | |
98 | (null? (gexp-inputs exp)) | |
99 | (gexp->sexp* exp)))) | |
100 | ||
101 | (test-equal "unquote" | |
102 | '(display `(foo ,(+ 2 3))) | |
103 | (let ((exp (gexp (display `(foo ,(+ 2 3)))))) | |
104 | (and (gexp? exp) | |
105 | (null? (gexp-inputs exp)) | |
106 | (gexp->sexp* exp)))) | |
107 | ||
108 | (test-assert "one input package" | |
109 | (let ((exp (gexp (display (ungexp coreutils))))) | |
110 | (and (gexp? exp) | |
111 | (match (gexp-inputs exp) | |
112 | (((p "out")) | |
113 | (eq? p coreutils))) | |
114 | (equal? `(display ,(derivation->output-path | |
115 | (package-derivation %store coreutils))) | |
116 | (gexp->sexp* exp))))) | |
5e2e4a51 LC |
117 | |
118 | (test-assert "one input package, dotted list" | |
119 | (let ((exp (gexp (coreutils . (ungexp coreutils))))) | |
120 | (and (gexp? exp) | |
121 | (match (gexp-inputs exp) | |
122 | (((p "out")) | |
123 | (eq? p coreutils))) | |
124 | (equal? `(coreutils . ,(derivation->output-path | |
125 | (package-derivation %store coreutils))) | |
126 | (gexp->sexp* exp))))) | |
21b679f6 | 127 | |
79c0c8cd LC |
128 | (test-assert "one input origin" |
129 | (let ((exp (gexp (display (ungexp (package-source coreutils)))))) | |
130 | (and (gexp? exp) | |
131 | (match (gexp-inputs exp) | |
132 | (((o "out")) | |
133 | (eq? o (package-source coreutils)))) | |
134 | (equal? `(display ,(derivation->output-path | |
135 | (package-source-derivation | |
136 | %store (package-source coreutils)))) | |
137 | (gexp->sexp* exp))))) | |
138 | ||
d9ae938f LC |
139 | (test-assert "one local file" |
140 | (let* ((file (search-path %load-path "guix.scm")) | |
141 | (local (local-file file)) | |
142 | (exp (gexp (display (ungexp local)))) | |
020f3e41 | 143 | (intd (add-to-store %store (basename file) #f |
d9ae938f LC |
144 | "sha256" file))) |
145 | (and (gexp? exp) | |
146 | (match (gexp-inputs exp) | |
147 | (((x "out")) | |
148 | (eq? x local))) | |
149 | (equal? `(display ,intd) (gexp->sexp* exp))))) | |
150 | ||
7833db1f LC |
151 | (test-assert "one local file, symlink" |
152 | (let ((file (search-path %load-path "guix.scm")) | |
153 | (link (tmpnam))) | |
154 | (dynamic-wind | |
155 | (const #t) | |
156 | (lambda () | |
157 | (symlink (canonicalize-path file) link) | |
158 | (let* ((local (local-file link "my-file" #:recursive? #f)) | |
159 | (exp (gexp (display (ungexp local)))) | |
160 | (intd (add-to-store %store "my-file" #f | |
161 | "sha256" file))) | |
162 | (and (gexp? exp) | |
163 | (match (gexp-inputs exp) | |
164 | (((x "out")) | |
165 | (eq? x local))) | |
166 | (equal? `(display ,intd) (gexp->sexp* exp))))) | |
167 | (lambda () | |
168 | (false-if-exception (delete-file link)))))) | |
169 | ||
4ff76a0a LC |
170 | (test-equal "local-file, relative file name" |
171 | (canonicalize-path (search-path %load-path "guix/base32.scm")) | |
172 | (let ((directory (dirname (search-path %load-path | |
173 | "guix/build-system/gnu.scm")))) | |
174 | (with-directory-excursion directory | |
175 | (let ((file (local-file "../guix/base32.scm"))) | |
176 | (local-file-absolute-file-name file))))) | |
177 | ||
0687fc9c LC |
178 | (test-assertm "local-file, #:select?" |
179 | (mlet* %store-monad ((select? -> (lambda (file stat) | |
180 | (member (basename file) | |
181 | '("guix.scm" "tests" | |
182 | "gexp.scm")))) | |
183 | (file -> (local-file ".." "directory" | |
184 | #:recursive? #t | |
185 | #:select? select?)) | |
186 | (dir (lower-object file))) | |
187 | (return (and (store-path? dir) | |
188 | (equal? (scandir dir) | |
189 | '("." ".." "guix.scm" "tests")) | |
190 | (equal? (scandir (string-append dir "/tests")) | |
191 | '("." ".." "gexp.scm")))))) | |
192 | ||
558e8b11 LC |
193 | (test-assert "one plain file" |
194 | (let* ((file (plain-file "hi" "Hello, world!")) | |
195 | (exp (gexp (display (ungexp file)))) | |
196 | (expected (add-text-to-store %store "hi" "Hello, world!"))) | |
197 | (and (gexp? exp) | |
198 | (match (gexp-inputs exp) | |
199 | (((x "out")) | |
200 | (eq? x file))) | |
201 | (equal? `(display ,expected) (gexp->sexp* exp))))) | |
202 | ||
21b679f6 LC |
203 | (test-assert "same input twice" |
204 | (let ((exp (gexp (begin | |
205 | (display (ungexp coreutils)) | |
206 | (display (ungexp coreutils)))))) | |
207 | (and (gexp? exp) | |
208 | (match (gexp-inputs exp) | |
209 | (((p "out")) | |
210 | (eq? p coreutils))) | |
211 | (let ((e `(display ,(derivation->output-path | |
212 | (package-derivation %store coreutils))))) | |
213 | (equal? `(begin ,e ,e) (gexp->sexp* exp)))))) | |
214 | ||
215 | (test-assert "two input packages, one derivation, one file" | |
216 | (let* ((drv (build-expression->derivation | |
217 | %store "foo" 'bar | |
218 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
219 | (txt (add-text-to-store %store "foo" "Hello, world!")) | |
220 | (exp (gexp (begin | |
221 | (display (ungexp coreutils)) | |
222 | (display (ungexp %bootstrap-guile)) | |
223 | (display (ungexp drv)) | |
224 | (display (ungexp txt)))))) | |
225 | (define (match-input thing) | |
226 | (match-lambda | |
227 | ((drv-or-pkg _ ...) | |
228 | (eq? thing drv-or-pkg)))) | |
229 | ||
230 | (and (gexp? exp) | |
231 | (= 4 (length (gexp-inputs exp))) | |
232 | (every (lambda (input) | |
233 | (find (match-input input) (gexp-inputs exp))) | |
234 | (list drv coreutils %bootstrap-guile txt)) | |
235 | (let ((e0 `(display ,(derivation->output-path | |
236 | (package-derivation %store coreutils)))) | |
237 | (e1 `(display ,(derivation->output-path | |
238 | (package-derivation %store %bootstrap-guile)))) | |
239 | (e2 `(display ,(derivation->output-path drv))) | |
240 | (e3 `(display ,txt))) | |
241 | (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp)))))) | |
242 | ||
a9e5e92f LC |
243 | (test-assert "file-append" |
244 | (let* ((drv (package-derivation %store %bootstrap-guile)) | |
245 | (fa (file-append %bootstrap-guile "/bin/guile")) | |
246 | (exp #~(here we go #$fa))) | |
247 | (and (match (gexp->sexp* exp) | |
248 | (('here 'we 'go (? string? result)) | |
249 | (string=? result | |
250 | (string-append (derivation->output-path drv) | |
251 | "/bin/guile")))) | |
252 | (match (gexp-inputs exp) | |
253 | (((thing "out")) | |
254 | (eq? thing fa)))))) | |
255 | ||
256 | (test-assert "file-append, output" | |
257 | (let* ((drv (package-derivation %store glibc)) | |
258 | (fa (file-append glibc "/lib" "/debug")) | |
259 | (exp #~(foo #$fa:debug))) | |
260 | (and (match (gexp->sexp* exp) | |
261 | (('foo (? string? result)) | |
262 | (string=? result | |
263 | (string-append (derivation->output-path drv "debug") | |
264 | "/lib/debug")))) | |
265 | (match (gexp-inputs exp) | |
266 | (((thing "debug")) | |
267 | (eq? thing fa)))))) | |
268 | ||
269 | (test-assert "file-append, nested" | |
270 | (let* ((drv (package-derivation %store glibc)) | |
271 | (dir (file-append glibc "/bin")) | |
272 | (slash (file-append dir "/")) | |
273 | (file (file-append slash "getent")) | |
274 | (exp #~(foo #$file))) | |
275 | (and (match (gexp->sexp* exp) | |
276 | (('foo (? string? result)) | |
277 | (string=? result | |
278 | (string-append (derivation->output-path drv) | |
279 | "/bin/getent")))) | |
280 | (match (gexp-inputs exp) | |
281 | (((thing "out")) | |
282 | (eq? thing file)))))) | |
283 | ||
667b2508 LC |
284 | (test-assert "ungexp + ungexp-native" |
285 | (let* ((exp (gexp (list (ungexp-native %bootstrap-guile) | |
286 | (ungexp coreutils) | |
287 | (ungexp-native glibc) | |
288 | (ungexp binutils)))) | |
289 | (target "mips64el-linux") | |
290 | (guile (derivation->output-path | |
291 | (package-derivation %store %bootstrap-guile))) | |
292 | (cu (derivation->output-path | |
293 | (package-cross-derivation %store coreutils target))) | |
294 | (libc (derivation->output-path | |
295 | (package-derivation %store glibc))) | |
296 | (bu (derivation->output-path | |
297 | (package-cross-derivation %store binutils target)))) | |
298 | (and (lset= equal? | |
299 | `((,%bootstrap-guile "out") (,glibc "out")) | |
300 | (gexp-native-inputs exp)) | |
301 | (lset= equal? | |
302 | `((,coreutils "out") (,binutils "out")) | |
303 | (gexp-inputs exp)) | |
304 | (equal? `(list ,guile ,cu ,libc ,bu) | |
305 | (gexp->sexp* exp target))))) | |
306 | ||
1123759b LC |
307 | (test-equal "ungexp + ungexp-native, nested" |
308 | (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out"))) | |
309 | (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils))) | |
310 | (ungexp %bootstrap-guile))))) | |
311 | (list (gexp-inputs exp) '<> (gexp-native-inputs exp)))) | |
312 | ||
5b14a790 LC |
313 | (test-equal "ungexp + ungexp-native, nested, special mixture" |
314 | `(() <> ((,coreutils "out"))) | |
315 | ||
316 | ;; (gexp-native-inputs exp) used to return '(), wrongfully. | |
317 | (let* ((foo (gexp (foo (ungexp-native coreutils)))) | |
318 | (exp (gexp (bar (ungexp foo))))) | |
319 | (list (gexp-inputs exp) '<> (gexp-native-inputs exp)))) | |
320 | ||
21b679f6 LC |
321 | (test-assert "input list" |
322 | (let ((exp (gexp (display | |
323 | '(ungexp (list %bootstrap-guile coreutils))))) | |
324 | (guile (derivation->output-path | |
325 | (package-derivation %store %bootstrap-guile))) | |
326 | (cu (derivation->output-path | |
327 | (package-derivation %store coreutils)))) | |
328 | (and (lset= equal? | |
329 | `((,%bootstrap-guile "out") (,coreutils "out")) | |
330 | (gexp-inputs exp)) | |
331 | (equal? `(display '(,guile ,cu)) | |
332 | (gexp->sexp* exp))))) | |
333 | ||
667b2508 LC |
334 | (test-assert "input list + ungexp-native" |
335 | (let* ((target "mips64el-linux") | |
336 | (exp (gexp (display | |
337 | (cons '(ungexp-native (list %bootstrap-guile coreutils)) | |
338 | '(ungexp (list glibc binutils)))))) | |
339 | (guile (derivation->output-path | |
340 | (package-derivation %store %bootstrap-guile))) | |
341 | (cu (derivation->output-path | |
342 | (package-derivation %store coreutils))) | |
343 | (xlibc (derivation->output-path | |
344 | (package-cross-derivation %store glibc target))) | |
345 | (xbu (derivation->output-path | |
346 | (package-cross-derivation %store binutils target)))) | |
347 | (and (lset= equal? | |
348 | `((,%bootstrap-guile "out") (,coreutils "out")) | |
349 | (gexp-native-inputs exp)) | |
350 | (lset= equal? | |
351 | `((,glibc "out") (,binutils "out")) | |
352 | (gexp-inputs exp)) | |
353 | (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu))) | |
354 | (gexp->sexp* exp target))))) | |
355 | ||
21b679f6 | 356 | (test-assert "input list splicing" |
a482cfdc | 357 | (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile)) |
21b679f6 LC |
358 | (outputs (list (derivation->output-path |
359 | (package-derivation %store glibc) | |
360 | "debug") | |
361 | (derivation->output-path | |
362 | (package-derivation %store %bootstrap-guile)))) | |
363 | (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs)))))) | |
364 | (and (lset= equal? | |
365 | `((,glibc "debug") (,%bootstrap-guile "out")) | |
366 | (gexp-inputs exp)) | |
367 | (equal? (gexp->sexp* exp) | |
368 | `(list ,@(cons 5 outputs)))))) | |
369 | ||
667b2508 | 370 | (test-assert "input list splicing + ungexp-native-splicing" |
5b14a790 LC |
371 | (let* ((inputs (list (gexp-input glibc "debug" #:native? #t) |
372 | %bootstrap-guile)) | |
0dbea56b LC |
373 | (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs)))))) |
374 | (and (lset= equal? | |
375 | `((,glibc "debug") (,%bootstrap-guile "out")) | |
376 | (gexp-native-inputs exp)) | |
377 | (null? (gexp-inputs exp)) | |
378 | (equal? (gexp->sexp* exp) ;native | |
379 | (gexp->sexp* exp "mips64el-linux"))))) | |
380 | ||
578dfbe0 LC |
381 | (test-assert "gexp list splicing + ungexp-splicing" |
382 | (let* ((inner (gexp (ungexp-native glibc))) | |
383 | (exp (gexp (list (ungexp-splicing (list inner)))))) | |
384 | (and (equal? `((,glibc "out")) (gexp-native-inputs exp)) | |
385 | (null? (gexp-inputs exp)) | |
386 | (equal? (gexp->sexp* exp) ;native | |
387 | (gexp->sexp* exp "mips64el-linux"))))) | |
388 | ||
4b23c466 LC |
389 | (test-equal "output list" |
390 | 2 | |
391 | (let ((exp (gexp (begin (mkdir (ungexp output)) | |
392 | (mkdir (ungexp output "bar")))))) | |
393 | (length (gexp-outputs exp)))) ;XXX: <output-ref> is private | |
394 | ||
395 | (test-assert "output list, combined gexps" | |
396 | (let* ((exp0 (gexp (mkdir (ungexp output)))) | |
397 | (exp1 (gexp (mkdir (ungexp output "foo")))) | |
398 | (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1))))) | |
399 | (and (lset= equal? | |
400 | (append (gexp-outputs exp0) (gexp-outputs exp1)) | |
401 | (gexp-outputs exp2)) | |
402 | (= 2 (length (gexp-outputs exp2)))))) | |
403 | ||
7e75a673 LC |
404 | (test-equal "output list, combined gexps, duplicate output" |
405 | 1 | |
406 | (let* ((exp0 (gexp (mkdir (ungexp output)))) | |
407 | (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0)))) | |
408 | (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1))))) | |
409 | (length (gexp-outputs exp2)))) | |
410 | ||
f9efe568 LC |
411 | (test-assert "output list + ungexp-splicing list, combined gexps" |
412 | (let* ((exp0 (gexp (mkdir (ungexp output)))) | |
413 | (exp1 (gexp (mkdir (ungexp output "foo")))) | |
414 | (exp2 (gexp (begin (display "hi!") | |
415 | (ungexp-splicing (list exp0 exp1)))))) | |
416 | (and (lset= equal? | |
417 | (append (gexp-outputs exp0) (gexp-outputs exp1)) | |
418 | (gexp-outputs exp2)) | |
419 | (= 2 (length (gexp-outputs exp2)))))) | |
420 | ||
21b679f6 LC |
421 | (test-assertm "gexp->file" |
422 | (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile)))) | |
423 | (guile (package-file %bootstrap-guile)) | |
424 | (sexp (gexp->sexp exp)) | |
425 | (drv (gexp->file "foo" exp)) | |
426 | (out -> (derivation->output-path drv)) | |
427 | (done (built-derivations (list drv))) | |
e74f64b9 | 428 | (refs (references* out))) |
21b679f6 LC |
429 | (return (and (equal? sexp (call-with-input-file out read)) |
430 | (equal? (list guile) refs))))) | |
431 | ||
a9e5e92f LC |
432 | (test-assertm "gexp->file + file-append" |
433 | (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile | |
434 | "/bin/guile")) | |
435 | (guile (package-file %bootstrap-guile)) | |
436 | (drv (gexp->file "foo" exp)) | |
437 | (out -> (derivation->output-path drv)) | |
438 | (done (built-derivations (list drv))) | |
e74f64b9 | 439 | (refs (references* out))) |
a9e5e92f LC |
440 | (return (and (equal? (string-append guile "/bin/guile") |
441 | (call-with-input-file out read)) | |
442 | (equal? (list guile) refs))))) | |
443 | ||
4fbd1a2b LC |
444 | (test-assertm "gexp->file + #:splice?" |
445 | (mlet* %store-monad ((exp -> (list | |
446 | #~(define foo 'bar) | |
447 | #~(define guile #$%bootstrap-guile))) | |
448 | (guile (package-file %bootstrap-guile)) | |
449 | (drv (gexp->file "splice" exp #:splice? #t)) | |
450 | (out -> (derivation->output-path drv)) | |
451 | (done (built-derivations (list drv))) | |
452 | (refs (references* out))) | |
453 | (pk 'splice out) | |
454 | (return (and (equal? `((define foo 'bar) | |
455 | (define guile ,guile) | |
456 | ,(call-with-input-string "" read)) | |
457 | (call-with-input-file out | |
458 | (lambda (port) | |
459 | (list (read port) (read port) (read port))))) | |
460 | (equal? (list guile) refs))))) | |
461 | ||
21b679f6 LC |
462 | (test-assertm "gexp->derivation" |
463 | (mlet* %store-monad ((file (text-file "foo" "Hello, world!")) | |
464 | (exp -> (gexp | |
465 | (begin | |
466 | (mkdir (ungexp output)) | |
467 | (chdir (ungexp output)) | |
468 | (symlink | |
469 | (string-append (ungexp %bootstrap-guile) | |
470 | "/bin/guile") | |
471 | "foo") | |
472 | (symlink (ungexp file) | |
473 | (ungexp output "2nd"))))) | |
474 | (drv (gexp->derivation "foo" exp)) | |
475 | (out -> (derivation->output-path drv)) | |
476 | (out2 -> (derivation->output-path drv "2nd")) | |
477 | (done (built-derivations (list drv))) | |
e74f64b9 LC |
478 | (refs (references* out)) |
479 | (refs2 (references* out2)) | |
21b679f6 LC |
480 | (guile (package-file %bootstrap-guile "bin/guile"))) |
481 | (return (and (string=? (readlink (string-append out "/foo")) guile) | |
482 | (string=? (readlink out2) file) | |
483 | (equal? refs (list (dirname (dirname guile)))) | |
484 | (equal? refs2 (list file)))))) | |
485 | ||
ce45eb4c | 486 | (test-assertm "gexp->derivation vs. grafts" |
ef8de985 LC |
487 | (mlet* %store-monad ((graft? (set-grafting #f)) |
488 | (p0 -> (dummy-package "dummy" | |
ce45eb4c LC |
489 | (arguments |
490 | '(#:implicit-inputs? #f)))) | |
491 | (r -> (package (inherit p0) (name "DuMMY"))) | |
492 | (p1 -> (package (inherit p0) (replacement r))) | |
493 | (exp0 -> (gexp (frob (ungexp p0) (ungexp output)))) | |
494 | (exp1 -> (gexp (frob (ungexp p1) (ungexp output)))) | |
495 | (void (set-guile-for-build %bootstrap-guile)) | |
ef8de985 LC |
496 | (drv0 (gexp->derivation "t" exp0 #:graft? #t)) |
497 | (drv1 (gexp->derivation "t" exp1 #:graft? #t)) | |
498 | (drv1* (gexp->derivation "t" exp1 #:graft? #f)) | |
499 | (_ (set-grafting graft?))) | |
ce45eb4c LC |
500 | (return (and (not (string=? (derivation->output-path drv0) |
501 | (derivation->output-path drv1))) | |
502 | (string=? (derivation->output-path drv0) | |
503 | (derivation->output-path drv1*)))))) | |
504 | ||
21b679f6 LC |
505 | (test-assertm "gexp->derivation, composed gexps" |
506 | (mlet* %store-monad ((exp0 -> (gexp (begin | |
507 | (mkdir (ungexp output)) | |
508 | (chdir (ungexp output))))) | |
509 | (exp1 -> (gexp (symlink | |
510 | (string-append (ungexp %bootstrap-guile) | |
511 | "/bin/guile") | |
512 | "foo"))) | |
513 | (exp -> (gexp (begin (ungexp exp0) (ungexp exp1)))) | |
514 | (drv (gexp->derivation "foo" exp)) | |
515 | (out -> (derivation->output-path drv)) | |
516 | (done (built-derivations (list drv))) | |
517 | (guile (package-file %bootstrap-guile "bin/guile"))) | |
518 | (return (string=? (readlink (string-append out "/foo")) | |
519 | guile)))) | |
520 | ||
5d098459 LC |
521 | (test-assertm "gexp->derivation, default system" |
522 | ;; The default system should be the one at '>>=' time, not the one at | |
523 | ;; invocation time. See <http://bugs.gnu.org/18002>. | |
524 | (let ((system (%current-system)) | |
525 | (mdrv (parameterize ((%current-system "foobar64-linux")) | |
526 | (gexp->derivation "foo" | |
527 | (gexp | |
528 | (mkdir (ungexp output))))))) | |
529 | (mlet %store-monad ((drv mdrv)) | |
530 | (return (string=? system (derivation-system drv)))))) | |
531 | ||
d9ae938f LC |
532 | (test-assertm "gexp->derivation, local-file" |
533 | (mlet* %store-monad ((file -> (search-path %load-path "guix.scm")) | |
020f3e41 | 534 | (intd (interned-file file #:recursive? #f)) |
d9ae938f LC |
535 | (local -> (local-file file)) |
536 | (exp -> (gexp (begin | |
537 | (stat (ungexp local)) | |
538 | (symlink (ungexp local) | |
539 | (ungexp output))))) | |
540 | (drv (gexp->derivation "local-file" exp))) | |
541 | (mbegin %store-monad | |
542 | (built-derivations (list drv)) | |
543 | (return (string=? (readlink (derivation->output-path drv)) | |
544 | intd))))) | |
545 | ||
68a61e9f LC |
546 | (test-assertm "gexp->derivation, cross-compilation" |
547 | (mlet* %store-monad ((target -> "mips64el-linux") | |
548 | (exp -> (gexp (list (ungexp coreutils) | |
549 | (ungexp output)))) | |
550 | (xdrv (gexp->derivation "foo" exp | |
551 | #:target target)) | |
e74f64b9 | 552 | (refs (references* |
68a61e9f LC |
553 | (derivation-file-name xdrv))) |
554 | (xcu (package->cross-derivation coreutils | |
555 | target)) | |
556 | (cu (package->derivation coreutils))) | |
557 | (return (and (member (derivation-file-name xcu) refs) | |
558 | (not (member (derivation-file-name cu) refs)))))) | |
559 | ||
667b2508 LC |
560 | (test-assertm "gexp->derivation, ungexp-native" |
561 | (mlet* %store-monad ((target -> "mips64el-linux") | |
562 | (exp -> (gexp (list (ungexp-native coreutils) | |
563 | (ungexp output)))) | |
564 | (xdrv (gexp->derivation "foo" exp | |
565 | #:target target)) | |
566 | (drv (gexp->derivation "foo" exp))) | |
567 | (return (string=? (derivation-file-name drv) | |
568 | (derivation-file-name xdrv))))) | |
569 | ||
570 | (test-assertm "gexp->derivation, ungexp + ungexp-native" | |
571 | (mlet* %store-monad ((target -> "mips64el-linux") | |
572 | (exp -> (gexp (list (ungexp-native coreutils) | |
573 | (ungexp glibc) | |
574 | (ungexp output)))) | |
575 | (xdrv (gexp->derivation "foo" exp | |
576 | #:target target)) | |
e74f64b9 | 577 | (refs (references* |
667b2508 LC |
578 | (derivation-file-name xdrv))) |
579 | (xglibc (package->cross-derivation glibc target)) | |
580 | (cu (package->derivation coreutils))) | |
581 | (return (and (member (derivation-file-name cu) refs) | |
582 | (member (derivation-file-name xglibc) refs))))) | |
583 | ||
584 | (test-assertm "gexp->derivation, ungexp-native + composed gexps" | |
585 | (mlet* %store-monad ((target -> "mips64el-linux") | |
586 | (exp0 -> (gexp (list 1 2 | |
587 | (ungexp coreutils)))) | |
588 | (exp -> (gexp (list 0 (ungexp-native exp0)))) | |
589 | (xdrv (gexp->derivation "foo" exp | |
590 | #:target target)) | |
591 | (drv (gexp->derivation "foo" exp))) | |
592 | (return (string=? (derivation-file-name drv) | |
593 | (derivation-file-name xdrv))))) | |
594 | ||
6fd1a796 LC |
595 | (test-assertm "gexp->derivation, store copy" |
596 | (let ((build-one #~(call-with-output-file #$output | |
597 | (lambda (port) | |
598 | (display "This is the one." port)))) | |
599 | (build-two (lambda (one) | |
600 | #~(begin | |
601 | (mkdir #$output) | |
602 | (symlink #$one (string-append #$output "/one")) | |
603 | (call-with-output-file (string-append #$output "/two") | |
604 | (lambda (port) | |
605 | (display "This is the second one." port)))))) | |
b53833b2 LC |
606 | (build-drv #~(begin |
607 | (use-modules (guix build store-copy)) | |
6fd1a796 | 608 | |
b53833b2 LC |
609 | (mkdir #$output) |
610 | (populate-store '("graph") #$output)))) | |
6fd1a796 LC |
611 | (mlet* %store-monad ((one (gexp->derivation "one" build-one)) |
612 | (two (gexp->derivation "two" (build-two one))) | |
b53833b2 | 613 | (drv (gexp->derivation "store-copy" build-drv |
6fd1a796 | 614 | #:references-graphs |
b53833b2 | 615 | `(("graph" ,two)) |
6fd1a796 LC |
616 | #:modules |
617 | '((guix build store-copy) | |
d4e9317b LC |
618 | (guix progress) |
619 | (guix records) | |
6892f0a2 | 620 | (guix sets) |
6fd1a796 LC |
621 | (guix build utils)))) |
622 | (ok? (built-derivations (list drv))) | |
623 | (out -> (derivation->output-path drv))) | |
624 | (let ((one (derivation->output-path one)) | |
625 | (two (derivation->output-path two))) | |
626 | (return (and ok? | |
627 | (file-exists? (string-append out "/" one)) | |
628 | (file-exists? (string-append out "/" two)) | |
629 | (file-exists? (string-append out "/" two "/two")) | |
630 | (string=? (readlink (string-append out "/" two "/one")) | |
631 | one))))))) | |
632 | ||
aa72d9af LC |
633 | (test-assertm "imported-files" |
634 | (mlet* %store-monad | |
635 | ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm")) | |
636 | ("a/b/c" . ,(search-path %load-path | |
637 | "guix/derivations.scm")) | |
638 | ("p/q" . ,(search-path %load-path "guix.scm")) | |
639 | ("p/z" . ,(search-path %load-path "guix/store.scm")))) | |
8df2eca6 | 640 | (dir (imported-files files))) |
aa72d9af | 641 | (mbegin %store-monad |
8df2eca6 LC |
642 | (return |
643 | (every (match-lambda | |
644 | ((path . source) | |
645 | (equal? (call-with-input-file (string-append dir "/" path) | |
646 | get-bytevector-all) | |
647 | (call-with-input-file source | |
648 | get-bytevector-all)))) | |
649 | files))))) | |
aa72d9af | 650 | |
d938a58b LC |
651 | (test-assertm "imported-files with file-like objects" |
652 | (mlet* %store-monad ((plain -> (plain-file "foo" "bar!")) | |
653 | (q-scm -> (search-path %load-path "ice-9/q.scm")) | |
654 | (files -> `(("a/b/c" . ,q-scm) | |
655 | ("p/q" . ,plain))) | |
656 | (drv (imported-files files))) | |
e529d468 LC |
657 | (define (file=? file1 file2) |
658 | ;; Assume deduplication is in place. | |
659 | (= (stat:ino (lstat file1)) | |
660 | (stat:ino (lstat file2)))) | |
661 | ||
d938a58b LC |
662 | (mbegin %store-monad |
663 | (built-derivations (list drv)) | |
664 | (mlet %store-monad ((dir -> (derivation->output-path drv)) | |
665 | (plain* (text-file "foo" "bar!")) | |
666 | (q-scm* (interned-file q-scm "c"))) | |
667 | (return | |
e529d468 LC |
668 | (and (file=? (string-append dir "/a/b/c") q-scm*) |
669 | (file=? (string-append dir "/p/q") plain*))))))) | |
d938a58b | 670 | |
0bb9929e LC |
671 | (test-equal "gexp-modules & ungexp" |
672 | '((bar) (foo)) | |
673 | ((@@ (guix gexp) gexp-modules) | |
674 | #~(foo #$(with-imported-modules '((foo)) #~+) | |
675 | #+(with-imported-modules '((bar)) #~-)))) | |
676 | ||
677 | (test-equal "gexp-modules & ungexp-splicing" | |
678 | '((foo) (bar)) | |
679 | ((@@ (guix gexp) gexp-modules) | |
680 | #~(foo #$@(list (with-imported-modules '((foo)) #~+) | |
681 | (with-imported-modules '((bar)) #~-))))) | |
682 | ||
2363bdd7 LC |
683 | (test-equal "gexp-modules and literal Scheme object" |
684 | '() | |
685 | (gexp-modules #t)) | |
686 | ||
aa72d9af LC |
687 | (test-assertm "gexp->derivation #:modules" |
688 | (mlet* %store-monad | |
689 | ((build -> #~(begin | |
690 | (use-modules (guix build utils)) | |
691 | (mkdir-p (string-append #$output "/guile/guix/nix")) | |
692 | #t)) | |
693 | (drv (gexp->derivation "test-with-modules" build | |
694 | #:modules '((guix build utils))))) | |
695 | (mbegin %store-monad | |
696 | (built-derivations (list drv)) | |
697 | (let* ((p (derivation->output-path drv)) | |
698 | (s (stat (string-append p "/guile/guix/nix")))) | |
699 | (return (eq? (stat:type s) 'directory)))))) | |
700 | ||
0bb9929e LC |
701 | (test-assertm "gexp->derivation & with-imported-modules" |
702 | ;; Same test as above, but using 'with-imported-modules'. | |
703 | (mlet* %store-monad | |
704 | ((build -> (with-imported-modules '((guix build utils)) | |
705 | #~(begin | |
706 | (use-modules (guix build utils)) | |
707 | (mkdir-p (string-append #$output "/guile/guix/nix")) | |
708 | #t))) | |
709 | (drv (gexp->derivation "test-with-modules" build))) | |
710 | (mbegin %store-monad | |
711 | (built-derivations (list drv)) | |
712 | (let* ((p (derivation->output-path drv)) | |
713 | (s (stat (string-append p "/guile/guix/nix")))) | |
714 | (return (eq? (stat:type s) 'directory)))))) | |
715 | ||
716 | (test-assertm "gexp->derivation & nested with-imported-modules" | |
717 | (mlet* %store-monad | |
718 | ((build1 -> (with-imported-modules '((guix build utils)) | |
719 | #~(begin | |
720 | (use-modules (guix build utils)) | |
721 | (mkdir-p (string-append #$output "/guile/guix/nix")) | |
722 | #t))) | |
723 | (build2 -> (with-imported-modules '((guix build bournish)) | |
724 | #~(begin | |
725 | (use-modules (guix build bournish) | |
726 | (system base compile)) | |
727 | #+build1 | |
728 | (call-with-output-file (string-append #$output "/b") | |
729 | (lambda (port) | |
730 | (write | |
731 | (read-and-compile (open-input-string "cd /foo") | |
732 | #:from %bournish-language | |
733 | #:to 'scheme) | |
734 | port)))))) | |
735 | (drv (gexp->derivation "test-with-modules" build2))) | |
736 | (mbegin %store-monad | |
737 | (built-derivations (list drv)) | |
738 | (let* ((p (derivation->output-path drv)) | |
739 | (s (stat (string-append p "/guile/guix/nix"))) | |
740 | (b (string-append p "/b"))) | |
741 | (return (and (eq? (stat:type s) 'directory) | |
742 | (equal? '(chdir "/foo") | |
743 | (call-with-input-file b read)))))))) | |
744 | ||
d938a58b LC |
745 | (test-assertm "gexp->derivation & with-imported-module & computed module" |
746 | (mlet* %store-monad | |
4fbd1a2b | 747 | ((module -> (scheme-file "x" #~(;; splice! |
d938a58b LC |
748 | (define-module (foo bar) |
749 | #:export (the-answer)) | |
750 | ||
4fbd1a2b LC |
751 | (define the-answer 42)) |
752 | #:splice? #t)) | |
d938a58b LC |
753 | (build -> (with-imported-modules `(((foo bar) => ,module) |
754 | (guix build utils)) | |
755 | #~(begin | |
756 | (use-modules (guix build utils) | |
757 | (foo bar)) | |
758 | mkdir-p | |
759 | (call-with-output-file #$output | |
760 | (lambda (port) | |
761 | (write the-answer port)))))) | |
762 | (drv (gexp->derivation "thing" build)) | |
763 | (out -> (derivation->output-path drv))) | |
764 | (mbegin %store-monad | |
765 | (built-derivations (list drv)) | |
766 | (return (= 42 (call-with-input-file out read)))))) | |
767 | ||
838e17d8 LC |
768 | (test-equal "gexp-extensions & ungexp" |
769 | (list sed grep) | |
770 | ((@@ (guix gexp) gexp-extensions) | |
771 | #~(foo #$(with-extensions (list grep) #~+) | |
772 | #+(with-extensions (list sed) #~-)))) | |
773 | ||
774 | (test-equal "gexp-extensions & ungexp-splicing" | |
775 | (list grep sed) | |
776 | ((@@ (guix gexp) gexp-extensions) | |
777 | #~(foo #$@(list (with-extensions (list grep) #~+) | |
778 | (with-imported-modules '((foo)) | |
779 | (with-extensions (list sed) #~-)))))) | |
780 | ||
781 | (test-equal "gexp-extensions and literal Scheme object" | |
782 | '() | |
783 | ((@@ (guix gexp) gexp-extensions) #t)) | |
784 | ||
785 | (test-assertm "gexp->derivation & with-extensions" | |
786 | ;; Create a fake Guile extension and make sure it is accessible both to the | |
787 | ;; imported modules and to the derivation build script. | |
788 | (mlet* %store-monad | |
789 | ((extension -> %extension-package) | |
790 | (module -> (scheme-file "x" #~( ;; splice! | |
791 | (define-module (foo) | |
792 | #:use-module (hg2g) | |
793 | #:export (multiply)) | |
794 | ||
795 | (define (multiply x) | |
796 | (* the-answer x))) | |
797 | #:splice? #t)) | |
798 | (build -> (with-extensions (list extension) | |
799 | (with-imported-modules `((guix build utils) | |
800 | ((foo) => ,module)) | |
801 | #~(begin | |
802 | (use-modules (guix build utils) | |
803 | (hg2g) (foo)) | |
804 | (call-with-output-file #$output | |
805 | (lambda (port) | |
806 | (write (list the-answer (multiply 2)) | |
807 | port))))))) | |
808 | (drv (gexp->derivation "thingie" build | |
809 | ;; %BOOTSTRAP-GUILE is 2.0. | |
810 | #:effective-version "2.0")) | |
811 | (out -> (derivation->output-path drv))) | |
812 | (mbegin %store-monad | |
813 | (built-derivations (list drv)) | |
814 | (return (equal? '(42 84) (call-with-input-file out read)))))) | |
815 | ||
b53833b2 LC |
816 | (test-assertm "gexp->derivation #:references-graphs" |
817 | (mlet* %store-monad | |
72cd8ec0 | 818 | ((one (text-file "one" (random-text))) |
b53833b2 LC |
819 | (two (gexp->derivation "two" |
820 | #~(symlink #$one #$output:chbouib))) | |
66a35ceb | 821 | (build -> (with-imported-modules '((guix build store-copy) |
d4e9317b LC |
822 | (guix progress) |
823 | (guix records) | |
6892f0a2 | 824 | (guix sets) |
66a35ceb LC |
825 | (guix build utils)) |
826 | #~(begin | |
827 | (use-modules (guix build store-copy)) | |
828 | (with-output-to-file #$output | |
829 | (lambda () | |
6892f0a2 LC |
830 | (write (map store-info-item |
831 | (call-with-input-file "guile" | |
832 | read-reference-graph))))) | |
66a35ceb LC |
833 | (with-output-to-file #$output:one |
834 | (lambda () | |
6892f0a2 LC |
835 | (write (map store-info-item |
836 | (call-with-input-file "one" | |
837 | read-reference-graph))))) | |
66a35ceb LC |
838 | (with-output-to-file #$output:two |
839 | (lambda () | |
6892f0a2 LC |
840 | (write (map store-info-item |
841 | (call-with-input-file "two" | |
842 | read-reference-graph)))))))) | |
66a35ceb | 843 | (drv (gexp->derivation "ref-graphs" build |
b53833b2 LC |
844 | #:references-graphs `(("one" ,one) |
845 | ("two" ,two "chbouib") | |
66a35ceb | 846 | ("guile" ,%bootstrap-guile)))) |
b53833b2 LC |
847 | (ok? (built-derivations (list drv))) |
848 | (guile-drv (package->derivation %bootstrap-guile)) | |
686784d0 LC |
849 | (bash (interned-file (search-bootstrap-binary "bash" |
850 | (%current-system)) | |
851 | "bash" #:recursive? #t)) | |
b53833b2 LC |
852 | (g-one -> (derivation->output-path drv "one")) |
853 | (g-two -> (derivation->output-path drv "two")) | |
854 | (g-guile -> (derivation->output-path drv))) | |
855 | (return (and ok? | |
856 | (equal? (call-with-input-file g-one read) (list one)) | |
72cd8ec0 LC |
857 | (lset= string=? |
858 | (call-with-input-file g-two read) | |
859 | (list one (derivation->output-path two "chbouib"))) | |
686784d0 LC |
860 | |
861 | ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash. | |
72cd8ec0 LC |
862 | (lset= string=? |
863 | (call-with-input-file g-guile read) | |
864 | (list (derivation->output-path guile-drv) bash)))))) | |
b53833b2 | 865 | |
c8351d9a LC |
866 | (test-assertm "gexp->derivation #:allowed-references" |
867 | (mlet %store-monad ((drv (gexp->derivation "allowed-refs" | |
868 | #~(begin | |
869 | (mkdir #$output) | |
870 | (chdir #$output) | |
871 | (symlink #$output "self") | |
872 | (symlink #$%bootstrap-guile | |
873 | "guile")) | |
874 | #:allowed-references | |
875 | (list "out" %bootstrap-guile)))) | |
876 | (built-derivations (list drv)))) | |
877 | ||
accb682c LC |
878 | (test-assertm "gexp->derivation #:allowed-references, specific output" |
879 | (mlet* %store-monad ((in (gexp->derivation "thing" | |
880 | #~(begin | |
881 | (mkdir #$output:ok) | |
882 | (mkdir #$output:not-ok)))) | |
883 | (drv (gexp->derivation "allowed-refs" | |
884 | #~(begin | |
885 | (pk #$in:not-ok) | |
886 | (mkdir #$output) | |
887 | (chdir #$output) | |
888 | (symlink #$output "self") | |
889 | (symlink #$in:ok "ok")) | |
890 | #:allowed-references | |
891 | (list "out" | |
892 | (gexp-input in "ok"))))) | |
893 | (built-derivations (list drv)))) | |
894 | ||
c8351d9a LC |
895 | (test-assert "gexp->derivation #:allowed-references, disallowed" |
896 | (let ((drv (run-with-store %store | |
897 | (gexp->derivation "allowed-refs" | |
898 | #~(begin | |
899 | (mkdir #$output) | |
900 | (chdir #$output) | |
901 | (symlink #$%bootstrap-guile "guile")) | |
902 | #:allowed-references '())))) | |
903 | (guard (c ((nix-protocol-error? c) #t)) | |
904 | (build-derivations %store (list drv)) | |
905 | #f))) | |
906 | ||
3f4ecf32 LC |
907 | (test-assertm "gexp->derivation #:disallowed-references, allowed" |
908 | (mlet %store-monad ((drv (gexp->derivation "disallowed-refs" | |
909 | #~(begin | |
910 | (mkdir #$output) | |
911 | (chdir #$output) | |
912 | (symlink #$output "self") | |
913 | (symlink #$%bootstrap-guile | |
914 | "guile")) | |
915 | #:disallowed-references '()))) | |
916 | (built-derivations (list drv)))) | |
917 | ||
918 | ||
919 | (test-assert "gexp->derivation #:disallowed-references" | |
920 | (let ((drv (run-with-store %store | |
921 | (gexp->derivation "disallowed-refs" | |
922 | #~(begin | |
923 | (mkdir #$output) | |
924 | (chdir #$output) | |
925 | (symlink #$%bootstrap-guile "guile")) | |
926 | #:disallowed-references (list %bootstrap-guile))))) | |
927 | (guard (c ((nix-protocol-error? c) #t)) | |
928 | (build-derivations %store (list drv)) | |
929 | #f))) | |
930 | ||
c17b5ab4 | 931 | (define shebang |
c1bc358f | 932 | (string-append "#!" (derivation->output-path (%guile-for-build)) |
c17b5ab4 LC |
933 | "/bin/guile --no-auto-compile")) |
934 | ||
935 | ;; If we're going to hit the silly shebang limit (128 chars on Linux-based | |
936 | ;; systems), then skip the following test. | |
47b3124a | 937 | (test-skip (if (> (string-length shebang) 127) 2 0)) |
c17b5ab4 | 938 | |
21b679f6 LC |
939 | (test-assertm "gexp->script" |
940 | (mlet* %store-monad ((n -> (random (expt 2 50))) | |
941 | (exp -> (gexp | |
942 | (system* | |
943 | (string-append (ungexp %bootstrap-guile) | |
944 | "/bin/guile") | |
945 | "-c" (object->string | |
946 | '(display (expt (ungexp n) 2)))))) | |
947 | (drv (gexp->script "guile-thing" exp | |
948 | #:guile %bootstrap-guile)) | |
949 | (out -> (derivation->output-path drv)) | |
950 | (done (built-derivations (list drv)))) | |
951 | (let* ((pipe (open-input-pipe out)) | |
952 | (str (get-string-all pipe))) | |
953 | (return (and (zero? (close-pipe pipe)) | |
954 | (= (expt n 2) (string->number str))))))) | |
955 | ||
92bcccc5 | 956 | (test-assert "gexp->script #:module-path" |
1ae16033 LC |
957 | (call-with-temporary-directory |
958 | (lambda (directory) | |
959 | (define str | |
960 | "Fake (guix base32) module!") | |
961 | ||
962 | (mkdir (string-append directory "/guix")) | |
963 | (call-with-output-file (string-append directory "/guix/base32.scm") | |
964 | (lambda (port) | |
965 | (write `(begin (define-module (guix base32)) | |
966 | (define-public %fake! ,str)) | |
967 | port))) | |
968 | ||
92bcccc5 LF |
969 | (run-with-store %store |
970 | (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32)) | |
971 | (gexp (begin | |
972 | (use-modules (guix base32)) | |
973 | (write (list %load-path | |
974 | %fake!)))))) | |
975 | (drv (gexp->script "guile-thing" exp | |
976 | #:guile %bootstrap-guile | |
977 | #:module-path (list directory))) | |
978 | (out -> (derivation->output-path drv)) | |
979 | (done (built-derivations (list drv)))) | |
980 | (let* ((pipe (open-input-pipe out)) | |
981 | (data (read pipe))) | |
982 | (return (and (zero? (close-pipe pipe)) | |
983 | (match data | |
984 | ((load-path str*) | |
985 | (and (string=? str* str) | |
986 | (not (member directory load-path))))))))))))) | |
1ae16033 | 987 | |
15a01c72 LC |
988 | (test-assertm "program-file" |
989 | (let* ((n (random (expt 2 50))) | |
0bb9929e LC |
990 | (exp (with-imported-modules '((guix build utils)) |
991 | (gexp (begin | |
992 | (use-modules (guix build utils)) | |
993 | (display (ungexp n)))))) | |
15a01c72 | 994 | (file (program-file "program" exp |
15a01c72 LC |
995 | #:guile %bootstrap-guile))) |
996 | (mlet* %store-monad ((drv (lower-object file)) | |
997 | (out -> (derivation->output-path drv))) | |
998 | (mbegin %store-monad | |
999 | (built-derivations (list drv)) | |
1000 | (let* ((pipe (open-input-pipe out)) | |
1001 | (str (get-string-all pipe))) | |
1002 | (return (and (zero? (close-pipe pipe)) | |
1003 | (= n (string->number str))))))))) | |
1004 | ||
92bcccc5 | 1005 | (test-assert "program-file #:module-path" |
427ec19e LC |
1006 | (call-with-temporary-directory |
1007 | (lambda (directory) | |
1008 | (define text (random-text)) | |
1009 | ||
1010 | (call-with-output-file (string-append directory "/stupid-module.scm") | |
1011 | (lambda (port) | |
1012 | (write `(begin (define-module (stupid-module)) | |
1013 | (define-public %stupid-thing ,text)) | |
1014 | port))) | |
1015 | ||
1016 | (let* ((exp (with-imported-modules '((stupid-module)) | |
1017 | (gexp (begin | |
1018 | (use-modules (stupid-module)) | |
1019 | (display %stupid-thing))))) | |
1020 | (file (program-file "program" exp | |
1021 | #:guile %bootstrap-guile | |
1022 | #:module-path (list directory)))) | |
92bcccc5 LF |
1023 | (run-with-store %store |
1024 | (mlet* %store-monad ((drv (lower-object file)) | |
1025 | (out -> (derivation->output-path drv))) | |
1026 | (mbegin %store-monad | |
1027 | (built-derivations (list drv)) | |
1028 | (let* ((pipe (open-input-pipe out)) | |
1029 | (str (get-string-all pipe))) | |
1030 | (return (and (zero? (close-pipe pipe)) | |
1031 | (string=? text str))))))))))) | |
427ec19e | 1032 | |
838e17d8 LC |
1033 | (test-assertm "program-file & with-extensions" |
1034 | (let* ((exp (with-extensions (list %extension-package) | |
1035 | (gexp (begin | |
1036 | (use-modules (hg2g)) | |
1037 | (display the-answer))))) | |
1038 | (file (program-file "program" exp | |
1039 | #:guile %bootstrap-guile))) | |
1040 | (mlet* %store-monad ((drv (lower-object file)) | |
1041 | (out -> (derivation->output-path drv))) | |
1042 | (mbegin %store-monad | |
1043 | (built-derivations (list drv)) | |
1044 | (let* ((pipe (open-input-pipe out)) | |
1045 | (str (get-string-all pipe))) | |
1046 | (return (and (zero? (close-pipe pipe)) | |
1047 | (= 42 (string->number str))))))))) | |
1048 | ||
e1c153e0 LC |
1049 | (test-assertm "scheme-file" |
1050 | (let* ((text (plain-file "foo" "Hello, world!")) | |
1051 | (scheme (scheme-file "bar" #~(list "foo" #$text)))) | |
1052 | (mlet* %store-monad ((drv (lower-object scheme)) | |
1053 | (text (lower-object text)) | |
1054 | (out -> (derivation->output-path drv))) | |
1055 | (mbegin %store-monad | |
1056 | (built-derivations (list drv)) | |
e74f64b9 | 1057 | (mlet %store-monad ((refs (references* out))) |
e1c153e0 LC |
1058 | (return (and (equal? refs (list text)) |
1059 | (equal? `(list "foo" ,text) | |
1060 | (call-with-input-file out read))))))))) | |
1061 | ||
462a3fa3 | 1062 | (test-assert "text-file*" |
e74f64b9 LC |
1063 | (run-with-store %store |
1064 | (mlet* %store-monad | |
1065 | ((drv (package->derivation %bootstrap-guile)) | |
1066 | (guile -> (derivation->output-path drv)) | |
1067 | (file (text-file "bar" "This is bar.")) | |
1068 | (text (text-file* "foo" | |
1069 | %bootstrap-guile "/bin/guile " | |
1070 | (gexp-input %bootstrap-guile "out") "/bin/guile " | |
1071 | drv "/bin/guile " | |
1072 | file)) | |
1073 | (done (built-derivations (list text))) | |
1074 | (out -> (derivation->output-path text)) | |
1075 | (refs (references* out))) | |
1076 | ;; Make sure we get the right references and the right content. | |
1077 | (return (and (lset= string=? refs (list guile file)) | |
1078 | (equal? (call-with-input-file out get-string-all) | |
1079 | (string-append guile "/bin/guile " | |
1080 | guile "/bin/guile " | |
1081 | guile "/bin/guile " | |
1082 | file))))) | |
1083 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
462a3fa3 | 1084 | |
b751cde3 LC |
1085 | (test-assertm "mixed-text-file" |
1086 | (mlet* %store-monad ((file -> (mixed-text-file "mixed" | |
1087 | "export PATH=" | |
1088 | %bootstrap-guile "/bin")) | |
1089 | (drv (lower-object file)) | |
1090 | (out -> (derivation->output-path drv)) | |
1091 | (guile-drv (package->derivation %bootstrap-guile)) | |
1092 | (guile -> (derivation->output-path guile-drv))) | |
1093 | (mbegin %store-monad | |
1094 | (built-derivations (list drv)) | |
e74f64b9 | 1095 | (mlet %store-monad ((refs (references* out))) |
b751cde3 LC |
1096 | (return (and (string=? (string-append "export PATH=" guile "/bin") |
1097 | (call-with-input-file out get-string-all)) | |
1098 | (equal? refs (list guile)))))))) | |
1099 | ||
5dec93bb LC |
1100 | (test-assertm "file-union" |
1101 | (mlet* %store-monad ((union -> (file-union "union" | |
1102 | `(("a" ,(plain-file "a" "1")) | |
1103 | ("b/c/d" ,(plain-file "d" "2")) | |
1104 | ("e" ,(plain-file "e" "3"))))) | |
1105 | (drv (lower-object union)) | |
1106 | (out -> (derivation->output-path drv))) | |
1107 | (define (contents=? file str) | |
1108 | (string=? (call-with-input-file (string-append out "/" file) | |
1109 | get-string-all) | |
1110 | str)) | |
1111 | ||
1112 | (mbegin %store-monad | |
1113 | (built-derivations (list drv)) | |
1114 | (return (and (contents=? "a" "1") | |
1115 | (contents=? "b/c/d" "2") | |
1116 | (contents=? "e" "3")))))) | |
1117 | ||
a8afb9ae LC |
1118 | (test-assert "gexp->derivation vs. %current-target-system" |
1119 | (let ((mval (gexp->derivation "foo" | |
1120 | #~(begin | |
1121 | (mkdir #$output) | |
1122 | (foo #+gnu-make)) | |
1123 | #:target #f))) | |
1124 | ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no | |
1125 | ;; influence. | |
1126 | (parameterize ((%current-target-system "fooooo")) | |
1127 | (derivation? (run-with-store %store mval))))) | |
1128 | ||
c2b84676 LC |
1129 | (test-assertm "lower-object" |
1130 | (mlet %store-monad ((drv1 (lower-object %bootstrap-guile)) | |
1131 | (drv2 (lower-object (package-source coreutils))) | |
1132 | (item (lower-object (plain-file "foo" "Hello!")))) | |
1133 | (return (and (derivation? drv1) (derivation? drv2) | |
1134 | (store-path? item))))) | |
1135 | ||
91937029 LC |
1136 | (test-assertm "lower-object, computed-file" |
1137 | (let* ((text (plain-file "foo" "Hello!")) | |
1138 | (exp #~(begin | |
1139 | (mkdir #$output) | |
1140 | (symlink #$%bootstrap-guile | |
1141 | (string-append #$output "/guile")) | |
1142 | (symlink #$text (string-append #$output "/text")))) | |
1143 | (computed (computed-file "computed" exp))) | |
1144 | (mlet* %store-monad ((text (lower-object text)) | |
1145 | (guile-drv (lower-object %bootstrap-guile)) | |
1146 | (comp-drv (lower-object computed)) | |
1147 | (comp -> (derivation->output-path comp-drv))) | |
1148 | (mbegin %store-monad | |
1149 | (built-derivations (list comp-drv)) | |
1150 | (return (and (string=? (readlink (string-append comp "/guile")) | |
1151 | (derivation->output-path guile-drv)) | |
1152 | (string=? (readlink (string-append comp "/text")) | |
1153 | text))))))) | |
1154 | ||
3e43166f LC |
1155 | (test-assert "lower-object & gexp-input-error?" |
1156 | (guard (c ((gexp-input-error? c) | |
1157 | (gexp-error-invalid-input c))) | |
1158 | (run-with-store %store | |
1159 | (lower-object (current-module)) | |
1160 | #:guile-for-build (%guile-for-build)))) | |
1161 | ||
2cf0ea0d LC |
1162 | (test-assert "printer" |
1163 | (string-match "^#<gexp \\(string-append .*#<package coreutils.*\ | |
1164 | \"/bin/uname\"\\) [[:xdigit:]]+>$" | |
1165 | (with-output-to-string | |
1166 | (lambda () | |
1167 | (write | |
1168 | (gexp (string-append (ungexp coreutils) | |
1169 | "/bin/uname"))))))) | |
1170 | ||
1171 | (test-assert "printer vs. ungexp-splicing" | |
1172 | (string-match "^#<gexp .* [[:xdigit:]]+>$" | |
1173 | (with-output-to-string | |
1174 | (lambda () | |
1175 | ;; #~(begin #$@#~()) | |
1176 | (write | |
1177 | (gexp (begin (ungexp-splicing (gexp ()))))))))) | |
1178 | ||
21b679f6 LC |
1179 | (test-equal "sugar" |
1180 | '(gexp (foo (ungexp bar) (ungexp baz "out") | |
1181 | (ungexp (chbouib 42)) | |
667b2508 LC |
1182 | (ungexp-splicing (list x y z)) |
1183 | (ungexp-native foo) (ungexp-native foo "out") | |
1184 | (ungexp-native (chbouib 42)) | |
1185 | (ungexp-native-splicing (list x y z)))) | |
1186 | '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z) | |
1187 | #+foo #+foo:out #+(chbouib 42) #+@(list x y z))) | |
21b679f6 LC |
1188 | |
1189 | (test-end "gexp") | |
1190 | ||
21b679f6 LC |
1191 | ;; Local Variables: |
1192 | ;; eval: (put 'test-assertm 'scheme-indent-function 1) | |
1193 | ;; End: |