[libcode-tidyall-perl] 16/374: test CLI

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:25:40 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 80b4f5576f60f320e5ce3f55bc4594b312de6069
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Wed Jun 13 14:09:54 2012 -0500

    test CLI
---
 bin/tidyall                               |   27 ++++++++-----
 lib/Code/TidyAll.pm                       |   46 ++++++++++++++-------
 lib/Code/TidyAll/Test/Plugin/RepeatBar.pm |   17 ++++++++
 lib/Code/TidyAll/Util.pm                  |   10 ++++-
 lib/Code/TidyAll/t/Basic.pm               |   63 ++++++++++++++++++++++-------
 5 files changed, 121 insertions(+), 42 deletions(-)

diff --git a/bin/tidyall b/bin/tidyall
index f94f320..d6c34aa 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -2,6 +2,7 @@
 use Getopt::Long;
 use Pod::Usage;
 use Code::TidyAll::Util qw(can_load);
+use Hash::MoreUtils qw(slice_def);
 use strict;
 use warnings;
 
@@ -12,27 +13,31 @@ sub usage {
     Pod::Usage::pod2usage( { verbose => 2 } );
 }
 
-my %params;
+my ( %params, $help );
 my $class = 'Code::TidyAll';
 
 GetOptions(
-    'backup-ttl'  => \$params{backup_ttl},
-    'class'       => \$class,
-    'data-dir'    => \$params{data_dir},
-    'no-backups'  => \$params{no_backups},
-    'no-cache'    => \$params{no_cache},
-    'c|conf'      => \$params{conf_file},
-    'h|help'      => \$help,
-    'r|recursive' => \$params{recursive},
+    'backup-ttl=i' => \$params{backup_ttl},
+    'class=s'      => \$class,
+    'data-dir=s'   => \$params{data_dir},
+    'no-backups'   => \$params{no_backups},
+    'no-cache'     => \$params{no_cache},
+    'c|conf=s'     => \$params{conf_file},
+    'h|help'       => \$help,
+    'r|recursive'  => \$params{recursive},
+    'v|verbose'    => \$params{verbose},
 ) or usage();
 
-usage("-c|--conf required") if !$params{conf_file};
+usage("") if $help;
+usage("-c|--conf required") if !defined( $params{conf_file} );
 die "cannot load '$class'" unless can_load($class);
 
+%params = slice_def( \%params );
+
 my @paths = @ARGV or usage("path(s) required");
 
 my $ct = $class->new(%params);
-$ct->process_paths($path);
+$ct->process_paths(@paths);
 
 1;
 
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 7d53599..27fe77c 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -2,7 +2,7 @@ package Code::TidyAll;
 use Cwd qw(realpath);
 use Config::INI::Reader;
 use Code::TidyAll::Cache;
-use Code::TidyAll::Util qw(basename can_load dirname mkpath read_file write_file);
+use Code::TidyAll::Util qw(basename can_load dirname dump_one_line mkpath read_file write_file);
 use Date::Format;
 use Digest::SHA1 qw(sha1_hex);
 use File::Find qw(find);
@@ -74,18 +74,29 @@ sub new {
     die "conf_file or plugins required"  unless $params{plugins};
     die "conf_file or root_dir required" unless $params{root_dir};
 
+    $class->msg( "constructing %s with these params: %s", $class, \%params )
+      if ( $params{verbose} );
+
     my $self = $class->SUPER::new(%params);
 
     $self->{root_dir} = realpath( $self->{root_dir} );
     $self->{data_dir} ||= $self->root_dir . "/.tidyall.d";
-    $self->{cache} = Code::TidyAll::Cache->new( cache_dir => $self->data_dir . "/cache" )
-      unless $self->no_cache;
-    $self->{backup_dir} = $self->data_dir . "/backups";
-    $self->{base_sig} = $self->_sig( [ $Code::TidyAll::VERSION || 0, $self->plugins ] );
-    $self->{backup_ttl} ||= '1 hour';
-    $self->{backup_ttl} = parse_duration( $self->{backup_ttl} )
-      unless $self->{backup_ttl} =~ /^\d+$/;
+
+    unless ( $self->no_cache ) {
+        $self->{cache} = Code::TidyAll::Cache->new( cache_dir => $self->data_dir . "/cache" );
+    }
+
+    unless ( $self->no_backups ) {
+        $self->{backup_dir} = $self->data_dir . "/backups";
+        mkpath( $self->backup_dir, 0, 0775 );
+        $self->{backup_ttl} ||= '1 hour';
+        $self->{backup_ttl} = parse_duration( $self->{backup_ttl} )
+          unless $self->{backup_ttl} =~ /^\d+$/;
+        $self->_purge_backups_periodically();
+    }
+
     my $plugins = $self->plugins;
+    $self->{base_sig} = $self->_sig( [ $Code::TidyAll::VERSION || 0, $plugins ] );
     $self->{plugin_objects} =
       [ map { $self->load_plugin( $_, $plugins->{$_} ) } keys( %{ $self->plugins } ) ];
 
@@ -177,13 +188,6 @@ sub _backup_file {
         my $backup_file = join( "/", $self->backup_dir, $self->_backup_filename($file) );
         mkpath( dirname($backup_file), 0, 0775 );
         write_file( $backup_file, read_file($file) );
-        if ( my $cache = $self->cache ) {
-            my $last_purge_backups = $cache->get("last_purge_backups") || 0;
-            if ( time > $last_purge_backups + $self->backup_ttl ) {
-                $self->_purge_backups();
-                $cache->set( "last_purge_backups", time() );
-            }
-        }
     }
 }
 
@@ -193,6 +197,17 @@ sub _backup_filename {
     return join( "", $self->_small_path($file), "-", time2str( "%Y%m%d-%H%M%S", time ), ".bak" );
 }
 
+sub _purge_backups_periodically {
+    my ($self) = @_;
+    if ( my $cache = $self->cache ) {
+        my $last_purge_backups = $cache->get("last_purge_backups") || 0;
+        if ( time > $last_purge_backups + $self->backup_ttl ) {
+            $self->_purge_backups();
+            $cache->set( "last_purge_backups", time() );
+        }
+    }
+}
+
 sub _purge_backups {
     my ($self) = @_;
     $self->msg("purging old backups") if $self->verbose;
@@ -248,6 +263,7 @@ sub _sig {
 
 sub msg {
     my ( $self, $format, @params ) = @_;
+    @params = map { ref($_) ? dump_one_line($_) : $_ } @params;
     printf( "$format\n", @params );
 }
 
diff --git a/lib/Code/TidyAll/Test/Plugin/RepeatBar.pm b/lib/Code/TidyAll/Test/Plugin/RepeatBar.pm
new file mode 100644
index 0000000..10f5a5c
--- /dev/null
+++ b/lib/Code/TidyAll/Test/Plugin/RepeatBar.pm
@@ -0,0 +1,17 @@
+package Code::TidyAll::Test::Plugin::RepeatBar;
+use Code::TidyAll::Util qw(read_file write_file);
+use base qw(Code::TidyAll::Plugin);
+use strict;
+use warnings;
+
+sub defaults {
+    return { include => qr/foo[^\/]+$/ };
+}
+
+sub process_source {
+    my ( $self, $source ) = @_;
+    my $times = $self->options->{times} || die "no times specified";
+    return $source x $times;
+}
+
+1;
diff --git a/lib/Code/TidyAll/Util.pm b/lib/Code/TidyAll/Util.pm
index 22a292a..aba9f2b 100644
--- a/lib/Code/TidyAll/Util.pm
+++ b/lib/Code/TidyAll/Util.pm
@@ -1,4 +1,5 @@
 package Code::TidyAll::Util;
+use Data::Dumper;
 use File::Basename;
 use File::Path;
 use File::Slurp qw(read_file write_file);
@@ -8,7 +9,8 @@ use strict;
 use warnings;
 use base qw(Exporter);
 
-our @EXPORT_OK = qw(basename can_load dirname mkpath read_file tempdir_simple write_file );
+our @EXPORT_OK =
+  qw(basename can_load dirname dump_one_line mkpath read_file tempdir_simple write_file );
 
 sub can_load {
 
@@ -39,4 +41,10 @@ sub tempdir_simple {
     return tempdir( $template, TMPDIR => 1, CLEANUP => 1 );
 }
 
+sub dump_one_line {
+    my ($value) = @_;
+
+    return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)->Terse(1)->Dump();
+}
+
 1;
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index 183bba8..14b1664 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -9,6 +9,8 @@ use Test::Class::Most parent => 'Code::TidyAll::Test::Class';
 sub test_plugin { "+Code::TidyAll::Test::Plugin::$_[0]" }
 my $UpperText  = test_plugin('UpperText');
 my $ReverseFoo = test_plugin('ReverseFoo');
+my $RepeatBar  = test_plugin('RepeatBar');
+my ( $conf1, $conf2 );
 
 sub create_dir {
     my ( $self, $files ) = @_;
@@ -158,26 +160,12 @@ sub test_errors : Tests {
     like( $output, qr/foo.txt: skipping, not underneath root dir/ );
 }
 
-my $conf1 = '
-backup_ttl = 5m
-no_cache = 1
-recursive = 1
-
-[PerlTidy]
-argv = -noll -it=2
-include = *.pl *.pm *.t
-
-[PodTidy]
-
-[PerlCritic]
-argv = -severity 3
-';
-
 sub test_conf_file : Tests {
     my $self      = shift;
     my $root_dir  = $self->create_dir();
     my $conf_file = "$root_dir/.tidyallrc";
     write_file( $conf_file, $conf1 );
+
     my $ct = Code::TidyAll->new( conf_file => $conf_file );
     my %expected = (
         backup_ttl => 300,
@@ -195,7 +183,52 @@ sub test_conf_file : Tests {
     while ( my ( $method, $value ) = each(%expected) ) {
         cmp_deeply( $ct->$method, $value, "$method" );
     }
+}
 
+sub test_cli : Tests {
+    my $self      = shift;
+    my $root_dir  = $self->create_dir();
+    my $conf_file = "$root_dir/.tidyallrc";
+    write_file( $conf_file,          $conf2 );
+    write_file( "$root_dir/foo.txt", "hello" );
+    my $output =
+      capture_stdout { system( "$^X", "bin/tidyall", "-c", $conf_file, "-v", "-r", $root_dir ) };
+    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' );
+    like( $params_msg, qr/recursive => '?1'?/,                                  'recursive' );
+    like( $params_msg, qr/verbose => '?1'?/,                                    'verbose' );
+    like( $params_msg, qr/\Qroot_dir => '$root_dir'\E/,                         'root_dir' );
+    like( $output,     qr/foo\.txt/,                                            'foo.txt' );
+    like( $output,     qr/applying '\+Code::TidyAll::Test::Plugin::UpperText'/, 'UpperText' );
+    like( $output,     qr/applying '\+Code::TidyAll::Test::Plugin::RepeatBar'/, 'RepeatBar' );
+    is( read_file("$root_dir/foo.txt"), "HELLOHELLOHELLO", "tidied" );
 }
 
+$conf1 = '
+backup_ttl = 5m
+no_cache = 1
+recursive = 1
+
+[PerlTidy]
+argv = -noll -it=2
+include = *.pl *.pm *.t
+
+[PodTidy]
+
+[PerlCritic]
+argv = -severity 3
+';
+
+$conf2 = "
+backup_ttl = 15m
+verbose = 1
+
+[$UpperText]
+
+[$RepeatBar]
+times = 3
+include = *.txt
+";
+
 1;

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