Read a given config file

Throw an exception if it cannot be read
This commit is contained in:
Duncan Ferguson 2011-07-09 09:00:24 +01:00
parent 3caf243048
commit 3fedc40eaf
4 changed files with 39 additions and 12 deletions

View file

@ -29,6 +29,7 @@ my $build = Module::Build->new(
'Test::Pod::Coverage' => 0, 'Test::Pod::Coverage' => 0,
'Test::Pod' => 0, 'Test::Pod' => 0,
'Test::Trap' => 0, 'Test::Trap' => 0,
'Readonly' => 0,
}, },
add_to_cleanup => ['App-ClusterSSH-*'], add_to_cleanup => ['App-ClusterSSH-*'],
create_makefile_pl => 'traditional', create_makefile_pl => 'traditional',

View file

@ -110,7 +110,15 @@ sub parse_config_file {
$self->debug( 2, 'Loading in config file: ', $config_file ); $self->debug( 2, 'Loading in config file: ', $config_file );
return if ( !-e $config_file || !-r $config_file ); if ( !-e $config_file || !-r $config_file ){
croak(
App::ClusterSSH::Exception::Config->throw(
error => $self->loc(
'File [_1] does not exist or cannot be read', $config_file
),
),
);
}
open( CFG, $config_file ) or die("Couldnt open $config_file: $!"); open( CFG, $config_file ) or die("Couldnt open $config_file: $!");
my $l; my $l;

View file

@ -1,12 +1,14 @@
use strict; use strict;
use warnings; use warnings;
use FindBin qw($Bin); use FindBin qw($Bin $Script);
use lib "$Bin/../lib"; use lib "$Bin/../lib";
use Test::More; use Test::More;
use Test::Trap; use Test::Trap;
use Readonly;
BEGIN { use_ok("App::ClusterSSH::Config") } BEGIN { use_ok("App::ClusterSSH::Config") }
my $config; my $config;
@ -14,7 +16,7 @@ my $config;
$config = App::ClusterSSH::Config->new(); $config = App::ClusterSSH::Config->new();
isa_ok( $config, 'App::ClusterSSH::Config' ); isa_ok( $config, 'App::ClusterSSH::Config' );
my $default_config = { Readonly::Hash my %default_config => {
terminal => "xterm", terminal => "xterm",
terminal_args => "", terminal_args => "",
terminal_title_opt => "-T", terminal_title_opt => "-T",
@ -76,7 +78,8 @@ my $default_config = {
lang => 'en', lang => 'en',
}; };
is_deeply( $config, $default_config, 'default config is correct' ); my %expected = %default_config;
is_deeply( $config, \%expected, 'default config is correct' );
trap { trap {
$config = $config->validate_args( $config = $config->validate_args(
@ -89,12 +92,15 @@ is( $trap->die,
'Unknown configuration parameters: doesnt_exist,whoops', 'Unknown configuration parameters: doesnt_exist,whoops',
'got correct error message' 'got correct error message'
); );
is_deeply( $trap->die->unknown_config, is_deeply(
['doesnt_exist','whoops'], 'Picked up unknown config array' ); $trap->die->unknown_config,
[ 'doesnt_exist', 'whoops' ],
'Picked up unknown config array'
);
$default_config->{extra_cluster_file} = '/etc/filename'; $expected{extra_cluster_file} = '/etc/filename';
$default_config->{rsh_args} = 'some args'; $expected{rsh_args} = 'some args';
$default_config->{max_addhost_menu_cluster_items} = 120; $expected{max_addhost_menu_cluster_items} = 120;
trap { trap {
$config = $config->validate_args( $config = $config->validate_args(
extra_cluster_file => '/etc/filename', extra_cluster_file => '/etc/filename',
@ -103,6 +109,18 @@ trap {
); );
}; };
is( $trap->die, undef, 'validated ok' ); is( $trap->die, undef, 'validated ok' );
is_deeply( $config, $default_config, 'default config is correct' ); is_deeply( $config, \%expected, 'default config is correct' );
%expected = %default_config;
my $file = "$Bin/$Script.doesntexist";
trap {
$config = $config->parse_config_file( $file, );
};
isa_ok( $trap->die, 'App::ClusterSSH::Exception::Config' );
is( $trap->die,
"File $file does not exist or cannot be read",
'got correct error message'
);
done_testing(); done_testing();