[libcode-tidyall-perl] 337/374: Rename API method process_files to more accurate process_paths; Fix recursive processing of directories

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26:47 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcode-tidyall-perl.

commit 7f2b1719b21faa9cd5541b239d5a3a19f17b43f5
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Wed Oct 24 12:12:38 2012 -0700

    Rename API method process_files to more accurate process_paths; Fix recursive processing of directories
---
 Changes                           |    6 ++++
 bin/tidyall                       |   29 +++++++--------
 lib/Code/TidyAll.pm               |   71 +++++++++++++++++++------------------
 lib/Code/TidyAll/Git/Precommit.pm |    2 +-
 lib/Code/TidyAll/Result.pm        |    2 +-
 lib/Code/TidyAll/SVN/Precommit.pm |    2 +-
 lib/Code/TidyAll/t/Basic.pm       |   38 ++++++++++++--------
 7 files changed, 80 insertions(+), 70 deletions(-)

diff --git a/Changes b/Changes
index 9e324f7..5c09dd5 100644
--- a/Changes
+++ b/Changes
@@ -4,6 +4,12 @@ Revision history for Code-TidyAll
 
 0.17  Oct 22, 2012
 
+* Improvements
+  - ** Rename API method process_files to more accurate process_paths
+
+* Fixes
+  - Fix recursive processing of directories (Mark Risman)
+
 0.16  Oct 22, 2012
 
 * Plugins
diff --git a/bin/tidyall b/bin/tidyall
index 0c42c32..41c471a 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -51,8 +51,8 @@ sub usage {
 }
 
 my (
-    %params,   $all_files, $conf_file, $conf_name, $git_files, $help,
-    $inc_dirs, $list,      $pipe,      $svn_files, $version
+    %params, $all_files, $conf_file, $conf_name, $git_files,
+    $help,   $inc_dirs,  $pipe,      $svn_files, $version
 );
 my @conf_names = Code::TidyAll->default_conf_names;
 
@@ -72,7 +72,7 @@ GetOptions(
     'a|all'           => \$all_files,
     'g|git'           => \$git_files,
     'h|help'          => \$help,
-    'l|list'          => \$list,
+    'l|list'          => \$params{list_only},
     'm|mode=s'        => \$params{mode},
     'p|pipe=s'        => \$pipe,
     'r|recursive'     => \$params{recursive},
@@ -94,7 +94,7 @@ unshift( @INC, split( /\s*,\s*/, $inc_dirs ) ) if defined($inc_dirs);
 
 my $tidyall_class = $params{tidyall_class} || 'Code::TidyAll';
 
-my ( $ct, @files );
+my ( $ct, @paths );
 
 if ($pipe) {
     my $status = handle_pipe($pipe);
@@ -107,19 +107,19 @@ elsif ( ( $all_files || $svn_files || $git_files ) ) {
     $ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
 
     if ($all_files) {
-        @files = $ct->find_matched_files;
+        @paths = $ct->find_matched_files;
     }
     elsif ($svn_files) {
         require Code::TidyAll::SVN::Util;
-        @files = Code::TidyAll::SVN::Util::svn_uncommitted_files( $ct->root_dir );
+        @paths = Code::TidyAll::SVN::Util::svn_uncommitted_files( $ct->root_dir );
     }
     elsif ($git_files) {
         require Code::TidyAll::Git::Util;
-        @files = Code::TidyAll::Git::Util::git_uncommitted_files( $ct->root_dir );
+        @paths = Code::TidyAll::Git::Util::git_uncommitted_files( $ct->root_dir );
     }
 }
-elsif ( @files = @ARGV ) {
-    $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, dirname( $files[0] ) );
+elsif ( @paths = @ARGV ) {
+    $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, dirname( $paths[0] ) );
     $ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
 }
 else {
@@ -127,14 +127,9 @@ else {
     usage();
 }
 
-if ($list) {
-    $ct->list_files(@files);
-}
-else {
-    my @results = $ct->process_files(@files);
-    my $status = ( grep { $_->error } @results ) ? 1 : 0;
-    exit($status);
-}
+my @results = $ct->process_paths(@paths);
+my $status = ( grep { $_->error } @results ) ? 1 : 0;
+exit($status);
 
 sub handle_pipe {
     my ($pipe_filename) = @_;
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 306cfff..9fd3e8d 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -23,6 +23,7 @@ has 'backup_ttl'    => ( is => 'ro', default => sub { '1 hour' } );
 has 'check_only'    => ( is => 'ro' );
 has 'data_dir'      => ( is => 'lazy' );
 has 'iterations'    => ( is => 'ro', default => sub { 1 } );
+has 'list_only'     => ( is => 'ro' );
 has 'mode'          => ( is => 'ro', default => sub { 'cli' } );
 has 'no_backups'    => ( is => 'ro' );
 has 'no_cache'      => ( is => 'ro' );
@@ -181,43 +182,45 @@ sub _plugin_conf_matches_mode {
 sub process_all {
     my $self = shift;
 
-    return $self->process_files( $self->find_matched_files );
+    return $self->process_paths( $self->find_matched_files );
 }
 
-sub process_files {
-    my ( $self, @files ) = @_;
+sub process_paths {
+    my ( $self, @paths ) = @_;
 
-    return map { $self->process_file( realpath($_) || rel2abs($_) ) } @files;
+    return map { $self->process_path( realpath($_) || rel2abs($_) ) } @paths;
 }
 
-sub list_files {
-    my ( $self, @files ) = @_;
+sub process_path {
+    my ( $self, $path ) = @_;
 
-    foreach my $file (@files) {
-        $file = realpath($file) || rel2abs($file);
-        my $path = $self->_small_path($file);
-        if ( my @plugins = $self->plugins_for_path($path) ) {
-            printf( "%s (%s)\n", $path, join( ", ", map { $_->name } @plugins ) );
+    if ( -d $path ) {
+        if ( $self->recursive ) {
+            return $self->process_paths( map { "$path/$_" } read_dir($path) );
+        }
+        else {
+            return ( $self->_error_result( "$path: is a directory (try -r/--recursive)", $path ) );
         }
     }
+    elsif ( -f $path ) {
+        return ( $self->process_file($path) );
+    }
+    else {
+        return ( $self->_error_result( "$path: not a file or directory", $path ) );
+    }
 }
 
 sub process_file {
     my ( $self, $file ) = @_;
+    die "$file is not a file" unless -f $file;
+
     my $path = $self->_small_path($file);
 
-    if ( -d $file ) {
-        if ( $self->recursive ) {
-            return $self->process_dir($file);
-        }
-        else {
-            print "$path: is a directory (try -r/--recursive)";
-            return;
+    if ( $self->list_only ) {
+        if ( my @plugins = $self->plugins_for_path($path) ) {
+            printf( "%s (%s)\n", $path, join( ", ", map { $_->name } @plugins ) );
         }
-    }
-    elsif ( !-f $file ) {
-        print "$path: not a file or directory\n";
-        return;
+        return Code::TidyAll::Result->new( path => $path, state => 'checked' );
     }
 
     my $cache     = $self->no_cache ? undef : $self->cache;
@@ -245,14 +248,6 @@ sub process_file {
     return $result;
 }
 
-sub process_dir {
-    my ( $self, $dir ) = @_;
-
-    foreach my $subfile ( read_dir($dir) ) {
-        $self->process_file("$dir/$subfile");
-    }
-}
-
 sub process_source {
     my ( $self, $contents, $path ) = @_;
 
@@ -298,8 +293,7 @@ sub process_source {
     }
 
     if ($error) {
-        $self->msg( "%s", $error );
-        return Code::TidyAll::Result->new( path => $path, state => 'error', error => $error );
+        return $self->_error_result( $error, $path );
     }
     elsif ($was_tidied) {
         return Code::TidyAll::Result->new(
@@ -478,6 +472,12 @@ sub msg {
     printf "$format\n", @params;
 }
 
+sub _error_result {
+    my ( $self, $msg, $path ) = @_;
+    $self->msg( "%s", $msg );
+    return Code::TidyAll::Result->new( path => $path, state => 'error', error => $msg );
+}
+
 1;
 
 __END__
@@ -512,7 +512,7 @@ Code::TidyAll - Engine for tidyall, your all-in-one code tidier and validator
 
     # then...
 
-    $ct->process_files($file1, $file2);
+    $ct->process_paths($file1, $file2);
 
     # or
 
@@ -592,9 +592,10 @@ C<backup_ttl> here).
 
 Process all files; this implements the C<tidyall -a> option.
 
-=item process_files (file, ...)
+=item process_paths (path, ...)
 
-Call L</process_file> on each file. Return a list of
+Call L</process_file> on each file; descend recursively into each directory if
+the C<recursive> flag is on. Return a list of
 L<Code::TidyAll::Result|Code::TidyAll::Result> objects, one for each file.
 
 =item process_file (file)
diff --git a/lib/Code/TidyAll/Git/Precommit.pm b/lib/Code/TidyAll/Git/Precommit.pm
index a30233f..34f4e39 100644
--- a/lib/Code/TidyAll/Git/Precommit.pm
+++ b/lib/Code/TidyAll/Git/Precommit.pm
@@ -51,7 +51,7 @@ sub check {
             mode       => 'commit',
             %{ $self->tidyall_options },
         );
-        my @results = $tidyall->process_files( map { "$root_dir/$_" } @files );
+        my @results = $tidyall->process_paths( map { "$root_dir/$_" } @files );
 
         if ( my @error_results = grep { $_->error } @results ) {
             my $error_count = scalar(@error_results);
diff --git a/lib/Code/TidyAll/Result.pm b/lib/Code/TidyAll/Result.pm
index 3259db2..add8d09 100644
--- a/lib/Code/TidyAll/Result.pm
+++ b/lib/Code/TidyAll/Result.pm
@@ -31,7 +31,7 @@ Code::TidyAll::Result - Result returned from processing a file/source
 Represents the result of
 L<Code::TidyAll::process_file|Code::TidyAll/process_file> and
 L<Code::TidyAll::process_file|Code::TidyAll/process_source>. A list of these is
-returned from L<Code::TidyAll::process_files|Code::TidyAll/process_files>.
+returned from L<Code::TidyAll::process_paths|Code::TidyAll/process_paths>.
 
 =head1 METHODS
 
diff --git a/lib/Code/TidyAll/SVN/Precommit.pm b/lib/Code/TidyAll/SVN/Precommit.pm
index 06fc013..457f7b2 100644
--- a/lib/Code/TidyAll/SVN/Precommit.pm
+++ b/lib/Code/TidyAll/SVN/Precommit.pm
@@ -89,7 +89,7 @@ sub check {
                 %{ $self->tidyall_options },
             );
             my $stdout = capture_stdout {
-                push( @results, $tidyall->process_files( map { "$tempdir/$_" } @files ) );
+                push( @results, $tidyall->process_paths( map { "$tempdir/$_" } @files ) );
             };
             if ($stdout) {
                 chomp($stdout);
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index 8815380..feebbc1 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -2,7 +2,7 @@ package Code::TidyAll::t::Basic;
 use Cwd qw(realpath);
 use Code::TidyAll::Util qw(dirname mkpath pushd read_file tempdir_simple write_file);
 use Code::TidyAll;
-use Capture::Tiny qw(capture capture_stdout);
+use Capture::Tiny qw(capture capture_stdout capture_merged);
 use File::Find qw(find);
 use Test::Class::Most parent => 'Code::TidyAll::Test::Class';
 
@@ -171,7 +171,7 @@ sub test_quiet_and_verbose : Tests {
                     root_dir => $root_dir,
                     ( $state eq 'normal' ? () : ( $state => 1 ) )
                 );
-                $ct->process_files("$root_dir/foo.txt");
+                $ct->process_paths("$root_dir/foo.txt");
             };
             if ($error) {
                 like( $output, qr/non-alpha content found/, "non-alpha content found ($state)" );
@@ -200,7 +200,7 @@ sub test_iterations : Tests {
         iterations => 2
     );
     my $file = "$root_dir/foo.txt";
-    $ct->process_files($file);
+    $ct->process_paths($file);
     is( read_file($file), scalar( "abc" x 9 ), "3^2 = 9" );
 }
 
@@ -220,7 +220,7 @@ sub test_caching_and_backups : Tests {
             my $output;
             my $file = "$root_dir/foo.txt";
             my $go   = sub {
-                $output = capture_stdout { $ct->process_files($file) };
+                $output = capture_stdout { $ct->process_paths($file) };
             };
 
             $go->();
@@ -299,15 +299,18 @@ sub test_dirs : Tests {
     my $root_dir = $self->create_dir( { map { $_ => 'hi' } @files } );
 
     foreach my $recursive ( 0 .. 1 ) {
-        my $output = capture_stdout {
+        my @results;
+        my $output = capture_merged {
             my $ct = Code::TidyAll->new(
                 plugins  => { %UpperText, %ReverseFoo },
                 root_dir => $root_dir,
                 ( $recursive ? ( recursive => 1 ) : () )
             );
-            $ct->process_file("$root_dir/a");
+            @results = $ct->process_paths("$root_dir/a");
         };
         if ($recursive) {
+            is( @results, 3, "3 results" );
+            is( scalar( grep { $_->state eq 'tidied' } @results ), 2, "2 tidied" );
             like( $output, qr/\[tidied\]  a\/foo.txt/ );
             like( $output, qr/\[tidied\]  a\/bar.txt/ );
             is( read_file("$root_dir/a/foo.txt"), "IH" );
@@ -316,6 +319,8 @@ sub test_dirs : Tests {
             is( read_file("$root_dir/b/foo.txt"), "hi" );
         }
         else {
+            is( @results,           1,       "1 result" );
+            is( $results[0]->state, "error", "error" );
             like( $output, qr/is a directory/ );
         }
     }
@@ -357,27 +362,30 @@ sub test_errors : Tests {
     qr/unknown options/;
 
     my $ct = Code::TidyAll->new( plugins => {%UpperText}, root_dir => $root_dir );
-    my $output = capture_stdout { $ct->process_files("$root_dir/baz/blargh.txt") };
+    my $output = capture_stdout { $ct->process_paths("$root_dir/baz/blargh.txt") };
     like( $output, qr/baz\/blargh.txt: not a file or directory/, "file not found" );
 
-    $output = capture_stdout { $ct->process_files("$root_dir/foo/bar.txt") };
+    $output = capture_stdout { $ct->process_paths("$root_dir/foo/bar.txt") };
     is( $output, "[tidied]  foo/bar.txt\n", "filename output" );
     is( read_file("$root_dir/foo/bar.txt"), "ABC", "tidied" );
     my $other_dir = realpath( tempdir_simple() );
     write_file( "$other_dir/foo.txt", "ABC" );
-    throws_ok { $ct->process_files("$other_dir/foo.txt") } qr/not underneath root dir/;
+    throws_ok { $ct->process_paths("$other_dir/foo.txt") } qr/not underneath root dir/;
 }
 
 sub test_cli : Tests {
     my $self = shift;
     my $output;
 
+    my @cmd = ( "$^X", "-Ilib", "bin/tidyall" );
+    my $run = sub { system( @cmd, @_ ) };
+
     $output = capture_stdout {
-        system( "$^X", "bin/tidyall", "--version" );
+        $run->("--version");
     };
     like( $output, qr/tidyall .* on perl/ );
     $output = capture_stdout {
-        system( "$^X", "bin/tidyall", "--help" );
+        $run->("--help");
     };
     like( $output, qr/Usage.*Options:/s );
 
@@ -388,7 +396,7 @@ sub test_cli : Tests {
 
         write_file( "$root_dir/foo.txt", "hello" );
         my $output = capture_stdout {
-            system( "$^X", "bin/tidyall", "$root_dir/foo.txt", "-v" );
+            $run->( "$root_dir/foo.txt", "-v" );
         };
 
         my ($params_msg) = ( $output =~ /constructing Code::TidyAll with these params:(.*)/ );
@@ -404,7 +412,7 @@ sub test_cli : Tests {
         write_file( "$root_dir/subdir/foo2.txt", "bye" );
         my $cwd = realpath();
         capture_stdout {
-            system("cd $root_dir/subdir; $^X $cwd/bin/tidyall foo.txt");
+            system("cd $root_dir/subdir; $^X -I$cwd/lib $cwd/bin/tidyall foo.txt");
         };
         is( read_file("$root_dir/subdir/foo.txt"),  "BYEBYEBYE", "foo.txt tidied" );
         is( read_file("$root_dir/subdir/foo2.txt"), "bye",       "foo2.txt not tidied" );
@@ -412,7 +420,7 @@ sub test_cli : Tests {
         # -p / --pipe success
         #
         my ( $stdout, $stderr ) = capture {
-            open( my $fh, "|-", "$^X", "bin/tidyall", "-p", "$root_dir/does_not_exist/foo.txt" );
+            open( my $fh, "|-", @cmd, "-p", "$root_dir/does_not_exist/foo.txt" );
             print $fh "echo";
         };
         is( $stdout, "ECHOECHOECHO", "pipe: stdin tidied" );
@@ -421,7 +429,7 @@ sub test_cli : Tests {
         # -p / --pipe error
         #
         ( $stdout, $stderr ) = capture {
-            open( my $fh, "|-", "$^X", "bin/tidyall", "--pipe", "$root_dir/foo.txt" );
+            open( my $fh, "|-", @cmd, "--pipe", "$root_dir/foo.txt" );
             print $fh "abc1";
         };
         is( $stdout, "abc1", "pipe: stdin mirrored to stdout" );

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcode-tidyall-perl.git



More information about the Pkg-perl-cvs-commits mailing list