Use File::Spec->abs2rel() instead of home-grown relative_path
[gnu-stow.git] / t / unstow_contents.t
blob7dd3d6b83d212c48a3e6cfc53886cd5424d73bed
1 #!/usr/local/bin/perl
4 # Testing unstow_contents()
7 # load as a library
8 BEGIN { use lib qw(.); require "t/util.pm"; require "stow"; }
10 use Test::More tests => 14;
11 use Test::Output;
12 use English qw(-no_match_vars);
14 # local utility
15 sub reset_state {
16 @Tasks = ();
17 @Conflicts = ();
18 %Link_Task_For = ();
19 %Dir_Task_For = ();
20 %Option = ();
21 return;
24 ### setup
25 eval { remove_dir('t/target'); };
26 eval { remove_dir('t/stow'); };
27 make_dir('t/target');
28 make_dir('t/stow');
30 chdir 't/target';
31 $Stow_Path= '../stow';
33 # Note that each of the following tests use a distinct set of files
36 # unstow a simple tree minimally
39 reset_state();
40 $Option{'verbose'} = 0;
42 make_dir('../stow/pkg1/bin1');
43 make_file('../stow/pkg1/bin1/file1');
44 make_link('bin1','../stow/pkg1/bin1');
46 unstow_contents('../stow/pkg1','./');
47 process_tasks();
48 ok(
49 scalar(@Conflicts) == 0 &&
50 -f '../stow/pkg1/bin1/file1' && ! -e 'bin1'
51 => 'unstow a simple tree'
55 # unstow a simple tree from an existing directory
57 reset_state();
58 $Option{'verbose'} = 0;
60 make_dir('lib2');
61 make_dir('../stow/pkg2/lib2');
62 make_file('../stow/pkg2/lib2/file2');
63 make_link('lib2/file2', '../../stow/pkg2/lib2/file2');
64 unstow_contents('../stow/pkg2','./');
65 process_tasks();
66 ok(
67 scalar(@Conflicts) == 0 &&
68 -f '../stow/pkg2/lib2/file2' && -d 'lib2'
69 => 'unstow simple tree from a pre-existing directory'
73 # fold tree after unstowing
75 reset_state();
76 $Option{'verbose'} = 0;
78 make_dir('bin3');
80 make_dir('../stow/pkg3a/bin3');
81 make_file('../stow/pkg3a/bin3/file3a');
82 make_link('bin3/file3a' => '../../stow/pkg3a/bin3/file3a'); # emulate stow
84 make_dir('../stow/pkg3b/bin3');
85 make_file('../stow/pkg3b/bin3/file3b');
86 make_link('bin3/file3b' => '../../stow/pkg3b/bin3/file3b'); # emulate stow
87 unstow_contents('../stow/pkg3b', './');
88 process_tasks();
89 ok(
90 scalar(@Conflicts) == 0 &&
91 -l 'bin3' &&
92 readlink('bin3') eq '../stow/pkg3a/bin3'
93 => 'fold tree after unstowing'
97 # existing link is owned by stow but is invalid so it gets removed anyway
99 reset_state();
100 $Option{'verbose'} = 0;
102 make_dir('bin4');
103 make_dir('../stow/pkg4/bin4');
104 make_file('../stow/pkg4/bin4/file4');
105 make_link('bin4/file4', '../../stow/pkg4/bin4/does-not-exist');
107 unstow_contents('../stow/pkg4', './');
108 process_tasks();
110 scalar(@Conflicts) == 0 &&
111 ! -e 'bin4/file4'
112 => q(remove invalid link owned by stow)
116 # Existing link is not owned by stow
118 reset_state();
119 $Option{'verbose'} = 0;
121 make_dir('../stow/pkg5/bin5');
122 make_link('bin5', '../not-stow');
124 unstow_contents('../stow/pkg5', './');
125 like(
126 $Conflicts[-1], qr(CONFLICT:.*existing target is not owned by stow)
127 => q(existing link not owned by stow)
130 # Target already exists, is owned by stow, but points to a different package
132 reset_state();
133 $Option{'verbose'} = 0;
135 make_dir('bin6');
136 make_dir('../stow/pkg6a/bin6');
137 make_file('../stow/pkg6a/bin6/file6');
138 make_link('bin6/file6', '../../stow/pkg6a/bin6/file6');
140 make_dir('../stow/pkg6b/bin6');
141 make_file('../stow/pkg6b/bin6/file6');
143 unstow_contents('../stow/pkg6b', './');
145 scalar(@Conflicts) == 0 &&
146 -l 'bin6/file6' &&
147 readlink('bin6/file6') eq '../../stow/pkg6a/bin6/file6'
148 => q(ignore existing link that points to a different package)
152 # Don't unlink anything under the stow directory
154 reset_state();
155 $Option{'verbose'} = 0;
157 make_dir('stow'); # make out stow dir a subdir of target
158 $Stow_Path = 'stow';
160 # emulate stowing into ourself (bizarre corner case or accident)
161 make_dir('stow/pkg7a/stow/pkg7b');
162 make_file('stow/pkg7a/stow/pkg7b/file7b');
163 make_link('stow/pkg7b', '../stow/pkg7a/stow/pkg7b');
165 unstow_contents('stow/pkg7b', './');
166 stderr_like(
167 sub { process_tasks(); },
168 qr/There are no outstanding operations to perform/,
169 'no tasks to process when unstowing pkg7b'
172 scalar(@Conflicts) == 0 &&
173 -l 'stow/pkg7b' &&
174 readlink('stow/pkg7b') eq '../stow/pkg7a/stow/pkg7b'
175 => q(don't unlink any nodes under the stow directory)
179 # Don't unlink any nodes under another stow directory
181 reset_state();
182 $Option{'verbose'} = 0;
184 make_dir('stow'); # make out stow dir a subdir of target
185 $Stow_Path = 'stow';
187 make_dir('stow2'); # make our alternate stow dir a subdir of target
188 make_file('stow2/.stow');
190 # emulate stowing into ourself (bizarre corner case or accident)
191 make_dir('stow/pkg8a/stow2/pkg8b');
192 make_file('stow/pkg8a/stow2/pkg8b/file8b');
193 make_link('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b');
195 unstow_contents('stow/pkg8a', './');
196 stderr_like(
197 sub { process_tasks(); },
198 qr/There are no outstanding operations to perform/,
199 'no tasks to process when unstowing pkg8a'
202 scalar(@Conflicts) == 0 &&
203 -l 'stow2/pkg8b' &&
204 readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b'
205 => q(don't unlink any nodes under another stow directory)
209 # overriding already stowed documentation
211 reset_state();
212 $Stow_Path = '../stow';
213 $Option{'verbose'} = 0;
214 $Option{'override'} = ['man9', 'info9'];
216 make_dir('../stow/pkg9a/man9/man1');
217 make_file('../stow/pkg9a/man9/man1/file9.1');
218 make_dir('man9/man1');
219 make_link('man9/man1/file9.1' => '../../../stow/pkg9a/man9/man1/file9.1'); # emulate stow
221 make_dir('../stow/pkg9b/man9/man1');
222 make_file('../stow/pkg9b/man9/man1/file9.1');
223 unstow_contents('../stow/pkg9b', './');
224 process_tasks();
225 ok(
226 scalar(@Conflicts) == 0 &&
227 !-l 'man9/man1/file9.1'
228 => 'overriding existing documentation files'
232 # deferring to already stowed documentation
234 reset_state();
235 $Option{'verbose'} = 0;
236 $Option{'defer'} = ['man10', 'info10'];
238 make_dir('../stow/pkg10a/man10/man1');
239 make_file('../stow/pkg10a/man10/man1/file10a.1');
240 make_dir('man10/man1');
241 make_link('man10/man1/file10a.1' => '../../../stow/pkg10a/man10/man1/file10a.1');
243 # need this to block folding
244 make_dir('../stow/pkg10b/man10/man1');
245 make_file('../stow/pkg10b/man10/man1/file10b.1');
246 make_link('man10/man1/file10b.1' => '../../../stow/pkg10b/man10/man1/file10b.1');
249 make_dir('../stow/pkg10c/man10/man1');
250 make_file('../stow/pkg10c/man10/man1/file10a.1');
251 unstow_contents('../stow/pkg10c', './');
252 stderr_like(
253 sub { process_tasks(); },
254 qr/There are no outstanding operations to perform/,
255 'no tasks to process when unstowing pkg10c'
257 ok(
258 scalar(@Conflicts) == 0 &&
259 readlink('man10/man1/file10a.1') eq '../../../stow/pkg10a/man10/man1/file10a.1'
260 => 'defer to existing documentation files'
264 # Ignore temp files
266 reset_state();
267 $Option{'verbose'} = 0;
268 $Option{'ignore'} = ['~', '\.#.*'];
270 make_dir('../stow/pkg12/man12/man1');
271 make_file('../stow/pkg12/man12/man1/file12.1');
272 make_file('../stow/pkg12/man12/man1/file12.1~');
273 make_file('../stow/pkg12/man12/man1/.#file12.1');
274 make_dir('man12/man1');
275 make_link('man12/man1/file12.1' => '../../../stow/pkg12/man12/man1/file12.1');
277 unstow_contents('../stow/pkg12', './');
278 process_tasks();
279 ok(
280 scalar(@Conflicts) == 0 &&
281 !-e 'man12/man1/file12.1'
282 => 'ignore temp files'
286 # Todo
288 # Test cleaning up subdirs with --paranoid option