Skip to content

Commit

Permalink
Keep spell check for 'code' separate
Browse files Browse the repository at this point in the history
* Also convert to using supply/tap to queue tests
* more aggressively subst for \n and \t
  • Loading branch information
coke committed Aug 24, 2020
1 parent 88870c7 commit e09360e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
83 changes: 63 additions & 20 deletions xt/aspell.t
Expand Up @@ -14,6 +14,9 @@ Spell check all the pod and most markdown files in the documentation directory.
Ignore case, and provide a repo-specific list of approved words,
which include technical jargon, method and class names, etc.
Split the lexicons for descriptive text and code, allowing us to specify "words"
that are only allowed in code (variable names, some output, etc.).
If the test fails, you can make it pass again by changing the
text (fixing the spelling issue), or adding the new word to
C<xt/words.pws> (if it's a word, a class/method name, known
Expand All @@ -24,7 +27,7 @@ text that is part of a code example)

my @files = Test-Files.documents.grep({not $_ ~~ / 'README.' .. '.md' /});

plan +@files;
plan +@files *2;

my $proc = shell('aspell -v');
if $proc.exitcode {
Expand All @@ -33,40 +36,80 @@ if $proc.exitcode {
}

# generate a combined words file
# a header is required, but is supplied by words.pws

my $dict = open "xt/aspell.pws", :w;
$dict.say('personal_ws-1.1 en 0 utf-8');
$dict.say("xt/words.pws".IO.slurp.chomp);
$dict.say("xt/code.pws".IO.slurp.chomp);
$dict.close;

my %output;

sub test-it($promises, $file) {
await Promise.allof: |$promises;
my $tasks = $promises».result;
my $jobs = supply {
for @files -> $file {
# We use either the raw markdown or the rendered/cached Pod.
my $input-file = $file.ends-with('.pod6') ?? Pod::Cache.cache-file($file) !! $file;

# split the input file into a block of code and a block of text
# anything with a leading space is considered code, and we just
# concat all the code and text into one block of each per file

my Str $code;
my Str $text;

# Process the text so that aspell understands it.
# Every line starts with a ^
# turn \n and \t into spaces to avoid \nFoo being read as "nFoo" by aspell
for $input-file.IO.slurp.lines -> $line {
my Bool $is-code = $line.starts-with(' ');

my $processed = '^' ~ $line.subst("\\t", ' ', :g).subst("\\n", ' ', :g) ~ "\n";

$code ~= $processed if $is-code;
$text ~= $processed unless $is-code;
}

for <code text> -> $type {
# Restrict dictionary used based on block type
my ($dict, $body);
if $type eq "code" {
$body = $code;
$dict = "./xt/aspell.pws";
} else {
$body = $text;
$dict = "./xt/words.pws";
}

my $proc = Proc::Async.new(:w, ['aspell', '-a', '-l', 'en_US', '--ignore-case', "--extra-dicts=$dict", '--mode=url']);
%output{$file}{$type}="";
$proc.stdout.tap(-> $buf { %output{$file}{$type} = %output{$file}{$type} ~ $buf });
$proc.stderr.tap(-> $buf {});
my $promise = $proc.start;

$proc.say("!");
$proc.say($body);
$proc.close-stdin;
emit([$promise, $file, $type]);
}
}
}

$jobs.tap( -> $job {
my ($promise, $file, $type) = |$job;
await $promise;

my $tasks = $promise.result;

my $count;
for %output{$file}.lines -> $line {
for %output{$file}{$type}.lines -> $line {
FIRST next; # dump first line
next if $line eq '';
diag $line;
$count++;
}

my $so-many = $count // "no";
ok !$count, "$file has $so-many spelling errors";
}

for @files -> $file {
my $input-file = $file.ends-with('.pod6') ?? Pod::Cache.cache-file($file) !! $file;

my $fixer = Proc::Async.newperl -pne BEGIN {print "!\n"} s/\S\K\\[tn]/ /g; s/^/^/ $input-file»);
my $proc = Proc::Async.new(<aspell -a -l en_US --ignore-case --extra-dicts=./xt/aspell.pws --mode=url>);
$proc.bind-stdin: $fixer.stdout: :bin;
%output{$file}="";
$proc.stdout.tap(-> $buf { %output{$file} = %output{$file} ~ $buf });
$proc.stderr.tap(-> $buf {});
test-it([$fixer.start, $proc.start], $file);
}
ok !$count, "$file ($type) has $so-many spelling errors";
});

# vim: expandtab shiftwidth=4 ft=perl6
1 change: 1 addition & 0 deletions xt/words.pws
@@ -1,3 +1,4 @@
personal_ws-1.1 en 0 utf-8
abi
absolutepath
accessor
Expand Down

0 comments on commit e09360e

Please sign in to comment.