t/unstow.t: convert to use subtests
[gnu-stow.git] / t / rc_options.t
blobcbbbee2738b2bb41aa73b0e69b6a5c9a807f4d25
1 #!/usr/bin/perl
3 # This file is part of GNU Stow.
5 # GNU Stow is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # GNU Stow is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see https://www.gnu.org/licenses/.
19 # Test processing of stowrc file.
22 use strict;
23 use warnings;
25 use Test::More tests => 34;
27 use testutil;
29 require 'stow';
31 # .stowrc files used for testing, relative to run_from/
32 my $CWD_RC_FILE = ".stowrc";
33 my $HOME_RC_FILE = "../.stowrc";
34 # Take the safe route and cowardly refuse to continue if there's
35 # already a file at $HOME_RC_FILE.
36 if (-e $HOME_RC_FILE) {
37 die "RC file location $HOME_RC_FILE already exists!\n";
40 my ($options, $pkgs_to_delete, $pkgs_to_stow);
42 # Init testing directory structure and overwrite ENV{HOME} to prevent
43 # squashing existing .stowrc file.
44 init_test_dirs();
46 # =========== RC Loading Tests ===========
47 # Basic parsing and loading rc file tests.
48 # ========================================
50 my $orig_HOME = $ENV{HOME};
53 # Test no .stowrc file anywhere
55 delete $ENV{HOME};
56 local @ARGV = ('dummy');
57 cd("$TEST_DIR/run_from");
58 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
59 is($options->{target}, "$ABS_TEST_DIR", "default --target with no .stowrc");
60 is($options->{dir}, "$ABS_TEST_DIR/run_from", "default -d with no .stowrc");
63 # Test .stowrc file in cwd with relative paths, and $HOME not defined
65 make_file($CWD_RC_FILE, <<HERE);
66 -d ../stow
67 --target ../target
68 HERE
69 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
70 is($options->{target}, "../target"
71 => "relative --target from \$PWD/.stowrc");
72 is($options->{dir}, "../stow"
73 => "relative -d from \$PWD/.stowrc");
75 $ENV{HOME} = $orig_HOME;
76 remove_file($CWD_RC_FILE);
79 # Test .stowrc file in cwd with absolute paths, and $HOME not defined
81 make_file($CWD_RC_FILE, <<HERE);
82 -d $ABS_TEST_DIR/stow
83 --target $ABS_TEST_DIR/target
84 HERE
85 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
86 is($options->{target}, "$ABS_TEST_DIR/target"
87 => "absolute --target from \$PWD/.stowrc");
88 is($options->{dir}, "$ABS_TEST_DIR/stow"
89 => "abs_test_dir -d from \$PWD/.stowrc");
91 $ENV{HOME} = $orig_HOME;
92 remove_file($CWD_RC_FILE);
95 # Test ~/.stowrc file with one relative option per line.
97 local @ARGV = ('dummy');
98 make_file($HOME_RC_FILE, <<HERE);
99 -d ../stow
100 --target ../target
101 HERE
103 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
104 is($options->{target}, "../target", "--target from \$HOME/.stowrc");
105 is($options->{dir}, "../stow", "-d from \$HOME/.stowrc");
108 # Test ~/.stowrc file with one absolute option per line.
110 local @ARGV = ('dummy');
111 make_file($HOME_RC_FILE, <<HERE);
112 -d $ABS_TEST_DIR/stow
113 --target $ABS_TEST_DIR/target
114 HERE
116 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
117 is($options->{target}, "$ABS_TEST_DIR/target"
118 => "--target from \$HOME/.stowrc");
119 is($options->{dir}, "$ABS_TEST_DIR/stow"
120 => "-d from \$HOME/.stowrc");
123 # Test that some but not all options ~/.stowrc file are overridden by
124 # .stowrc in cwd.
126 local @ARGV = ('dummy');
127 make_file($HOME_RC_FILE, <<HERE);
128 -d $ABS_TEST_DIR/stow-will-be-overridden
129 --target $ABS_TEST_DIR/target-will-be-overridden
130 --defer=info
131 HERE
132 make_file($CWD_RC_FILE, <<HERE);
133 -d $ABS_TEST_DIR/stow
134 --target $ABS_TEST_DIR/target
135 --defer=man
136 HERE
138 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
139 is($options->{target}, "$ABS_TEST_DIR/target"
140 => "--target overridden by \$PWD/.stowrc");
141 is($options->{dir}, "$ABS_TEST_DIR/stow"
142 => "-d overridden \$PWD/.stowrc");
143 is_deeply($options->{defer}, [qr(\Ainfo), qr(\Aman)],
144 'defer man and info');
145 unlink($CWD_RC_FILE) or die "Failed to unlink $CWD_RC_FILE";
148 # Test that scalar cli option overwrites conflicting ~/.stowrc option.
150 local @ARGV = ('-d', "$ABS_TEST_DIR/stow", 'dummy');
151 make_file($HOME_RC_FILE, <<HERE);
152 -d bad/path
153 HERE
154 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
155 is($options->{dir}, "$ABS_TEST_DIR/stow", "cli overwrite scalar rc option.");
158 # Test that list cli option merges with conflicting .stowrc option.
159 # Documentation states that .stowrc options are prepended to cli options.
161 local @ARGV = (
162 '--defer=man',
163 'dummy'
165 make_file($HOME_RC_FILE, <<HERE);
166 --defer=info
167 HERE
168 ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
169 is_deeply($options->{defer}, [qr(\Ainfo), qr(\Aman)],
170 'defer man and info');
172 # ======== Filepath Expansion Tests ========
173 # Test proper filepath expansion in rc file.
174 # Expansion is only applied to options that
175 # take a filepath, namely target and dir.
176 # ==========================================
180 # Test environment variable expansion function.
182 # Basic expansion
183 is(expand_environment('$HOME/stow'), "$ABS_TEST_DIR/stow", 'expand $HOME');
184 is(expand_environment('${HOME}/stow'), "$ABS_TEST_DIR/stow", 'expand ${HOME}');
186 delete $ENV{UNDEFINED}; # just in case
187 foreach my $var ('$UNDEFINED', '${UNDEFINED}') {
188 eval {
189 expand_environment($var, "--foo option");
193 "--foo option references undefined environment variable \$UNDEFINED; " .
194 "aborting!\n",
195 "expand $var"
199 # Expansion with an underscore.
200 $ENV{'WITH_UNDERSCORE'} = 'test string';
201 is(expand_environment('${WITH_UNDERSCORE}'), 'test string',
202 'expand ${WITH_UNDERSCORE}');
203 delete $ENV{'WITH_UNDERSCORE'};
204 # Expansion with escaped $
205 is(expand_environment('\$HOME/stow'), '$HOME/stow', 'expand \$HOME');
208 # Test tilde (~) expansion
210 # Basic expansion
211 is(expand_tilde('~/path'), "$ENV{HOME}/path", 'tilde expansion to $HOME');
212 # Should not expand if middle of path
213 is(expand_tilde('/path/~/here'), '/path/~/here', 'middle ~ not expanded');
214 # Test escaped ~
215 is(expand_tilde('\~/path'), '~/path', 'escaped tilde');
218 # Test that environment variable expansion is applied.
220 make_file($HOME_RC_FILE, <<'HERE');
221 --dir=$HOME/stow
222 --target=$HOME/stow
223 --ignore=\$HOME
224 --defer=\$HOME
225 --override=\$HOME
226 HERE
227 ($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options();
228 is($options->{dir}, "$ABS_TEST_DIR/stow",
229 "apply environment expansion on \$HOME/.stowrc --dir");
230 is($options->{target}, "$ABS_TEST_DIR/stow",
231 "apply environment expansion on \$HOME/.stowrc --target");
232 is_deeply($options->{ignore}, [qr(\$HOME\z)],
233 "environment expansion not applied on --ignore");
234 is_deeply($options->{defer}, [qr(\A\$HOME)],
235 "environment expansion not applied on --defer");
236 is_deeply($options->{override}, [qr(\A\$HOME)],
237 "environment expansion not applied on --override");
240 # Test that tilde expansion is applied in correct places.
242 make_file($HOME_RC_FILE, <<'HERE');
243 --dir=~/stow
244 --target=~/stow
245 --ignore=~/stow
246 --defer=~/stow
247 --override=~/stow
248 HERE
249 ($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options();
250 is($options->{dir}, "$ABS_TEST_DIR/stow",
251 "apply tilde expansion on \$HOME/.stowrc --dir");
252 is($options->{target}, "$ABS_TEST_DIR/stow",
253 "apply tilde expansion on \$HOME/.stowrc --target");
254 is_deeply($options->{ignore}, [qr(~/stow\z)],
255 "tilde expansion not applied on --ignore");
256 is_deeply($options->{defer}, [qr(\A~/stow)],
257 "tilde expansion not applied on --defer");
258 is_deeply($options->{override}, [qr(\A~/stow)],
259 "tilde expansion not applied on --override");
262 # Clean up files used for testing.
264 unlink $HOME_RC_FILE or die "Unable to clean up $HOME_RC_FILE.\n";
265 remove_dir($ABS_TEST_DIR);