10 if ( !eval { require MooseX::ConfigFromFile } )
12 plan skip_all => 'Test requires MooseX::ConfigFromFile';
19 with 'MooseX::Getopt';
20 with 'MooseX::ConfigFromFile';
22 has 'config_from_override' => (
28 has 'optional_from_config' => (
34 has 'required_from_config' => (
40 has 'required_from_argv' => (
46 sub get_config_from_file
48 my ( $class, $file ) = @_;
51 required_from_config => 'from_config_1',
52 optional_from_config => 'from_config_2',
55 my $cpath = File::Spec->canonpath('/notused/default');
56 if ( $file ne $cpath ) {
57 $config{config_from_override} = 1;
65 package App::DefaultConfigFile;
70 has '+configfile' => (
71 default => File::Spec->canonpath('/notused/default'),
76 package App::DefaultConfigFileCodeRef;
81 has '+configfile' => (
82 default => sub { return File::Spec->canonpath('/notused/default') },
87 package App::ConfigFileFromProjectOption;
97 has '+configfile' => ();
99 around _get_configfile_from_cli => sub {
100 my ($orig, $self) = @_;
102 my $opt_parser = Getopt::Long::Parser->new( config => [ qw( pass_through ) ] );
103 $opt_parser->getoptions( "project=s" => \$project );
104 return "/notused/specific/$project/config";
108 # No config specified
110 local @ARGV = qw( --required_from_argv 1 );
112 throws_ok { App->new_with_options } qr/(Required option missing: required_from_config|Attribute \(required_from_config\) is required)/;
115 my $app = App::DefaultConfigFile->new_with_options;
116 isa_ok( $app, 'App::DefaultConfigFile' );
119 ok( !$app->config_from_override,
120 '... config_from_override false as expected' );
122 is( $app->configfile, File::Spec->canonpath('/notused/default'),
123 '... configfile is /notused/default as expected' );
127 # No config specified
129 local @ARGV = qw( --required_from_argv 1 );
132 my $app = App::DefaultConfigFileCodeRef->new_with_options;
133 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
136 ok( !$app->config_from_override,
137 '... config_from_override false as expected' );
139 is( $app->configfile, File::Spec->canonpath('/notused/default'),
140 '... configfile is /notused/default as expected' );
146 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
149 my $app = App->new_with_options;
150 isa_ok( $app, 'App' );
155 my $app = App::DefaultConfigFile->new_with_options;
156 isa_ok( $app, 'App::DefaultConfigFile' );
159 ok( $app->config_from_override,
160 '... config_from_override true as expected' );
162 is( $app->configfile, File::Spec->canonpath('/notused'),
163 '... configfile is /notused as expected' );
166 my $app = App::DefaultConfigFileCodeRef->new_with_options;
167 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
170 ok( $app->config_from_override,
171 '... config_from_override true as expected' );
173 is( $app->configfile, File::Spec->canonpath('/notused'),
174 '... configfile is /notused as expected' );
178 # No config specified
180 local @ARGV = qw( --required_from_argv 1 --project quux );
183 my $app = App::ConfigFileFromProjectOption->new_with_options;
184 isa_ok( $app, 'App::ConfigFileFromProjectOption' );
187 ok( $app->config_from_override,
188 '... config_from_override true as expected' );
190 is( $app->project, 'quux',
191 '... project is quux as expected' );
193 is( $app->configfile, File::Spec->canonpath('/notused/specific/quux/config'),
194 '... configfile is /notused/specific/quux/config as expected' );
200 local @ARGV = qw( --configfile /notused --required_from_argv 1 --project quux );
203 my $app = App::ConfigFileFromProjectOption->new_with_options;
204 isa_ok( $app, 'App::ConfigFileFromProjectOption' );
207 ok( $app->config_from_override,
208 '... config_from_override true as expected' );
210 is( $app->project, 'quux',
211 '... project is quux as expected' );
213 is( $app->configfile, File::Spec->canonpath('/notused'),
214 '... configfile is /notused as expected' );
218 # Required arg not supplied from cmdline
220 local @ARGV = qw( --configfile /notused );
221 throws_ok { App->new_with_options } qr/(Required option missing: required_from_argv|Attribute \(required_from_argv\) is required)/;
224 # Config file value overriden from cmdline
226 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
228 my $app = App->new_with_options;
229 isa_ok( $app, 'App' );
231 is( $app->required_from_config, 'override',
232 '... required_from_config is override as expected' );
234 is( $app->optional_from_config, 'from_config_2',
235 '... optional_from_config is from_config_2 as expected' );
240 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
242 my $app = App->new_with_options;
243 isa_ok( $app, 'App' );
245 is( $app->required_from_config, 'noconfig',
246 '... required_from_config is noconfig as expected' );
248 ok( !defined $app->optional_from_config,
249 '... optional_from_config is undef as expected' );
253 package BaseApp::WithConfig;
255 with 'MooseX::ConfigFromFile';
257 sub get_config_from_file { return {}; }
261 package DerivedApp::Getopt;
263 extends 'BaseApp::WithConfig';
264 with 'MooseX::Getopt';
267 # With DerivedApp, the Getopt role was applied at a different level
268 # than the ConfigFromFile role
270 lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
276 is( $app->required_from_config, 'from_config_1',
277 '... required_from_config is from_config_1 as expected' );
279 is( $app->optional_from_config, 'from_config_2',
280 '... optional_from_config is from_config_2 as expected' );
282 is( $app->required_from_argv, '1',
283 '... required_from_argv is 1 as expected' );