/usr/share/perl5/Test/Spelling.pm is in libtest-spelling-perl 0.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | package Test::Spelling;
use 5.006;
use strict;
use warnings;
use base 'Exporter';
use Pod::Spell;
use Test::Builder;
use File::Spec;
use IPC::Open3;
use Symbol 'gensym';
our $VERSION = '0.15';
our @EXPORT = qw(
pod_file_spelling_ok
all_pod_files_spelling_ok
add_stopwords
set_spell_cmd
all_pod_files
set_pod_file_filter
has_working_spellchecker
);
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 -l',
);
}
sub has_working_spellchecker {
my $dryrun_results = _get_spellcheck_results("dry run", 1);
if (ref $dryrun_results) {
return;
}
return $SPELLCHECKER;
}
sub _get_spellcheck_results {
my $document = shift;
my $dryrun = shift;
my @errors;
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);
print $child_in $document;
# 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;
};
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. also we need to expose the spellchecker we're using in
# has_working_spellchecker
set_spell_cmd($spellchecker)
if !$SPELLCHECKER;
return @words;
}
push @errors, "Unable to run '$spellchecker': $@";
}
# no working spellcheckers during a dry run
return \"no spellchecker" if $dryrun;
# 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;
my $document = '';
open my $handle, '>', \$document;
# save digested POD to the string $document
my $checker = Pod::Spell->new;
$checker->parse_from_file($file, $handle);
my @words = _get_spellcheck_results($document);
chomp for @words;
return @words;
}
sub pod_file_spelling_ok {
my $file = shift;
my $name = shift || "POD spelling for $file";
if (!-r $file) {
$TEST->ok(0, $name);
$TEST->diag("$file does not exist or is unreadable");
return;
}
my @words = invalid_words_in($file);
# remove stopwords, select unique errors
@words = grep { !$Pod::Wordlist::Wordlist{$_} } @words;
my %seen;
@seen{@words} = ();
@words = sort keys %seen;
# emit output
my $ok = @words == 0;
$TEST->ok($ok, "$name");
if (!$ok) {
$TEST->diag("Errors:\n" . join '', map { " $_\n" } @words);
}
return $ok;
}
sub all_pod_files_spelling_ok {
my @files = all_pod_files(@_);
if (!has_working_spellchecker()) {
return $TEST->plan(skip_all => "no working spellchecker found");
}
$TEST->plan(tests => scalar @files);
my $ok = 1;
for my $file (@files) {
pod_file_spelling_ok($file) or undef $ok;
}
return $ok;
}
sub all_pod_files {
my @queue = @_ ? @_ : _starting_points();
my @pod;
while (@queue) {
my $file = shift @queue;
# recurse into subdirectories
if (-d $file) {
opendir(my $dirhandle, $file) or next;
my @newfiles = readdir($dirhandle);
closedir $dirhandle;
@newfiles = File::Spec->no_upwards(@newfiles);
@newfiles = grep { $_ ne "CVS" && $_ ne ".svn" } @newfiles;
push @queue, map "$file/$_", @newfiles;
}
# add the file if it meets our criteria
if (-f $file) {
next unless _is_perl($file);
next unless $FILE_FILTER->($file);
push @pod, $file;
}
}
return @pod;
}
sub _starting_points {
return 'blib' if -d 'blib';
return 'lib';
}
sub _is_perl {
my $file = shift;
return 1 if $file =~ /\.PL$/;
return 1 if $file =~ /\.p(l|lx|m|od)$/;
return 1 if $file =~ /\.t$/;
open my $handle, '<', $file or return;
my $first = <$handle>;
return 1 if defined $first && ($first =~ /^#!.*perl/);
return 0;
}
sub add_stopwords {
for (@_) {
# explicit copy so we don't modify constants as in add_stopwords("SQLite")
my $word = $_;
# XXX: the processing this performs is to support "perl t/spell.t 2>>
# t/spell.t" which is bunk. in the near future the processing here will
# become more modern
$word =~ s/^#?\s*//;
$word =~ s/\s+$//;
next if $word =~ /\s/ or $word =~ /:/;
$Pod::Wordlist::Wordlist{$word} = 1;
}
}
sub set_spell_cmd {
$SPELLCHECKER = shift;
}
sub set_pod_file_filter {
$FILE_FILTER = shift;
}
1;
__END__
=head1 NAME
Test::Spelling - check for spelling errors in POD files
=head1 SYNOPSIS
use Test::More;
BEGIN {
plan skip_all => "Spelling tests only for authors"
unless -d 'inc/.author';
}
use Test::Spelling;
all_pod_files_spelling_ok();
=head1 DESCRIPTION
C<Test::Spelling> lets you check the spelling of a POD file, and report
its results in standard C<Test::More> fashion. This module requires a
spellcheck program such as F<spell>, F<aspell>, F<ispell>, or F<hunspell>.
use Test::Spelling;
pod_file_spelling_ok('lib/Foo/Bar.pm', 'POD file spelling OK');
Note that it is a bad idea to run spelling tests during an ordinary CPAN
distribution install, or in a package that will run in an uncontrolled
environment. There is no way of predicting whether the word list or spellcheck
program used will give the same results. You B<can> include the test in your
distribution, but be sure to run it only for authors of the module by guarding
it in a C<skip_all unless -d 'inc/.author'> clause, or by putting the test in
your distribution's F<xt/> directory. Anyway, people installing your module
really do not need to run such tests, as it is unlikely that the documentation
will acquire typos while in transit. :-)
You can add your own stop words, which are words that should be ignored by the
spell check, like so:
add_stopwords(qw(asdf thiswordiscorrect));
Adding stop words in this fashion affects all files checked for the remainder of
the test script. See L<Pod::Spell> (which this module is built upon) for a
variety of ways to add per-file stop words to each .pm file.
If you have a lot of stop words, it's useful to put them in your test file's
C<DATA> section like so:
use Test::Spelling;
add_stopwords(<DATA>);
all_pod_files_spelling_ok();
__END__
folksonomy
Jifty
Zakirov
To maintain backwards compatibility, comment markers and some whitespace are
ignored. In the near future, the preprocessing we do on the arguments to
L<add_stopwords> will be changed and documented properly.
=head1 FUNCTIONS
=head2 all_pod_files_spelling_ok( [@files/@directories] )
Checks all the files for POD spelling. It gathers L<all_pod_files()> on each
file/directory, and declares a L<Test::More/plan> for you (one test for each
file), so you must not call C<plan> yourself.
If C<@files> is empty, the function finds all POD files in the F<blib>
directory if it exists, or the F<lib> directory if it does not. A POD file is
one that ends with F<.pod>, F<.pl>, F<.plx>, or F<.pm>; or any file where the
first line looks like a perl shebang line.
If there is no working spellchecker (determined by
L</has_working_spellchecker>), this test will issue a "skip all" directive.
If you're testing a distribution, just create a F<t/pod-spell.t> with the code
in the L</SYNOPSIS>.
Returns true if every POD file has correct spelling, or false if any of them fail.
This function will show any spelling errors as diagnostics.
=head2 pod_file_spelling_ok( $filename[, $testname ] )
C<pod_file_spelling_ok> will test that the given POD file has no spelling
errors.
When it fails, C<pod_file_spelling_ok> will show any spelling errors as
diagnostics.
The optional second argument is the name of the test. If it is
omitted, C<pod_file_spelling_ok> chooses a default test name "POD
spelling for C<$filename>".
=head2 all_pod_files( [@dirs] )
Returns a list of all the Perl files in each directory and its subdirectories,
recursively. If no directories are passed, it defaults to F<blib> if F<blib>
exists, or else F<lib> if not. Skips any files in F<CVS> or F<.svn> directories.
A Perl file is:
=over 4
=item * Any file that ends in F<.PL>, F<.pl>, F<.plx>, F<.pm>, F<.pod> or F<.t>.
=item * Any file that has a first line with a shebang and "perl" on it.
=back
Furthermore, files for which the filter set by L</set_pod_file_filter> return
false are skipped. By default, this filter passes everything through.
The order of the files returned is machine-dependent. If you want them
sorted, you'll have to sort them yourself.
=head2 add_stopwords(@words)
Add words that should be skipped by the spellcheck. Note that L<Pod::Spell>
already skips words believed to be code, such as everything in verbatim
(indented) blocks and code marked up with C<< C<...> >>, as well as some common
Perl jargon.
=head2 has_working_spellchecker
C<has_working_spellchecker> will return C<undef> if there is no working
spellchecker, or a true value (the spellchecker command itself) if there is.
The module performs a dry-run to determine whether any of the spellcheckers it
can will use work on the current system. You can use this to skip tests if
there is no spellchecker. Note that L</all_pod_files_spelling_ok> will do this
for you.
=head2 set_spell_cmd($command)
If you want to force this module to use a particular spellchecker, then you can
specify which one with C<set_spell_cmd>. This is useful to ensure a more
consistent lexicon between developers, or if you have an unusual environment.
Any command that takes text from standard input and prints a list of misspelled
words, one per line, to standard output will do.
=head2 set_pod_file_filter($code)
If your project has POD documents written in languages other than English, then
obviously you don't want to be running a spellchecker on every Perl file.
C<set_pod_file_filter> lets you filter out files returned from
L</all_pod_files> (and hence, the documents tested by
L</all_pod_files_spelling_ok>).
set_pod_file_filter(sub {
my $filename = shift;
return 0 if $filename =~ /_ja.pod$/; # skip Japanese translations
return 1;
});
=head1 SEE ALSO
L<Pod::Spell>
=head1 ORIGINAL AUTHOR
Ivan Tubert-Brohman C<< <itub@cpan.org> >>
Heavily based on L<Test::Pod> by Andy Lester and brian d foy.
=head1 MAINTAINER
Shawn M Moore C<< <sartak@bestpractical.com> >>
=head1 COPYRIGHT
Copyright 2005, Ivan Tubert-Brohman, All Rights Reserved.
You may use, modify, and distribute this package under the
same terms as Perl itself.
=cut
|