Grafted root commit from savannah git master:
[gnu-stow.git] / t / unstow_contents.t
blob06880f3ba053636985019609c968440ab6f25a59
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 => 11;
11 use English qw(-no_match_vars);
13 # local utility
14 sub reset_state {
15 @Tasks = ();
16 @Conflicts = ();
17 %Link_Task_For = ();
18 %Dir_Task_For = ();
19 %Options = ();
20 return;
23 ### setup
24 eval { remove_dir('t/target'); };
25 eval { remove_dir('t/stow'); };
26 make_dir('t/target');
27 make_dir('t/stow');
29 chdir 't/target';
30 $Stow_Path= '../stow';
32 # Note that each of the following tests use a distinct set of files
35 # unstow a simple tree minimally
38 reset_state();
39 $Option{'verbose'} = 0;
41 make_dir('../stow/pkg1/bin1');
42 make_file('../stow/pkg1/bin1/file1');
43 make_link('bin1','../stow/pkg1/bin1');
45 unstow_contents('../stow/pkg1','./');
46 process_tasks();
47 ok(
48 scalar(@Conflicts) == 0 &&
49 -f '../stow/pkg1/bin1/file1' && ! -e 'bin1'
50 => 'unstow a simple tree'
54 # unstow a simple tree from an existing directory
56 reset_state();
57 $Option{'verbose'} = 0;
59 make_dir('lib2');
60 make_dir('../stow/pkg2/lib2');
61 make_file('../stow/pkg2/lib2/file2');
62 make_link('lib2/file2', '../../stow/pkg2/lib2/file2');
63 unstow_contents('../stow/pkg2','./');
64 process_tasks();
65 ok(
66 scalar(@Conflicts) == 0 &&
67 -f '../stow/pkg2/lib2/file2' && -d 'lib2'
68 => 'unstow simple tree from a pre-existing directory'
72 # fold tree after unstowing
74 reset_state();
75 $Option{'verbose'} = 0;
77 make_dir('bin3');
79 make_dir('../stow/pkg3a/bin3');
80 make_file('../stow/pkg3a/bin3/file3a');
81 make_link('bin3/file3a' => '../../stow/pkg3a/bin3/file3a'); # emulate stow
83 make_dir('../stow/pkg3b/bin3');
84 make_file('../stow/pkg3b/bin3/file3b');
85 make_link('bin3/file3b' => '../../stow/pkg3b/bin3/file3b'); # emulate stow
86 unstow_contents('../stow/pkg3b', './');
87 process_tasks();
88 ok(
89 scalar(@Conflicts) == 0 &&
90 -l 'bin3' &&
91 readlink('bin3') eq '../stow/pkg3a/bin3'
92 => 'fold tree after unstowing'
96 # existing link is owned by stow but is invalid so it gets removed anyway
98 reset_state();
99 $Option{'verbose'} = 0;
101 make_dir('bin4');
102 make_dir('../stow/pkg4/bin4');
103 make_file('../stow/pkg4/bin4/file4');
104 make_link('bin4/file4', '../../stow/pkg4/bin4/does-not-exist');
106 unstow_contents('../stow/pkg4', './');
107 process_tasks();
109 scalar(@Conflicts) == 0 &&
110 ! -e 'bin4/file4'
111 => q(remove invalid link owned by stow)
115 # Existing link is not owned by stow
117 reset_state();
118 $Option{'verbose'} = 0;
120 make_dir('../stow/pkg5/bin5');
121 make_link('bin5', '../not-stow');
123 unstow_contents('../stow/pkg5', './');
124 like(
125 $Conflicts[-1], qr(CONFLICT:.*existing target is not owned by stow)
126 => q(existing link not owned by stow)
129 # Target already exists, is owned by stow, but points to a different package
131 reset_state();
132 $Option{'verbose'} = 0;
134 make_dir('bin6');
135 make_dir('../stow/pkg6a/bin6');
136 make_file('../stow/pkg6a/bin6/file6');
137 make_link('bin6/file6', '../../stow/pkg6a/bin6/file6');
139 make_dir('../stow/pkg6b/bin6');
140 make_file('../stow/pkg6b/bin6/file6');
142 unstow_contents('../stow/pkg6b', './');
144 scalar(@Conflicts) == 0 &&
145 -l 'bin6/file6' &&
146 readlink('bin6/file6') eq '../../stow/pkg6a/bin6/file6'
147 => q(ignore existing link that points to a different package)
151 # Don't unlink anything under the stow directory
153 reset_state();
154 $Option{'verbose'} = 0;
156 make_dir('stow'); # make out stow dir a subdir of target
157 $Stow_Path = 'stow';
159 # emulate stowing into ourself (bizzare corner case or accident)
160 make_dir('stow/pkg7a/stow/pkg7b');
161 make_file('stow/pkg7a/stow/pkg7b/file7b');
162 make_link('stow/pkg7b', '../stow/pkg7a/stow/pkg7b');
164 unstow_contents('stow/pkg7b', './');
165 process_tasks();
167 scalar(@Conflicts) == 0 &&
168 -l 'stow/pkg7b' &&
169 readlink('stow/pkg7b') eq '../stow/pkg7a/stow/pkg7b'
170 => q(don't unlink any nodes under the stow directory)
174 # Don't unlink any nodes under another stow directory
176 reset_state();
177 $Option{'verbose'} = 0;
179 make_dir('stow'); # make out stow dir a subdir of target
180 $Stow_Path = 'stow';
182 make_dir('stow2'); # make our alternate stow dir a subdir of target
183 make_file('stow2/.stow');
185 # emulate stowing into ourself (bizzare corner case or accident)
186 make_dir('stow/pkg8a/stow2/pkg8b');
187 make_file('stow/pkg8a/stow2/pkg8b/file8b');
188 make_link('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b');
190 unstow_contents('stow/pkg8a', './');
191 process_tasks();
193 scalar(@Conflicts) == 0 &&
194 -l 'stow2/pkg8b' &&
195 readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b'
196 => q(don't unlink any nodes under another stow directory)
200 # overriding already stowed documentation
202 reset_state();
203 $Stow_Path = '../stow';
204 $Option{'verbose'} = 0;
205 $Option{'override'} = ['man9', 'info9'];
207 make_dir('../stow/pkg9a/man9/man1');
208 make_file('../stow/pkg9a/man9/man1/file9.1');
209 make_dir('man9/man1');
210 make_link('man9/man1/file9.1' => '../../../stow/pkg9a/man9/man1/file9.1'); # emulate stow
212 make_dir('../stow/pkg9b/man9/man1');
213 make_file('../stow/pkg9b/man9/man1/file9.1');
214 unstow_contents('../stow/pkg9b', './');
215 process_tasks();
216 ok(
217 scalar(@Conflicts) == 0 &&
218 !-l 'man9/man1/file9.1'
219 => 'overriding existing documentation files'
223 # deferring to already stowed documentation
225 reset_state();
226 $Option{'verbose'} = 0;
227 $Option{'defer'} = ['man10', 'info10'];
229 make_dir('../stow/pkg10a/man10/man1');
230 make_file('../stow/pkg10a/man10/man1/file10a.1');
231 make_dir('man10/man1');
232 make_link('man10/man1/file10a.1' => '../../../stow/pkg10a/man10/man1/file10a.1');
234 # need this to block folding
235 make_dir('../stow/pkg10b/man10/man1');
236 make_file('../stow/pkg10b/man10/man1/file10b.1');
237 make_link('man10/man1/file10b.1' => '../../../stow/pkg10b/man10/man1/file10b.1');
240 make_dir('../stow/pkg10c/man10/man1');
241 make_file('../stow/pkg10c/man10/man1/file10a.1');
242 unstow_contents('../stow/pkg10c', './');
243 process_tasks();
244 ok(
245 scalar(@Conflicts) == 0 &&
246 readlink('man10/man1/file10a.1') eq '../../../stow/pkg10a/man10/man1/file10a.1'
247 => 'defer to existing documentation files'
251 # Ignore temp files
253 reset_state();
254 $Option{'verbose'} = 0;
255 $Option{'ignore'} = ['~', '\.#.*'];
257 make_dir('../stow/pkg12/man12/man1');
258 make_file('../stow/pkg12/man12/man1/file12.1');
259 make_file('../stow/pkg12/man12/man1/file12.1~');
260 make_file('../stow/pkg12/man12/man1/.#file12.1');
261 make_dir('man12/man1');
262 make_link('man12/man1/file12.1' => '../../../stow/pkg12/man12/man1/file12.1');
264 unstow_contents('../stow/pkg12', './');
265 process_tasks();
266 ok(
267 scalar(@Conflicts) == 0 &&
268 !-e 'man12/man1/file12.1'
269 => 'ignore temp files'
273 # Todo
275 # Test cleaning up subdirs with --paranoid option