File: mutable_tree_while_iterating.t

package info (click to toggle)
libpath-tiny-perl 0.148-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 636 kB
  • sloc: perl: 1,300; makefile: 2; sh: 1
file content (59 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download | duplicates (6)
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
use strict;
use warnings;
use Test::More 0.88;
use Path::Tiny;

use lib 't/lib';
use TestUtils qw/exception tempd/;
use Path::Tiny;

my $wd = tempd;

my @tree = qw(
  base/Bethlehem/XDG/gift_list.txt
  base/Vancouver/ETHER/.naughty
  base/Vancouver/ETHER/gift_list.txt
  base/New_York/XDG/gift_list.txt
);
path($_)->touchpath for @tree;

subtest 'iterator' => sub {
    my @files;
    my $iter = path('base')->iterator( { recurse => 1 } );
    my $exception = exception {
        while ( my $path = $iter->() ) {
            $path->remove_tree if $path->child('.naughty')->is_file;
            push @files, $path if $path->is_file;
        }
    };

    is( $exception, '', 'can remove directories while traversing' );
    is_deeply(
        [ sort @files ],
        [ 'base/Bethlehem/XDG/gift_list.txt', 'base/New_York/XDG/gift_list.txt' ],
        'remaining files',
    );
};

subtest 'visit' => sub {
    my @files;
    my $exception = exception {
        path('base')->visit(
            sub {
                my $path = shift;
                $path->remove_tree if $path->child('.naughty')->is_file;
                push @files, $path if $path->is_file;
            },
            { recurse => 1 },
        );
    };

    is( $exception, '', 'can remove directories while traversing' );
    is_deeply(
        [ sort @files ],
        [ 'base/Bethlehem/XDG/gift_list.txt', 'base/New_York/XDG/gift_list.txt' ],
        'remaining files',
    );
};

done_testing;