| 1 |
package Test::Spelling; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Exporter qw(import); |
| 7 |
use Pod::Spell; |
| 8 |
use Test::Builder; |
| 9 |
use Text::Wrap; |
| 10 |
use File::Spec; |
| 11 |
use IPC::Run3; |
| 12 |
use Symbol 'gensym'; |
| 13 |
|
| 14 |
our $VERSION = '0.25'; |
| 15 |
|
| 16 |
our @EXPORT = qw( |
| 17 |
pod_file_spelling_ok |
| 18 |
all_pod_files_spelling_ok |
| 19 |
add_stopwords |
| 20 |
set_spell_cmd |
| 21 |
all_pod_files |
| 22 |
set_pod_file_filter |
| 23 |
has_working_spellchecker |
| 24 |
set_pod_parser |
| 25 |
); |
| 26 |
|
| 27 |
my $TEST = Test::Builder->new; |
| 28 |
|
| 29 |
my $SPELLCHECKER; |
| 30 |
my $FILE_FILTER = sub { 1 }; |
| 31 |
my $POD_PARSER; |
| 32 |
our %ALL_WORDS; |
| 33 |
|
| 34 |
sub spellchecker_candidates { |
| 35 |
# if they've specified a spellchecker, use only that one |
| 36 |
return $SPELLCHECKER if $SPELLCHECKER; |
| 37 |
|
| 38 |
return ( |
| 39 |
'hunspell -l', # hunspell is now the most common spell checker |
| 40 |
'spell', # for back-compat, this is the top candidate ... |
| 41 |
'aspell list -l en -p /dev/null', # ... but this should become first soon |
| 42 |
'ispell -l', |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
sub has_working_spellchecker { |
| 47 |
my $dryrun_results = _get_spellcheck_results("dry run", 1); |
| 48 |
|
| 49 |
if (ref $dryrun_results) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
return $SPELLCHECKER; |
| 54 |
} |
| 55 |
|
| 56 |
sub _get_spellcheck_results { |
| 57 |
my $document = shift; |
| 58 |
my $dryrun = shift; |
| 59 |
|
| 60 |
my @errors; |
| 61 |
|
| 62 |
for my $spellchecker (spellchecker_candidates()) { |
| 63 |
my @words; |
| 64 |
my $ok = eval { |
| 65 |
|
| 66 |
my ($spellcheck_results, $errors); |
| 67 |
IPC::Run3::run3($spellchecker, \$document, \$spellcheck_results, \$errors); |
| 68 |
|
| 69 |
@words = split /\n/, $spellcheck_results; |
| 70 |
|
| 71 |
die "spellchecker had errors: $errors" if length $errors; |
| 72 |
|
| 73 |
1; |
| 74 |
}; |
| 75 |
|
| 76 |
if ($ok) { |
| 77 |
# remember the one we used, so that it's consistent for all the files |
| 78 |
# this run, and we don't keep retrying the same spellcheckers that will |
| 79 |
# never work. also we need to expose the spellchecker we're using in |
| 80 |
# has_working_spellchecker |
| 81 |
set_spell_cmd($spellchecker) |
| 82 |
if !$SPELLCHECKER; |
| 83 |
return @words; |
| 84 |
} |
| 85 |
|
| 86 |
push @errors, "Unable to run '$spellchecker': $@"; |
| 87 |
} |
| 88 |
|
| 89 |
# no working spellcheckers during a dry run |
| 90 |
return \"no spellchecker" if $dryrun; |
| 91 |
|
| 92 |
# no working spellcheckers; report all the errors |
| 93 |
require Carp; |
| 94 |
Carp::croak |
| 95 |
"Unable to find a working spellchecker:\n" |
| 96 |
. join("\n", map { " $_\n" } @errors) |
| 97 |
} |
| 98 |
|
| 99 |
sub invalid_words_in { |
| 100 |
my $file = shift; |
| 101 |
|
| 102 |
my $document = ''; |
| 103 |
open my $handle, '>', \$document; |
| 104 |
# the UTF-8 parsing seems to have broken many tests |
| 105 |
#open my $infile, '<:encoding(UTF-8)', $file; |
| 106 |
|
| 107 |
# save digested POD to the string $document |
| 108 |
#get_pod_parser()->parse_from_filehandle($infile, $handle); |
| 109 |
get_pod_parser()->parse_from_file($file, $handle); |
| 110 |
my @words = _get_spellcheck_results($document); |
| 111 |
|
| 112 |
chomp for @words; |
| 113 |
return @words; |
| 114 |
} |
| 115 |
|
| 116 |
sub pod_file_spelling_ok { |
| 117 |
my $file = shift; |
| 118 |
my $name = shift || "POD spelling for $file"; |
| 119 |
|
| 120 |
if (!-r $file) { |
| 121 |
$TEST->ok(0, $name); |
| 122 |
$TEST->diag("$file does not exist or is unreadable"); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
my @words = invalid_words_in($file); |
| 127 |
|
| 128 |
# remove stopwords, select unique errors |
| 129 |
my $WL = \%Pod::Wordlist::Wordlist; |
| 130 |
@words = grep { !$WL->{$_} && !$WL->{lc $_} } @words; |
| 131 |
$ALL_WORDS{$_}++ for @words; |
| 132 |
my %seen; |
| 133 |
@seen{@words} = (); |
| 134 |
@words = sort keys %seen; |
| 135 |
|
| 136 |
# emit output |
| 137 |
my $ok = @words == 0; |
| 138 |
$TEST->ok($ok, "$name"); |
| 139 |
if (!$ok) { |
| 140 |
$TEST->diag("Errors:\n" . join '', map { " $_\n" } @words); |
| 141 |
} |
| 142 |
|
| 143 |
return $ok; |
| 144 |
} |
| 145 |
|
| 146 |
sub all_pod_files_spelling_ok { |
| 147 |
my @files = all_pod_files(@_); |
| 148 |
local %ALL_WORDS; |
| 149 |
if (!has_working_spellchecker()) { |
| 150 |
return $TEST->plan(skip_all => "no working spellchecker found"); |
| 151 |
} |
| 152 |
|
| 153 |
$TEST->plan(tests => scalar @files); |
| 154 |
|
| 155 |
my $ok = 1; |
| 156 |
for my $file (@files) { |
| 157 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 158 |
pod_file_spelling_ok($file) or undef $ok; |
| 159 |
} |
| 160 |
if ( keys %ALL_WORDS ) { |
| 161 |
# Invert k => v to v => [ k ] |
| 162 |
my %values; |
| 163 |
push @{ $values{ $ALL_WORDS{$_} } }, $_ for keys %ALL_WORDS; |
| 164 |
|
| 165 |
my $labelformat = q[%6s: ]; |
| 166 |
my $indent = q[ ] x 10; |
| 167 |
|
| 168 |
$TEST->diag(qq[\nAll incorrect words, by number of occurrences:\n] . |
| 169 |
join qq[\n], map { wrap( ( sprintf $labelformat, $_ ), $indent, join q[, ], sort @{ $values{$_} } ) } |
| 170 |
sort { $a <=> $b } keys %values |
| 171 |
); |
| 172 |
} |
| 173 |
return $ok; |
| 174 |
} |
| 175 |
|
| 176 |
sub all_pod_files { |
| 177 |
my @queue = @_ ? @_ : _starting_points(); |
| 178 |
my @pod; |
| 179 |
|
| 180 |
while (@queue) { |
| 181 |
my $file = shift @queue; |
| 182 |
|
| 183 |
# recurse into subdirectories |
| 184 |
if (-d $file) { |
| 185 |
opendir(my $dirhandle, $file) or next; |
| 186 |
my @newfiles = readdir($dirhandle); |
| 187 |
closedir $dirhandle; |
| 188 |
|
| 189 |
@newfiles = File::Spec->no_upwards(@newfiles); |
| 190 |
@newfiles = grep { $_ ne "CVS" && $_ ne ".svn" } @newfiles; |
| 191 |
|
| 192 |
push @queue, map "$file/$_", @newfiles; |
| 193 |
} |
| 194 |
|
| 195 |
# add the file if it meets our criteria |
| 196 |
if (-f $file) { |
| 197 |
next unless _is_perl($file); |
| 198 |
next unless $FILE_FILTER->($file); |
| 199 |
push @pod, $file; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return @pod; |
| 204 |
} |
| 205 |
|
| 206 |
sub _starting_points { |
| 207 |
return 'blib' if -d 'blib'; |
| 208 |
return 'lib'; |
| 209 |
} |
| 210 |
|
| 211 |
sub _is_perl { |
| 212 |
my $file = shift; |
| 213 |
|
| 214 |
return 1 if $file =~ /\.PL$/; |
| 215 |
return 1 if $file =~ /\.p(l|lx|m|od)$/; |
| 216 |
return 1 if $file =~ /\.t$/; |
| 217 |
|
| 218 |
open my $handle, '<', $file or return; |
| 219 |
my $first = <$handle>; |
| 220 |
|
| 221 |
return 1 if defined $first && ($first =~ /^#!.*perl/); |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
sub add_stopwords { |
| 227 |
for (@_) { |
| 228 |
# explicit copy so we don't modify constants as in add_stopwords("SQLite") |
| 229 |
my $word = $_; |
| 230 |
|
| 231 |
# XXX: the processing this performs is to support "perl t/spell.t 2>> |
| 232 |
# t/spell.t" which is bunk. in the near future the processing here will |
| 233 |
# become more modern |
| 234 |
$word =~ s/^#?\s*//; |
| 235 |
$word =~ s/\s+$//; |
| 236 |
next if $word =~ /\s/ or $word =~ /:/; |
| 237 |
$Pod::Wordlist::Wordlist{$word} = 1; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
sub set_spell_cmd { |
| 242 |
$SPELLCHECKER = shift; |
| 243 |
} |
| 244 |
|
| 245 |
sub set_pod_file_filter { |
| 246 |
$FILE_FILTER = shift; |
| 247 |
} |
| 248 |
|
| 249 |
# A new Pod::Spell object should be used for every file; people |
| 250 |
# providing custom pod parsers will have to do this themselves |
| 251 |
sub get_pod_parser { |
| 252 |
return $POD_PARSER || Pod::Spell->new; |
| 253 |
} |
| 254 |
|
| 255 |
sub set_pod_parser { |
| 256 |
$POD_PARSER = shift; |
| 257 |
} |
| 258 |
|
| 259 |
1; |
| 260 |
|
| 261 |
__END__ |
| 262 |
|
| 263 |
=head1 NAME |
| 264 |
|
| 265 |
Test::Spelling - Check for spelling errors in POD files |
| 266 |
|
| 267 |
=head1 SYNOPSIS |
| 268 |
|
| 269 |
Place a file, C<pod-spell.t> in your distribution's C<xt/author> directory: |
| 270 |
|
| 271 |
use strict; |
| 272 |
use warnings; |
| 273 |
use Test::More; |
| 274 |
|
| 275 |
use Test::Spelling; |
| 276 |
use Pod::Wordlist; |
| 277 |
|
| 278 |
add_stopwords(<DATA>); |
| 279 |
all_pod_files_spelling_ok( qw( bin lib ) ); |
| 280 |
|
| 281 |
__DATA__ |
| 282 |
SomeBizarreWord |
| 283 |
YetAnotherBIzarreWord |
| 284 |
|
| 285 |
Or, you can gate the spelling test with the environment variable C<AUTHOR_TESTING>: |
| 286 |
|
| 287 |
use strict; |
| 288 |
use warnings; |
| 289 |
use Test::More; |
| 290 |
|
| 291 |
BEGIN { |
| 292 |
plan skip_all => "Spelling tests only for authors" |
| 293 |
unless $ENV{AUTHOR_TESTING}; |
| 294 |
} |
| 295 |
|
| 296 |
use Test::Spelling; |
| 297 |
use Pod::Wordlist; |
| 298 |
|
| 299 |
all_pod_files_spelling_ok(); |
| 300 |
|
| 301 |
=head1 DESCRIPTION |
| 302 |
|
| 303 |
L<Test::Spelling> lets you check the spelling of a C<POD> file, and report |
| 304 |
its results in standard L<Test::More> fashion. This module requires a |
| 305 |
spellcheck program such as L<Hunspell|http://hunspell.github.io/>, |
| 306 |
F<aspell>, F<spell>, or, F<ispell>. We suggest using Hunspell. |
| 307 |
|
| 308 |
use Test::Spelling; |
| 309 |
pod_file_spelling_ok('lib/Foo/Bar.pm', 'POD file spelling OK'); |
| 310 |
|
| 311 |
Note that it is a bad idea to run spelling tests during an ordinary CPAN |
| 312 |
distribution install, or in a package that will run in an uncontrolled |
| 313 |
environment. There is no way of predicting whether the word list or spellcheck |
| 314 |
program used will give the same results. You B<can> include the test in your |
| 315 |
distribution, but be sure to run it only for authors of the module by guarding |
| 316 |
it in a C<skip_all unless $ENV{AUTHOR_TESTING}> clause, or by putting the test in |
| 317 |
your distribution's F<xt/author> directory. Anyway, people installing your module |
| 318 |
really do not need to run such tests, as it is unlikely that the documentation |
| 319 |
will acquire typos while in transit. |
| 320 |
|
| 321 |
You can add your own stop words, which are words that should be ignored by the |
| 322 |
spell check, like so: |
| 323 |
|
| 324 |
add_stopwords(qw(asdf thiswordiscorrect)); |
| 325 |
|
| 326 |
Adding stop words in this fashion affects all files checked for the remainder of |
| 327 |
the test script. See L<Pod::Spell> (which this module is built upon) for a |
| 328 |
variety of ways to add per-file stop words to each .pm file. |
| 329 |
|
| 330 |
If you have a lot of stop words, it's useful to put them in your test file's |
| 331 |
C<DATA> section like so: |
| 332 |
|
| 333 |
use strict; |
| 334 |
use warnings; |
| 335 |
use Test::More; |
| 336 |
|
| 337 |
use Test::Spelling; |
| 338 |
use Pod::Wordlist; |
| 339 |
|
| 340 |
add_stopwords(<DATA>); |
| 341 |
all_pod_files_spelling_ok(); |
| 342 |
|
| 343 |
__DATA__ |
| 344 |
folksonomy |
| 345 |
Jifty |
| 346 |
Zakirov |
| 347 |
|
| 348 |
To maintain backwards compatibility, comment markers and some whitespace are |
| 349 |
ignored. In the near future, the preprocessing we do on the arguments to |
| 350 |
L<Test::Spelling/"add_stopwords"> will be changed and documented properly. |
| 351 |
|
| 352 |
=head1 FUNCTIONS |
| 353 |
|
| 354 |
L<Test::Spelling> makes the following methods available. |
| 355 |
|
| 356 |
=head2 add_stopwords |
| 357 |
|
| 358 |
add_stopwords(@words); |
| 359 |
add_stopwords(<DATA>); # pull in stop words from the DATA section |
| 360 |
|
| 361 |
Add words that should be skipped by the spell checker. Note that L<Pod::Spell> |
| 362 |
already skips words believed to be code, such as everything in verbatim |
| 363 |
(indented) blocks and code marked up with C<< C<...> >>, as well as some common |
| 364 |
Perl jargon. |
| 365 |
|
| 366 |
=head2 all_pod_files |
| 367 |
|
| 368 |
all_pod_files(); |
| 369 |
all_pod_files(@list_of_directories); |
| 370 |
|
| 371 |
Returns a list of all the Perl files in each directory and its subdirectories, |
| 372 |
recursively. If no directories are passed, it defaults to F<blib> if F<blib> |
| 373 |
exists, or else F<lib> if not. Skips any files in F<CVS> or F<.svn> directories. |
| 374 |
|
| 375 |
A Perl file is: |
| 376 |
|
| 377 |
=over 4 |
| 378 |
|
| 379 |
=item * Any file that ends in F<.PL>, F<.pl>, F<.plx>, F<.pm>, F<.pod> or F<.t>. |
| 380 |
|
| 381 |
=item * Any file that has a first line with a shebang and "perl" on it. |
| 382 |
|
| 383 |
=back |
| 384 |
|
| 385 |
Furthermore, files for which the filter set by L</set_pod_file_filter> return |
| 386 |
false are skipped. By default, this filter passes everything through. |
| 387 |
|
| 388 |
The order of the files returned is machine-dependent. If you want them |
| 389 |
sorted, you'll have to sort them yourself. |
| 390 |
|
| 391 |
=head2 all_pod_files_spelling_ok |
| 392 |
|
| 393 |
all_pod_files_spelling_ok(@list_of_files); |
| 394 |
all_pod_files_spelling_ok(@list_of_directories); |
| 395 |
|
| 396 |
Checks all the files for C<POD> spelling. It gathers |
| 397 |
L<Test::Spelling/"all_pod_files"> on each file/directory, and |
| 398 |
declares a L<Test::More/plan> for you (one test for each file), so you |
| 399 |
must not call C<plan> yourself. |
| 400 |
|
| 401 |
If C<@files> is empty, the function finds all C<POD> files in the F<blib> |
| 402 |
directory if it exists, or the F<lib> directory if it does not. A C<POD> file is |
| 403 |
one that ends with F<.pod>, F<.pl>, F<.plx>, or F<.pm>; or any file where the |
| 404 |
first line looks like a perl shebang line. |
| 405 |
|
| 406 |
If there is no working spellchecker (determined by |
| 407 |
L<Test:Spelling/"has_working_spellchecker">), this test will issue a |
| 408 |
C<skip all> directive. |
| 409 |
|
| 410 |
If you're testing a distribution, just create an F<xt/author/pod-spell.t> with the code |
| 411 |
in the L</SYNOPSIS>. |
| 412 |
|
| 413 |
Returns true if every C<POD> file has correct spelling, or false if any of them fail. |
| 414 |
This function will show any spelling errors as diagnostics. |
| 415 |
|
| 416 |
* B<NOTE:> This only tests using bytes. This is not decoded content, etc. Do |
| 417 |
not expect this to work with Unicode content, for example. This uses an open |
| 418 |
with no layers and no decoding. |
| 419 |
|
| 420 |
=head2 get_pod_parser |
| 421 |
|
| 422 |
# a Pod::Spell -like object |
| 423 |
my $object = get_pod_parser(); |
| 424 |
|
| 425 |
Get the object we're using to parse the C<POD>. A new L<Pod::Spell> object |
| 426 |
should be used for every file. People providing custom parsers will have |
| 427 |
to do this themselves. |
| 428 |
|
| 429 |
=head2 has_working_spellchecker |
| 430 |
|
| 431 |
my $cmd = has_working_spellchecker; |
| 432 |
|
| 433 |
C<has_working_spellchecker> will return C<undef> if there is no working |
| 434 |
spellchecker, or a true value (the spellchecker command itself) if there is. |
| 435 |
The module performs a dry-run to determine whether any of the spellcheckers it |
| 436 |
can will use work on the current system. You can use this to skip tests if |
| 437 |
there is no spellchecker. Note that L</all_pod_files_spelling_ok> will do this |
| 438 |
for you. |
| 439 |
|
| 440 |
A full list of spellcheckers which this method might test can be found in the |
| 441 |
source of the C<spellchecker_candidates> method. |
| 442 |
|
| 443 |
=head2 pod_file_spelling_ok |
| 444 |
|
| 445 |
pod_file_spelling_ok('/path/to/Foo.pm'); |
| 446 |
pod_file_spelling_ok('/path/to/Foo.pm', 'Foo is well spelled!'); |
| 447 |
|
| 448 |
C<pod_file_spelling_ok> will test that the given C<POD> file has no spelling |
| 449 |
errors. |
| 450 |
|
| 451 |
When it fails, C<pod_file_spelling_ok> will show any spelling errors as |
| 452 |
diagnostics. |
| 453 |
|
| 454 |
The optional second argument is the name of the test. If it is |
| 455 |
omitted, C<pod_file_spelling_ok> chooses a default test name |
| 456 |
C<< POD spelling for $filename >>. |
| 457 |
|
| 458 |
* B<NOTE:> This only tests using bytes. This is not decoded content, etc. Do |
| 459 |
not expect this to work with Unicode content, for example. This uses an open |
| 460 |
with no layers and no decoding. |
| 461 |
|
| 462 |
=head2 set_pod_file_filter |
| 463 |
|
| 464 |
# code ref |
| 465 |
set_pod_file_filter(sub { |
| 466 |
my $filename = shift; |
| 467 |
return 0 if $filename =~ /_ja.pod$/; # skip Japanese translations |
| 468 |
return 1; |
| 469 |
}); |
| 470 |
|
| 471 |
If your project has C<POD> documents written in languages other than English, then |
| 472 |
obviously you don't want to be running a spellchecker on every Perl file. |
| 473 |
C<set_pod_file_filter> lets you filter out files returned from |
| 474 |
L</all_pod_files> (and hence, the documents tested by |
| 475 |
L</all_pod_files_spelling_ok>). |
| 476 |
|
| 477 |
=head2 set_pod_parser |
| 478 |
|
| 479 |
my $object = Pod::Spell->new(); |
| 480 |
set_pod_parser($object); |
| 481 |
|
| 482 |
By default L<Pod::Spell> is used to generate text suitable for spellchecking |
| 483 |
from the input POD. If you want to use a different parser, perhaps a |
| 484 |
customized subclass of L<Pod::Spell>, call C<set_pod_parser> with an object |
| 485 |
that is-a L<Pod::Parser>. Be sure to create a fresh parser object for |
| 486 |
each file (don't use this with L</all_pod_files_spelling_ok>). |
| 487 |
|
| 488 |
=head2 set_spell_cmd |
| 489 |
|
| 490 |
set_spell_cmd('hunspell -l'); # current preferred |
| 491 |
set_spell_cmd('aspell list'); |
| 492 |
set_spell_cmd('spell'); |
| 493 |
set_spell_cmd('ispell -l'); |
| 494 |
|
| 495 |
If you want to force this module to use a particular spellchecker, then you can |
| 496 |
specify which one with C<set_spell_cmd>. This is useful to ensure a more |
| 497 |
consistent lexicon between developers, or if you have an unusual environment. |
| 498 |
Any command that takes text from standard input and prints a list of misspelled |
| 499 |
words, one per line, to standard output will do. |
| 500 |
|
| 501 |
=head1 SEE ALSO |
| 502 |
|
| 503 |
L<Pod::Spell> |
| 504 |
|
| 505 |
=head1 AUTHOR |
| 506 |
|
| 507 |
Ivan Tubert-Brohman C<< <[email protected]> >> |
| 508 |
|
| 509 |
Heavily based on L<Test::Pod> by Andy Lester and brian d foy. |
| 510 |
|
| 511 |
=head1 COPYRIGHT & LICENSE |
| 512 |
|
| 513 |
Copyright 2005, Ivan Tubert-Brohman, All Rights Reserved. |
| 514 |
|
| 515 |
You may use, modify, and distribute this package under the |
| 516 |
same terms as Perl itself. |
| 517 |
|
| 518 |
=cut |