guix package: Never remove the current generation and warn about it.
[jackhill/guix/guix.git] / tests / guix-package.sh
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 # Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
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 #
21 # Test the `guix package' command-line utility.
22 #
23
24 guix package --version
25
26 readlink_base ()
27 {
28 basename `readlink "$1"`
29 }
30
31 # Return true if a typical shebang in the store would not exceed Linux's
32 # default static limit.
33 shebang_not_too_long ()
34 {
35 test `echo $NIX_STORE_DIR/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bootstrap-binaries-0/bin/bash | wc -c` \
36 -lt 128
37 }
38
39 module_dir="t-guix-package-$$"
40 profile="t-profile-$$"
41 rm -f "$profile"
42
43 trap 'rm -f "$profile" "$profile-"[0-9]* ; rm -rf "$module_dir" t-home-'"$$" EXIT
44
45 # Use `-e' with a non-package expression.
46 if guix package --bootstrap -e +;
47 then false; else true; fi
48
49 guix package --bootstrap -p "$profile" -i guile-bootstrap
50 test -L "$profile" && test -L "$profile-1-link"
51 test -f "$profile/bin/guile"
52
53 # Make sure the profile is a GC root.
54 guix gc --list-live | grep "`readlink "$profile-1-link"`"
55
56 # Installing the same package a second time does nothing.
57 guix package --bootstrap -p "$profile" -i guile-bootstrap
58 test -L "$profile" && test -L "$profile-1-link"
59 ! test -f "$profile-2-link"
60 test -f "$profile/bin/guile"
61
62 # No search path env. var. here.
63 guix package --search-paths -p "$profile"
64 test "`guix package --search-paths -p "$profile" | wc -l`" = 0
65
66 # Check whether we have network access and an acceptable shebang length.
67 if guile -c '(getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)' 2> /dev/null \
68 && shebang_not_too_long
69 then
70 boot_make="(@@ (gnu packages commencement) gnu-make-boot0)"
71 boot_make_drv="`guix build -e "$boot_make" | grep -v -e -debug`"
72 guix package --bootstrap -p "$profile" -i "$boot_make_drv"
73 test -L "$profile-2-link"
74 test -f "$profile/bin/make" && test -f "$profile/bin/guile"
75
76
77 # Check whether `--list-installed' works.
78 # XXX: Change the tests when `--install' properly extracts the package
79 # name and version string.
80 installed="`guix package -p "$profile" --list-installed | cut -f1 | xargs echo | sort`"
81 case "x$installed" in
82 "guile-bootstrap make-boot0")
83 true;;
84 "make-boot0 guile-bootstrap")
85 true;;
86 "*")
87 false;;
88 esac
89
90 test "`guix package -p "$profile" -I 'g.*e' | cut -f1`" = "guile-bootstrap"
91
92 # List generations.
93 test "`guix package -p "$profile" -l | cut -f1 | grep guile | head -n1`" \
94 = " guile-bootstrap"
95
96 # Exit with 1 when a generation does not exist.
97 if guix package -p "$profile" --list-generations=42;
98 then false; else true; fi
99 if guix package -p "$profile" --switch-generation=99;
100 then false; else true; fi
101
102 # Remove a package.
103 guix package --bootstrap -p "$profile" -r "guile-bootstrap"
104 test -L "$profile-3-link"
105 test -f "$profile/bin/make" && ! test -f "$profile/bin/guile"
106
107 # Roll back.
108 guix package --roll-back -p "$profile"
109 test "`readlink_base "$profile"`" = "$profile-2-link"
110 test -x "$profile/bin/guile" && test -x "$profile/bin/make"
111 guix package --roll-back -p "$profile"
112 test "`readlink_base "$profile"`" = "$profile-1-link"
113 test -x "$profile/bin/guile" && ! test -x "$profile/bin/make"
114
115 # Switch to the rolled generation and switch back.
116 guix package -p "$profile" --switch-generation=2
117 test "`readlink_base "$profile"`" = "$profile-2-link"
118 guix package -p "$profile" --switch-generation=-1
119 test "`readlink_base "$profile"`" = "$profile-1-link"
120
121 # Move to the empty profile.
122 for i in `seq 1 3`
123 do
124 guix package --bootstrap --roll-back -p "$profile"
125 ! test -f "$profile/bin"
126 ! test -f "$profile/lib"
127 test "`readlink_base "$profile"`" = "$profile-0-link"
128 done
129
130 # Test that '--list-generations' does not output the zeroth generation.
131 test -z "`guix package -p "$profile" -l 0`"
132
133 # Reinstall after roll-back to the empty profile.
134 guix package --bootstrap -p "$profile" -e "$boot_make"
135 test "`readlink_base "$profile"`" = "$profile-1-link"
136 test -x "$profile/bin/guile" && ! test -x "$profile/bin/make"
137
138 # Check that the first generation is the current one.
139 test "`guix package -p "$profile" -l 1 | cut -f3 | head -n1`" = "(current)"
140
141 # Roll-back to generation 0, and install---all at once.
142 guix package --bootstrap -p "$profile" --roll-back -i guile-bootstrap
143 test "`readlink_base "$profile"`" = "$profile-1-link"
144 test -x "$profile/bin/guile" && ! test -x "$profile/bin/make"
145
146 # Install Make.
147 guix package --bootstrap -p "$profile" -e "$boot_make"
148 test "`readlink_base "$profile"`" = "$profile-2-link"
149 test -x "$profile/bin/guile" && test -x "$profile/bin/make"
150 grep "`guix build -e "$boot_make"`" "$profile/manifest"
151
152 # Make a "hole" in the list of generations, and make sure we can
153 # roll back and switch "over" it.
154 rm "$profile-1-link"
155 guix package --bootstrap -p "$profile" --roll-back
156 test "`readlink_base "$profile"`" = "$profile-0-link"
157 guix package -p "$profile" --switch-generation=+1
158 test "`readlink_base "$profile"`" = "$profile-2-link"
159
160 # Make sure LIBRARY_PATH gets listed by `--search-paths'.
161 guix package --bootstrap -p "$profile" -i guile-bootstrap -i gcc-bootstrap
162 guix package --search-paths -p "$profile" | grep LIBRARY_PATH
163
164 # Roll back so we can delete #3 below.
165 guix package -p "$profile" --switch-generation=2
166
167 # Delete the third generation and check that it was actually deleted.
168 guix package -p "$profile" --delete-generations=3
169 test -z "`guix package -p "$profile" -l 3`"
170
171 # Exit with 1 when a generation does not exist.
172 if guix package -p "$profile" --delete-generations=42;
173 then false; else true; fi
174
175 # Exit with 0 when trying to delete the zeroth generation.
176 guix package -p "$profile" --delete-generations=0
177 fi
178
179 # Make sure multiple arguments to -i works.
180 guix package --bootstrap -i guile gcc -p "$profile" -n
181
182 # Make sure the `:' syntax works.
183 guix package --bootstrap -i "glibc:debug" -p "$profile" -n
184
185 # Make sure nonexistent outputs are reported.
186 guix package --bootstrap -i "guile-bootstrap:out" -p "$profile" -n
187 if guix package --bootstrap -i "guile-bootstrap:does-not-exist" -p "$profile" -n;
188 then false; else true; fi
189 if guix package --bootstrap -i "guile-bootstrap:does-not-exist" -p "$profile";
190 then false; else true; fi
191
192 # Check whether `--list-available' returns something sensible.
193 guix package -p "$profile" -A 'gui.*e' | grep guile
194
195 # Check whether `--show' returns something sensible.
196 guix package --show=guile | grep "^name: guile"
197
198 # Ensure `--show' doesn't fail for packages with non-package inputs.
199 guix package --show=texlive
200
201 # Search.
202 LC_MESSAGES=C
203 export LC_MESSAGES
204 test "`guix package -s "An example GNU package" | grep ^name:`" = \
205 "name: hello"
206 test -z "`guix package -s "n0t4r341p4ck4g3"`"
207
208 # Make sure `--search' can display all the packages.
209 guix package --search="" > /dev/null
210
211 # There's no generation older than 12 months, so the following command should
212 # have no effect.
213 generation="`readlink_base "$profile"`"
214 if guix package -p "$profile" --delete-generations=12m;
215 then false; else true; fi
216 test "`readlink_base "$profile"`" = "$generation"
217
218 # The following command should not delete the current generation, even though
219 # it matches the given pattern (see <http://bugs.gnu.org/19978>.) And since
220 # there's nothing else to delete, it should just fail.
221 guix package --list-generations -p "$profile"
222 if guix package --bootstrap -p "$profile" --delete-generations=1..
223 then false; else true; fi
224 test "`readlink_base "$profile"`" = "$generation"
225
226 # Make sure $profile is a GC root at this point.
227 real_profile="`readlink -f "$profile"`"
228 if guix gc -d "$real_profile"
229 then false; else true; fi
230 test -d "$real_profile"
231
232 # Now, let's remove all the symlinks to $real_profile, and make sure
233 # $real_profile is no longer a GC root.
234 rm "$profile" "$profile"-[0-9]-link
235 guix gc -d "$real_profile"
236 [ ! -d "$real_profile" ]
237
238 #
239 # Try with the default profile.
240 #
241
242 XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
243 export XDG_CACHE_HOME
244 HOME="$PWD/t-home-$$"
245 export HOME
246
247 mkdir -p "$HOME"
248
249 # Get the canonical directory name so that 'guix package' recognizes it.
250 HOME="`cd $HOME; pwd -P`"
251
252 guix package --bootstrap -i guile-bootstrap
253 test -L "$HOME/.guix-profile"
254 test -f "$HOME/.guix-profile/bin/guile"
255
256 if guile -c '(getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)' 2> /dev/null
257 then
258 guix package --bootstrap -e "$boot_make"
259 test -f "$HOME/.guix-profile/bin/make"
260 first_environment="`cd $HOME/.guix-profile ; pwd`"
261
262 guix package --bootstrap --roll-back
263 test -f "$HOME/.guix-profile/bin/guile"
264 ! test -f "$HOME/.guix-profile/bin/make"
265 test "`cd $HOME/.guix-profile ; pwd`" = "$first_environment"
266 fi
267
268 # Move to the empty profile.
269 default_profile="`readlink "$HOME/.guix-profile"`"
270 for i in `seq 1 3`
271 do
272 # Make sure the current generation is a GC root.
273 profile_link="`readlink "$default_profile"`"
274 guix gc --list-live | grep "`readlink "$profile_link"`"
275
276 guix package --bootstrap --roll-back
277 ! test -f "$HOME/.guix-profile/bin"
278 ! test -f "$HOME/.guix-profile/lib"
279 test "`readlink "$default_profile"`" = "$default_profile-0-link"
280 done
281
282 # Check whether '-p ~/.guix-profile' makes any difference.
283 # See <http://bugs.gnu.org/17939>.
284 if test -e "$HOME/.guix-profile-0-link"; then false; fi
285 if test -e "$HOME/.guix-profile-1-link"; then false; fi
286 guix package --bootstrap -p "$HOME/.guix-profile" -i guile-bootstrap
287 if test -e "$HOME/.guix-profile-1-link"; then false; fi
288 guix package --bootstrap --roll-back -p "$HOME/.guix-profile"
289 if test -e "$HOME/.guix-profile-0-link"; then false; fi
290
291 # Extraneous argument.
292 if guix package install foo-bar;
293 then false; else true; fi
294
295 # Make sure the "broken pipe" doesn't yield an error.
296 # Note: 'pipefail' is a Bash-specific option.
297 set -o pipefail || true
298 guix package -A g | head -1 2> "$HOME/err1"
299 guix package -I | head -1 2> "$HOME/err2"
300 test "`cat "$HOME/err1" "$HOME/err2"`" = ""
301
302 # Make sure '-L' extends the package module search path.
303 mkdir "$module_dir"
304
305 cat > "$module_dir/foo.scm"<<EOF
306 (define-module (foo)
307 #:use-module (guix packages)
308 #:use-module (gnu packages emacs))
309
310 (define-public x
311 (package (inherit emacs)
312 (name "emacs-foo-bar")
313 (version "42")))
314 EOF
315
316 guix package -A emacs-foo-bar -L "$module_dir" | grep 42
317 guix package -i emacs-foo-bar-42 -n -L "$module_dir"
318
319 # Same thing using the 'GUIX_PACKAGE_PATH' environment variable.
320 GUIX_PACKAGE_PATH="$module_dir"
321 export GUIX_PACKAGE_PATH
322 guix package -A emacs-foo-bar | grep 42
323 guix package -i emacs-foo-bar-42 -n
324
325 # Make sure patches that live under $GUIX_PACKAGE_PATH are found.
326 cat > "$module_dir/emacs.patch"<<EOF
327 This is a fake patch.
328 EOF
329 cat > "$module_dir/foo.scm"<<EOF
330 (define-module (foo)
331 #:use-module (guix packages)
332 #:use-module (gnu packages)
333 #:use-module (gnu packages emacs))
334
335 (define-public x
336 (package (inherit emacs)
337 (source (origin (inherit (package-source emacs))
338 (patches (list (search-patch "emacs.patch")))))
339 (name "emacs-foo-bar-patched")
340 (version "42")))
341 EOF
342 guix package -i emacs-foo-bar-patched -n
343
344 unset GUIX_PACKAGE_PATH
345
346 # Using 'GUIX_BUILD_OPTIONS'.
347 available="`guix package -A | sort`"
348 GUIX_BUILD_OPTIONS="--dry-run"
349 export GUIX_BUILD_OPTIONS
350
351 # Make sure $GUIX_BUILD_OPTIONS is not simply appended to the command-line,
352 # which would break 'guix package -A' and similar.
353 available2="`guix package -A | sort`"
354 test "$available2" = "$available"
355 guix package -I