[libcode-tidyall-perl] 14/374: test no_cache, no_backups, conf file
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 14dc07a9912062a1f969b3165093541b5871d526
Author: Jonathan Swartz <swartz at pobox.com>
Date: Tue Jun 12 13:32:31 2012 -0700
test no_cache, no_backups, conf file
---
bin/tidyall | 12 ++---
lib/Code/TidyAll.pm | 6 ++-
lib/Code/TidyAll/Plugin.pm | 12 ++++-
lib/Code/TidyAll/Util.pm | 1 +
lib/Code/TidyAll/t/Basic.pm | 120 ++++++++++++++++++++++++++++++++-----------
5 files changed, 113 insertions(+), 38 deletions(-)
diff --git a/bin/tidyall b/bin/tidyall
index 53c7db4..f94f320 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -55,7 +55,7 @@ tidyall - Your all-in-one code tidier and validator
-c, --conf Required configuration file
-h, --help Print help message
-r, --recursive Descend into directories recursively
- --backup-ttl When backup files can be purged. Defaults to "1d"
+ --backup-ttl When backup files can be purged. Defaults to "1d"
--class Code::TidyAll subclass to use. Defaults to "Code::TidyAll"
--data-dir Container for backups, cache, etc. Defaults to dir of conf file
--no-backup Don't backup files
@@ -109,7 +109,7 @@ scripts, modules and test files.
Section 2 says to apply C<PerlCritic> with severity 3. Since there is no
C<include> clause, the default for C<PerlCritic> plugin is used, which happens
-to be the same: "*.pl *.pm *.t".
+to be the same as above: "*.pl *.pm *.t".
Section 3 says to apply C<PodTidy> with default settings, to the same set of
default files "*.pl *.pm *.t".
@@ -135,13 +135,13 @@ with a '+', e.g.
=item include
-One or more glob patterns indicating which files to include. If not specified,
-use the default set for the plugin.
+One or more glob patterns indicating which files to include, separated by
+whitespace. If not specified, use the default set for the plugin.
=item exclude
-One or more glob patterns indicating which files to exclude. This overrides
-C<include>.
+One or more glob patterns indicating which files to exclude, separated by
+whitespace. This overrides C<include>.
=back
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 1adb928..8616e8e 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -67,8 +67,10 @@ sub new {
$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} = parse_duration( $self->{backup_ttl} || "1 day" );
+ $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+$/;
my $plugins = $self->plugins;
$self->{plugin_objects} =
[ map { $self->load_plugin( $_, $plugins->{$_} ) } keys( %{ $self->plugins } ) ];
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index dec67d1..322b0f2 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -57,7 +57,8 @@ sub _build_options {
sub _match_spec_to_coderef {
my ( $self, $type, $spec ) = @_;
- $spec = qr/$spec/ if ( !ref($spec) );
+
+ $spec = $self->_glob_to_regex($spec) if !ref($spec);
if ( ref($spec) eq 'Regexp' ) {
return sub { $_[0] =~ $spec };
}
@@ -69,4 +70,13 @@ sub _match_spec_to_coderef {
}
}
+sub _glob_to_regex {
+ my ( $self, $spec ) = @_;
+
+ my @patterns = split( /\s+/, $spec );
+ foreach (@patterns) { s/\./\\\./g; s/\*/.*/g }
+ my $regex = join( '|', @patterns );
+ return qr/$regex/;
+}
+
1;
diff --git a/lib/Code/TidyAll/Util.pm b/lib/Code/TidyAll/Util.pm
index 3824abc..22a292a 100644
--- a/lib/Code/TidyAll/Util.pm
+++ b/lib/Code/TidyAll/Util.pm
@@ -20,6 +20,7 @@ sub can_load {
my $result;
try {
eval "require $class_name";
+ die $@ if $@;
$result = 1;
}
catch {
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index 41cf851..ec44aea 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -78,43 +78,65 @@ sub test_basic : Tests {
sub test_caching_and_backups : Tests {
my $self = shift;
- my $root_dir = $self->create_dir( { "foo.txt" => "abc" } );
- my $ct = Code::TidyAll->new( plugins => { $UpperText => {} }, root_dir => $root_dir );
- my $output;
- my $file = "$root_dir/foo.txt";
- my $go = sub {
- $output = capture_stdout { $ct->process_paths($file) };
- };
-
- $go->();
- is( read_file($file), "ABC", "file changed" );
- is( $output, "foo.txt\n", 'output' );
-
- $go->();
- is( $output, '', 'no output' );
-
- write_file( $file, "def" );
- $go->();
- is( read_file($file), "DEF", "file changed" );
- is( $output, "foo.txt\n", 'output' );
-
- my $backup_dir = $ct->data_dir . "/backups";
- my @files;
- find( { follow => 0, wanted => sub { push @files, $_ if -f }, no_chdir => 1 }, $backup_dir );
- ok( scalar(@files) == 1 || scalar(@files) == 2, "1 or 2 backup files" );
- foreach my $file (@files) {
- like( $file, qr|\.tidyall\.d/backups/foo\.txt-\d+-\d+\.bak|, "backup filename" );
+ foreach my $no_cache ( 0 .. 1 ) {
+ foreach my $no_backups ( 0 .. 1 ) {
+ my $root_dir = $self->create_dir( { "foo.txt" => "abc" } );
+ my $ct = Code::TidyAll->new(
+ plugins => { $UpperText => {} },
+ root_dir => $root_dir,
+ ( $no_cache ? ( no_cache => 1 ) : () ), ( $no_backups ? ( no_backups => 1 ) : () )
+ );
+ my $output;
+ my $file = "$root_dir/foo.txt";
+ my $go = sub {
+ $output = capture_stdout { $ct->process_paths($file) };
+ };
+
+ $go->();
+ is( read_file($file), "ABC", "file changed" );
+ is( $output, "foo.txt\n", 'output' );
+
+ $go->();
+ if ($no_cache) {
+ is( $output, "foo.txt\n", 'output' );
+ }
+ else {
+ is( $output, '', 'no output' );
+ }
+
+ write_file( $file, "def" );
+ $go->();
+ is( read_file($file), "DEF", "file changed" );
+ is( $output, "foo.txt\n", 'output' );
+
+ 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 );
+ if ($no_backups) {
+ ok( @files == 0, "no backup files" );
+ }
+ else {
+ ok( scalar(@files) == 1 || scalar(@files) == 2, "1 or 2 backup files" );
+ foreach my $file (@files) {
+ like( $file, qr|\.tidyall\.d/backups/foo\.txt-\d+-\d+\.bak|,
+ "backup filename" );
+ }
+ }
+ }
}
}
sub test_errors : Tests {
my $self = shift;
- my $data_dir = tempdir_simple();
- throws_ok { Code::TidyAll->new( data_dir => $data_dir ) } qr/conf_file or plugins required/;
+ my $root_dir = $self->create_dir( { "foo/bar.txt" => "abc" } );
+ throws_ok { Code::TidyAll->new( root_dir => $root_dir ) } qr/conf_file or plugins required/;
throws_ok { Code::TidyAll->new( plugins => {} ) } qr/conf_file or root_dir required/;
+ throws_ok { Code::TidyAll->new( root_dir => $root_dir, plugins => { 'DoesNotExist' => {} } ) }
+ qr/could not load plugin class/;
- my $root_dir = $self->create_dir( { "foo/bar.txt" => "abc" } );
my $ct = Code::TidyAll->new( plugins => { $UpperText => {} }, root_dir => $root_dir );
my $output = capture_stdout { $ct->process_paths("$root_dir/foo/bar.txt") };
is( $output, "foo/bar.txt\n", "filename output" );
@@ -127,4 +149,44 @@ 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,
+ no_backups => undef,
+ no_cache => 1,
+ recursive => 1,
+ root_dir => dirname($conf_file),
+ data_dir => "$root_dir/.tidyall.d",
+ plugins => {
+ PerlTidy => { argv => '-noll -it=2', include => '*.pl *.pm *.t' },
+ PodTidy => {},
+ PerlCritic => { argv => '-severity 3' },
+ }
+ );
+ while ( my ( $method, $value ) = each(%expected) ) {
+ cmp_deeply( $ct->$method, $value, "$method" );
+ }
+
+}
+
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