4 # Testing ignore lists.
10 use File
::Temp
qw(tempdir);
11 use Test
::More tests
=> 286;
14 use Stow
::Util
qw(join_paths);
17 cd
("$OUT_DIR/target");
19 my $stow = new_Stow
();
22 my ($stow_path, $package, $context, @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);
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) = @_;
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!;
54 $stow_path, $package, $context,
55 $path => $expect_ignores,
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!;
69 $stow_path, $package, $context,
70 $path => $expect_ignores,
72 $suffix => $expect_ignores,
78 sub test_user_global_list
{
79 my ($stow_path, $package, $context, $expect_ignores) = @_;
81 for my $path ('', 'foo/bar/') {
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);
102 .+substring.+ # here's a comment
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;
119 my $stow_path = '../stow';
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
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);
146 $stow_path, $package, $context,
150 'foo2/bar/random' => 0,
155 # Test package-local .stow-local-ignore with only path segment regexps
156 $local_ignore = setup_package_local_list
($stow_path, $package, <<EOF);
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);
164 $stow_path, $package, $context,
168 'foo2/bar/random' => 1,
173 # Test package-local .stow-local-ignore with only full path regexps
174 $local_ignore = setup_package_local_list
($stow_path, $package, <<EOF);
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);
182 $stow_path, $package, $context,
186 'foo2/bar/random' => 1,
191 # Test package-local .stow-local-ignore with a mixture of regexps
192 $local_ignore = setup_package_local_list
($stow_path, $package, <<EOF);
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);
202 $stow_path, $package, $context,
206 'foo2/bar/random' => 1,
211 test_examples_in_manual
($stow_path);
212 test_invalid_regexp
($stow_path, "Invalid segment regexp in list", <<EOF);
217 test_invalid_regexp
($stow_path, "Invalid full path regexp in list", <<EOF);
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");
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");
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);
254 $stow_path, $package, $context,
255 "foo/bar/bazqux" => 1,
258 like
($@
, qr/^Failed to compile regexp: Quantifier follows nothing in regex;/,
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');
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");