Commits

shl...@52c325ad-5fd0-0310-8a0f-c43feede02cc  committed 1e8e30a

Add t/01traverse.t as a test which passes successfully.

  • Participants
  • Parent commits 8b0cc4e

Comments (0)

Files changed (3)

File libfilefind/c_glib_based/t/01traverse1.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+BEGIN
+{
+    use File::Spec;
+    use lib File::Spec->catdir(File::Spec->curdir(), "t", "lib");
+}
+
+use File::Find::Object::TreeCreate;
+use File::Find::Object;
+
+use File::Path;
+
+{
+    my $tree =
+    {
+        'name' => "traverse-1/",
+        'subs' =>
+        [
+            {
+                'name' => "b.doc",
+                'contents' => "This file was spotted in the wild.",
+            },            
+            {
+                'name' => "a/",
+            },
+            {
+                'name' => "foo/",
+                'subs' =>
+                [
+                    {
+                        'name' => "yet/",
+                    },
+                ],
+            },
+        ],
+    };
+
+    my $t = File::Find::Object::TreeCreate->new();
+    $t->create_tree("./t/sample-data/", $tree);
+
+    open my $lff_fh, "./minifind " . $t->get_path("./t/sample-data/traverse-1") . "|"
+        or die "Cannot execute minifind";
+
+    my @results = <$lff_fh>;
+    chomp(@results);
+
+    close($lff_fh);
+
+    # TEST
+    is_deeply(
+        \@results,
+        [(map { $t->get_path("t/sample-data/traverse-1/$_") }
+            ("", qw(
+                a
+                b.doc
+                foo
+                foo/yet
+            ))),
+        ],
+        "Checking for regular, lexicographically sorted order",
+    );
+
+    rmtree($t->get_path("./t/sample-data/traverse-1"));
+}

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

+package File::Find::Object::TreeCreate;
+
+use strict;
+use warnings;
+
+use File::Spec;
+
+sub new
+{
+    my $class = shift;
+    my $self = {};
+    bless $self, $class;
+    $self->_initialize(@_);
+    return $self;
+}
+
+sub _initialize
+{
+}
+
+sub get_path
+{
+    my $self = shift;
+    my $path = shift;
+
+    my @components;
+
+    if ($path =~ s{^\./}{})
+    {
+        push @components, File::Spec->curdir();
+    }
+    
+    my $is_dir = ($path =~ s{/$}{});
+    push @components, split(/\//, $path);
+    if ($is_dir)
+    {
+        return File::Spec->catdir(@components);
+    }
+    else
+    {
+        return File::Spec->catfile(@components);
+    }
+}
+
+sub exist
+{
+    my $self = shift;
+    return (-e $self->get_path(@_));
+}
+
+sub is_file
+{
+    my $self = shift;
+    return (-f $self->get_path(@_));
+}
+
+sub is_dir
+{
+    my $self = shift;
+    return (-d $self->get_path(@_));
+}
+
+sub cat
+{
+    my $self = shift;
+    open my $in, "<", $self->get_path(@_) or
+        return 0;
+    my $data;
+    {
+        local $/;
+        $data = <$in>;
+    }
+    close($in);
+    return $data;
+}
+
+sub ls
+{
+    my $self = shift;
+    opendir my $dir, $self->get_path(@_) or
+        return undef;
+    my @files = 
+        sort { $a cmp $b } 
+        grep { !(($_ eq ".") || ($_ eq "..")) }
+        readdir($dir);
+    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;
+

File libfilefind/c_glib_based/t/sample-data/placeholder

Empty file added.