From 404da7d5ee522841874a9641e8584883ce8d0646 Mon Sep 17 00:00:00 2001 From: Keiran Raine Date: Thu, 6 Mar 2014 13:45:17 +0000 Subject: [PATCH] Fixed #9, now create empty unwritable folder in test --- t/pcapCli.t | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/t/pcapCli.t b/t/pcapCli.t index b33bd2e..8b0a14a 100644 --- a/t/pcapCli.t +++ b/t/pcapCli.t @@ -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'; @@ -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} @@ -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'); @@ -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); + } }; };