Skip to content

Commit

Permalink
Merge pull request #2 from rwstauner/fake_checker
Browse files Browse the repository at this point in the history
Use IPC::Run3 for portability and test with a fake spell check cmd
  • Loading branch information
sartak committed Jan 27, 2013
2 parents 1265114 + 8a1d2c8 commit f320db7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ all_from 'lib/Test/Spelling.pm';
githubmeta;

requires 'Pod::Spell' => '1.01';
requires 'IPC::Run3' => '0.044';
test_requires 'Test::More' => '0.88';
test_requires 'Test::Tester';

Expand Down
21 changes: 4 additions & 17 deletions lib/Test/Spelling.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use base 'Exporter';
use Pod::Spell;
use Test::Builder;
use File::Spec;
use IPC::Open3;
use IPC::Run3;
use Symbol 'gensym';

our $VERSION = '0.16';
Expand Down Expand Up @@ -60,27 +60,14 @@ sub _get_spellcheck_results {
for my $spellchecker (spellchecker_candidates()) {
my @words;
my $ok = eval {
# IPC::Open3 says "If CHLD_ERR is false [...] then STDOUT and
# STDERR of the child are on the same filehandle (this means that
# an autovivified lexical cannot be used for the STDERR
# filehandle [...])" - what a crummy API!
my $child_error = gensym;

my $pid = open3(my ($child_in, $spellcheck_results), $child_error, $spellchecker);
my ($spellcheck_results, $errors);
IPC::Run3::run3($spellchecker, \$document, \$spellcheck_results, \$errors);

print $child_in $document;
@words = split /\n/, $spellcheck_results;

# signal to spellchecker that we're done giving it words
close $child_in or die $!;

@words = <$spellcheck_results>;

my $errors = do { local $/; <$child_error> };
die "spellchecker had errors: $errors" if length $errors;

# wait for spellchecker to clean up
waitpid $pid, 0;

1;
};

Expand Down
41 changes: 41 additions & 0 deletions t/fake_checker.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use Test::Tester;
use Test::More;
use Test::Spelling;

# Use perl to fake a working spell checker
# so we can test module portability even where no spell checker is present.

my $spell_cmd = $^X . q< -e "print STDERR q[FOOBAR]">;
set_spell_cmd($spell_cmd);

is eval { pod_file_spelling_ok('t/corpus/bad-pod.pm', 'expect STDERR'); 1 },
undef, 'spell check died';

like $@,
qr/Unable to find a working spellchecker:\n Unable to run '\Q$spell_cmd\E': spellchecker had errors: FOOBAR/,
'died with text found on STDERR';


my $stopword = 'xzaue';
$spell_cmd = $^X . qq< -ane "print grep { /$stopword/ } \@F">;
set_spell_cmd($spell_cmd);

check_test(sub { pod_file_spelling_ok('t/corpus/good-pod.pm', 'no mistakes') }, {
ok => 1,
name => 'no mistakes',
});

check_test(sub { pod_file_spelling_ok('t/corpus/stopword.pm', 'found misspelled word') }, {
ok => 0,
name => 'found misspelled word',
diag => "Errors:\n $stopword",
});

add_stopwords($stopword);

check_test(sub { pod_file_spelling_ok('t/corpus/stopword.pm', 'used stopword') }, {
ok => 1,
name => 'used stopword',
});

done_testing;

0 comments on commit f320db7

Please sign in to comment.