NAME
Test::Files - A Test::Builder based module to ease testing with files and dirs
SYNOPSIS
use Test::More tests => 5;
use Test::Files;
file_ok("path/to/some/file", "contents\nof file", "some file");
compare_ok("path/to/a/file", "path/to/correct/file", "they're the same");
dir_contains_ok ( "some/dir", [qw(files some/dir should contain)] );
dir_only_contains_ok( "some/dir", [qw(files some/dir should contain)] );
compare_dirs_ok("some/dir", "some/other/dir/with/exactly/the/same/stuff");
compare_dirs_filter_ok("some/dir", "some/other/dir", \&filter_fcn);
ABSTRACT
Test::Builder based test helper which helps you test files and their contents.
Includes facilities for comparing whole directory trees for structure and/or
content.
DESCRIPTION
This module is like Test::More, in fact you should use that first as shown above. It exports
- file_ok
-
compare the contents of a file to a string
- compare_ok
-
compare the contents of two files
- dir_contains_ok
-
checks a directory for the presence of a list files
- dir_contains_only_ok
-
checks a directory to ensure that the listed files are present and that they are the only ones present
- compare_dirs_ok
-
compares all text files in two directories reporting any differences
- compare_dirs_filter_ok
-
works like compare_dirs_ok, but calls a filter function on each line of input, allowing you to exclude or alter some text to avoid spurious failures (like timestamp disagreements).
Though the SYNOPSIS examples don't all have names, you can and should provide a name for each test. Names are omitted above only to reduce clutter and line widths.
Most of the functions are self explanatory. One exception is compare_dirs_filter_ok
which compares two directory trees, like compare_dirs_ok
but with a twist. The twist is a filter which each line is fed through before comparison. I wanted this because some files are really the same, but look different textually. In particular, I was comparing files with machine generated dates. Everything in them was identical, except those dates.
The filter function receives each line of each file. It may perform any necessary transformations (like excising dates), then it must return the line in (possibly) transformed state. For example, my filter was
sub chop_dates {
my $line = shift;
$line =~ s/\d{4}(.\d\d){5}//;
return $line;
}
This removes all strings like 2003.10.14.14.17.37. Everything else is unchanged and my failing tests started passing as expected. If you want to exclude the line from consideration, return "" (do not return undef, that makes it harder to chain filters together and might lead to warnings).
EXPORT
file_ok compare_ok dir_contains_ok dir_only_contains_ok compare_dirs_ok compare_dirs_filter_ok
DEPENDENCIES
Test::Builder Test::More Test::Differences
SEE ALSO
Consult Test::Simple, Test::More, and Test::Builder for more testing help. This module really just adds functions to what Test::More does.
AUTHOR
Phil Crow, <philcrow2000@yahoo.com<gt>
COPYRIGHT AND LICENSE
Copyright 2003 by Phil Crow
This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5.8.1 itself.