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 like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
92 my $app = App::DefaultConfigFile->new_with_options;
93 isa_ok( $app, 'App::DefaultConfigFile' );
96 ok( !$app->config_from_override,
97 '... config_from_override false as expected' );
99 is( $app->configfile, File::Spec->canonpath('/notused/default'),
100 '... configfile is /notused/default as expected' );
104 # No config specified
106 local @ARGV = qw( --required_from_argv 1 );
109 my $app = App::DefaultConfigFileCodeRef->new_with_options;
110 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
113 ok( !$app->config_from_override,
114 '... config_from_override false as expected' );
116 is( $app->configfile, File::Spec->canonpath('/notused/default'),
117 '... configfile is /notused/default as expected' );
123 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
126 my $app = App->new_with_options;
127 isa_ok( $app, 'App' );
132 my $app = App::DefaultConfigFile->new_with_options;
133 isa_ok( $app, 'App::DefaultConfigFile' );
136 ok( $app->config_from_override,
137 '... config_from_override true as expected' );
139 is( $app->configfile, File::Spec->canonpath('/notused'),
140 '... configfile is /notused as expected' );
143 my $app = App::DefaultConfigFileCodeRef->new_with_options;
144 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
147 ok( $app->config_from_override,
148 '... config_from_override true as expected' );
150 is( $app->configfile, File::Spec->canonpath('/notused'),
151 '... configfile is /notused as expected' );
155 # Required arg not supplied from cmdline
157 local @ARGV = qw( --configfile /notused );
158 like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
161 # Config file value overriden from cmdline
163 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
165 my $app = App->new_with_options;
166 isa_ok( $app, 'App' );
168 is( $app->required_from_config, 'override',
169 '... required_from_config is override as expected' );
171 is( $app->optional_from_config, 'from_config_2',
172 '... optional_from_config is from_config_2 as expected' );
177 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
179 my $app = App->new_with_options;
180 isa_ok( $app, 'App' );
182 is( $app->required_from_config, 'noconfig',
183 '... required_from_config is noconfig as expected' );
185 ok( !defined $app->optional_from_config,
186 '... optional_from_config is undef as expected' );
190 package BaseApp::WithConfig;
192 with 'MooseX::ConfigFromFile';
194 sub get_config_from_file { return {}; }
198 package DerivedApp::Getopt;
200 extends 'BaseApp::WithConfig';
201 with 'MooseX::Getopt';
204 # With DerivedApp, the Getopt role was applied at a different level
205 # than the ConfigFromFile role
207 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
213 is( $app->required_from_config, 'from_config_1',
214 '... required_from_config is from_config_1 as expected' );
216 is( $app->optional_from_config, 'from_config_2',
217 '... optional_from_config is from_config_2 as expected' );
219 is( $app->required_from_argv, '1',
220 '... required_from_argv is 1 as expected' );