[Bps-public-commit] Test-Spelling branch, master, updated. ed59bb5f58d1bf988248f249291110617647ea84

Shawn Moore sartak at bestpractical.com
Mon Apr 25 14:56:46 EDT 2011


The branch, master has been updated
       via  ed59bb5f58d1bf988248f249291110617647ea84 (commit)
       via  925d143022eb40e1801e059f62086421283db50a (commit)
      from  5cb226be363cfe0858ba58034547b810c775ca83 (commit)

Summary of changes:
 Changes              |    2 +
 Makefile.PL          |    1 -
 lib/Test/Spelling.pm |   75 ++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 65 insertions(+), 13 deletions(-)

- Log -----------------------------------------------------------------
commit 925d143022eb40e1801e059f62086421283db50a
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Mon Apr 25 14:25:47 2011 -0400

    Try various spellcheck commands [rt.cpan.org #56483]
    
        spell, aspell, ispell, hunspell

diff --git a/Changes b/Changes
index 0609f2b..768b0b4 100644
--- a/Changes
+++ b/Changes
@@ -8,6 +8,8 @@ Revision history for Test-Spelling
           (reported by David Hand)
         - Add set_pod_file_filter for skipping translations, etc.
           [rt.cpan.org #63755] (reported by me :))
+        - Try various spellcheck programs [rt.cpan.org #56483] (reported by
+          Lars Dɪᴇᴄᴋᴏᴡ, et al)
 
 0.11  2005-11-15
         - Some documentation fixes.
diff --git a/lib/Test/Spelling.pm b/lib/Test/Spelling.pm
index e889935..da1503e 100644
--- a/lib/Test/Spelling.pm
+++ b/lib/Test/Spelling.pm
@@ -8,7 +8,6 @@ use Pod::Spell;
 use Test::Builder;
 use File::Spec;
 use File::Temp;
-use Carp;
 
 our $VERSION = '0.12';
 
@@ -21,10 +20,54 @@ our @EXPORT = qw(
     set_pod_file_filter
 );
 
-my $Test        = Test::Builder->new;
-my $Spell_cmd   = 'spell';
+my $Test = Test::Builder->new;
+
+my $SPELLCHECKER;
 my $file_filter = sub { 1 };
 
+sub spellchecker_candidates {
+    # if they've specified a spellchecker, use only that one
+    return $SPELLCHECKER if $SPELLCHECKER;
+
+    return (
+        'spell', # for back-compat, this is the top candidate ...
+        'aspell list -l en', # ... but this should become first soon
+        'ispell -l',
+        'hunspell',
+    );
+}
+
+sub _get_spellcheck_results {
+    my $scratch = shift;
+
+    my @errors;
+
+    for my $spellchecker (spellchecker_candidates()) {
+        my $spellcheck_handle;
+        if (open $spellcheck_handle, "$spellchecker < $scratch |") {
+            push @errors, "Unable to run '$spellchecker': $!";
+            next;
+        }
+
+        my @words = <$spellcheck_handle>;
+        close $spellcheck_handle or die $!;
+
+        # remember the one we used, so that it's consistent for all the files
+        # this run, and we don't keep retrying the same spellcheckers that will
+        # never work
+        set_spell_cmd($spellchecker)
+            if !$SPELLCHECKER;
+
+        return @words;
+    }
+
+    # no working spellcheckers; report all the errors
+    require Carp;
+    Carp::croak
+        "Unable to find a working spellchecker:\n"
+        . join("\n", map { "    $_\n" } @errors)
+}
+
 sub invalid_words_in {
     my $file = shift;
 
@@ -34,11 +77,7 @@ sub invalid_words_in {
     my $checker = Pod::Spell->new;
     $checker->parse_from_file($file, $scratch);
 
-    # run spell command and fetch output
-    open my $spellcheck_results, "$Spell_cmd < $scratch|"
-        or croak "Couldn't run spellcheck command '$Spell_cmd'";
-    my @words = <$spellcheck_results>;
-    close $spellcheck_results or die;
+    my @words = _get_spellcheck_results($scratch);
 
     chomp for @words;
     return @words;
@@ -144,7 +183,7 @@ sub add_stopwords {
 }
 
 sub set_spell_cmd {
-    $Spell_cmd = shift;
+    $SPELLCHECKER = shift;
 }
 
 sub set_pod_file_filter {

commit ed59bb5f58d1bf988248f249291110617647ea84
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Mon Apr 25 14:54:42 2011 -0400

    Use IPC::Open2 to avoid File::Temp and shell interpolation
    
        opening "$spellchecker < $scratch |" is kind of worrying given that
        $spellchecker and $scratch are not escaped or quoted.
    
        And now that we give the document to the spellchecker ourselves, we
        can avoid using File::Temp by using an in-memory filehandle

diff --git a/Makefile.PL b/Makefile.PL
index 57780e4..fc61b9c 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -7,7 +7,6 @@ repository 'http://github.com/bestpractical/test-spelling';
 requires 'Pod::Spell' => '1.01';
 
 test_requires 'Test::More' => 0;
-test_requires 'File::Temp' => 0;
 
 WriteAll;
 
diff --git a/lib/Test/Spelling.pm b/lib/Test/Spelling.pm
index da1503e..c88ac3c 100644
--- a/lib/Test/Spelling.pm
+++ b/lib/Test/Spelling.pm
@@ -7,7 +7,7 @@ use base 'Exporter';
 use Pod::Spell;
 use Test::Builder;
 use File::Spec;
-use File::Temp;
+use IPC::Open2;
 
 our $VERSION = '0.12';
 
@@ -38,27 +38,38 @@ sub spellchecker_candidates {
 }
 
 sub _get_spellcheck_results {
-    my $scratch = shift;
+    my $document = shift;
 
     my @errors;
 
     for my $spellchecker (spellchecker_candidates()) {
-        my $spellcheck_handle;
-        if (open $spellcheck_handle, "$spellchecker < $scratch |") {
-            push @errors, "Unable to run '$spellchecker': $!";
-            next;
-        }
+        my @words;
+        my $ok = eval {
+            my $pid = open2((my ($spellcheck_results, $child_in)), $spellchecker);
+
+            print $child_in $document;
+
+            # signal to spellchecker that we're done giving it words
+            close $child_in or die $!;
 
-        my @words = <$spellcheck_handle>;
-        close $spellcheck_handle or die $!;
+            @words = <$spellcheck_results>;
 
-        # remember the one we used, so that it's consistent for all the files
-        # this run, and we don't keep retrying the same spellcheckers that will
-        # never work
-        set_spell_cmd($spellchecker)
-            if !$SPELLCHECKER;
+            # wait for spellchecker to clean up
+            waitpid $pid, 0;
+
+            1;
+        };
+
+        if ($ok) {
+            # remember the one we used, so that it's consistent for all the files
+            # this run, and we don't keep retrying the same spellcheckers that will
+            # never work
+            set_spell_cmd($spellchecker)
+                if !$SPELLCHECKER;
+            return @words;
+        }
 
-        return @words;
+        push @errors, "Unable to run '$spellchecker': $@";
     }
 
     # no working spellcheckers; report all the errors
@@ -71,13 +82,14 @@ sub _get_spellcheck_results {
 sub invalid_words_in {
     my $file = shift;
 
-    my $scratch = File::Temp->new->filename;
+    my $document = '';
+    open my $handle, '>', \$document;
 
     # save digested POD to temp file
     my $checker = Pod::Spell->new;
-    $checker->parse_from_file($file, $scratch);
+    $checker->parse_from_file($file, $handle);
 
-    my @words = _get_spellcheck_results($scratch);
+    my @words = _get_spellcheck_results($document);
 
     chomp for @words;
     return @words;

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list