[libcode-tidyall-perl] 305/374: Specify order that plugins run (tidiers before validators, then alpha) and document that they are applied atomically

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26: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 a88cab281bb19981dd860ab7d93748be1d95e7c1
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Wed Oct 3 17:21:04 2012 -0700

    Specify order that plugins run (tidiers before validators, then alpha) and document that they are applied atomically
---
 .ispell_english                            |    1 +
 Changes                                    |    2 ++
 bin/tidyall                                |    9 +++++++
 lib/Code/TidyAll.pm                        |   20 ++++++++------
 lib/Code/TidyAll/Plugin.pm                 |   25 +++++++++++++-----
 lib/Code/TidyAll/Test/Plugin/AToZ.pm       |   17 ++++++++++++
 lib/Code/TidyAll/Test/Plugin/CheckUpper.pm |   10 +++++++
 lib/Code/TidyAll/t/Basic.pm                |   39 ++++++++++++++++++++++++++--
 8 files changed, 107 insertions(+), 16 deletions(-)

diff --git a/.ispell_english b/.ispell_english
index 3049691..76e9ce9 100644
--- a/.ispell_english
+++ b/.ispell_english
@@ -8,6 +8,7 @@ Simakov
 TIDYALL
 Thalhammer
 argv
+atomicity
 cmd
 conf
 config
diff --git a/Changes b/Changes
index c99059d..b32d036 100644
--- a/Changes
+++ b/Changes
@@ -6,6 +6,8 @@ Revision history for Code-TidyAll
 
 * Improvements
   - Allow a plugin to be listed multiple times in config with different configuration
+  - Specify order that plugins run (tidiers before validators, then alpha) and document
+    that they are applied atomically
   - Add -r/--recursive flag to process directories recursively
   - In -p/--pipe mode, mirror input to output in case of error (Justin Devuyst)
   - Add --version option
diff --git a/bin/tidyall b/bin/tidyall
index 273b3df..50f6cf7 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -436,6 +436,15 @@ arguments to pass to the underlying command-line utility.
 
 =back
 
+=head1 PLUGIN ORDER AND ATOMICITY
+
+If multiple plugins match a file, tidiers are applied before validators so that
+validators are checking the final result. Within those two groups, the plugins
+are applied in alphabetical order by plugin name/description.
+
+The application of multiple plugins is all-or-nothing. If an error occurs
+during the application of any plugin, the file is not modified at all.
+
 =head1 COMMAND-LINE OPTIONS
 
 =over
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 74d9fc1..abdebaf 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -82,10 +82,13 @@ sub _build_plugins_for_mode {
 
 sub _build_plugin_objects {
     my $self = shift;
-    return [
-        map { $self->_load_plugin( $_, $self->plugins->{$_} ) }
-        sort keys( %{ $self->plugins_for_mode } )
-    ];
+    my @plugin_objects =
+      map { $self->_load_plugin( $_, $self->plugins->{$_} ) } keys( %{ $self->plugins_for_mode } );
+
+    # Sort tidiers before validators, then alphabetical
+    #
+    return [ sort { ( $a->is_validator <=> $b->is_validator ) || ( $a->name cmp $b->name ) }
+          @plugin_objects ];
 }
 
 sub BUILD {
@@ -143,19 +146,20 @@ sub _load_plugin {
     #
     my ($plugin_fname) = ( $plugin_name =~ /^(\S+)/ );
 
-    my $class_name = (
+    my $plugin_class = (
         $plugin_fname =~ /^\+/
         ? substr( $plugin_fname, 1 )
         : "Code::TidyAll::Plugin::$plugin_fname"
     );
     try {
-        can_load($class_name) || die "not found";
+        can_load($plugin_class) || die "not found";
     }
     catch {
-        die "could not load plugin class '$class_name': $_";
+        die "could not load plugin class '$plugin_class': $_";
     };
 
-    return $class_name->new(
+    return $plugin_class->new(
+        class   => $plugin_class,
         name    => $plugin_name,
         tidyall => $self,
         %$plugin_conf
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index 8d057e0..54d386e 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -5,12 +5,15 @@ use Scalar::Util qw(weaken);
 use Moo;
 
 # External
-has 'argv'    => ( is => 'ro', default => sub { '' } );
-has 'cmd'     => ( is => 'lazy' );
-has 'ignore'  => ( is => 'ro' );
-has 'name'    => ( is => 'ro', required => 1 );
-has 'select'  => ( is => 'ro' );
-has 'tidyall' => ( is => 'ro', required => 1, weak_ref => 1 );
+has 'argv'         => ( is => 'ro', default => sub { '' } );
+has 'class'        => ( is => 'ro' );
+has 'cmd'          => ( is => 'lazy' );
+has 'ignore'       => ( is => 'ro' );
+has 'is_tidier'    => ( is => 'lazy' );
+has 'is_validator' => ( is => 'lazy' );
+has 'name'         => ( is => 'ro', required => 1 );
+has 'select'       => ( is => 'ro' );
+has 'tidyall'      => ( is => 'ro', required => 1, weak_ref => 1 );
 
 # Internal
 has 'ignore_regex' => ( is => 'lazy' );
@@ -43,6 +46,16 @@ sub _build_ignore_regex {
     return zglobs_to_regex( @{ $self->ignores } );
 }
 
+sub _build_is_tidier {
+    my ($self) = @_;
+    return ( $self->can('transform_source') || $self->can('transform_file') ) ? 1 : 0;
+}
+
+sub _build_is_validator {
+    my ($self) = @_;
+    return ( $self->can('validate_source') || $self->can('validate_file') ) ? 1 : 0;
+}
+
 sub BUILD {
     my ( $self, $params ) = @_;
 
diff --git a/lib/Code/TidyAll/Test/Plugin/AToZ.pm b/lib/Code/TidyAll/Test/Plugin/AToZ.pm
new file mode 100644
index 0000000..42aff30
--- /dev/null
+++ b/lib/Code/TidyAll/Test/Plugin/AToZ.pm
@@ -0,0 +1,17 @@
+package Code::TidyAll::Test::Plugin::AToZ;
+use Moo;
+extends 'Code::TidyAll::Plugin';
+
+sub preprocess_source {
+    my ( $self, $source ) = @_;
+    $source =~ tr/Aa/Zz/;
+    return $source;
+}
+
+sub postprocess_source {
+    my ( $self, $source ) = @_;
+    $source =~ tr/Zz/Aa/;
+    return $source;
+}
+
+1;
diff --git a/lib/Code/TidyAll/Test/Plugin/CheckUpper.pm b/lib/Code/TidyAll/Test/Plugin/CheckUpper.pm
new file mode 100644
index 0000000..87941b7
--- /dev/null
+++ b/lib/Code/TidyAll/Test/Plugin/CheckUpper.pm
@@ -0,0 +1,10 @@
+package Code::TidyAll::Test::Plugin::CheckUpper;
+use Moo;
+extends 'Code::TidyAll::Plugin';
+
+sub validate_source {
+    my ( $self, $source ) = @_;
+    die "lowercase found" if $source =~ /[a-z]/;
+}
+
+1;
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index 81a60b8..5648df3 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -10,6 +10,9 @@ sub test_plugin { "+Code::TidyAll::Test::Plugin::$_[0]" }
 my %UpperText  = ( test_plugin('UpperText')  => { select => '**/*.txt' } );
 my %ReverseFoo = ( test_plugin('ReverseFoo') => { select => '**/foo*' } );
 my %RepeatFoo  = ( test_plugin('RepeatFoo')  => { select => '**/foo*' } );
+my %CheckUpper = ( test_plugin('CheckUpper') => { select => '**/*.txt' } );
+my %AToZ       = ( test_plugin('AToZ')       => { select => '**/*.txt' } );
+
 my $cli_conf;
 
 sub create_dir {
@@ -53,6 +56,9 @@ sub tidy {
     while ( my ( $path, $content ) = each( %{ $params{dest} } ) ) {
         is( read_file("$root_dir/$path"), $content, "$desc - $path content" );
     }
+    if ( my $like_output = $params{like_output} ) {
+        like( $output, $like_output, "$desc - output" );
+    }
 }
 
 sub test_basic : Tests {
@@ -80,8 +86,6 @@ sub test_basic : Tests {
         desc    => 'one file reversals mode',
         options => { mode      => 'reversals' },
     );
-    return;
-
     $self->tidy(
         plugins => { %UpperText, %ReverseFoo },
         source  => {
@@ -124,6 +128,37 @@ sub test_multiple_plugin_instances : Tests {
     );
 }
 
+sub test_plugin_order_and_atomicity : Tests {
+    my $self    = shift;
+    my @plugins = map {
+        (
+            %ReverseFoo,
+            test_plugin("UpperText $_")  => { select => '**/*.txt' },
+            test_plugin("CheckUpper $_") => { select => '**/*.txt' }
+          )
+    } ( 1 .. 3 );
+    my $output = capture_stdout {
+        $self->tidy(
+            plugins => {@plugins},
+            options => { verbose => 1 },
+            source  => { "foo.txt" => "abc" },
+            dest    => { "foo.txt" => "CBA" },
+            like_output =>
+              qr/.*ReverseFoo, .*UpperText 1, .*UpperText 2, .*UpperText 3, .*CheckUpper 1, .*CheckUpper 2, .*CheckUpper 3/
+        );
+    };
+
+    $self->tidy(
+        plugins => { %AToZ, %ReverseFoo, %CheckUpper },
+        options     => { verbose   => 1 },
+        source      => { "foo.txt" => "abc" },
+        dest        => { "foo.txt" => "abc" },
+        errors      => qr/lowercase found/,
+        like_output => qr/foo.txt (.*ReverseFoo, .*CheckUpper)/
+    );
+
+}
+
 sub test_quiet_and_verbose : Tests {
     my $self = shift;
 

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