diff options
Diffstat (limited to 't/lib/Test/DocKnot/Spin.pm')
-rw-r--r-- | t/lib/Test/DocKnot/Spin.pm | 91 |
1 files changed, 36 insertions, 55 deletions
diff --git a/t/lib/Test/DocKnot/Spin.pm b/t/lib/Test/DocKnot/Spin.pm index 2c520a2..8cc461f 100644 --- a/t/lib/Test/DocKnot/Spin.pm +++ b/t/lib/Test/DocKnot/Spin.pm @@ -6,17 +6,19 @@ # Modules and declarations ############################################################################## -package Test::DocKnot::Spin 1.00; +package Test::DocKnot::Spin 2.00; use 5.024; use autodie; use warnings; use Cwd qw(getcwd); +use Encode qw(encode); use Exporter qw(import); use File::Compare qw(compare); use File::Find qw(find); -use Perl6::Slurp qw(slurp); +use Path::Iterator::Rule (); +use Path::Tiny qw(path); use Test::RRA qw(is_file_contents); use Test::More; @@ -36,7 +38,7 @@ our @EXPORT_OK = qw(is_spin_output is_spin_output_tree); # $message - The descriptive message of the test sub is_spin_output { my ($output_file, $expected, $message) = @_; - my $results = slurp($output_file); + my $results = path($output_file)->slurp_utf8(); # Map dates to %DATE% and ignore the different output when the # modification date is the same as the generation date. @@ -60,7 +62,7 @@ sub is_spin_output { $results =~ s{ DocKnot [ ] \d+ [.] \d+ }{DocKnot %VERSION%}xms; # Check the results against the expected file. - is_file_contents($results, $expected, $message); + is_file_contents(encode('utf-8', $results), $expected, $message); return; } @@ -74,61 +76,38 @@ sub is_spin_output { # Returns: The number of tests run. sub is_spin_output_tree { my ($output, $expected, $message) = @_; - my $cwd = getcwd(); - my %seen; - my @missing; - - # Function that compares each of the output files in the tree, called from - # File::Find on the output directory. - my $check_output = sub { - my $file = $_; - if ($file eq '.git') { - $File::Find::prune = 1; - return; - } - return if -d $file; - - # Determine the relative path and mark it as seen. - my $path = File::Spec->abs2rel($File::Find::name, $output); - $seen{$path} = 1; + my (%seen, @missing); - # Find the corresponding expected file. - my $expected_file - = File::Spec->rel2abs(File::Spec->catfile($expected, $path), $cwd); + # Compare each of the output files in the tree. + my $rule = Path::Iterator::Rule->new()->skip_dirs('.git')->file(); + my $iter = $rule->iter("$output", { follow_symlinks => 0 }); + while (defined(my $file = $iter->())) { + my $path = path($file)->relative($output); + $seen{"$path"} = 1; + my $expected_file = $path->absolute($expected); # Compare HTML output using is_spin_output and all other files as # copies. - if ($file =~ m{ [.] (?: html | rss ) \z }xms) { + if ($path->basename() =~ m{ [.] (?: html | rss ) \z }xms) { is_spin_output($file, $expected_file, "$message ($path)"); } else { is(compare($file, $expected_file), 0, "$message ($path)"); } - return; - }; - - # Function that checks that every file in the expected output tree was - # seen in the generated output tree, called from File::Find on the - # expected directory. - my $check_files = sub { - my $file = $_; - return if -d $file; - - # Determine the relative path and make sure it was in the %seen hash. - my $path = File::Spec->abs2rel($File::Find::name, $expected); - if ($seen{$path}) { - delete $seen{$path}; + } + my $count = keys(%seen); + + # Check every file in the expected output tree was seen in the generated + # output tree. + $rule = Path::Iterator::Rule->new()->skip_dirs('.git')->file(); + $iter = $rule->iter("$expected", { follow_symlinks => 0 }); + while (defined(my $file = $iter->())) { + my $path = path($file)->relative($expected); + if ($seen{"$path"}) { + delete $seen{"$path"}; } else { push(@missing, $path); } - return; - }; - - # Compare the output. - find($check_output, $output); - my $count = keys(%seen); - - # Check that there aren't any missing files. - find($check_files, $expected); + } is_deeply(\@missing, [], 'All expected files generated'); # Return the count of tests. @@ -143,7 +122,7 @@ sub is_spin_output_tree { __END__ =for stopwords -Allbery Allbery sublicense MERCHANTABILITY NONINFRINGEMENT DocKnot +Allbery Allbery sublicense MERCHANTABILITY NONINFRINGEMENT DocKnot RSS =head1 NAME @@ -176,16 +155,18 @@ should be explicitly imported. =item is_spin_output(OUTPUT, EXPECTED, MESSAGE) -Given OUTPUT, which should be the path to a file generated by +Given OUTPUT, which should be a Path::Tiny object pointing to the output from App::DocKnot::Spin, compare it to the expected output in the file named -EXPECTED. MESSAGE is the message to print with the test results for easy -identification. +EXPECTED (also a Path::Tiny object). MESSAGE is the message to print with the +test results for easy identification. =item is_spin_output_tree(OUTPUT, EXPECTED, MESSAGE) -Compare the output tree at OUTPUT with the expected output tree at EXPECTED, -using the same comparison algorithm as is_spin_output(). MESSAGE with the -message to print with the test results for easy identification. +Compare the output tree at OUTPUT with the expected output tree at EXPECTED +(both Path::Tiny objects), using the same comparison algorithm as +is_spin_output() for HTML and RSS files and a straight content comparison for +all other files. MESSAGE with the message to print with the test results for +easy identification. =back |