summaryrefslogtreecommitdiff
path: root/t/ignore.t
blob: 3da9dd4cf6bfe018edf3cfbbb41ca0fc43772a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/perl
#
# This file is part of GNU Stow.
#
# GNU Stow is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNU Stow is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.

#
# Testing ignore lists.
#

use strict;
use warnings;

use File::Temp qw(tempdir);
use Test::More tests => 287;

use testutil;
use Stow::Util qw(join_paths);

init_test_dirs();
cd("$TEST_DIR/target");

my $stow = new_Stow();

sub test_ignores {
    my ($stow_path, $package, $context, @tests) = @_;
    $context ||= '';
    while (@tests) {
        my $path          = shift @tests;
        my $should_ignore = shift @tests;
        my $not = $should_ignore ? '' : ' not';
        my $was_ignored = $stow->ignore($stow_path, $package, $path);
        is(
            $was_ignored, $should_ignore,
            "Should$not ignore $path $context"
        );
    }
}

sub test_local_ignore_list_always_ignored_at_top_level {
    my ($stow_path, $package, $context) = @_;
    test_ignores(
        $stow_path, $package, $context,
        $Stow::LOCAL_IGNORE_FILE             => 1,
        "subdir/" . $Stow::LOCAL_IGNORE_FILE => 0,
    );
}

sub test_built_in_list {
    my ($stow_path, $package, $context, $expect_ignores) = @_;

    for my $ignored ('CVS', '.cvsignore', '#autosave#') {
        for my $path ($ignored, "foo/bar/$ignored") {
            my $suffix = "$path.suffix";
            (my $prefix = $path) =~ s!([^/]+)$!prefix.$1!;

            test_ignores(
                $stow_path, $package, $context,
                $path   => $expect_ignores,
                $prefix => 0,
                $suffix => 0,
            );
        }
    }

    # The pattern catching lock files allows suffixes but not prefixes
    for my $ignored ('.#lock-file') {
        for my $path ($ignored, "foo/bar/$ignored") {
            my $suffix = "$path.suffix";
            (my $prefix = $path) =~ s!([^/]+)$!prefix.$1!;

            test_ignores(
                $stow_path, $package, $context,
                $path   => $expect_ignores,
                $prefix => 0,
                $suffix => $expect_ignores,
            );
        }
    }
}

sub test_user_global_list {
    my ($stow_path, $package, $context, $expect_ignores) = @_;

    for my $path ('', 'foo/bar/') {
        test_ignores(
            $stow_path, $package, $context,
            $path . 'exact'       => $expect_ignores,
            $path . '0exact'      => 0,
            $path . 'exact1'      => 0,
            $path . '0exact1'     => 0,

            $path . 'substring'   => 0,
            $path . '0substring'  => 0,
            $path . 'substring1'  => 0,
            $path . '0substring1' => $expect_ignores,
        );
    }
}

sub setup_user_global_list {
    # Now test with global ignore list in home directory
    $ENV{HOME} = tempdir();
    make_file(join_paths($ENV{HOME}, $Stow::GLOBAL_IGNORE_FILE), <<EOF);
exact
.+substring.+ # here's a comment
.+\.extension
myprefix.+       #hi mum
EOF
}

sub setup_package_local_list {
    my ($stow_path, $package, $list) = @_;
    my $package_path = join_paths($stow_path, $package);
    make_path($package_path);
    my $local_ignore = join_paths($package_path, $Stow::LOCAL_IGNORE_FILE);
    make_file($local_ignore, $list);
    $stow->invalidate_memoized_regexp($local_ignore);
    return $local_ignore;
}

sub main {
    my $stow_path = '../stow';
    my $package;
    my $context;

    # Test built-in list first.  init_test_dirs() already set
    # $ENV{HOME} to ensure that we're not using the user's global
    # ignore list.
    $package = 'non-existent-package';
    $context = "when using built-in list";
    test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
    test_built_in_list($stow_path, $package, $context, 1);

    # Test ~/.stow-global-ignore
    setup_user_global_list();
    $context = "when using ~/$Stow::GLOBAL_IGNORE_FILE";
    test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
    test_built_in_list($stow_path, $package, $context, 0);
    test_user_global_list($stow_path, $package, $context, 1);

    # Test empty package-local .stow-local-ignore
    $package = 'ignorepkg';
    my $local_ignore = setup_package_local_list($stow_path, $package, "");
    $context = "when using empty $local_ignore";
    test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
    test_built_in_list($stow_path, $package, $context, 0);
    test_user_global_list($stow_path, $package, $context, 0);
    test_ignores(
        $stow_path, $package, $context,
        'random'          => 0,
        'foo2/bar'        => 0,
        'foo2/bars'       => 0,
        'foo2/bar/random' => 0,
        'foo2/bazqux'     => 0,
        'xfoo2/bazqux'    => 0,
    );

    # Test package-local .stow-local-ignore with only path segment regexps
    $local_ignore = setup_package_local_list($stow_path, $package, <<EOF);
random
EOF
    $context = "when using $local_ignore with only path segment regexps";
    test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
    test_built_in_list($stow_path, $package, $context, 0);
    test_user_global_list($stow_path, $package, $context, 0);
    test_ignores(
        $stow_path, $package, $context,
        'random'          => 1,
        'foo2/bar'        => 0,
        'foo2/bars'       => 0,
        'foo2/bar/random' => 1,
        'foo2/bazqux'     => 0,
        'xfoo2/bazqux'    => 0,
    );

    # Test package-local .stow-local-ignore with only full path regexps
    $local_ignore = setup_package_local_list($stow_path, $package, <<EOF);
foo2/bar
EOF
    $context = "when using $local_ignore with only full path regexps";
    test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
    test_built_in_list($stow_path, $package, $context, 0);
    test_user_global_list($stow_path, $package, $context, 0);
    test_ignores(
        $stow_path, $package, $context,
        'random'          => 0,
        'foo2/bar'        => 1,
        'foo2/bars'       => 0,
        'foo2/bar/random' => 1,
        'foo2/bazqux'     => 0,
        'xfoo2/bazqux'    => 0,
    );

    # Test package-local .stow-local-ignore with a mixture of regexps
    $local_ignore = setup_package_local_list($stow_path, $package, <<EOF);
foo2/bar
random
foo2/baz.+
EOF
    $context = "when using $local_ignore with mixture of regexps";
    test_local_ignore_list_always_ignored_at_top_level($stow_path, $package, $context);
    test_built_in_list($stow_path, $package, $context, 0);
    test_user_global_list($stow_path, $package, $context, 0);
    test_ignores(
        $stow_path, $package, $context,
        'random'          => 1,
        'foo2/bar'        => 1,
        'foo2/bars'       => 0,
        'foo2/bar/random' => 1,
        'foo2/bazqux'     => 1,
        'xfoo2/bazqux'    => 0,
    );

    test_examples_in_manual($stow_path);
    test_invalid_regexp($stow_path, "Invalid segment regexp in list", <<EOF);
this one's ok
this one isn't|*!
but this one is
EOF
    test_invalid_regexp($stow_path, "Invalid full path regexp in list", <<EOF);
this one's ok
this/one isn't|*!
but this one is
EOF
    test_ignore_via_stow($stow_path);
}

sub test_examples_in_manual {
    my ($stow_path) = @_;
    my $package = 'ignorepkg';
    my $context = "(example from manual)";

    for my $re ('bazqux', 'baz.*', '.*qux', 'bar/.*x', '^/foo/.*qux') {
        my $local_ignore = setup_package_local_list($stow_path, $package, "$re\n");
        test_ignores(
            $stow_path, $package, $context,
            "foo/bar/bazqux" => 1,
        );
    }

    for my $re ('bar', 'baz', 'qux', 'o/bar/b') {
        my $local_ignore = setup_package_local_list($stow_path, $package, "$re\n");
        test_ignores(
            $stow_path, $package, $context,
            "foo/bar/bazqux" => 0,
        );
    }
}

sub test_invalid_regexp {
    my ($stow_path, $context, $list) = @_;
    my $package = 'ignorepkg';

    my $local_ignore = setup_package_local_list($stow_path, $package, $list);
    eval {
        test_ignores(
            $stow_path, $package, $context,
            "foo/bar/bazqux" => 1,
        );
    };
    like($@, qr/^Failed to compile regexp: Quantifier follows nothing in regex;/,
         $context);
}

sub test_ignore_via_stow {
    my ($stow_path) = @_;

    my $package = 'pkg1';
    make_path("$stow_path/$package/foo/bar");
    make_file("$stow_path/$package/foo/bar/baz");

    setup_package_local_list($stow_path, $package, 'foo');
    $stow->plan_stow($package);
    is($stow->get_tasks(),     0, 'top dir ignored');
    is($stow->get_conflicts(), 0, 'top dir ignored, no conflicts');

    make_path("foo");
    for my $ignore ('bar', 'foo/bar', '/foo/bar', '^/foo/bar', '^/fo.+ar') {
        setup_package_local_list($stow_path, $package, $ignore);
        $stow->plan_stow($package);
        is($stow->get_tasks(), 0, "bar ignored via $ignore");
        is($stow->get_conflicts(), 0, 'bar ignored, no conflicts');
    }

    make_file("$stow_path/$package/foo/qux");
    $stow->plan_stow($package);
    $stow->process_tasks();
    is($stow->get_conflicts(), 0, 'no conflicts stowing qux');
    ok(! -e "foo/bar", "bar ignore prevented stow");
    ok(-l "foo/qux",   "qux not ignored and stowed");
    is(readlink("foo/qux"), "../$stow_path/$package/foo/qux", "qux stowed correctly");
}

main();