import cran: Generate a valid 'license' field for "GPL".
[jackhill/guix/guix.git] / tests / profiles.scm
CommitLineData
a2078770 1;;; GNU Guix --- Functional package management for GNU
176febe3 2;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
343745c8 3;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
a2078770
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-profiles)
c1bc358f 21 #:use-module (guix tests)
a2078770 22 #:use-module (guix profiles)
462f5cca
LC
23 #:use-module (guix store)
24 #:use-module (guix monads)
ef8de985 25 #:use-module (guix grafts)
462f5cca
LC
26 #:use-module (guix packages)
27 #:use-module (guix derivations)
a0dac7a0 28 #:use-module (guix build-system trivial)
462f5cca 29 #:use-module (gnu packages bootstrap)
e39d1461 30 #:use-module ((gnu packages base) #:prefix packages:)
dedb17ad 31 #:use-module ((gnu packages guile) #:prefix packages:)
a2078770 32 #:use-module (ice-9 match)
ef8993e2 33 #:use-module (ice-9 regex)
d664f1b4
LC
34 #:use-module (ice-9 popen)
35 #:use-module (rnrs io ports)
ccda8f7d 36 #:use-module (srfi srfi-1)
79601521 37 #:use-module (srfi srfi-11)
a654dc4b 38 #:use-module (srfi srfi-34)
a2078770
LC
39 #:use-module (srfi srfi-64))
40
343745c8 41;; Test the (guix profiles) module.
a2078770 42
462f5cca 43(define %store
c1bc358f 44 (open-connection-for-tests))
a2078770 45
ef8de985
LC
46;; Globally disable grafts because they can trigger early builds.
47(%graft? #f)
48
ebf5ad46
LC
49(define-syntax-rule (test-assertm name exp)
50 (test-assert name
51 (run-with-store %store exp
52 #:guile-for-build (%guile-for-build))))
53
22ef06b8
LC
54(define-syntax-rule (test-equalm name value exp)
55 (test-equal name
56 value
57 (run-with-store %store exp
58 #:guile-for-build (%guile-for-build))))
59
a2078770
LC
60;; Example manifest entries.
61
f7554030
AK
62(define guile-1.8.8
63 (manifest-entry
64 (name "guile")
65 (version "1.8.8")
66 (item "/gnu/store/...")
67 (output "out")))
68
a2078770
LC
69(define guile-2.0.9
70 (manifest-entry
71 (name "guile")
72 (version "2.0.9")
a54c94a4 73 (item "/gnu/store/...")
a2078770
LC
74 (output "out")))
75
76(define guile-2.0.9:debug
77 (manifest-entry (inherit guile-2.0.9)
78 (output "debug")))
79
79601521
LC
80(define glibc
81 (manifest-entry
82 (name "glibc")
83 (version "2.19")
84 (item "/gnu/store/...")
85 (output "out")))
86
a2078770
LC
87\f
88(test-begin "profiles")
89
90(test-assert "manifest-installed?"
91 (let ((m (manifest (list guile-2.0.9 guile-2.0.9:debug))))
92 (and (manifest-installed? m (manifest-pattern (name "guile")))
93 (manifest-installed? m (manifest-pattern
94 (name "guile") (output "debug")))
95 (manifest-installed? m (manifest-pattern
96 (name "guile") (output "out")
97 (version "2.0.9")))
98 (not (manifest-installed?
99 m (manifest-pattern (name "guile") (version "1.8.8"))))
100 (not (manifest-installed?
101 m (manifest-pattern (name "guile") (output "foobar")))))))
102
103(test-assert "manifest-matching-entries"
104 (let* ((e (list guile-2.0.9 guile-2.0.9:debug))
105 (m (manifest e)))
106 (and (null? (manifest-matching-entries m
107 (list (manifest-pattern
108 (name "python")))))
109 (equal? e
110 (manifest-matching-entries m
111 (list (manifest-pattern
112 (name "guile")
113 (output #f)))))
114 (equal? (list guile-2.0.9)
115 (manifest-matching-entries m
116 (list (manifest-pattern
117 (name "guile")
118 (version "2.0.9"))))))))
119
120(test-assert "manifest-remove"
121 (let* ((m0 (manifest (list guile-2.0.9 guile-2.0.9:debug)))
122 (m1 (manifest-remove m0
123 (list (manifest-pattern (name "guile")))))
124 (m2 (manifest-remove m1
125 (list (manifest-pattern (name "guile"))))) ; same
126 (m3 (manifest-remove m2
127 (list (manifest-pattern
128 (name "guile") (output "debug")))))
129 (m4 (manifest-remove m3
130 (list (manifest-pattern (name "guile"))))))
131 (match (manifest-entries m2)
132 ((($ <manifest-entry> "guile" "2.0.9" "debug"))
133 (and (equal? m1 m2)
134 (null? (manifest-entries m3))
135 (null? (manifest-entries m4)))))))
136
f7554030
AK
137(test-assert "manifest-add"
138 (let* ((m0 (manifest '()))
139 (m1 (manifest-add m0 (list guile-1.8.8)))
140 (m2 (manifest-add m1 (list guile-2.0.9)))
141 (m3 (manifest-add m2 (list guile-2.0.9:debug)))
142 (m4 (manifest-add m3 (list guile-2.0.9:debug))))
143 (and (match (manifest-entries m1)
144 ((($ <manifest-entry> "guile" "1.8.8" "out")) #t)
145 (_ #f))
146 (match (manifest-entries m2)
147 ((($ <manifest-entry> "guile" "2.0.9" "out")) #t)
148 (_ #f))
149 (equal? m3 m4))))
150
343745c8
AK
151(test-assert "manifest-perform-transaction"
152 (let* ((m0 (manifest (list guile-2.0.9 guile-2.0.9:debug)))
153 (t1 (manifest-transaction
154 (install (list guile-1.8.8))
155 (remove (list (manifest-pattern (name "guile")
156 (output "debug"))))))
157 (t2 (manifest-transaction
158 (remove (list (manifest-pattern (name "guile")
159 (version "2.0.9")
160 (output #f))))))
161 (m1 (manifest-perform-transaction m0 t1))
162 (m2 (manifest-perform-transaction m1 t2))
163 (m3 (manifest-perform-transaction m0 t2)))
164 (and (match (manifest-entries m1)
165 ((($ <manifest-entry> "guile" "1.8.8" "out")) #t)
166 (_ #f))
167 (equal? m1 m2)
168 (null? (manifest-entries m3)))))
169
79601521
LC
170(test-assert "manifest-transaction-effects"
171 (let* ((m0 (manifest (list guile-1.8.8)))
172 (t (manifest-transaction
173 (install (list guile-2.0.9 glibc))
174 (remove (list (manifest-pattern (name "coreutils")))))))
46b23e1a 175 (let-values (((remove install upgrade downgrade)
79601521 176 (manifest-transaction-effects m0 t)))
46b23e1a 177 (and (null? remove) (null? downgrade)
79601521 178 (equal? (list glibc) install)
ef8993e2
LC
179 (equal? (list (cons guile-1.8.8 guile-2.0.9)) upgrade)))))
180
46b23e1a
LC
181(test-assert "manifest-transaction-effects and downgrades"
182 (let* ((m0 (manifest (list guile-2.0.9)))
183 (t (manifest-transaction (install (list guile-1.8.8)))))
184 (let-values (((remove install upgrade downgrade)
185 (manifest-transaction-effects m0 t)))
186 (and (null? remove) (null? install) (null? upgrade)
187 (equal? (list (cons guile-2.0.9 guile-1.8.8)) downgrade)))))
188
3bea13bb
LC
189(test-assert "manifest-transaction-effects and pseudo-upgrades"
190 (let* ((m0 (manifest (list guile-2.0.9)))
191 (t (manifest-transaction (install (list guile-2.0.9)))))
192 (let-values (((remove install upgrade downgrade)
193 (manifest-transaction-effects m0 t)))
194 (and (null? remove) (null? install) (null? downgrade)
195 (equal? (list (cons guile-2.0.9 guile-2.0.9)) upgrade)))))
196
c8c25704
LC
197(test-assert "manifest-transaction-null?"
198 (manifest-transaction-null? (manifest-transaction)))
199
ebf5ad46
LC
200(test-assertm "profile-derivation"
201 (mlet* %store-monad
202 ((entry -> (package->manifest-entry %bootstrap-guile))
203 (guile (package->derivation %bootstrap-guile))
204 (drv (profile-derivation (manifest (list entry))
a6562c7e
LC
205 #:hooks '()
206 #:locales? #f))
ebf5ad46
LC
207 (profile -> (derivation->output-path drv))
208 (bindir -> (string-append profile "/bin"))
209 (_ (built-derivations (list drv))))
210 (return (and (file-exists? (string-append bindir "/guile"))
211 (string=? (dirname (readlink bindir))
212 (derivation->output-path guile))))))
462f5cca 213
e39d1461
LC
214(test-assertm "profile-derivation, inputs"
215 (mlet* %store-monad
216 ((entry -> (package->manifest-entry packages:glibc "debug"))
217 (drv (profile-derivation (manifest (list entry))
a6562c7e
LC
218 #:hooks '()
219 #:locales? #f)))
e39d1461
LC
220 (return (derivation-inputs drv))))
221
176febe3
LC
222(test-assertm "profile-derivation, cross-compilation"
223 (mlet* %store-monad
224 ((manifest -> (packages->manifest (list packages:sed packages:grep)))
225 (target -> "arm-linux-gnueabihf")
226 (grep (package->cross-derivation packages:grep target))
227 (sed (package->cross-derivation packages:sed target))
228 (locales (package->derivation packages:glibc-utf8-locales))
229 (drv (profile-derivation manifest
230 #:hooks '()
231 #:locales? #t
232 #:target target)))
233 (define (find-input name)
234 (let ((name (string-append name ".drv")))
235 (any (lambda (input)
236 (let ((input (derivation-input-path input)))
237 (and (string-suffix? name input) input)))
238 (derivation-inputs drv))))
239
240 ;; The inputs for grep and sed should be cross-build derivations, but that
241 ;; for the glibc-utf8-locales should be a native build.
242 (return (and (string=? (derivation-system drv) (%current-system))
243 (string=? (find-input (package-full-name packages:grep))
244 (derivation-file-name grep))
245 (string=? (find-input (package-full-name packages:sed))
246 (derivation-file-name sed))
247 (string=? (find-input
248 (package-full-name packages:glibc-utf8-locales))
249 (derivation-file-name locales))))))
250
9e90fc77
LC
251(test-assert "package->manifest-entry defaults to \"out\""
252 (let ((outputs (package-outputs packages:glibc)))
253 (equal? (manifest-entry-output
254 (package->manifest-entry (package
255 (inherit packages:glibc)
256 (outputs (reverse outputs)))))
257 (manifest-entry-output
258 (package->manifest-entry packages:glibc))
259 "out")))
260
dedb17ad
LC
261(test-assertm "profile-manifest, search-paths"
262 (mlet* %store-monad
263 ((guile -> (package
264 (inherit %bootstrap-guile)
265 (native-search-paths
266 (package-native-search-paths packages:guile-2.0))))
267 (entry -> (package->manifest-entry guile))
268 (drv (profile-derivation (manifest (list entry))
a6562c7e
LC
269 #:hooks '()
270 #:locales? #f))
dedb17ad
LC
271 (profile -> (derivation->output-path drv)))
272 (mbegin %store-monad
273 (built-derivations (list drv))
274
275 ;; Read the manifest back and make sure search paths are preserved.
276 (let ((manifest (profile-manifest profile)))
277 (match (manifest-entries manifest)
278 ((result)
279 (return (equal? (manifest-entry-search-paths result)
280 (manifest-entry-search-paths entry)
281 (package-native-search-paths
282 packages:guile-2.0)))))))))
d664f1b4 283
ccda8f7d
LC
284(test-assert "package->manifest-entry, search paths"
285 ;; See <http://bugs.gnu.org/22073>.
286 (let ((mpl (@ (gnu packages python) python2-matplotlib)))
287 (lset= eq?
288 (package-transitive-native-search-paths mpl)
289 (manifest-entry-search-paths
290 (package->manifest-entry mpl)))))
291
55b4715f
LC
292(test-equal "packages->manifest, propagated inputs"
293 (map (match-lambda
294 ((label package)
295 (list (package-name package) (package-version package)
296 package)))
297 (package-propagated-inputs packages:guile-2.2))
298 (map (lambda (entry)
299 (list (manifest-entry-name entry)
300 (manifest-entry-version entry)
301 (manifest-entry-item entry)))
302 (manifest-entry-dependencies
303 (package->manifest-entry packages:guile-2.2))))
304
b3a00885
LC
305(test-assert "manifest-entry-parent"
306 (let ((entry (package->manifest-entry packages:guile-2.2)))
307 (match (manifest-entry-dependencies entry)
308 ((dependencies ..1)
309 (and (every (lambda (parent)
310 (eq? entry (force parent)))
311 (map manifest-entry-parent dependencies))
312 (not (force (manifest-entry-parent entry))))))))
313
55b4715f
LC
314(test-assertm "read-manifest"
315 (mlet* %store-monad ((manifest -> (packages->manifest
316 (list (package
317 (inherit %bootstrap-guile)
318 (native-search-paths
319 (package-native-search-paths
320 packages:guile-2.0))))))
321 (drv (profile-derivation manifest
322 #:hooks '()
323 #:locales? #f))
324 (out -> (derivation->output-path drv)))
325 (define (entry->sexp entry)
326 (list (manifest-entry-name entry)
327 (manifest-entry-version entry)
328 (manifest-entry-search-paths entry)
b3a00885
LC
329 (manifest-entry-dependencies entry)
330 (force (manifest-entry-parent entry))))
55b4715f
LC
331
332 (mbegin %store-monad
333 (built-derivations (list drv))
334 (let ((manifest2 (profile-manifest out)))
335 (return (equal? (map entry->sexp (manifest-entries manifest))
336 (map entry->sexp (manifest-entries manifest2))))))))
337
a654dc4b
LC
338(test-equal "collision"
339 '(("guile-bootstrap" "2.0") ("guile-bootstrap" "42"))
340 (guard (c ((profile-collision-error? c)
341 (let ((entry1 (profile-collision-error-entry c))
342 (entry2 (profile-collision-error-conflict c)))
343 (list (list (manifest-entry-name entry1)
344 (manifest-entry-version entry1))
345 (list (manifest-entry-name entry2)
346 (manifest-entry-version entry2))))))
347 (run-with-store %store
348 (mlet* %store-monad ((p0 -> (package
349 (inherit %bootstrap-guile)
350 (version "42")))
351 (p1 -> (dummy-package "p1"
352 (propagated-inputs `(("p0" ,p0)))))
353 (manifest -> (packages->manifest
354 (list %bootstrap-guile p1)))
355 (drv (profile-derivation manifest
356 #:hooks '()
357 #:locales? #f)))
358 (return #f)))))
359
360(test-equal "collision of propagated inputs"
361 '(("guile-bootstrap" "2.0") ("guile-bootstrap" "42"))
362 (guard (c ((profile-collision-error? c)
363 (let ((entry1 (profile-collision-error-entry c))
364 (entry2 (profile-collision-error-conflict c)))
365 (list (list (manifest-entry-name entry1)
366 (manifest-entry-version entry1))
367 (list (manifest-entry-name entry2)
368 (manifest-entry-version entry2))))))
369 (run-with-store %store
370 (mlet* %store-monad ((p0 -> (package
371 (inherit %bootstrap-guile)
372 (version "42")))
373 (p1 -> (dummy-package "p1"
374 (propagated-inputs
375 `(("guile" ,%bootstrap-guile)))))
376 (p2 -> (dummy-package "p2"
377 (propagated-inputs
378 `(("guile" ,p0)))))
379 (manifest -> (packages->manifest (list p1 p2)))
380 (drv (profile-derivation manifest
381 #:hooks '()
382 #:locales? #f)))
383 (return #f)))))
384
385(test-assertm "no collision"
386 ;; Here we have an entry that is "lowered" (its 'item' field is a store file
387 ;; name) and another entry (its 'item' field is a package) that is
388 ;; equivalent.
389 (mlet* %store-monad ((p -> (dummy-package "p"
390 (propagated-inputs
391 `(("guile" ,%bootstrap-guile)))))
392 (guile (package->derivation %bootstrap-guile))
393 (entry -> (manifest-entry
394 (inherit (package->manifest-entry
395 %bootstrap-guile))
396 (item (derivation->output-path guile))))
397 (manifest -> (manifest
398 (list entry
399 (package->manifest-entry p))))
400 (drv (profile-derivation manifest)))
401 (return (->bool drv))))
402
d664f1b4
LC
403(test-assertm "etc/profile"
404 ;; Make sure we get an 'etc/profile' file that at least defines $PATH.
405 (mlet* %store-monad
406 ((guile -> (package
407 (inherit %bootstrap-guile)
408 (native-search-paths
409 (package-native-search-paths packages:guile-2.0))))
410 (entry -> (package->manifest-entry guile))
411 (drv (profile-derivation (manifest (list entry))
a6562c7e
LC
412 #:hooks '()
413 #:locales? #f))
d664f1b4
LC
414 (profile -> (derivation->output-path drv)))
415 (mbegin %store-monad
416 (built-derivations (list drv))
417 (let* ((pipe (open-input-pipe
9e006fb3
TUBK
418 (string-append "unset GUIX_PROFILE; "
419 ;; 'source' is a Bashism; use '.' (dot).
420 ". " profile "/etc/profile; "
421 ;; Don't try to parse set(1) output because
422 ;; it differs among shells; just use echo.
423 "echo $PATH")))
424 (path (get-string-all pipe)))
d664f1b4
LC
425 (return
426 (and (zero? (close-pipe pipe))
9e006fb3 427 (string-contains path (string-append profile "/bin"))))))))
d664f1b4 428
a0dac7a0
LC
429(test-assertm "etc/profile when etc/ already exists"
430 ;; Here 'union-build' makes the profile's etc/ a symlink to the package's
431 ;; etc/ directory, which makes it read-only. Make sure the profile build
432 ;; handles that.
433 (mlet* %store-monad
434 ((thing -> (dummy-package "dummy"
435 (build-system trivial-build-system)
436 (arguments
437 `(#:guile ,%bootstrap-guile
438 #:builder
439 (let ((out (assoc-ref %outputs "out")))
440 (mkdir out)
441 (mkdir (string-append out "/etc"))
442 (call-with-output-file (string-append out "/etc/foo")
443 (lambda (port)
444 (display "foo!" port))))))))
445 (entry -> (package->manifest-entry thing))
446 (drv (profile-derivation (manifest (list entry))
a6562c7e
LC
447 #:hooks '()
448 #:locales? #f))
a0dac7a0
LC
449 (profile -> (derivation->output-path drv)))
450 (mbegin %store-monad
451 (built-derivations (list drv))
452 (return (and (file-exists? (string-append profile "/etc/profile"))
453 (string=? (call-with-input-file
454 (string-append profile "/etc/foo")
455 get-string-all)
456 "foo!"))))))
457
113c17a0
LC
458(test-assertm "etc/profile when etc/ is a symlink"
459 ;; When etc/ is a symlink, the unsymlink code in 0.8.2 would fail
460 ;; gracelessly because 'scandir' would return #f.
461 (mlet* %store-monad
462 ((thing -> (dummy-package "dummy"
463 (build-system trivial-build-system)
464 (arguments
465 `(#:guile ,%bootstrap-guile
466 #:builder
467 (let ((out (assoc-ref %outputs "out")))
468 (mkdir out)
469 (mkdir (string-append out "/foo"))
470 (symlink "foo" (string-append out "/etc"))
471 (call-with-output-file (string-append out "/etc/bar")
472 (lambda (port)
473 (display "foo!" port))))))))
474 (entry -> (package->manifest-entry thing))
475 (drv (profile-derivation (manifest (list entry))
a6562c7e
LC
476 #:hooks '()
477 #:locales? #f))
113c17a0
LC
478 (profile -> (derivation->output-path drv)))
479 (mbegin %store-monad
480 (built-derivations (list drv))
481 (return (and (file-exists? (string-append profile "/etc/profile"))
482 (string=? (call-with-input-file
483 (string-append profile "/etc/bar")
484 get-string-all)
485 "foo!"))))))
486
22ef06b8
LC
487(test-equalm "union vs. dangling symlink" ;<https://bugs.gnu.org/26949>
488 "does-not-exist"
489 (mlet* %store-monad
490 ((thing1 -> (dummy-package "dummy"
491 (build-system trivial-build-system)
492 (arguments
493 `(#:guile ,%bootstrap-guile
494 #:builder
495 (let ((out (assoc-ref %outputs "out")))
496 (mkdir out)
497 (symlink "does-not-exist"
498 (string-append out "/dangling"))
499 #t)))))
500 (thing2 -> (package (inherit thing1) (name "dummy2")))
501 (drv (profile-derivation (packages->manifest
502 (list thing1 thing2))
503 #:hooks '()
504 #:locales? #f))
505 (profile -> (derivation->output-path drv)))
506 (mbegin %store-monad
507 (built-derivations (list drv))
508 (return (readlink (readlink (string-append profile "/dangling")))))))
509
a2078770
LC
510(test-end "profiles")
511
a2078770
LC
512;;; Local Variables:
513;;; eval: (put 'dummy-package 'scheme-indent-function 1)
514;;; End: