From: Karen Etheridge Date: Fri, 8 Feb 2013 00:32:55 +0000 (-0800) Subject: include configfile=>$value to new() X-Git-Tag: v0.54~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bde911ddcfdec86961080d6292ff414753671793;p=gitmo%2FMooseX-Getopt.git include configfile=>$value to new() --- diff --git a/Changes b/Changes index 29a24f2..97dce7a 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,8 @@ Revision history for Perl extension MooseX-Getopt - fix broken tests on win32 with file comparisons - allow configfiles called "0" - support more mechanisms for overriding default configfile + - when using in conjunction with MooseX::ConfigFromFile, configfile value now + properly passed to new() 0.53 2013-02-05 09:59:00 PST-0800 - properly indicate optional dependency in tests using diff --git a/lib/MooseX/Getopt/Basic.pm b/lib/MooseX/Getopt/Basic.pm index 71de569..691f672 100644 --- a/lib/MooseX/Getopt/Basic.pm +++ b/lib/MooseX/Getopt/Basic.pm @@ -31,12 +31,12 @@ sub process_argv { $opt_parser->getoptions( "configfile=s" => \$configfile ); my $cfmeta = $class->meta->find_attribute_by_name('configfile'); + my $init_arg = $cfmeta->init_arg; # was it passed to the constructor? if (!defined $configfile) { - my $key = $cfmeta->init_arg; - $configfile = $constructor_params->{$key} if defined $key; + $configfile = $constructor_params->{$init_arg} if defined $init_arg; } if(!defined $configfile) { @@ -59,6 +59,9 @@ sub process_argv { die $_ unless /Specified configfile '\Q$configfile\E' does not exist/; }; } + + $constructor_params->{$init_arg} = $configfile + if defined $configfile and defined $init_arg; } else { $config_from_file = $class->get_config_from_file($configfile); diff --git a/t/008_configfromfile.t b/t/008_configfromfile.t index cb54a1c..3e420ac 100644 --- a/t/008_configfromfile.t +++ b/t/008_configfromfile.t @@ -2,11 +2,14 @@ use strict; use warnings; use Test::Requires { 'MooseX::ConfigFromFile' => '0.06' }; # skip all if not installed -use Test::More tests => 50; +use Test::More tests => 56; use Test::Fatal; +use Test::Deep '!blessed'; use Path::Tiny; +use Scalar::Util 'blessed'; use Test::NoWarnings 1.04 ':early'; +my %constructor_args; { package App; @@ -53,6 +56,13 @@ use Test::NoWarnings 1.04 ':early'; return \%config; } + + around BUILDARGS => sub + { + my ($orig, $class) = (shift, shift); + my $args = $class->$orig(@_); + $constructor_args{$class} = $args; + }; } { @@ -106,6 +116,14 @@ use Test::NoWarnings 1.04 ':early'; is( $app->configfile, path('/notused/default'), '... configfile is /notused/default as expected' ); + + cmp_deeply( + $constructor_args{blessed($app)}, + superhashof({ + configfile => str(path('/notused/default')), + }), + 'correct constructor args passed', + ); } { @@ -118,6 +136,14 @@ use Test::NoWarnings 1.04 ':early'; is( $app->configfile, path('/notused/default'), '... configfile is /notused/default as expected' ); + + cmp_deeply( + $constructor_args{blessed $app}, + superhashof({ + configfile => str(path('/notused/default')), + }), + 'correct constructor args passed', + ); } { @@ -130,6 +156,14 @@ use Test::NoWarnings 1.04 ':early'; is( $app->configfile, path('/notused/default'), '... configfile is /notused/default as expected' ); + + cmp_deeply( + $constructor_args{blessed $app}, + superhashof({ + configfile => str(path('/notused/default')), + }), + 'correct constructor args passed', + ); } } @@ -153,6 +187,14 @@ use Test::NoWarnings 1.04 ':early'; is( $app->configfile, path('/notused/override'), '... configfile is /notused/override as expected' ); + + cmp_deeply( + $constructor_args{blessed $app}, + superhashof({ + configfile => str(path('/notused/override')), + }), + 'correct constructor args passed', + ); } { my $app = App::DefaultConfigFileCodeRef->new_with_options; @@ -164,6 +206,14 @@ use Test::NoWarnings 1.04 ':early'; is( $app->configfile, path('/notused/override'), '... configfile is /notused/override as expected' ); + + cmp_deeply( + $constructor_args{blessed $app}, + superhashof({ + configfile => str(path('/notused/override')), + }), + 'correct constructor args passed', + ); } TODO: { my $app = App::ConfigFileWrapped->new_with_options; @@ -179,6 +229,14 @@ use Test::NoWarnings 1.04 ':early'; local $TODO = 'MooseX::ConfigFromFile needs fixes'; is( $app->configfile, path('/notused/override'), '... configfile is /notused as expected' ); + + cmp_deeply( + $constructor_args{blessed $app}, + superhashof({ + configfile => str(path('/notused/override')), + }), + 'correct constructor args passed', + ); } }