Switch to renaming to manual via install hook as suggested by Stefano Lattarini
[gnu-stow.git] / t / ignore.t
blob83966fc1fa19bf97301177965198ab19e06eb19a
1 #!/usr/local/bin/perl
4 # Testing ignore lists.
7 use strict;
8 use warnings;
10 use File::Temp qw(tempdir);
11 use Test::More tests => 286;
13 use testutil;
14 use Stow::Util qw(join_paths);
16 init_test_dirs();
17 cd("$OUT_DIR/target");
19 my $stow = new_Stow();
21 sub test_ignores {
22 my ($stow_path, $package, $context, @tests) = @_;
23 $context ||= '';
24 while (@tests) {
25 my $path = shift @tests;
26 my $should_ignore = shift @tests;
27 my $not = $should_ignore ? '' : ' not';
28 my $was_ignored = $stow->ignore($stow_path, $package, $path);
29 is(
30 $was_ignored, $should_ignore,
31 "Should$not ignore $path $context"
36 sub test_local_ignore_list_always_ignored_at_top_level {
37 my ($stow_path, $package, $context) = @_;
38 test_ignores(
39 $stow_path, $package, $context,
40 $Stow::LOCAL_IGNORE_FILE => 1,
41 "subdir/" . $Stow::LOCAL_IGNORE_FILE => 0,
45 sub test_built_in_list {
46 my ($stow_path, $package, $context, $expect_ignores) = @_;
48 for my $ignored ('CVS', '.cvsignore', '#autosave#') {
49 for my $path ($ignored, "foo/bar/$ignored") {
50 my $suffix = "$path.suffix";
51 (my $prefix = $path) =~ s!([^/]+)$!prefix.$1!;
53 test_ignores(
54 $stow_path, $package, $context,
55 $path => $expect_ignores,
56 $prefix => 0,
57 $suffix => 0,
62 # The pattern catching lock files allows suffixes but not prefixes
63 for my $ignored ('.#lock-file') {
64 for my $path ($ignored, "foo/bar/$ignored") {
65 my $suffix = "$path.suffix";
66 (my $prefix = $path) =~ s!([^/]+)$!prefix.$1!;
68 test_ignores(
69 $stow_path, $package, $context,
70 $path => $expect_ignores,
71 $prefix => 0,
72 $suffix => $expect_ignores,
78 sub test_user_global_list {
79 my ($stow_path, $package, $context, $expect_ignores) = @_;
81 for my $path ('', 'foo/bar/') {
82 test_ignores(
83 $stow_path, $package, $context,
84 $path . 'exact' => $expect_ignores,
85 $path . '0exact' => 0,
86 $path . 'exact1' => 0,
87 $path . '0exact1' => 0,
89 $path . 'substring' => 0,
90 $path . '0substring' => 0,
91 $path . 'substring1' => 0,
92 $path . '0substring1' => $expect_ignores,
97 sub setup_user_global_list {
98 # Now test with global ignore list in home directory
99 $ENV{HOME} = tempdir();
100 make_file(join_paths($ENV{HOME}, $Stow::GLOBAL_IGNORE_FILE), <<EOF);
101 exact
102 .+substring.+ # here's a comment
103 .+\.extension
104 myprefix.+ #hi mum
108 sub setup_package_local_list {
109 my ($stow_path, $package, $list) = @_;
110 my $package_path = join_paths($stow_path, $package);
111 make_dir($package_path);
112 my $local_ignore = join_paths($package_path, $Stow::LOCAL_IGNORE_FILE);
113 make_file($local_ignore, $list);
114 $stow->invalidate_memoized_regexp($local_ignore);
115 return $local_ignore;
118 sub main {
119 my $stow_path = '../stow';
120 my $package;
121 my $context;
123 # Test built-in list first. init_test_dirs() already set
124 # $ENV{HOME} to ensure that we're not using the user's global
125 # ignore list.
126 $package = 'non-existent-package';
127 $context = "when using built-in list";
128 test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
129 test_built_in_list($stow_path, $package, $context, 1);
131 # Test ~/.stow-global-ignore
132 setup_user_global_list();
133 $context = "when using ~/$Stow::GLOBAL_IGNORE_FILE";
134 test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
135 test_built_in_list($stow_path, $package, $context, 0);
136 test_user_global_list($stow_path, $package, $context, 1);
138 # Test empty package-local .stow-local-ignore
139 $package = 'ignorepkg';
140 my $local_ignore = setup_package_local_list($stow_path, $package, "");
141 $context = "when using empty $local_ignore";
142 test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
143 test_built_in_list($stow_path, $package, $context, 0);
144 test_user_global_list($stow_path, $package, $context, 0);
145 test_ignores(
146 $stow_path, $package, $context,
147 'random' => 0,
148 'foo2/bar' => 0,
149 'foo2/bars' => 0,
150 'foo2/bar/random' => 0,
151 'foo2/bazqux' => 0,
152 'xfoo2/bazqux' => 0,
155 # Test package-local .stow-local-ignore with only path segment regexps
156 $local_ignore = setup_package_local_list($stow_path, $package, <<EOF);
157 random
159 $context = "when using $local_ignore with only path segment regexps";
160 test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
161 test_built_in_list($stow_path, $package, $context, 0);
162 test_user_global_list($stow_path, $package, $context, 0);
163 test_ignores(
164 $stow_path, $package, $context,
165 'random' => 1,
166 'foo2/bar' => 0,
167 'foo2/bars' => 0,
168 'foo2/bar/random' => 1,
169 'foo2/bazqux' => 0,
170 'xfoo2/bazqux' => 0,
173 # Test package-local .stow-local-ignore with only full path regexps
174 $local_ignore = setup_package_local_list($stow_path, $package, <<EOF);
175 foo2/bar
177 $context = "when using $local_ignore with only full path regexps";
178 test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
179 test_built_in_list($stow_path, $package, $context, 0);
180 test_user_global_list($stow_path, $package, $context, 0);
181 test_ignores(
182 $stow_path, $package, $context,
183 'random' => 0,
184 'foo2/bar' => 1,
185 'foo2/bars' => 0,
186 'foo2/bar/random' => 1,
187 'foo2/bazqux' => 0,
188 'xfoo2/bazqux' => 0,
191 # Test package-local .stow-local-ignore with a mixture of regexps
192 $local_ignore = setup_package_local_list($stow_path, $package, <<EOF);
193 foo2/bar
194 random
195 foo2/baz.+
197 $context = "when using $local_ignore with mixture of regexps";
198 test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
199 test_built_in_list($stow_path, $package, $context, 0);
200 test_user_global_list($stow_path, $package, $context, 0);
201 test_ignores(
202 $stow_path, $package, $context,
203 'random' => 1,
204 'foo2/bar' => 1,
205 'foo2/bars' => 0,
206 'foo2/bar/random' => 1,
207 'foo2/bazqux' => 1,
208 'xfoo2/bazqux' => 0,
211 test_examples_in_manual($stow_path);
212 test_invalid_regexp($stow_path, "Invalid segment regexp in list", <<EOF);
213 this one's ok
214 this one isn't|*!
215 but this one is
217 test_invalid_regexp($stow_path, "Invalid full path regexp in list", <<EOF);
218 this one's ok
219 this/one isn't|*!
220 but this one is
222 test_ignore_via_stow($stow_path);
225 sub test_examples_in_manual {
226 my ($stow_path) = @_;
227 my $package = 'ignorepkg';
228 my $context = "(example from manual)";
230 for my $re ('bazqux', 'baz.*', '.*qux', 'bar/.*x', '^/foo/.*qux') {
231 my $local_ignore = setup_package_local_list($stow_path, $package, "$re\n");
232 test_ignores(
233 $stow_path, $package, $context,
234 "foo/bar/bazqux" => 1,
238 for my $re ('bar', 'baz', 'qux') {
239 my $local_ignore = setup_package_local_list($stow_path, $package, "$re\n");
240 test_ignores(
241 $stow_path, $package, $context,
242 "foo/bar/bazqux" => 0,
247 sub test_invalid_regexp {
248 my ($stow_path, $context, $list) = @_;
249 my $package = 'ignorepkg';
251 my $local_ignore = setup_package_local_list($stow_path, $package, $list);
252 eval {
253 test_ignores(
254 $stow_path, $package, $context,
255 "foo/bar/bazqux" => 1,
258 like($@, qr/^Failed to compile regexp: Quantifier follows nothing in regex;/,
259 $context);
262 sub test_ignore_via_stow {
263 my ($stow_path) = @_;
265 my $package = 'pkg1';
266 make_dir("$stow_path/$package/foo/bar");
267 make_file("$stow_path/$package/foo/bar/baz");
269 setup_package_local_list($stow_path, $package, 'foo');
270 $stow->plan_stow($package);
271 is($stow->get_tasks(), 0, 'top dir ignored');
272 is($stow->get_conflicts(), 0, 'top dir ignored, no conflicts');
274 make_dir("foo");
275 for my $ignore ('bar', 'foo/bar', '/foo/bar', '^/foo/bar', '^/fo.+ar') {
276 setup_package_local_list($stow_path, $package, $ignore);
277 $stow->plan_stow($package);
278 is($stow->get_tasks(), 0, "bar ignored via $ignore");
279 is($stow->get_conflicts(), 0, 'bar ignored, no conflicts');
282 make_file("$stow_path/$package/foo/qux");
283 $stow->plan_stow($package);
284 $stow->process_tasks();
285 is($stow->get_conflicts(), 0, 'no conflicts stowing qux');
286 ok(! -e "foo/bar", "bar ignore prevented stow");
287 ok(-l "foo/qux", "qux not ignored and stowed");
288 is(readlink("foo/qux"), "../$stow_path/$package/foo/qux", "qux stowed correctly");
291 main();