Commits

Shlomi Fish committed bf7f0b5

Add the basic functionality.

  • Participants
  • Parent commits 1ba5960

Comments (0)

Files changed (4)

Test-TrailingSpace/lib/Test/TrailingSpace.pm

 use warnings;
 package Test::TrailingSpace;
 
+use autodie;
+
+use Test::More;
+
+use File::Find::Object::Rule 0.0301;
+
+sub new
+{
+    my $class = shift;
+
+    my $self = bless {}, $class;
+
+    $self->_init(@_);
+
+    return $self;
+}
+
+sub _filename_regex
+{
+    my $self = shift;
+
+    if (@_)
+    {
+        $self->{_filename_regex} = shift;
+    }
+
+    return $self->{_filename_regex};
+}
+
+sub _root_path
+{
+    my $self = shift;
+
+    if (@_)
+    {
+        $self->{_root_path} = shift;
+    }
+
+    return $self->{_root_path};
+}
+sub _init
+{
+    my ($self, $args) = @_;
+
+    $self->_root_path(exists($args->{root}) ? $args->{root} : '.');
+    $self->_filename_regex($args->{filename_regex});
+
+    return;
+}
+
+sub no_trailing_space
+{
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my ($self, $blurb) = @_;
+
+    my $num_found = 0;
+
+    my $subrule = File::Find::Object::Rule->new;
+
+    my $rule = $subrule->or(
+        $subrule->new->directory->name(qr/(?:\A|\/)(?:blib|_build|CVS|\.svn|\.bzr\.hg|\.git)\z/)->prune->discard,
+        $subrule->new->file()->name($self->_filename_regex())
+    )->start( $self->_root_path() );
+
+    while ( my $path = $rule->match() )
+    {
+        open my $fh, '<', $path;
+        LINES:
+        while (my $line = <$fh>)
+        {
+            chomp($line);
+            if ($line =~ /[ \t]+\r?\z/)
+            {
+                $num_found++;
+                diag ("Found trailing space in file '$path'");
+                last LINES;
+            }
+        }
+        close ($fh);
+    }
+
+    is ($num_found, 0, $blurb);
+}
+
 1;
 
 __END__
     );
 
     # TEST
-    $finder->is_without_trailing_space(
+    $finder->no_trailing_space(
         "No trailing space was found."
     );
 
 matching the pattern will be searched (excpet for those under version
 control directories, "blib", "_build", etc.).
 
-=head2 $finder->is_without_trailing_space($blurb)
+=head2 $finder->no_trailing_space($blurb)
 
 Determines if there is no trailing space in the source files. Returns 1
 if there isn't and 0 if there's not, and is equivalent to the Test::More::ok(),

Test-TrailingSpace/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;
+

Test-TrailingSpace/t/object-test.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib './t/lib';
+
+use Test::Builder::Tester tests => 2;
+
+use File::Path qw( rmtree );
+
+use File::Find::Object::TreeCreate;
+use Test::TrailingSpace;
+
+{
+    my $test_id = "no-trailing-space-1";
+    my $test_dir = "t/sample-data/$test_id";
+    my $tree =
+    {
+        'name' => "$test_id/",
+        'subs' =>
+        [
+            {
+                'name' => "a/",
+                subs =>
+                [
+                    {
+                        'name' => "b.pm",
+                        'contents' => "This file was spotted in the wild.",
+                    },
+                ],
+            },
+            {
+                'name' => "foo/",
+                'subs' =>
+                [
+                    {
+                        'name' => "t.door.txt",
+                        'contents' => "A T Door",
+                    },
+                    {
+                        'name' => "yet/",
+                    },
+                ],
+            },
+        ],
+    };
+
+    my $t = File::Find::Object::TreeCreate->new();
+    $t->create_tree("./t/sample-data/", $tree);
+
+    my $finder = Test::TrailingSpace->new(
+        {
+            root => "./$test_dir",
+            filename_regex => qr/\.(?:pm|txt)\z/,
+        }
+    );
+
+    test_out("ok 1 - no trailing space FOO");
+    $finder->no_trailing_space("no trailing space FOO");
+    test_test("no trailing space was reported");
+    rmtree($t->get_path("./$test_dir"))
+}
+
+{
+    my $test_id = "with-trailing-space-1";
+    my $test_dir = "t/sample-data/$test_id";
+    my $tree =
+    {
+        'name' => "$test_id/",
+        'subs' =>
+        [
+            {
+                'name' => "a/",
+                subs =>
+                [
+                    {
+                        'name' => "b.pm",
+                        'contents' =>
+                        "This file.    \nI don't like it.",
+                    },
+                ],
+            },
+            {
+                'name' => "foo/",
+                'subs' =>
+                [
+                    {
+                        'name' => "t.door.txt",
+                        'contents' => "A T Door",
+                    },
+                    {
+                        'name' => "yet/",
+                    },
+                ],
+            },
+        ],
+    };
+
+    my $t = File::Find::Object::TreeCreate->new();
+    $t->create_tree("./t/sample-data/", $tree);
+
+    my $finder = Test::TrailingSpace->new(
+        {
+            root => "./$test_dir",
+            filename_regex => qr/\.(?:pm|txt)\z/,
+        }
+    );
+
+    test_out("not ok 1 - with trailing space CLAM");
+    test_fail(+1);
+    $finder->no_trailing_space("with trailing space CLAM");
+    test_test(title => "with trailing space was reported", skip_err => 1,);
+    rmtree($t->get_path("./$test_dir"))
+}

Test-TrailingSpace/t/sample-data/PLACEHOLDER

Empty file added.