Scroll to navigation

Files(3pm) User Contributed Perl Documentation Files(3pm)


Test::Files - A Test::Builder based module to ease testing with files and dirs


    use Test::More tests => 5;
    use Test::Files;
    use File::Spec;
    my $some_file  = File::Spec->catfile( qw/ path to some file / );
    my $other_file = File::Spec->catfile( qw/ path to other file / );
    my $some_dir   = File::Spec->catdir ( qw/ some dir / );
    my $other_dir  = File::Spec->catdir ( qw/ dir with same stuff / );
    file_ok($some_file, "contents\nof file", "some file has contents");
        "filtered contents\nof file",
        "some file has contents"
    compare_ok($some_file, $other_file, "files are the same");
            $file1, $file2, \&filter, "they're almost the same"
            [qw(files some_dir must contain)],
            "$some_dir has all files in list"
        [qw(files some_dir should contain)],
        "$some_dir has exactly the files in the list"
    compare_dirs_ok($some_dir, $other_dir);
    compare_dirs_filter_ok($some_dir, $other_dir, \&filter_fcn);


  Test::Builder based test helper for file and directory contents.


This module is like Test::More, in fact you should use that first as shown above. It exports

compare the contents of a file to a string
compare the contents of a file to a string, but filter the file first. (You must filter your own string if needed.)
compare the contents of two files
compare the contents of two files, but sends each line through a filter so things that shouldn't count against success can be stripped
checks a directory for the presence of a list files
checks a directory to ensure that the listed files are present and that they are the only ones present
compares all text files in two directories reporting any differences
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.

You should follow the lead of the SYNOPSIS examples and use File::Spec. This makes it much more likely that your tests will pass on a different operating system.

All of the content comparison routines provide diff diagnostic output when they report failure. Currently that diff output is always in table form and can't be changed.

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 first filter was

    sub chop_dates {
        my $line = shift;
        $line =~ s/\d{4}(.\d\d){5}//;
        return $line;

This removes all strings like 2003. Everything else is unchanged and my failing tests started passing when they shold. 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).

"compare_filter_ok" works in a similar manner for a single file comparison, while "file_filter_ok" filters the file before comparing it to your unfiltered string.

The test suite has examples of the use of each function and what the output looks like on failure, though it that doesn't necessarily make them easy to read.


"compare_dirs_ok" and "compare_dirs_filter_ok" do not test for whether the first directory has all the files that are in the second. If you care about missing files in the first direcotry, you must also call "dir_contains_ok" or "dir_contains_only_ok". The "compare_dirs_*" routines do notice when the second directory does not have a files that the first one has.




    Test::Builder::Tester (used only during testing)


Consult Test::Simple, Test::More, and Test::Builder for more testing help. This module really just adds functions to what Test::More does.


Phil Crow, <<gt> Jurij Fajnberg, <<gt>


Copyright 2003-2007 by Phil Crow Copyright 2020 by Jurij Fajnberg

This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5.8.1 itself.

2020-12-05 perl v5.32.0