Skip to content

Commit

Permalink
Fixed #9, now create empty unwritable folder in test
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranmraine committed Mar 6, 2014
1 parent afabd25 commit 404da7d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions t/pcapCli.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use strict;
use Test::More;
use Test::Fatal;
use File::Spec;
use File::Path qw(remove_tree);
use File::Path qw(make_path remove_tree);
use Try::Tiny qw(try catch finally);
use Fcntl qw( :mode );

use constant MODULE => 'PCAP::Cli';

Expand All @@ -12,6 +13,7 @@ use_ok(MODULE);
use FindBin qw($Bin);
my $test_data = "$Bin/../testData";


subtest 'file_for_reading' => sub {
is(exception{ PCAP::Cli::file_for_reading('test', undef) }
, qq{Option 'test' has not been defined.\n}
Expand All @@ -35,16 +37,19 @@ subtest 'out_dir_check' => sub {
is(exception{ PCAP::Cli::out_dir_check('test', undef) }
, qq{Option 'test' has not been defined.\n}
, 'Fail when no path provided');
like(exception{ PCAP::Cli::out_dir_check('test', File::Spec->catfile($test_data, 'nonWritableDir')) }
, qr/Option 'test' points to an existing WRITE PROTECTED directory:/m
, 'Fail when pointed to non-writable area');
like(exception{ PCAP::Cli::out_dir_check('test', File::Spec->catfile($test_data, 'data.file')) }
, qr/Option 'test' points to an existing entity \(not a directory\):/m
, 'Fail when pointed to file');

my $tmp_dir = File::Spec->catdir($test_data, 'test_folder');
my $non_write = File::Spec->catfile($test_data, 'nonWritableDir');
# need to ensure folder is removed
try {
make_path($non_write);
chmod S_IRUSR, $non_write;
like(exception{ PCAP::Cli::out_dir_check('test', $non_write) }
, qr/Option 'test' points to an existing WRITE PROTECTED directory:/m
, 'Fail when pointed to non-writable area');
is(PCAP::Cli::out_dir_check('test', $tmp_dir), $tmp_dir, 'Success when able to create directory');
is(PCAP::Cli::out_dir_check('test', $tmp_dir), $tmp_dir, 'Success when directory exists and writable');
ok((chmod 0400, $tmp_dir), 'make test folder readonly for next test');
Expand All @@ -58,8 +63,14 @@ subtest 'out_dir_check' => sub {

} catch{ }
finally {
chmod 0700, $tmp_dir; # if it don't work it's knackered anyway
remove_tree($tmp_dir) if(-e $tmp_dir);
if(-e $tmp_dir) {
chmod S_IRWXU, $tmp_dir; # if it don't work it's knackered anyway
remove_tree($tmp_dir);
}
if(-e $non_write) {
chmod S_IRWXU, $non_write;
remove_tree($non_write);
}
};
};

Expand Down

0 comments on commit 404da7d

Please sign in to comment.