Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corpus.t, htmlbat.t: don't write to t directory during testing #162

Merged
merged 14 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 37 additions & 52 deletions t/corpus.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
use strict;
use warnings;

use Test::More;
BEGIN {
use Config;
if ($Config::Config{'extensions'} !~ /\bEncode\b/) {
print "1..0 # Skip: Encode was not built\n";
exit 0;
if ($Config{extensions} !~ /\bEncode\b/) {
plan skip_all => "Encode was not built";
}
if (ord("A") != 65) {
print "1..0 # Skip: Encode not fully working on non-ASCII platforms at this time\n";
exit 0;
plan skip_all => "Encode not fully working on non-ASCII platforms at this time";
}
}

#use Pod::Simple::Debug (10);
use Test::More;

use File::Spec;
use Cwd ();
Expand All @@ -25,11 +23,11 @@ my(@testfiles, %xmlfiles, %wouldxml);
#use Pod::Simple::Debug (10);
BEGIN {
my $corpusdir = File::Spec->catdir(File::Basename::dirname(Cwd::abs_path(__FILE__)), 'corpus');
print "#Corpusdir: $corpusdir\n";
diag "Corpusdir: $corpusdir";

opendir(INDIR, $corpusdir) or die "Can't opendir corpusdir : $!";
my @f = map File::Spec::->catfile($corpusdir, $_), readdir(INDIR);
closedir(INDIR);
opendir(my $indir, $corpusdir) or die "Can't opendir $corpusdir : $!";
my @f = map File::Spec::->catfile($corpusdir, $_), readdir($indir);
closedir($indir);
my %f;
@f{@f} = ();
foreach my $maybetest (sort @f) {
Expand All @@ -50,29 +48,28 @@ BEGIN {
plan tests => (2*@testfiles - 1);
}

my $HACK = 1;
#@testfiles = ('nonesuch.txt');
my $HACK = 0;
# 1: write generated XML dump to *.xml_out files for debugging
# 2: write generated XML to *.xml files, updating/overwriting test corpus

my $skippy = ($] < 5.008) ? "skip because perl ($]) pre-dates v5.8.0" : 0;
if($skippy) {
print "# This is just perl v$], so I'm skipping many many tests.\n";
}
#@testfiles = ('nonesuch.txt');

{
my @x = @testfiles;
print "# Files to test:\n";
while(@x) { print "# ", join(' ', splice @x,0,3), "\n" }
diag "Files to test:";
while(@x) { diag " ", join(' ', splice @x,0,3); }
}

require Pod::Simple::DumpAsXML;


foreach my $f (@testfiles) {
my $xml = $xmlfiles{$f};
diag "";
if($xml) {
print "#\n#To test $f against $xml\n";
diag "To test $f against $xml";
} else {
print "#\n# $f has no xml to test it against\n";
diag "$f has no xml to test it against";
}

my $outstring;
Expand All @@ -84,62 +81,50 @@ foreach my $f (@testfiles) {
};

if($@) {
my $x = "#** Couldn't parse $f:\n $@";
$x =~ s/([\n\r]+)/\n#** /g;
print $x, "\n";
diag "** Couldn't parse $f:\n $@";
ok 0;
ok 0;
next;
} else {
print "# OK, parsing $f generated ", length($outstring), " bytes\n";
ok 1;
}

ok 1, "OK, parsing $f generated " . length($outstring) . " bytes";

die "Null outstring?" unless $outstring;

next if $f =~ /nonesuch/;

my $outfilename = ($HACK > 1) ? $wouldxml{$f} : "$wouldxml{$f}\_out";
if($HACK) {
open OUT, ">$outfilename" or die "Can't write-open $outfilename: $!\n";
binmode(OUT);
print OUT $outstring;
close(OUT);
open my $out, ">", $outfilename or die "Can't write-open $outfilename: $!\n";
binmode($out);
print $out $outstring;
close($out);
}
unless($xml) {
print "# (no comparison done)\n";
diag " (no comparison done)";
ok 1;
next;
}

open(IN, "<$xml") or die "Can't read-open $xml: $!";
open(my $in, "<", $xml) or die "Can't read-open $xml: $!";
#binmode(IN);
local $/;
my $xmlsource = <IN>;
close(IN);
my $xmlsource = <$in>;
close($in);

print "# There's errata!\n" if $outstring =~ m/start_line="-321"/;
diag "There's errata!" if $outstring =~ m/start_line="-321"/;

if(
$xmlsource eq $outstring
or do {
$xmlsource =~ s/[\n\r]+/\n/g;
$outstring =~ s/[\n\r]+/\n/g;
$xmlsource eq $outstring;
}
) {
print "# (Perfect match to $xml)\n";
unlink $outfilename unless $outfilename =~ m/\.xml$/is;
$xmlsource =~ s/[\n\r]+/\n/g;
$outstring =~ s/[\n\r]+/\n/g;
if($xmlsource eq $outstring) {
diag " (Perfect match to $xml)";
unlink $outfilename if $HACK == 1;
ok 1;
next;
}

if($skippy) {
skip $skippy, 0;
} else {
print STDERR "# $outfilename and $xml don't match!\n";
print STDERR `diff $xml $outfilename`;
ok 0;
}
diag " $outfilename and $xml don't match!";
print STDERR `diff $xml $outfilename` if $HACK;
ok 0;

}
94 changes: 38 additions & 56 deletions t/htmlbat.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,79 @@
use strict;
use warnings;

use Test::More tests => 15;
use Test::More tests => 13;

#sub Pod::Simple::HTMLBatch::DEBUG () {5};
#sub Pod::Simple::HTMLBatch::DEBUG () {5}

my $DEBUG = 0;
require Pod::Simple::HTMLBatch;

require Pod::Simple::HTMLBatch;;

use File::Spec;
use Cwd;
my $cwd = cwd();
print "# CWD: $cwd\n" if $DEBUG;

use File::Spec;
use File::Spec ();
use Cwd ();
use File::Basename ();

my $t_dir = File::Basename::dirname(Cwd::abs_path(__FILE__));
my $corpus_dir = File::Spec->catdir($t_dir, 'testlib1');

print "# OK, found the test corpus as $corpus_dir\n" if $DEBUG;

my $temp_dir = File::Spec->tmpdir;
my $outdir;
while(1) {
my $rand = sprintf "%05x", rand( 0x100000 );
$outdir = File::Spec->catdir( $t_dir, "delme-$rand-out" );
last unless -e $outdir;
my $rand = sprintf "%05x", rand( 0x100000 );
$outdir = File::Spec->catdir( $temp_dir, "delme-$rand-out" );
last unless -e $outdir;
}

END {
use File::Path;
rmtree $outdir, 0, 0;
}

ok 1;
print "# Output dir: $outdir\n" if $DEBUG;
note "Output dir: $outdir";

mkdir $outdir, 0777 or die "Can't mkdir $outdir: $!";

print "# Converting $corpus_dir => $outdir\n" if $DEBUG;
note "Converting $corpus_dir => $outdir";
my $conv = Pod::Simple::HTMLBatch->new;
$conv->verbose(0);
$conv->index(1);
$conv->batch_convert( [$corpus_dir], $outdir );
ok 1;
print "# OK, back from converting.\n" if $DEBUG;
note "OK, back from converting";

my @files;
use File::Find;
find( sub {
push @files, $File::Find::name;
if (/[.]html$/ && $_ !~ /perl|index/) {
# Make sure an index was generated.
open HTML, $_ or die "Cannot open $_: $!\n";
my $html = do { local $/; <HTML> };
close HTML;
like $html, qr/<div class='indexgroup'>/;
}
return;
push @files, $File::Find::name;
if (/[.]html\z/ && !/perl|index/) {
# Make sure an index was generated.
open my $fh, '<', $_ or die "Cannot open $_: $!\n";
my $html = do { local $/; <$fh> };
close $fh;
like $html, qr/<div class='indexgroup'>/;
}
}, $outdir );

{
my $long = ( grep m/zikzik\./i, @files )[0];
ok($long) or print "# How odd, no zikzik file in $outdir!?\n";
if($long) {
$long =~ s{zikzik\.html?$}{}s;
for(@files) { substr($_, 0, length($long)) = '' }
@files = grep length($_), @files;
}
my $long = ( grep m/zikzik\./i, @files )[0];
ok($long) or diag "How odd, no zikzik file in $outdir!?";
if($long) {
$long =~ s{zikzik\.html?\z}{};
for(@files) { substr($_, 0, length($long)) = ''; }
@files = grep length($_), @files;
}
}

if ($DEBUG) {
print "#Produced in $outdir ...\n";
foreach my $f (sort @files) {
print "# $f\n";
}
print "# (", scalar(@files), " items total)\n";
note "Produced in $outdir ...";
foreach my $f (sort @files) {
note " $f";
}
note "(", scalar(@files), " items total)";

# Some minimal sanity checks:
ok scalar(grep m/\.css/i, @files) > 5;
ok scalar(grep m/\.html?/i, @files) > 5;
ok scalar grep m{squaa\W+Glunk.html?}i, @files;

if (my @long = grep { /^[^.]{9,}/ } map { s{^[^/]/}{} } @files) {
ok 0;
print "# File names too long:\n",
map { "# $_\n" } @long;
} else {
ok 1;
cmp_ok scalar(grep m/\.css\z/i, @files), '>', 5;
cmp_ok scalar(grep m/\.html?\z/i, @files), '>', 5;
cmp_ok scalar(grep m{squaa\W+Glunk\.html?\z}i, @files), '>', 0;

my @long = grep { /^[^.]{9,}/ } map { File::Basename::basename($_) } @files;
unless (is scalar(@long), 0, "Generated filenames fit in 8.* format") {
diag " File names too long:";
diag " $_" for @long;
}

# use Pod::Simple;
# *pretty = \&Pod::Simple::BlackBox::pretty;
Loading