websites need tests, too
Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 [Tue, 6 Apr 2010 11:52:21 +0000 (13:52 +0200)]
stolen from Padre repo

Build.PL [new file with mode: 0644]
inc/Local/Moose/Website/Build.pm [new file with mode: 0644]
t/01-validate.t [new file with mode: 0644]
t/02-spelling.t [new file with mode: 0644]

diff --git a/Build.PL b/Build.PL
new file mode 100644 (file)
index 0000000..3da0b3b
--- /dev/null
+++ b/Build.PL
@@ -0,0 +1,40 @@
+use lib 'inc';
+use Local::Moose::Website::Build;
+my $self = Local::Moose::Website::Build->new(
+    module_name        => 'Moose::Website',
+    configure_requires => {
+        'File::Path'    => 0,
+        'Module::Build' => 0,
+    },
+    build_requires => {
+        'Capture::Tiny' => 0,
+        Encode          => 0,
+        'File::Next'    => 0,
+        'File::Temp'    => 0,
+        'File::Which'   => 0,
+        'Test::More'    => 0,
+        'XML::LibXML'   => 0,
+        'XML::LibXSLT'  => 0,
+    },
+    requires => {
+        'Locale::Maketext::Lexicon'  => 0,
+        Moose                        => 0,
+        'MooseX::Types::Path::Class' => 0,
+        'Path::Class'                => 0,
+        Template                     => 0,
+        'YAML::XS'                   => 0,
+    },
+);
+die "perldoc Build.PL\n" unless $self->destdir;
+
+$self->create_build_script;
+
+__END__
+
+=encoding UTF-8
+
+=head1 SYNOPSIS
+
+    perl Build.PL --destdir=out
+    ./Build test
+    ./Build distclean
diff --git a/inc/Local/Moose/Website/Build.pm b/inc/Local/Moose/Website/Build.pm
new file mode 100644 (file)
index 0000000..a9a1448
--- /dev/null
@@ -0,0 +1,15 @@
+package Local::Moose::Website::Build;
+use parent 'Module::Build';
+use File::Path qw(make_path);
+
+sub ACTION_code {
+    my ($self) = @_;
+    my $out = $self->destdir;
+    make_path($out);
+    system $^X, 'bin/build_site.pl', '--outdir', $out;
+    $self->add_to_cleanup($out);
+    $self->depends_on('config_data');
+    return;
+}
+
+1;
diff --git a/t/01-validate.t b/t/01-validate.t
new file mode 100644 (file)
index 0000000..41ade21
--- /dev/null
@@ -0,0 +1,42 @@
+#!perl
+use 5.010;
+use utf8;
+use strict;
+use warnings FATAL => 'all';
+use Capture::Tiny qw(capture);
+use File::Next qw();
+use File::Which qw(which);
+use Test::More;
+
+# Skip means sweep bugs under the rug.
+# I want this test to be actually run.
+BAIL_OUT 'xmllint (part of the libxml2 package) not installed.'
+  unless which 'xmllint';
+
+my $destdir;
+{
+    my $runtime_params_file = '_build/runtime_params';
+    my $runtime_params      = do $runtime_params_file;
+    die "Could not load $runtime_params_file. Run Build.PL first.\n"
+      unless $runtime_params;
+    $destdir = $runtime_params->{destdir};
+}
+
+my $iter = File::Next::files({
+        file_filter => sub {/\.html \z/msx},
+        sort_files  => 1,
+    },
+    $destdir
+);
+
+my $file_counter;
+while (defined(my $html_file = $iter->())) {
+    $file_counter++;
+    my (undef, $stderr) = capture {
+        system qw(xmllint --noout), $html_file;
+    };
+    ok !$stderr, "$html_file validates";
+    diag $stderr if $stderr;
+}
+
+done_testing($file_counter);
diff --git a/t/02-spelling.t b/t/02-spelling.t
new file mode 100644 (file)
index 0000000..44f91dd
--- /dev/null
@@ -0,0 +1,96 @@
+#!perl
+use 5.010;
+use utf8;
+use strict;
+use warnings FATAL => 'all';
+use autodie qw(:all);
+use Capture::Tiny qw(capture);
+use Encode qw(decode_utf8);
+use File::Next qw();
+use File::Temp qw(tempfile);
+use File::Which qw(which);
+use Test::More;
+use XML::LibXML qw();
+use XML::LibXSLT qw();
+
+binmode Test::More->builder->$_, ':encoding(UTF-8)'
+    for qw(output failure_output todo_output);
+
+# Skip means sweep bugs under the rug.
+# I want this test to be actually run.
+BAIL_OUT 'aspell is not installed.' unless which 'aspell';
+
+my @stopwords;
+for (<DATA>) {
+    chomp;
+    push @stopwords, $_ unless /\A (?: \# | \s* \z)/msx;    # skip comments, whitespace
+}
+
+my $destdir;
+{
+    my $runtime_params_file = '_build/runtime_params';
+    my $runtime_params      = do $runtime_params_file;
+    die "Could not load $runtime_params_file. Run Build.PL first.\n"
+      unless $runtime_params;
+    $destdir = $runtime_params->{destdir};
+}
+
+my $iter = File::Next::files({
+        file_filter => sub {/\.html \z/msx},
+        sort_files  => 1,
+    },
+    $destdir
+);
+
+my $file_counter;
+
+my $stylesheet = XML::LibXSLT->new->parse_stylesheet(
+    XML::LibXML->load_xml(string => <<''));
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:xhtml="http://www.w3.org/1999/xhtml" version="1.0">
+    <xsl:template match="@* | node()"> <!-- apply identity function to rest of nodes -->
+        <xsl:copy>
+            <xsl:apply-templates select="@* | node()"/>
+        </xsl:copy>
+    </xsl:template>
+</xsl:stylesheet>
+
+while (defined(my $html_file = $iter->())) {
+    $file_counter++;
+
+    my ($temp_handle, $temp_file) = tempfile;
+    my $transformed = $stylesheet->transform(XML::LibXML->load_xml(location => $html_file, load_ext_dtd => 0,));
+    $stylesheet->output_fh($transformed, $temp_handle);
+
+    my ($stdout) = capture {
+        system "aspell -H --encoding=UTF-8 -l en list < $temp_file";
+    };
+    my @misspelt_words = grep {!($_ ~~ @stopwords)} split /\n/, decode_utf8 $stdout;
+    ok !@misspelt_words, "$html_file ($temp_file) spell-check";
+    diag join "\n", sort @misspelt_words if @misspelt_words;
+}
+
+done_testing($file_counter);
+
+__DATA__
+## personal names
+
+## proper names
+
+## Moose-specific
+
+## computerese
+
+## other jargon
+
+## neologisms
+
+## compound
+
+## slang
+
+## things that should be in the dictionary, but are not
+
+## single foreign words
+
+## misspelt on purpose