Skip to content

jozef/Test-Dirs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME

    Test::Dirs - easily copy and compare folders inside tests

SYNOPSIS

            use Test::More tests => 5;
            use Test::Dirs;
            
            # make a temporary copy of a folder
            my $tmp_dir = temp_copy_ok($src_dir, 'copy template to tmp folder');
            
            # compare one folder with another
            is_dir($src_dir, $tmp_dir, 'temp copy should be the same as source');
            
            # set files to ignore
            my @ignore_files = qw(.ignore_me);
            open(my $fh, '>', File::Spec->catfile($tmp_dir, '.ignore_me')) or die $!;
            is_dir($src_dir, $tmp_dir, 'temp copy should be the same as source', \@ignore_files);
            
            TODO: {
                    local $TODO = 'do something with the extra file in the future';
                    is_dir($src_dir, $tmp_dir, 'fails without @ignore_files');
            };
            
            # force verbose, print out the diff if doesn't match
            is_dir($src_dir, $tmp_dir, 'test with verbose on', \@ignore_files, 'verbose');
            

DESCRIPTION

    Exports test function "is_dir" to compare two folders if their file
    structure match and a function to make a temporary copy of a folder
    "temp_copy_ok" so it can be safely manipulated and compared to another
    folder.

    Can be used to test modules or programs that are manipulating a whole
    folder structure via making a temporary copy of a initial folder state.
    Calling module or a program to manipulate files inside this temporary
    folder and then comparing it to a desired folder state.

    In addition there is a "dir_cleanup_ok" function that can be used to
    completely remove folder structures that are not important for
    comparing.

EXPORTS

        temp_copy_ok()
        is_dir()
        dir_cleanup_ok()

FUNCTIONS

 temp_copy_ok($src_dir, $message)

    Will recursively copy $src_dir to a "newdir" in File::Temp folder and
    returning File::Temp::Dir object. This object will stringify to a path
    and when destroyed (will leave the scope) folder is automatically
    deleted.

    $message is optional.

 is_dir($is_dir, $expected_dir, $message, \@ignore_files, $verbose)

    Compares $is_dir with $expected_dir. Files that has to be ignored (are
    not important) can be specified as @ignore_files. The filenames are
    relative to the $dir1(2) folders.

    $verbose will output unified diff between $expected_dir and $is_dir.

    Setting FIXIT env to true will make this function copy $is_dir folder
    content into $expected_dir. Usefull for bootstraping test results or
    for acnkowledging results after code update.

    $message, \@ignore_files, $verbose are optional. Default verbose value
    is $ENV{TEST_VERBOSE}.

 dir_cleanup_ok($filename, $message)

    If the $filename is a folder. Removes this folder and all empty folders
    upwards.

    If the $filename is a file. Removes parent folder of this file and all
    empty folders upwards.

    $message is optional.

    PS: Just be careful :-)

SEE ALSO

    File::DirCompare, File::Copy::Recursive, File::Temp

LICENSE AND COPYRIGHT

    This program is free software; you can redistribute it and/or modify it
    under the terms of either: the GNU General Public License as published
    by the Free Software Foundation; or the Artistic License.

    See http://dev.perl.org/licenses/ for more information.

AUTHOR

    Jozef Kutej

About

easily copy and compare folders inside tests

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages