[libcode-tidyall-perl] 27/374: fix inf loop in find_conf_file, other changes

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:25:42 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 9a47317e3c0eb3d0b82dc523a6b312a1e4d4f050
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Fri Jun 15 22:06:02 2012 -0500

    fix inf loop in find_conf_file, other changes
---
 bin/tidyall                 |    6 +++---
 lib/Code/TidyAll.pm         |   27 +++++++++++++++++++++-----
 lib/Code/TidyAll/Plugin.pm  |    5 +++--
 lib/Code/TidyAll/Util.pm    |    4 ++--
 lib/Code/TidyAll/t/Basic.pm |   45 ++++++++++++++++++++++++++++++++++++-------
 tidyall.ini                 |    2 +-
 6 files changed, 69 insertions(+), 20 deletions(-)

diff --git a/bin/tidyall b/bin/tidyall
index 19df2bc..37c45bc 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -1,5 +1,5 @@
 #!/usr/bin/perl
-use Cwd qw(realpath);
+use Cwd qw(cwd realpath);
 use Getopt::Long;
 use Pod::Usage;
 use Code::TidyAll::Util qw(can_load dirname);
@@ -40,13 +40,13 @@ $params{conf_file} ||= "$params{root_dir}/tidyall.ini" if ( $params{root_dir} );
 
 my $result;
 if ($all) {
-    $params{conf_file} ||= $class->find_conf_file( realpath(), "tidyall.ini" );
+    $params{conf_file} ||= $class->find_conf_file( cwd() );
     my $ct = $class->new(%params);
     $result = $ct->process_all();
 }
 else {
     my @files = @ARGV or die "file(s) or -a required";
-    $params{conf_file} ||= $class->find_conf_file( dirname( $files[0] ), "tidyall.ini" );
+    $params{conf_file} ||= $class->find_conf_file( dirname( $files[0] ) );
     my $ct = $class->new(%params);
     $result = $ct->process_files(@files);
 }
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 71f092a..78c6023 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -3,7 +3,7 @@ use Cwd qw(realpath);
 use Config::INI::Reader;
 use Code::TidyAll::Cache;
 use Code::TidyAll::Util
-  qw(abs2rel basename can_load dirname dump_one_line mkpath read_dir read_file uniq write_file);
+  qw(abs2rel basename can_load dirname dump_one_line mkpath read_dir read_file rel2abs uniq write_file);
 use Code::TidyAll::Result;
 use Date::Format;
 use Digest::SHA1 qw(sha1_hex);
@@ -41,6 +41,8 @@ use Object::Tiny qw(
   plugin_objects
 );
 
+my $ini_name = 'tidyall.ini';
+
 sub new {
     my $class  = shift;
     my %params = @_;
@@ -234,13 +236,27 @@ sub _purge_backups {
 }
 
 sub find_conf_file {
-    my ( $class, $search_dir, $search_file ) = @_;
+    my ( $class, $start_dir ) = @_;
+
+    my $path1     = rel2abs($start_dir);
+    my $path2     = realpath($start_dir);
+    my $conf_file = $class->_find_conf_file_upward($path1)
+      || $class->_find_conf_file_upward($path2);
+    unless ( defined $conf_file ) {
+        die sprintf( "could not find $ini_name upwards from %s",
+            ( $path1 eq $path2 ) ? "'$path1'" : "'$path1' or '$path2'" );
+    }
+    return $conf_file;
+}
+
+sub _find_conf_file_upward {
+    my ( $class, $search_dir ) = @_;
 
-    $search_dir  =~ s{/+$}{};
-    $search_file =~ s{^/+}{};
+    $search_dir =~ s{/+$}{};
 
+    my $cnt = 0;
     while (1) {
-        my $try_path = "$search_dir/$search_file";
+        my $try_path = "$search_dir/$ini_name";
         if ( -f $try_path ) {
             return $try_path;
         }
@@ -250,6 +266,7 @@ sub find_conf_file {
         else {
             $search_dir = dirname($search_dir);
         }
+        die "inf loop!" if ++$cnt > 100;
     }
 }
 
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index 4014a29..9ba07ce 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -11,7 +11,8 @@ sub new {
     die "name required" unless $self->{name};
 
     my $name = $self->{name};
-    $self->{select} = $self->{conf}->{select} or die "select required for '$name'";
+    $self->{select} = $self->{conf}->{select}
+      or die "select required for '$name'";
     die "select for '$name' should not begin with /" if substr( $self->{select}, 0, 1 ) eq '/';
     $self->{ignore} = $self->{conf}->{ignore};
     die "ignore for '$name' should not begin with /"
@@ -25,7 +26,7 @@ sub process_file {
     my ( $self, $file ) = @_;
     my $source = read_file($file);
     my $dest   = $self->process_source($source);
-    write_file( $file, $dest );
+    write_file( $file, $dest ) if $dest ne $source;
 }
 
 sub process_source {
diff --git a/lib/Code/TidyAll/Util.pm b/lib/Code/TidyAll/Util.pm
index 6745f6e..c0b4ffe 100644
--- a/lib/Code/TidyAll/Util.pm
+++ b/lib/Code/TidyAll/Util.pm
@@ -3,7 +3,7 @@ use Data::Dumper;
 use File::Basename;
 use File::Path;
 use File::Slurp qw(read_file write_file read_dir);
-use File::Spec::Functions qw(abs2rel);
+use File::Spec::Functions qw(abs2rel rel2abs);
 use File::Temp qw(tempdir);
 use List::MoreUtils qw(uniq);
 use Try::Tiny;
@@ -12,7 +12,7 @@ use warnings;
 use base qw(Exporter);
 
 our @EXPORT_OK =
-  qw(abs2rel basename can_load dirname dump_one_line mkpath read_dir read_file tempdir_simple uniq write_file );
+  qw(abs2rel basename can_load dirname dump_one_line mkpath read_dir read_file rel2abs tempdir_simple uniq write_file );
 
 sub can_load {
 
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index 32f4b94..1d12b15 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -69,8 +69,18 @@ sub test_basic : Tests {
     );
     $self->tidy(
         plugins => { %UpperText, %ReverseFoo },
-        source => { "foo.txt" => "abc", "bar.txt" => "def", "foo.tx" => "ghi", "bar.tx" => "jkl" },
-        dest   => { "foo.txt" => "CBA", "bar.txt" => "DEF", "foo.tx" => "ihg", "bar.tx" => "jkl" },
+        source  => {
+            "foo.txt" => "abc",
+            "bar.txt" => "def",
+            "foo.tx"  => "ghi",
+            "bar.tx"  => "jkl"
+        },
+        dest => {
+            "foo.txt" => "CBA",
+            "bar.txt" => "DEF",
+            "foo.tx"  => "ihg",
+            "bar.tx"  => "jkl"
+        },
         desc => 'four files UpperText ReverseFoo',
     );
     $self->tidy(
@@ -92,7 +102,8 @@ sub test_caching_and_backups : Tests {
             my $ct       = Code::TidyAll->new(
                 plugins  => {%UpperText},
                 root_dir => $root_dir,
-                ( $no_cache ? ( no_cache => 1 ) : () ), ( $no_backups ? ( no_backups => 1 ) : () )
+                ( $no_cache   ? ( no_cache   => 1 ) : () ),
+                ( $no_backups ? ( no_backups => 1 ) : () )
             );
             my $output;
             my $file = "$root_dir/foo.txt";
@@ -124,8 +135,15 @@ sub test_caching_and_backups : Tests {
             my $backup_dir = $ct->data_dir . "/backups";
             mkpath( $backup_dir, 0, 0775 );
             my @files;
-            find( { follow => 0, wanted => sub { push @files, $_ if -f }, no_chdir => 1 },
-                $backup_dir );
+            find(
+                {
+                    follow   => 0,
+                    wanted   => sub { push @files, $_ if -f },
+                    no_chdir => 1
+                },
+                $backup_dir
+            );
+
             if ($no_backups) {
                 ok( @files == 0, "no backup files $desc" );
             }
@@ -204,9 +222,12 @@ sub test_cli : Tests {
     my $self      = shift;
     my $root_dir  = $self->create_dir();
     my $conf_file = "$root_dir/tidyall.ini";
-    write_file( $conf_file,          $conf2 );
+    write_file( $conf_file, $conf2 );
+
     write_file( "$root_dir/foo.txt", "hello" );
-    my $output = capture_stdout { system( "$^X", "bin/tidyall", "$root_dir/foo.txt", "-v" ) };
+    my $output = capture_stdout {
+        system( "$^X", "bin/tidyall", "$root_dir/foo.txt", "-v" );
+    };
     my ($params_msg) = ( $output =~ /constructing Code::TidyAll with these params:(.*)/ );
     ok( defined($params_msg), "params msg" );
     like( $params_msg, qr/backup_ttl => '15m'/,                              'backup_ttl' );
@@ -214,6 +235,16 @@ sub test_cli : Tests {
     like( $params_msg, qr/\Qroot_dir => '$root_dir'\E/,                      'root_dir' );
     like( $output,     qr/\[tidied\]  foo.txt \(.*RepeatFoo, .*UpperText\)/, 'foo.txt' );
     is( read_file("$root_dir/foo.txt"), "HELLOHELLOHELLO", "tidied" );
+
+    mkpath( "$root_dir/subdir", 0, 0775 );
+    write_file( "$root_dir/subdir/foo.txt",  "bye" );
+    write_file( "$root_dir/subdir/foo2.txt", "bye" );
+    my $cwd = realpath();
+    capture_stdout {
+        system("cd $root_dir/subdir; $^X $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" );
 }
 
 $conf1 = '
diff --git a/tidyall.ini b/tidyall.ini
index 486706a..ec8ca0f 100644
--- a/tidyall.ini
+++ b/tidyall.ini
@@ -1,5 +1,5 @@
 [PerlTidy]
-argv = -noll
+argv = -noll -l=100
 perl_tidy_class = JS::Perl::Tidy
 select = {bin,lib,t}/**/{tidyall,*.{pl,pm,t}}
 

-- 
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