4 use Test::More tests => 38;
7 use Test::NoWarnings 1.04 ':early';
9 use Test::Requires 'MooseX::ConfigFromFile';
15 with 'MooseX::Getopt';
16 with 'MooseX::ConfigFromFile';
18 has 'config_from_override' => (
24 has 'optional_from_config' => (
30 has 'required_from_config' => (
36 has 'required_from_argv' => (
42 sub get_config_from_file
44 my ( $class, $file ) = @_;
47 required_from_config => 'from_config_1',
48 optional_from_config => 'from_config_2',
51 my $cpath = File::Spec->canonpath('/notused/default');
52 if ( $file ne $cpath ) {
53 $config{config_from_override} = 1;
61 package App::DefaultConfigFile;
66 has '+configfile' => (
67 default => File::Spec->canonpath('/notused/default'),
72 package App::DefaultConfigFileCodeRef;
77 has '+configfile' => (
78 default => sub { return File::Spec->canonpath('/notused/default') },
84 local @ARGV = qw( --required_from_argv 1 );
86 like exception { App->new_with_options },
87 ($Getopt::Long::Descriptive::VERSION >= 0.091
88 ? qr/Mandatory parameter 'required_from_config' missing/
89 : 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 },
159 ($Getopt::Long::Descriptive::VERSION >= 0.091
160 ? qr/Mandatory parameter 'required_from_argv' missing/
161 : qr/Required option missing: required_from_argv/);
164 # Config file value overriden from cmdline
166 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
168 my $app = App->new_with_options;
169 isa_ok( $app, 'App' );
171 is( $app->required_from_config, 'override',
172 '... required_from_config is override as expected' );
174 is( $app->optional_from_config, 'from_config_2',
175 '... optional_from_config is from_config_2 as expected' );
180 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
182 my $app = App->new_with_options;
183 isa_ok( $app, 'App' );
185 is( $app->required_from_config, 'noconfig',
186 '... required_from_config is noconfig as expected' );
188 ok( !defined $app->optional_from_config,
189 '... optional_from_config is undef as expected' );
193 package BaseApp::WithConfig;
195 with 'MooseX::ConfigFromFile';
197 sub get_config_from_file { return {}; }
201 package DerivedApp::Getopt;
203 extends 'BaseApp::WithConfig';
204 with 'MooseX::Getopt';
207 # With DerivedApp, the Getopt role was applied at a different level
208 # than the ConfigFromFile role
210 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
216 is( $app->required_from_config, 'from_config_1',
217 '... required_from_config is from_config_1 as expected' );
219 is( $app->optional_from_config, 'from_config_2',
220 '... optional_from_config is from_config_2 as expected' );
222 is( $app->required_from_argv, '1',
223 '... required_from_argv is 1 as expected' );