6 use Test::More tests => 37;
11 'MooseX::ConfigFromFile' => 0.01, # skip all if not installed
18 with 'MooseX::Getopt';
19 with 'MooseX::ConfigFromFile';
21 has 'config_from_override' => (
27 has 'optional_from_config' => (
33 has 'required_from_config' => (
39 has 'required_from_argv' => (
45 sub get_config_from_file
47 my ( $class, $file ) = @_;
50 required_from_config => 'from_config_1',
51 optional_from_config => 'from_config_2',
54 my $cpath = File::Spec->canonpath('/notused/default');
55 if ( $file ne $cpath ) {
56 $config{config_from_override} = 1;
64 package App::DefaultConfigFile;
69 has '+configfile' => (
70 default => File::Spec->canonpath('/notused/default'),
75 package App::DefaultConfigFileCodeRef;
80 has '+configfile' => (
81 default => sub { return File::Spec->canonpath('/notused/default') },
87 local @ARGV = qw( --required_from_argv 1 );
89 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
90 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_config' missing/;
93 like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
97 my $app = App::DefaultConfigFile->new_with_options;
98 isa_ok( $app, 'App::DefaultConfigFile' );
101 ok( !$app->config_from_override,
102 '... config_from_override false as expected' );
104 is( $app->configfile, File::Spec->canonpath('/notused/default'),
105 '... configfile is /notused/default as expected' );
109 # No config specified
111 local @ARGV = qw( --required_from_argv 1 );
114 my $app = App::DefaultConfigFileCodeRef->new_with_options;
115 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
118 ok( !$app->config_from_override,
119 '... config_from_override false as expected' );
121 is( $app->configfile, File::Spec->canonpath('/notused/default'),
122 '... configfile is /notused/default as expected' );
128 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
131 my $app = App->new_with_options;
132 isa_ok( $app, 'App' );
137 my $app = App::DefaultConfigFile->new_with_options;
138 isa_ok( $app, 'App::DefaultConfigFile' );
141 ok( $app->config_from_override,
142 '... config_from_override true as expected' );
144 is( $app->configfile, File::Spec->canonpath('/notused'),
145 '... configfile is /notused as expected' );
148 my $app = App::DefaultConfigFileCodeRef->new_with_options;
149 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
152 ok( $app->config_from_override,
153 '... config_from_override true as expected' );
155 is( $app->configfile, File::Spec->canonpath('/notused'),
156 '... configfile is /notused as expected' );
160 # Required arg not supplied from cmdline
162 local @ARGV = qw( --configfile /notused );
163 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
164 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_argv' missing/;
167 like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
171 # Config file value overriden from cmdline
173 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
175 my $app = App->new_with_options;
176 isa_ok( $app, 'App' );
178 is( $app->required_from_config, 'override',
179 '... required_from_config is override as expected' );
181 is( $app->optional_from_config, 'from_config_2',
182 '... optional_from_config is from_config_2 as expected' );
187 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
189 my $app = App->new_with_options;
190 isa_ok( $app, 'App' );
192 is( $app->required_from_config, 'noconfig',
193 '... required_from_config is noconfig as expected' );
195 ok( !defined $app->optional_from_config,
196 '... optional_from_config is undef as expected' );
200 package BaseApp::WithConfig;
202 with 'MooseX::ConfigFromFile';
204 sub get_config_from_file { return {}; }
208 package DerivedApp::Getopt;
210 extends 'BaseApp::WithConfig';
211 with 'MooseX::Getopt';
214 # With DerivedApp, the Getopt role was applied at a different level
215 # than the ConfigFromFile role
217 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
223 is( $app->required_from_config, 'from_config_1',
224 '... required_from_config is from_config_1 as expected' );
226 is( $app->optional_from_config, 'from_config_2',
227 '... optional_from_config is from_config_2 as expected' );
229 is( $app->required_from_argv, '1',
230 '... required_from_argv is 1 as expected' );