shell-scripts/file_processing/t/01-subroutines.t

79 lines
2.1 KiB
Perl
Raw Permalink Normal View History

2023-09-11 20:01:15 -05:00
use Test2::V0;
use Test2::Plugin::Times;
use Test2::Plugin::ExitSummary;
use Test2::Plugin::NoWarnings;
use FindBin qw($Bin);
use File::Path::Tiny;
use Time::Piece qw/localtime/;
use lib "$Bin/../lib";
use ProcessDirectories;
plan(2);
# Preconditions
my $now = localtime;
my $year = $now->year;
my $lastyear = $now->add_years(-1)->year;
diag("Current year: $year Last year: $lastyear\n");
## Define and create test source folder
my $current_year_dir = 't/current_year/';
my $archive_folder = 't/1_archives/';
my $prev_year_dir = $archive_folder . $lastyear . '_Archives';
diag("Source test dir $current_year_dir Dest test dir $prev_year_dir");
if(!File::Path::Tiny::mk($current_year_dir)) {
die "Could not create test source directory: '$current_year_dir': $!";
}
my @test_files = qw( 2003_file-1 2003_file-2 2002_file-1 2002_file-2 2002_file-3 );
for my $file (@test_files) {
create_file($current_year_dir . '/' . $file);
}
# Begin testing
ProcessDirectories::create_archive_dirs( $current_year_dir, $prev_year_dir );
ok( ( -d $prev_year_dir ), "Previous year archive directory was created" );
ok( ProcessDirectories::compare_dirs( $current_year_dir, $prev_year_dir ), "Previous year archive directory contents matches current directory contents" );
# clean_dirs
# End Testing
# Cleanup
# if(!File::Path::Tiny::rm($current_year_dir)) {
# die "Could not remove test source directory: '$current_year_dir': $!";
# }
# if(!File::Path::Tiny::rm($archive_folder)) {
# die "Could not remove test archive directory: '$archive_folder': $!";
# }
sub create_file {
my $filepath = shift;
open (my $fh, ">", $filepath) or die "Can't open file '$filepath' for writing: $!";
# you can control the range of characters here
my $minimum = 4;
my $range = 16;
my $num_bytes = '100';
for (my $bytes = 0; $bytes < $num_bytes; $bytes += 4) {
my $rand = int(rand($range ** 4));
my $string = '';
for (1..4) {
$string .= chr($rand % $range + $minimum);
$rand = int($rand / $range);
}
print $fh $string;
}
close($fh) or warn "Can't close file '$filepath': $!";
return;
}