Skip to content

Commit

Permalink
add max_depth option to list_tree method in Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 2, 2018
1 parent 389f297 commit e3592a2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

8.09 2018-12-01
8.09 2018-12-02
- Added max_depth option to list_tree method in Mojo::File.

8.08 2018-11-26
- Added stat method to Mojo::File.
Expand Down
18 changes: 14 additions & 4 deletions lib/Mojo/File.pm
Expand Up @@ -65,10 +65,14 @@ sub list_tree {
local $File::Find::dont_use_nlink = 1 if $options->{dont_use_nlink};

my %all;
my $wanted = {wanted => sub { $all{$File::Find::name}++ }, no_chdir => 1};
$wanted->{postprocess} = sub { delete $all{$File::Find::dir} }
unless $options->{dir};
find $wanted, $$self if -d $$self;
my $wanted = sub {
if ($options->{max_depth}) {
(my $rel = $File::Find::name) =~ s!^\Q$$self\E/?!!;
$File::Find::prune = 1 if splitdir($rel) >= $options->{max_depth};
}
$all{$File::Find::name}++ if $options->{dir} || !-d $File::Find::name;
};
find {wanted => $wanted, no_chdir => 1}, $$self if -d $$self;
delete $all{$$self};

return Mojo::Collection->new(map { $self->new(canonpath $_) } sort keys %all);
Expand Down Expand Up @@ -350,6 +354,12 @@ Force L<File::Find> to always stat directories.
Include hidden files and directories.
=item max_depth
max_depth => 3
Maximum number of levels to descend when searching for files.
=back
=head2 make_path
Expand Down
37 changes: 37 additions & 0 deletions t/mojo/file.t
Expand Up @@ -219,6 +219,43 @@ my @all = map { path($lib)->child(split '/') } (
);
is_deeply path($lib)->list_tree({dir => 1, hidden => 1})->map('to_string')
->to_array, [@all], 'right files';
my @one = map { path($lib)->child(split '/') } (
'DeprecationTest.pm', 'LoaderException.pm',
'LoaderException2.pm', 'TestConnectProxy.pm'
);
is_deeply path($lib)->list_tree({max_depth => 1})->map('to_string')->to_array,
[@one], 'right files';
my @one_dir = map { path($lib)->child(split '/') } (
'BaseTest', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest', 'Server',
'TestConnectProxy.pm'
);
is_deeply path($lib)->list_tree({dir => 1, max_depth => 1})->map('to_string')
->to_array, [@one_dir], 'right files';
my @two = map { path($lib)->child(split '/') } (
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest/A.pm', 'LoaderTest/B.pm',
'LoaderTest/C.pm',,
'TestConnectProxy.pm'
);
is_deeply path($lib)->list_tree({max_depth => 2})->map('to_string')->to_array,
[@two], 'right files';
my @three = map { path($lib)->child(split '/') } (
'.hidden.txt', '.test',
'.test/hidden.txt', 'BaseTest',
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest', 'LoaderTest/A.pm',
'LoaderTest/B.pm', 'LoaderTest/C.pm',
'Server', 'Server/Morbo',
'Server/Morbo/Backend', 'TestConnectProxy.pm'
);
is_deeply path($lib)->list_tree({dir => 1, hidden => 1, max_depth => 3})
->map('to_string')->to_array, [@three], 'right files';

# I/O
$dir = tempdir;
Expand Down

0 comments on commit e3592a2

Please sign in to comment.