4 use Test::More tests => 38;
7 use Test::NoWarnings 1.04 ':early';
10 'MooseX::ConfigFromFile' => 0.01, # skip all if not installed
17 with 'MooseX::Getopt';
18 with 'MooseX::ConfigFromFile';
20 has 'config_from_override' => (
26 has 'optional_from_config' => (
32 has 'required_from_config' => (
38 has 'required_from_argv' => (
44 sub get_config_from_file
46 my ( $class, $file ) = @_;
49 required_from_config => 'from_config_1',
50 optional_from_config => 'from_config_2',
53 my $cpath = File::Spec->canonpath('/notused/default');
54 if ( $file ne $cpath ) {
55 $config{config_from_override} = 1;
63 package App::DefaultConfigFile;
68 has '+configfile' => (
69 default => File::Spec->canonpath('/notused/default'),
74 package App::DefaultConfigFileCodeRef;
79 has '+configfile' => (
80 default => sub { return File::Spec->canonpath('/notused/default') },
86 local @ARGV = qw( --required_from_argv 1 );
88 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
89 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_config' missing/;
92 like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
96 my $app = App::DefaultConfigFile->new_with_options;
97 isa_ok( $app, 'App::DefaultConfigFile' );
100 ok( !$app->config_from_override,
101 '... config_from_override false as expected' );
103 is( $app->configfile, File::Spec->canonpath('/notused/default'),
104 '... configfile is /notused/default as expected' );
108 # No config specified
110 local @ARGV = qw( --required_from_argv 1 );
113 my $app = App::DefaultConfigFileCodeRef->new_with_options;
114 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
117 ok( !$app->config_from_override,
118 '... config_from_override false as expected' );
120 is( $app->configfile, File::Spec->canonpath('/notused/default'),
121 '... configfile is /notused/default as expected' );
127 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
130 my $app = App->new_with_options;
131 isa_ok( $app, 'App' );
136 my $app = App::DefaultConfigFile->new_with_options;
137 isa_ok( $app, 'App::DefaultConfigFile' );
140 ok( $app->config_from_override,
141 '... config_from_override true as expected' );
143 is( $app->configfile, File::Spec->canonpath('/notused'),
144 '... configfile is /notused as expected' );
147 my $app = App::DefaultConfigFileCodeRef->new_with_options;
148 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
151 ok( $app->config_from_override,
152 '... config_from_override true as expected' );
154 is( $app->configfile, File::Spec->canonpath('/notused'),
155 '... configfile is /notused as expected' );
159 # Required arg not supplied from cmdline
161 local @ARGV = qw( --configfile /notused );
162 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
163 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_argv' missing/;
166 like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
170 # Config file value overriden from cmdline
172 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
174 my $app = App->new_with_options;
175 isa_ok( $app, 'App' );
177 is( $app->required_from_config, 'override',
178 '... required_from_config is override as expected' );
180 is( $app->optional_from_config, 'from_config_2',
181 '... optional_from_config is from_config_2 as expected' );
186 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
188 my $app = App->new_with_options;
189 isa_ok( $app, 'App' );
191 is( $app->required_from_config, 'noconfig',
192 '... required_from_config is noconfig as expected' );
194 ok( !defined $app->optional_from_config,
195 '... optional_from_config is undef as expected' );
199 package BaseApp::WithConfig;
201 with 'MooseX::ConfigFromFile';
203 sub get_config_from_file { return {}; }
207 package DerivedApp::Getopt;
209 extends 'BaseApp::WithConfig';
210 with 'MooseX::Getopt';
213 # With DerivedApp, the Getopt role was applied at a different level
214 # than the ConfigFromFile role
216 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
222 is( $app->required_from_config, 'from_config_1',
223 '... required_from_config is from_config_1 as expected' );
225 is( $app->optional_from_config, 'from_config_2',
226 '... optional_from_config is from_config_2 as expected' );
228 is( $app->required_from_argv, '1',
229 '... required_from_argv is 1 as expected' );