Commits

Anonymous committed 18a4d19

1. Added another test to test ->prune().

2. Added the ->prune() method and documented it.

3. Added a $self->get_current_node_files_list(); call to set_traverse_to
to make sure we scan the current directory for sub-items first.

4. Fixed a typo of the _traverse_to() accessor in ->get_traverse_to().

  • Participants
  • Parent commits 774e8b9

Comments (0)

Files changed (2)

File lib/File/Find/Object.pm

 sub set_traverse_to
 {
     my ($self, $children) = @_;
+
+    # Make sure we scan the current directory for sub-items first.
+    $self->get_current_node_files_list();
+
     $self->_current->_traverse_to([@$children]);
 }
 
 {
     my $self = shift;
 
-    return [ @{$self->_current->traverse_to()} ];
+    return [ @{$self->_current->_traverse_to()} ];
 }
 
 sub get_current_node_files_list
     # open_dir can return undef if $self->_current is not a directory.
     if ($self->open_dir($self->_current))
     {
-        return [ @{$self->_current->_files()}];    
+        return [ @{$self->_current->_files()}];
     }
     else
     {
     }
 }
 
+sub prune
+{
+    my $self = shift;
+
+    return $self->set_traverse_to([]);
+}
 
 1;
 
 Sets the children to traverse to from the current node. Useful for pruning
 items to traverse.
 
+=head2 $ff->prune()
+
+Prunes the current directory. Equivalent to $ff->set_traverse_to([]).
+
 =head2 [@children] = $ff->get_traverse_to()
 
 Retrieves the children that will be traversed to.
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 4;
 
 BEGIN
 {
                     @{$ff->get_current_node_files_list()}
                 ]
             );
+
+            # TEST
+            is_deeply ($ff->get_traverse_to(), ["h.rnd", "lambda.calculus"],
+                "Testing ->get_traverse_to()"
+            );
         }
         push @results, $file;
     }
     rmtree($t->get_path("./t/sample-data/traverse-2"))
 }
 
+{
+    my $tree =
+    {
+        'name' => "traverse-2/",
+        'subs' =>
+        [
+            {
+                'name' => "b.doc",
+                'contents' => "This file was spotted in the wild.",
+            },            
+            {
+                'name' => "a/",
+            },
+            {
+                'name' => "foo/",
+                'subs' =>
+                [
+                    {
+                        'name' => "please-prune-me/",
+                        'subs' =>
+                        [
+                            {
+                                'name' => "a-non-reachable-dir/",
+                                'subs' =>
+                                [
+                                    {
+                                        'name' => "dir1/",
+                                    },
+                                    {
+                                        'name' => "dir2/",
+                                    },
+                                    {
+                                        'name' => 
+                                            "if-we-get-this-its-wrong.txt",
+                                        'content' => "Hi ho!",
+                                    },
+                                ],
+                            },                        
+                            {
+                                'name' => "h.rnd",
+                                'contents' => "This file is empty.",
+                            },
+                            {
+                                'name' => "lambda.calculus",
+                                'contents' => '\f \x (f (f x))'
+                            },
+                        ],
+                    },
+                    {
+                        'name' => "zardoz/",
+                        'subs' =>
+                        [
+                            {
+                                'name' => "p.txt",
+                                'contents' => "Intentionally Left Boring.",
+                            },
+                        ],
+                    },
+                ],
+            },
+            {
+                'name' => "zardoz/",
+                'subs' =>
+                [
+                    {
+                        'name' => "p.txt",
+                        'contents' => "Intentionally Left Boring.",
+                    },
+                ],
+            },
 
+        ],
+    };
+
+    my $t = File::Find::Object::TreeCreate->new();
+    $t->create_tree("./t/sample-data/", $tree);
+    my $ff = 
+        File::Find::Object->new(
+            {},
+            $t->get_path("./t/sample-data/traverse-2")
+        );
+    my @results;
+    for my $i (1 .. 9)
+    {
+        my $file = $ff->next();
+        if ($file eq 
+            $t->get_path("t/sample-data/traverse-2/foo/please-prune-me")
+           )
+        {
+            $ff->prune();
+        }
+        push @results, $file;
+    }
+
+    # TEST
+    ok (!defined($ff->next()), "Testing that the scan has completed.");
+
+    # TEST
+    is_deeply(
+        \@results,
+        [(map { $t->get_path("t/sample-data/traverse-2/$_") }
+            ("", 
+            qw(
+                a
+                b.doc
+                foo
+                foo/please-prune-me
+                foo/zardoz
+                foo/zardoz/p.txt
+                zardoz
+                zardoz/p.txt
+            )))
+        ],
+        "Checking for regular, lexicographically sorted order",
+    );
+
+    rmtree($t->get_path("./t/sample-data/traverse-2"))
+}