Refactored reset_state() into t/util.pm
[gnu-stow.git] / t / stow_contents.t
blobd589844194dfa1a100f26cc428ba2b5d9d82b5e0
1 #!/usr/local/bin/perl
4 # Testing
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 ### setup
15 eval { remove_dir('t/target'); };
16 eval { remove_dir('t/stow'); };
17 make_dir('t/target');
18 make_dir('t/stow');
20 chdir 't/target';
21 $Stow_Path= '../stow';
23 # Note that each of the following tests use a distinct set of files
26 # stow a simple tree minimally
28 reset_state();
29 $Option{'verbose'} = 0;
31 make_dir('../stow/pkg1/bin1');
32 make_file('../stow/pkg1/bin1/file1');
33 stow_contents('../stow/pkg1', './', '../stow/pkg1');
34 process_tasks();
35 is(
36 readlink('bin1'),
37 '../stow/pkg1/bin1',
38 => 'minimal stow of a simple tree'
42 # stow a simple tree into an existing directory
44 reset_state();
45 $Option{'verbose'} = 0;
47 make_dir('../stow/pkg2/lib2');
48 make_file('../stow/pkg2/lib2/file2');
49 make_dir('lib2');
50 stow_contents('../stow/pkg2', '.', '../stow/pkg2');
51 process_tasks();
52 is(
53 readlink('lib2/file2'),
54 '../../stow/pkg2/lib2/file2',
55 => 'stow simple tree to existing directory'
59 # unfold existing tree
61 reset_state();
62 $Option{'verbose'} = 0;
64 make_dir('../stow/pkg3a/bin3');
65 make_file('../stow/pkg3a/bin3/file3a');
66 make_link('bin3' => '../stow/pkg3a/bin3'); # emulate stow
68 make_dir('../stow/pkg3b/bin3');
69 make_file('../stow/pkg3b/bin3/file3b');
70 stow_contents('../stow/pkg3b', './', '../stow/pkg3b');
71 process_tasks();
72 ok(
73 -d 'bin3' &&
74 readlink('bin3/file3a') eq '../../stow/pkg3a/bin3/file3a' &&
75 readlink('bin3/file3b') eq '../../stow/pkg3b/bin3/file3b'
76 => 'target already has 1 stowed package'
80 # Link to a new dir conflicts with existing non-dir (can't unfold)
82 reset_state();
83 $Option{'verbose'} = 0;
85 make_file('bin4'); # this is a file but named like a directory
86 make_dir('../stow/pkg4/bin4');
87 make_file('../stow/pkg4/bin4/file4');
88 stow_contents('../stow/pkg4', './', '../stow/pkg4');
89 like(
90 $Conflicts[-1], qr(CONFLICT:.*existing target is neither a link nor a directory)
91 => 'link to new dir conflicts with existing non-directory'
95 # Target already exists but is not owned by stow
97 reset_state();
98 $Option{'verbose'} = 0;
100 make_dir('bin5');
101 make_link('bin5/file5','../../empty');
102 make_dir('../stow/pkg5/bin5/file5');
103 stow_contents('../stow/pkg5', './', '../stow/pkg5');
104 like(
105 $Conflicts[-1], qr(CONFLICT:.*not owned by stow)
106 => 'target already exists but is not owned by stow'
110 # Replace existing but invalid target
112 reset_state();
113 $Option{'verbose'} = 0;
115 make_link('file6','../stow/path-does-not-exist');
116 make_dir('../stow/pkg6');
117 make_file('../stow/pkg6/file6');
118 eval{ stow_contents('../stow/pkg6', './', '../stow/pkg6'); process_tasks() };
119 is(
120 readlink('file6'),
121 '../stow/pkg6/file6'
122 => 'replace existing but invalid target'
126 # Target already exists, is owned by stow, but points to a non-directory
127 # (can't unfold)
129 reset_state();
130 $Option{'verbose'} = 0;
132 make_dir('bin7');
133 make_dir('../stow/pkg7a/bin7');
134 make_file('../stow/pkg7a/bin7/node7');
135 make_link('bin7/node7','../../stow/pkg7a/bin7/node7');
136 make_dir('../stow/pkg7b/bin7/node7');
137 make_file('../stow/pkg7b/bin7/node7/file7');
138 stow_contents('../stow/pkg7b', './', '../stow/pkg7b');
139 like(
140 $Conflicts[-1], qr(CONFLICT:.*existing target is stowed to a different package)
141 => 'link to new dir conflicts with existing stowed non-directory'
145 # stowing directories named 0
147 reset_state();
148 $Option{'verbose'} = 0;
150 make_dir('../stow/pkg8a/0');
151 make_file('../stow/pkg8a/0/file8a');
152 make_link('0' => '../stow/pkg8a/0'); # emulate stow
154 make_dir('../stow/pkg8b/0');
155 make_file('../stow/pkg8b/0/file8b');
156 stow_contents('../stow/pkg8b', './', '../stow/pkg8b');
157 process_tasks();
158 ok(
159 scalar(@Conflicts) == 0 &&
160 -d '0' &&
161 readlink('0/file8a') eq '../../stow/pkg8a/0/file8a' &&
162 readlink('0/file8b') eq '../../stow/pkg8b/0/file8b'
163 => 'stowing directories named 0'
167 # overriding already stowed documentation
169 reset_state();
170 $Option{'verbose'} = 0;
171 $Option{'override'} = ['man9', 'info9'];
173 make_dir('../stow/pkg9a/man9/man1');
174 make_file('../stow/pkg9a/man9/man1/file9.1');
175 make_dir('man9/man1');
176 make_link('man9/man1/file9.1' => '../../../stow/pkg9a/man9/man1/file9.1'); # emulate stow
178 make_dir('../stow/pkg9b/man9/man1');
179 make_file('../stow/pkg9b/man9/man1/file9.1');
180 stow_contents('../stow/pkg9b', './', '../stow/pkg9b');
181 process_tasks();
182 ok(
183 scalar(@Conflicts) == 0 &&
184 readlink('man9/man1/file9.1') eq '../../../stow/pkg9b/man9/man1/file9.1'
185 => 'overriding existing documentation files'
189 # deferring to already stowed documentation
191 reset_state();
192 $Option{'verbose'} = 0;
193 $Option{'defer'} = ['man10', 'info10'];
195 make_dir('../stow/pkg10a/man10/man1');
196 make_file('../stow/pkg10a/man10/man1/file10.1');
197 make_dir('man10/man1');
198 make_link('man10/man1/file10.1' => '../../../stow/pkg10a/man10/man1/file10.1'); # emulate stow
200 make_dir('../stow/pkg10b/man10/man1');
201 make_file('../stow/pkg10b/man10/man1/file10.1');
202 stow_contents('../stow/pkg10b', './', '../stow/pkg10b');
203 stderr_like(
204 sub { process_tasks(); },
205 qr/There are no outstanding operations to perform/,
206 'no tasks to process'
208 ok(
209 scalar(@Conflicts) == 0 &&
210 readlink('man10/man1/file10.1') eq '../../../stow/pkg10a/man10/man1/file10.1'
211 => 'defer to existing documentation files'
215 # Ignore temp files
217 reset_state();
218 $Option{'verbose'} = 0;
219 $Option{'ignore'} = ['~', '\.#.*'];
221 make_dir('../stow/pkg11/man11/man1');
222 make_file('../stow/pkg11/man11/man1/file11.1');
223 make_file('../stow/pkg11/man11/man1/file11.1~');
224 make_file('../stow/pkg11/man11/man1/.#file11.1');
225 make_dir('man11/man1');
227 stow_contents('../stow/pkg11', './', '../stow/pkg11');
228 process_tasks();
229 ok(
230 scalar(@Conflicts) == 0 &&
231 readlink('man11/man1/file11.1') eq '../../../stow/pkg11/man11/man1/file11.1' &&
232 !-e 'man11/man1/file11.1~' &&
233 !-e 'man11/man1/.#file11.1'
234 => 'ignore temp files'
238 # stowing links library files
240 reset_state();
241 $Option{'verbose'} = 0;
243 make_dir('../stow/pkg12/lib12/');
244 make_file('../stow/pkg12/lib12/lib.so');
245 make_link('../stow/pkg12/lib12/lib.so.1','lib.so');
247 make_dir('lib12/');
248 stow_contents('../stow/pkg12', './', '../stow/pkg12');
249 process_tasks();
250 ok(
251 scalar(@Conflicts) == 0 &&
252 readlink('lib12/lib.so.1') eq '../../stow/pkg12/lib12/lib.so.1'
253 => 'stow links to libraries'
257 # unfolding to stow links to library files
259 reset_state();
260 $Option{'verbose'} = 0;
262 make_dir('../stow/pkg13a/lib13/');
263 make_file('../stow/pkg13a/lib13/liba.so');
264 make_link('../stow/pkg13a/lib13/liba.so.1', 'liba.so');
265 make_link('lib13','../stow/pkg13a/lib13');
267 make_dir('../stow/pkg13b/lib13/');
268 make_file('../stow/pkg13b/lib13/libb.so');
269 make_link('../stow/pkg13b/lib13/libb.so.1', 'libb.so');
271 stow_contents('../stow/pkg13b', './', '../stow/pkg13b');
272 process_tasks();
273 ok(
274 scalar(@Conflicts) == 0 &&
275 readlink('lib13/liba.so.1') eq '../../stow/pkg13a/lib13/liba.so.1' &&
276 readlink('lib13/libb.so.1') eq '../../stow/pkg13b/lib13/libb.so.1'
277 => 'unfolding to stow links to libraries'