4 use Test::More tests => 37;
9 'MooseX::ConfigFromFile' => 0.01, # skip all if not installed
16 with 'MooseX::Getopt';
17 with 'MooseX::ConfigFromFile';
19 has 'config_from_override' => (
25 has 'optional_from_config' => (
31 has 'required_from_config' => (
37 has 'required_from_argv' => (
43 sub get_config_from_file
45 my ( $class, $file ) = @_;
48 required_from_config => 'from_config_1',
49 optional_from_config => 'from_config_2',
52 my $cpath = File::Spec->canonpath('/notused/default');
53 if ( $file ne $cpath ) {
54 $config{config_from_override} = 1;
62 package App::DefaultConfigFile;
67 has '+configfile' => (
68 default => File::Spec->canonpath('/notused/default'),
73 package App::DefaultConfigFileCodeRef;
78 has '+configfile' => (
79 default => sub { return File::Spec->canonpath('/notused/default') },
85 local @ARGV = qw( --required_from_argv 1 );
87 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
88 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_config' missing/;
91 like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
95 my $app = App::DefaultConfigFile->new_with_options;
96 isa_ok( $app, 'App::DefaultConfigFile' );
99 ok( !$app->config_from_override,
100 '... config_from_override false as expected' );
102 is( $app->configfile, File::Spec->canonpath('/notused/default'),
103 '... configfile is /notused/default as expected' );
107 # No config specified
109 local @ARGV = qw( --required_from_argv 1 );
112 my $app = App::DefaultConfigFileCodeRef->new_with_options;
113 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
116 ok( !$app->config_from_override,
117 '... config_from_override false as expected' );
119 is( $app->configfile, File::Spec->canonpath('/notused/default'),
120 '... configfile is /notused/default as expected' );
126 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
129 my $app = App->new_with_options;
130 isa_ok( $app, 'App' );
135 my $app = App::DefaultConfigFile->new_with_options;
136 isa_ok( $app, 'App::DefaultConfigFile' );
139 ok( $app->config_from_override,
140 '... config_from_override true as expected' );
142 is( $app->configfile, File::Spec->canonpath('/notused'),
143 '... configfile is /notused as expected' );
146 my $app = App::DefaultConfigFileCodeRef->new_with_options;
147 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
150 ok( $app->config_from_override,
151 '... config_from_override true as expected' );
153 is( $app->configfile, File::Spec->canonpath('/notused'),
154 '... configfile is /notused as expected' );
158 # Required arg not supplied from cmdline
160 local @ARGV = qw( --configfile /notused );
161 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
162 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_argv' missing/;
165 like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
169 # Config file value overriden from cmdline
171 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
173 my $app = App->new_with_options;
174 isa_ok( $app, 'App' );
176 is( $app->required_from_config, 'override',
177 '... required_from_config is override as expected' );
179 is( $app->optional_from_config, 'from_config_2',
180 '... optional_from_config is from_config_2 as expected' );
185 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
187 my $app = App->new_with_options;
188 isa_ok( $app, 'App' );
190 is( $app->required_from_config, 'noconfig',
191 '... required_from_config is noconfig as expected' );
193 ok( !defined $app->optional_from_config,
194 '... optional_from_config is undef as expected' );
198 package BaseApp::WithConfig;
200 with 'MooseX::ConfigFromFile';
202 sub get_config_from_file { return {}; }
206 package DerivedApp::Getopt;
208 extends 'BaseApp::WithConfig';
209 with 'MooseX::Getopt';
212 # With DerivedApp, the Getopt role was applied at a different level
213 # than the ConfigFromFile role
215 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
221 is( $app->required_from_config, 'from_config_1',
222 '... required_from_config is from_config_1 as expected' );
224 is( $app->optional_from_config, 'from_config_2',
225 '... optional_from_config is from_config_2 as expected' );
227 is( $app->required_from_argv, '1',
228 '... required_from_argv is 1 as expected' );