Commits

Anonymous committed 7eba30e

Added the create_tree method with the corresponding tests.

Comments (0)

Files changed (2)

t/02tree-create.t

 use strict;
 use warnings;
 
-use Test::More tests => 13;
+use Test::More tests => 21;
 
 BEGIN
 {
         # Cleanup
         rmtree ($t->get_path("./t/sample-data/ls-test"));
     }
+
+    {
+        my $tree =
+        {
+            'name' => "tree-test-1/",
+            'subs' =>
+            [
+                {
+                    'name' => "b.doc",
+                    'contents' => "This file was spotted in the wild.",
+                },            
+                {
+                    'name' => "a/",
+                },
+                {
+                    'name' => "foo/",
+                    'subs' =>
+                    [
+                        {
+                            'name' => "yet/",
+                        },
+                    ],
+                },
+            ],
+        };
+
+        $t->create_tree("./t/sample-data/", $tree);
+
+        # TEST
+        is_deeply ($t->ls("./t/sample-data/tree-test-1"),
+            ["a", "b.doc", "foo"],
+            "Testing the contents of the root tree"
+        );
+
+        # TEST
+        ok ($t->is_dir("./t/sample-data/tree-test-1/a"), 
+            "a is a dir"
+        );
+
+        # TEST
+        is_deeply ($t->ls("./t/sample-data/tree-test-1/a"),
+            [],
+            "Testing the contents of a"
+        );
+
+        # TEST
+        is_deeply ($t->ls("./t/sample-data/tree-test-1/foo"),
+            ["yet"],
+            "Testing the contents of foo"
+        );
+
+        # TEST
+        ok ($t->is_dir("./t/sample-data/tree-test-1/foo/yet"),
+            "Testing that foo/yet is a dir"
+        );
+
+        # TEST
+        is_deeply ($t->ls("./t/sample-data/tree-test-1/foo/yet"),
+            [],
+            "Testing that foo/yet is a dir"
+        );
+
+        # TEST
+        ok ($t->is_file("./t/sample-data/tree-test-1/b.doc"),
+            "Checking that b.doc is a file"
+        );
+
+        # TEST
+        is ($t->cat("./t/sample-data/tree-test-1/b.doc"),
+            "This file was spotted in the wild.",
+            "Checking for contents of b.doc"
+        );
+
+        # Cleanup
+        rmtree ($t->get_path("./t/sample-data/tree-test-1"));
+    }
 }

t/lib/File/Find/Object/TreeCreate.pm

     closedir($dir);    
     return \@files;
 }
+
+sub create_tree
+{
+    my ($self, $unix_init_path, $tree) = @_;
+    my $real_init_path = $self->get_path($unix_init_path);
+    return $self->_real_create_tree($real_init_path, $tree);
+}
+
+sub _real_create_tree
+{
+    my ($self, $init_path, $tree) = @_;
+    my $name = $tree->{'name'};
+    if ($name =~ s{/$}{})
+    {
+        my $dir_name = File::Spec->catfile($init_path, $name);
+        mkdir($dir_name);
+        if (exists($tree->{'subs'}))
+        {
+            foreach my $sub (@{$tree->{'subs'}})
+            {
+                $self->_real_create_tree($dir_name, $sub);
+            }
+        }
+    }
+    else
+    {
+        open my $out, ">", File::Spec->catfile($init_path, $name);
+        print {$out} +(exists($tree->{'contents'}) ? $tree->{'contents'} : "");
+        close($out);
+    }
+    return 0;
+}
 1;