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 unstowing packages in compat mode
25 use File
::Spec
qw(make_path);
26 use Test
::More tests
=> 37;
28 use English
qw(-no_match_vars);
31 use Stow
::Util
qw(canon_path);
34 cd
("$TEST_DIR/target");
36 # Note that each of the following tests use a distinct set of files
42 # unstow a simple tree minimally
45 $stow = new_compat_Stow
();
47 make_path
('../stow/pkg1/bin1');
48 make_file
('../stow/pkg1/bin1/file1');
49 make_link
('bin1', '../stow/pkg1/bin1');
51 $stow->plan_unstow('pkg1');
52 $stow->process_tasks();
54 $stow->get_conflict_count == 0 &&
55 -f
'../stow/pkg1/bin1/file1' && ! -e
'bin1'
56 => 'unstow a simple tree'
60 # unstow a simple tree from an existing directory
62 $stow = new_compat_Stow
();
65 make_path
('../stow/pkg2/lib2');
66 make_file
('../stow/pkg2/lib2/file2');
67 make_link
('lib2/file2', '../../stow/pkg2/lib2/file2');
68 $stow->plan_unstow('pkg2');
69 $stow->process_tasks();
71 $stow->get_conflict_count == 0 &&
72 -f
'../stow/pkg2/lib2/file2' && -d
'lib2'
73 => 'unstow simple tree from a pre-existing directory'
77 # fold tree after unstowing
79 $stow = new_compat_Stow
();
83 make_path
('../stow/pkg3a/bin3');
84 make_file
('../stow/pkg3a/bin3/file3a');
85 make_link
('bin3/file3a' => '../../stow/pkg3a/bin3/file3a'); # emulate stow
87 make_path
('../stow/pkg3b/bin3');
88 make_file
('../stow/pkg3b/bin3/file3b');
89 make_link
('bin3/file3b' => '../../stow/pkg3b/bin3/file3b'); # emulate stow
90 $stow->plan_unstow('pkg3b');
91 $stow->process_tasks();
93 $stow->get_conflict_count == 0 &&
95 readlink('bin3') eq '../stow/pkg3a/bin3'
96 => 'fold tree after unstowing'
100 # existing link is owned by stow but is invalid so it gets removed anyway
102 $stow = new_compat_Stow
();
105 make_path
('../stow/pkg4/bin4');
106 make_file
('../stow/pkg4/bin4/file4');
107 make_invalid_link
('bin4/file4', '../../stow/pkg4/bin4/does-not-exist');
109 $stow->plan_unstow('pkg4');
110 $stow->process_tasks();
112 $stow->get_conflict_count == 0 &&
114 => q
(remove invalid
link owned by stow
)
118 # Existing link is not owned by stow
120 $stow = new_compat_Stow
();
122 make_path
('../stow/pkg5/bin5');
123 make_invalid_link
('bin5', '../not-stow');
125 $stow->plan_unstow('pkg5');
126 # Unlike the corresponding stow_contents.t test, this doesn't
127 # cause any conflicts.
130 # $Conflicts[-1], qr(can't unlink.*not owned by stow)
131 # => q(existing link not owned by stow)
134 -l
'bin5' && readlink('bin5') eq '../not-stow'
135 => q
(existing
link not owned by stow
)
139 # Target already exists, is owned by stow, but points to a different package
141 $stow = new_compat_Stow
();
144 make_path
('../stow/pkg6a/bin6');
145 make_file
('../stow/pkg6a/bin6/file6');
146 make_link
('bin6/file6', '../../stow/pkg6a/bin6/file6');
148 make_path
('../stow/pkg6b/bin6');
149 make_file
('../stow/pkg6b/bin6/file6');
151 $stow->plan_unstow('pkg6b');
153 $stow->get_conflict_count == 0 &&
155 readlink('bin6/file6') eq '../../stow/pkg6a/bin6/file6'
156 => q
(ignore existing
link that points to a different
package)
160 # Don't unlink anything under the stow directory
162 make_path
('stow'); # make out stow dir a subdir of target
163 $stow = new_compat_Stow
(dir
=> 'stow');
165 # emulate stowing into ourself (bizarre corner case or accident)
166 make_path
('stow/pkg7a/stow/pkg7b');
167 make_file
('stow/pkg7a/stow/pkg7b/file7b');
168 make_link
('stow/pkg7b', '../stow/pkg7a/stow/pkg7b');
171 $stow->plan_unstow('pkg7b');
172 is
($stow->get_tasks, 0, 'no tasks to process when unstowing pkg7b');
174 $stow->get_conflict_count == 0 &&
176 readlink('stow/pkg7b') eq '../stow/pkg7a/stow/pkg7b'
177 => q
(don
't unlink any nodes under the stow directory)
180 qr/WARNING: skipping target which was current stow directory stow/
181 => "warn when unstowing from ourself");
185 # Don't
unlink any nodes under another stow directory
187 $stow = new_compat_Stow
(dir
=> 'stow');
189 make_path
('stow2'); # make our alternate stow dir a subdir of target
190 make_file
('stow2/.stow');
192 # emulate stowing into ourself (bizarre corner case or accident)
193 make_path
('stow/pkg8a/stow2/pkg8b');
194 make_file
('stow/pkg8a/stow2/pkg8b/file8b');
195 make_link
('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b');
198 $stow->plan_unstow('pkg8a');
199 is
($stow->get_tasks, 0, 'no tasks to process when unstowing pkg8a');
201 $stow->get_conflict_count == 0 &&
203 readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b'
204 => q
(don
't unlink any nodes under another stow directory)
207 qr/WARNING: skipping target which was current stow directory stow/
208 => "warn when skipping unstowing");
212 # overriding already stowed documentation
215 # This will be used by this and subsequent tests
216 sub check_protected_dirs_skipped {
217 for my $dir (qw{stow stow2}) {
219 qr/WARNING: skipping marked Stow directory $dir/
220 => "warn when skipping marked directory $dir");
225 $stow = new_compat_Stow(override => ['man9
', 'info9
']);
226 make_file('stow
/.stow
');
228 make_path('../stow/pkg
9a
/man9/man
1');
229 make_file('../stow/pkg
9a
/man9/man
1/file9
.1
');
230 make_path('man9
/man1
');
231 make_link('man9
/man1/file9
.1
' => '../../../stow/pkg
9a
/man9/man
1/file9
.1
'); # emulate stow
233 make_path('../stow/pkg
9b
/man9/man
1');
234 make_file('../stow/pkg
9b
/man9/man
1/file9
.1
');
236 $stow->plan_unstow('pkg9b
');
237 $stow->process_tasks();
239 $stow->get_conflict_count == 0 &&
240 !-l 'man9
/man1/file9
.1
'
241 => 'overriding existing documentation files
'
243 check_protected_dirs_skipped();
246 # deferring to already stowed documentation
248 $stow = new_compat_Stow(defer => ['man10
', 'info10
']);
250 make_path('../stow/pkg
10a
/man10/man
1');
251 make_file('../stow/pkg
10a
/man10/man
1/file10a
.1');
252 make_path('man10
/man1
');
253 make_link('man10
/man1/file10a
.1' => '../../../stow/pkg
10a
/man10/man
1/file10a
.1');
255 # need this to block folding
256 make_path('../stow/pkg
10b
/man10/man
1');
257 make_file('../stow/pkg
10b
/man10/man
1/file10b
.1');
258 make_link('man10
/man1/file10b
.1' => '../../../stow/pkg
10b
/man10/man
1/file10b
.1');
261 make_path('../stow/pkg
10c
/man10/man
1');
262 make_file('../stow/pkg
10c
/man10/man
1/file10a
.1');
264 $stow->plan_unstow('pkg10c
');
265 is($stow->get_tasks, 0, 'no tasks to process
when unstowing pkg10c
');
267 $stow->get_conflict_count == 0 &&
268 readlink('man10
/man1/file10a
.1') eq '../../../stow/pkg
10a
/man10/man
1/file10a
.1'
269 => 'defer to existing documentation files
'
271 check_protected_dirs_skipped();
276 $stow = new_compat_Stow(ignore => ['~', '\
.#.*']);
278 make_path
('../stow/pkg12/man12/man1');
279 make_file
('../stow/pkg12/man12/man1/file12.1');
280 make_file
('../stow/pkg12/man12/man1/file12.1~');
281 make_file
('../stow/pkg12/man12/man1/.#file12.1');
282 make_path
('man12/man1');
283 make_link
('man12/man1/file12.1' => '../../../stow/pkg12/man12/man1/file12.1');
286 $stow->plan_unstow('pkg12');
287 $stow->process_tasks();
289 $stow->get_conflict_count == 0 &&
290 !-e
'man12/man1/file12.1'
291 => 'ignore temp files'
293 check_protected_dirs_skipped
();
296 # Unstow an already unstowed package
298 $stow = new_compat_Stow
();
300 $stow->plan_unstow('pkg12');
301 is
($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12');
303 $stow->get_conflict_count == 0
304 => 'unstow already unstowed package pkg12'
306 check_protected_dirs_skipped
();
309 # Unstow a never stowed package
312 eval { remove_dir
("$TEST_DIR/target"); };
313 mkdir("$TEST_DIR/target");
315 $stow = new_compat_Stow
();
317 $stow->plan_unstow('pkg12');
318 is
($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12 which was never stowed');
320 $stow->get_conflict_count == 0
321 => 'unstow never stowed package pkg12'
323 check_protected_dirs_skipped
();
326 # Unstowing when target contains a real file shouldn't be an issue.
328 make_file
('man12/man1/file12.1');
330 $stow = new_compat_Stow
();
332 $stow->plan_unstow('pkg12');
333 is
($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12 for third time');
334 %conflicts = $stow->get_conflicts;
336 $stow->get_conflict_count == 1 &&
337 $conflicts{unstow
}{pkg12
}[0]
338 =~ m!existing target is neither a link nor a directory: man12/man1/file12\.1!
339 => 'unstow pkg12 for third time'
341 check_protected_dirs_skipped
();
344 # unstow a simple tree minimally when cwd isn't target
347 $stow = new_Stow
(dir
=> "$TEST_DIR/stow", target
=> "$TEST_DIR/target");
349 make_path
("$TEST_DIR/stow/pkg13/bin13");
350 make_file
("$TEST_DIR/stow/pkg13/bin13/file13");
351 make_link
("$TEST_DIR/target/bin13", '../stow/pkg13/bin13');
353 $stow->plan_unstow('pkg13');
354 $stow->process_tasks();
356 $stow->get_conflict_count == 0 &&
357 -f
"$TEST_DIR/stow/pkg13/bin13/file13" && ! -e
"$TEST_DIR/target/bin13"
358 => 'unstow a simple tree'
362 # unstow a simple tree minimally with absolute stow dir when cwd isn't
365 $stow = new_Stow
(dir
=> canon_path
("$TEST_DIR/stow"),
366 target
=> "$TEST_DIR/target");
368 make_path
("$TEST_DIR/stow/pkg14/bin14");
369 make_file
("$TEST_DIR/stow/pkg14/bin14/file14");
370 make_link
("$TEST_DIR/target/bin14", '../stow/pkg14/bin14');
372 $stow->plan_unstow('pkg14');
373 $stow->process_tasks();
375 $stow->get_conflict_count == 0 &&
376 -f
"$TEST_DIR/stow/pkg14/bin14/file14" && ! -e
"$TEST_DIR/target/bin14"
377 => 'unstow a simple tree with absolute stow dir'
381 # unstow a simple tree minimally with absolute stow AND target dirs
382 # when cwd isn't target
384 $stow = new_Stow
(dir
=> canon_path
("$TEST_DIR/stow"),
385 target
=> canon_path
("$TEST_DIR/target"));
387 make_path
("$TEST_DIR/stow/pkg15/bin15");
388 make_file
("$TEST_DIR/stow/pkg15/bin15/file15");
389 make_link
("$TEST_DIR/target/bin15", '../stow/pkg15/bin15');
391 $stow->plan_unstow('pkg15');
392 $stow->process_tasks();
394 $stow->get_conflict_count == 0 &&
395 -f
"$TEST_DIR/stow/pkg15/bin15/file15" && ! -e
"$TEST_DIR/target/bin15"
396 => 'unstow a simple tree with absolute stow and target dirs'
402 # Test cleaning up subdirs with --paranoid option