Test::Dirs - easily copy and compare folders inside tests
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');
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.
temp_copy_ok() is_dir() dir_cleanup_ok()
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.
$src_dir
$message is optional.
$message
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.
$is_dir
$expected_dir
@ignore_files
$dir1(2)
$verbose will output unified diff between $expected_dir and $is_dir.
$verbose
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.
FIXIT
$message, \@ignore_files, $verbose are optional. Default verbose value is $ENV{TEST_VERBOSE}.
\@ignore_files
$ENV{TEST_VERBOSE}
If the $filename is a folder. Removes this folder and all empty folders upwards.
$filename
If the $filename is a file. Removes parent folder of this file and all empty folders upwards.
PS: Just be careful :-)
File::DirCompare, File::Copy::Recursive, File::Temp
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.
Jozef Kutej
To install Test::Dirs, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Dirs
CPAN shell
perl -MCPAN -e shell install Test::Dirs
For more information on module installation, please visit the detailed CPAN module installation guide.