10 if ( !eval { require MooseX::ConfigFromFile } )
12 plan skip_all => 'Test requires MooseX::ConfigFromFile';
23 with 'MooseX::Getopt';
24 with 'MooseX::ConfigFromFile';
26 has 'config_from_override' => (
32 has 'optional_from_config' => (
38 has 'required_from_config' => (
44 has 'required_from_argv' => (
50 sub get_config_from_file
52 my ( $class, $file ) = @_;
55 required_from_config => 'from_config_1',
56 optional_from_config => 'from_config_2',
59 my $cpath = File::Spec->canonpath('/notused/default');
60 if ( $file ne $cpath ) {
61 $config{config_from_override} = 1;
69 package App::DefaultConfigFile;
74 has '+configfile' => (
75 default => File::Spec->canonpath('/notused/default'),
80 package App::DefaultConfigFileCodeRef;
85 has '+configfile' => (
86 default => sub { return File::Spec->canonpath('/notused/default') },
92 local @ARGV = qw( --required_from_argv 1 );
94 throws_ok { 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 throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
166 # Config file value overriden from cmdline
168 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
170 my $app = App->new_with_options;
171 isa_ok( $app, 'App' );
173 is( $app->required_from_config, 'override',
174 '... required_from_config is override as expected' );
176 is( $app->optional_from_config, 'from_config_2',
177 '... optional_from_config is from_config_2 as expected' );
182 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
184 my $app = App->new_with_options;
185 isa_ok( $app, 'App' );
187 is( $app->required_from_config, 'noconfig',
188 '... required_from_config is noconfig as expected' );
190 ok( !defined $app->optional_from_config,
191 '... optional_from_config is undef as expected' );
195 package BaseApp::WithConfig;
197 with 'MooseX::ConfigFromFile';
199 sub get_config_from_file { return {}; }
203 package DerivedApp::Getopt;
205 extends 'BaseApp::WithConfig';
206 with 'MooseX::Getopt';
209 # With DerivedApp, the Getopt role was applied at a different level
210 # than the ConfigFromFile role
212 lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
218 is( $app->required_from_config, 'from_config_1',
219 '... required_from_config is from_config_1 as expected' );
221 is( $app->optional_from_config, 'from_config_2',
222 '... optional_from_config is from_config_2 as expected' );
224 is( $app->required_from_argv, '1',
225 '... required_from_argv is 1 as expected' );