Improve test coverage

This commit is contained in:
Duncan Ferguson 2014-07-04 21:41:52 +01:00
parent 3e2392e9b6
commit c6747288d5
7 changed files with 418 additions and 108 deletions

View file

@ -704,7 +704,6 @@ sub open_client_windows(@) {
}
if ( $servers{$server}{pid} == 0 ) {
# this is the child
# Since this is the child, we can mark any server unresolved without
# affecting the main program

View file

@ -13,6 +13,7 @@ use Exception::Class (
'App::ClusterSSH::Exception::Cluster',
'App::ClusterSSH::Exception::LoadFile',
'App::ClusterSSH::Exception::Helper',
'App::ClusterSSH::Exception::Getopt',
);
# Don't use SVN revision as it can cause problems

View file

@ -37,6 +37,13 @@ sub new {
sub add_option {
my ( $self, %args ) = @_;
my $spec = $args{spec};
if ( !$spec ) {
croak(
App::ClusterSSH::Exception::Getopt->throw(
error => 'No "spec" passed to add_option',
),
);
}
my ( $option, $arg ) = $spec =~ m/^(.*?)(?:[\+=:](.*))?$/;
if ($arg) {
my $arg_type = defined $args{arg_desc} ? "<$args{arg_desc}>" : undef;
@ -264,21 +271,15 @@ sub getopts {
pod2usage( -verbose => 1 ) if ( $options->{'h'} || $options->{help} );
pod2usage( -verbose => 2 ) if ( $options->{H} || $options->{man} );
# record what was given on the command line in case this
# object is ever dumped out
$self->{options_parsed} = $options;
if ( $options->{'generate-pod'} ) {
$self->_generate_pod;
$self->exit;
}
if ( $options->{usage} ) {
$self->usage;
$self->exit;
}
if ( $options->{help} ) {
$self->help;
$self->exit;
}
if ( $options->{version} ) {
print "Version: $VERSION\n";
$self->exit;
@ -291,11 +292,20 @@ sub getopts {
foreach my $option ( sort keys( %{ $self->{command_options} } ) ) {
my $accessor = $self->{command_options}->{$option}->{accessor};
my $default = $self->{command_options}->{$option}->{default};
if ( my $acc = $accessor ) {
$accessor =~ s/-/_/g;
no strict 'refs';
# hide warnings when getopts is run multiple times, esp. for testing
no warnings 'redefine';
*$accessor = sub {
return $options->{$acc};
return $options->{$acc} || $default;
# defined $options->{$acc} ? $options->{$acc}
# : defined $self->{command_options}->{$acc}->{default}
# ? $self->{command_options}->{$acc}->{default}
# : undef;
};
}
}
@ -314,10 +324,14 @@ sub getopts {
= !$self->parent->config->{unique_servers} || 0;
}
$self->parent->config->{title} = $self->title if ( $self->title );
$self->parent->config->{command} = $self->action if ( $self->action );
$self->parent->config->{user} = $self->username if ( $self->username );
$self->parent->config->{port} = $self->port if ( $self->port );
$self->parent->config->{title} = $self->title if ( $self->title );
$self->parent->config->{user} = $self->username if ( $self->username );
$self->parent->config->{port} = $self->port if ( $self->port );
# note, need to check if these actions can be performed as they are
# not common acorss all communiction methods
$self->parent->config->{command} = $self->action
if ( $self->can('action') && $self->action );
$self->parent->config->{terminal_font} = $self->font if ( $self->font );
$self->parent->config->{terminal_args} = $self->term_args
@ -341,32 +355,6 @@ sub getopts {
return $self;
}
sub usage {
my ($self) = @_;
#print $self->loc('US
my $options_pod;
$options_pod .= "=over\n\n";
foreach my $option ( sort keys( %{ $self->{command_options} } ) ) {
my ( $short, $long )
= $self->{command_options}{$option}{help} =~ m/^(.*)\n\t(.*)/;
$options_pod .= "=item $short\n\n";
$options_pod .= "$long\n\n";
}
$options_pod .= "=back\n\n";
return $self;
}
sub help {
my ($self) = @_;
warn "** HELP **";
return $self;
}
sub output {
my (@text) = @_;
@ -454,7 +442,7 @@ This will test the mechanisms used to open windows to hosts. This could be due
next if ( $self->{command_options}->{$longopt}->{hidden} );
output '=item ', $self->{command_options}->{$longopt}->{option_desc};
output $self->{command_options}->{$longopt}->{help};
output $self->{command_options}->{$longopt}->{help} || 'No help';
if ( $self->{command_options}->{$longopt}->{default} ) {
output $self->loc('Default'), ': ',
@ -1019,40 +1007,6 @@ See http://dev.perl.org/licenses/ for more information.
return $self;
}
sub _pod_output_list_section {
my ( $self, $section ) = @_;
output '=over';
for ( 1 .. 50 ) {
# there might not be 10 sections so catch errors
my ( $item, $text );
eval { $item = $self->loc( '_' . $section . '_ITEM_' . $_ ); };
eval { $text = $self->loc( '_' . $section . '_TEXT_' . $_ ); };
# and if there is an error we have gone past the last item
#last if($@);
if ($item) {
output '=item ', $item;
}
if ($text) {
output '=item *' if ( !$item );
output $text;
}
}
output '=back';
return $self;
}
#use overload (
# q{""} => sub {
# my ($self) = @_;
# return $self->{hostname};
# },
# fallback => 1,
#);
1;
__DATA__

View file

@ -22,7 +22,7 @@ sub new {
sub script {
my ( $self, $config ) = @_;
if(! defined $config || ref $config ne "HASH") {
if(! defined $config || ! ref $config || ref $config ne "App::ClusterSSH::Config") {
croak(
App::ClusterSSH::Exception::Helper->throw(
error => 'No configuration provided or in wrong format',

View file

@ -137,8 +137,6 @@ is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
$base = undef;
$get_config;
my $object;
trap {
$base = App::ClusterSSH::Base->new( debug => 3, parent => 'guardian' );
};

View file

@ -1,6 +1,40 @@
use strict;
use warnings;
package Test::ClusterSSH::Mock;
# generate purpose object used to simplfy testing
sub new {
my ( $class, %args ) = @_;
my $config = {%args};
return bless $config, $class;
}
sub parent {
my ($self) = @_;
return $self;
}
sub config {
my ($self) = @_;
return $self;
}
sub load_configs {
my ($self) = @_;
return $self;
}
sub config_file {
my ($self) = @_;
return {};
}
1;
package main;
use FindBin qw($Bin);
use lib "$Bin/../lib";
@ -11,10 +45,340 @@ BEGIN { use_ok('App::ClusterSSH::Getopt') }
my $getopts;
$getopts = App::ClusterSSH::Getopt->new();
my $mock_object = Test::ClusterSSH::Mock->new();
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
isa_ok( $getopts, 'App::ClusterSSH::Getopt' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on new object okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
isa_ok( $getopts, 'App::ClusterSSH::Getopt' );
diag('testing output') if ( $ENV{TEST_VERBOSE} );
trap {
$getopts->add_option();
};
is( $trap->leaveby, 'die', 'adding an empty option failed' );
is( $trap->die,
q{No "spec" passed to add_option},
'empty add_option message'
);
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->add_option( spec => 'option' );
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->option;
};
is( $trap->leaveby, 'return', 'calling option' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option, undef, 'Expecting no die message' );
local @ARGV = '--option1';
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->add_option( spec => 'option1' );
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->option1;
};
is( $trap->leaveby, 'return', 'calling option' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option1, 1, 'Expecting no die message' );
local @ARGV = undef;
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->add_option( spec => 'option1', default => 5 );
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->option1;
};
is( $trap->leaveby, 'return', 'calling option' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option1, 5, 'correct default value' );
local @ARGV = ( '--option1', '8' );
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->add_option( spec => 'option1=i', default => 5, );
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->option1;
};
is( $trap->leaveby, 'return', 'calling option' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option1, 8, 'default value overridden' );
@ARGV = ( '--option1', '--option2', 'string', '--option3', '10' );
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->add_option( spec => 'hidden', hidden => 1, no_acessor => 1, );
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->add_option( spec => 'option1', help => 'help for 1' );
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->add_option( spec => 'option2|o=s', help => 'help for 2' );
};
is( $trap->leaveby, 'return', 'adding option2 failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->add_option(
spec => 'option3|alt_opt|O=i',
help => 'help for 3',
default => 5
);
};
is( $trap->leaveby, 'return', 'adding option3 failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->option1;
};
is( $trap->leaveby, 'return', 'calling option1' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option1, 1, 'option1 is as expected' );
trap {
$getopts->option1;
};
is( $trap->leaveby, 'return', 'calling option2' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option2, 'string', 'option2 is as expected' );
trap {
$getopts->option3;
};
is( $trap->leaveby, 'return', 'calling option3' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $getopts->option3, 10, 'option3 is as expected' );
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->add_common_ssh_options;
};
is( $trap->leaveby, 'return', 'calling option2' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->add_common_session_options;
};
is( $trap->leaveby, 'return', 'calling option2' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'getops on object with spec okay' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
my $pod;
@ARGV = ('--generate-pod');
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
$getopts->add_option(
spec => 'long_opt|l=s',
help => 'long opt help',
default => 'default string'
);
$getopts->add_option( spec => 'another_long_opt|L=i', );
$getopts->add_option( spec => 'a=s', help => 'short option only', );
$getopts->add_option( spec => 'long', help => 'long option only', );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'exit', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
ok( defined( $trap->stdout ), 'Expecting no STDOUT' );
$pod = $trap->stdout;
# run pod through a checker at some point as it should be 'clean'
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
@ARGV = ('--help');
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'exit', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
ok( defined( $trap->stdout ), 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
@ARGV = ('-?');
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'exit', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
ok( defined( $trap->stdout ), 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
@ARGV = ('-v');
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'exit', 'version option exist okay' );
is( $trap->die, undef, 'no error when spec provided' );
like( $trap->stdout, qr/^Version: /, 'Version string correct' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
@ARGV = ('-@');
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'exit', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
ok( defined( $trap->stdout ), 'Expecting no STDOUT' );
like( $trap->stderr, qr{Unknown option: @}, 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
# test some common options
@ARGV = (
'--unique-servers', '--title', 'title', '-l',
'username', '-p', '22', '--autoquit',
'--tile', '--autoclose','10',
);
$mock_object->{auto_close} = 0;
$mock_object->{auto_quit} = 0;
$mock_object->{window_tiling} = 0;
$mock_object->{show_history} = 0;
$mock_object->{use_all_a_records} = 1;
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object, );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $mock_object->{auto_close}, 10, 'auto_close set right');
is( $mock_object->{auto_quit}, 1, 'auto_quit set right');
is( $mock_object->{window_tiling}, 1, 'window_tiling set right');
is( $mock_object->{show_history}, 0, 'show_history set right');
is( $mock_object->{use_all_a_records}, 1, 'use_all_a_records set right');
@ARGV = (
'--unique-servers', '--title', 'title', '-l',
'username', '-p', '22', '--autoquit',
'--tile', '--show-history', '-A',
);
$getopts = App::ClusterSSH::Getopt->new( parent => $mock_object, );
trap {
$getopts->getopts;
};
is( $trap->leaveby, 'return', 'adding an empty option failed' );
is( $trap->die, undef, 'no error when spec provided' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->die, undef, 'Expecting no die message' );
is( $mock_object->{auto_close}, 10, 'auto_close set right');
is( $mock_object->{auto_quit}, 0, 'auto_quit set right');
is( $mock_object->{window_tiling}, 0, 'window_tiling set right');
is( $mock_object->{show_history}, 1, 'show_history set right');
is( $mock_object->{use_all_a_records}, 0, 'use_all_a_records set right');
done_testing;

View file

@ -11,6 +11,16 @@ use File::Temp qw(tempdir);
use Readonly;
package App::ClusterSSH::Config;
sub new {
my ($class, %args) = @_;
my $self = {%args};
return bless $self, $class;
}
package main;
BEGIN {
use_ok("App::ClusterSSH::Helper") || BAIL_OUT('failed to use module');
}
@ -39,8 +49,9 @@ is( $trap->stderr, q{}, 'Expecting no STDERR' );
is( $trap->die, 'No configuration provided or in wrong format',
'bad format' );
my $mock_config = App::ClusterSSH::Config->new();
trap {
$script = $helper->script( { something => 'nothing' } );
$script = $helper->script( $mock_config );
};
is( $trap->leaveby, 'die', 'returned ok' );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
@ -49,46 +60,29 @@ is( $trap->stdout, q{}, 'Expecting no STDOUT' );
#is( $trap->stderr, q{}, 'Expecting no STDERR' );
is( $trap->die, q{Config 'comms' not provided}, 'missing arg' );
$mock_config->{comms} = 'method';
trap {
$script = $helper->script( { comms => 'method' } );
$script = $helper->script( $mock_config );
};
is( $trap->leaveby, 'die', 'returned ok' );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is( $trap->die, q{Config 'method' not provided}, 'missing arg' );
$mock_config->{method} = 'binary';
trap {
$script = $helper->script( { comms => 'method', method => 'binary', } );
$script = $helper->script( $mock_config );
};
is( $trap->leaveby, 'die', 'returned ok' );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is( $trap->die, q{Config 'method_args' not provided}, 'missing arg' );
$mock_config->{method_args} = 'rubbish';
$mock_config->{command} = 'echo';
$mock_config->{auto_close} = 5;
trap {
$script = $helper->script(
{ comms => 'method',
method => 'binary',
method_args => 'rubbish',
command => 'echo',
auto_close => 0,
}
);
};
is( $trap->leaveby, 'return', 'returned ok' );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is( $trap->die, undef, 'not died' );
trap {
$script = $helper->script(
{ comms => 'method',
method => 'binary',
method_args => 'rubbish',
command => 'echo',
auto_close => 5,
}
);
$script = $helper->script( $mock_config );
};
is( $trap->leaveby, 'return', 'returned ok' );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );