4 use Test::Requires 'MooseX::ConfigFromFile'; # skip all if not installed
5 use Test::More tests => 56;
7 use Test::Deep '!blessed';
9 use Scalar::Util 'blessed';
10 use Test::NoWarnings 1.04 ':early';
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 if ( $file ne Path::Tiny::path('/notused/default') ) {
54 $config{config_from_override} = 1;
60 around BUILDARGS => sub
62 my ($orig, $class) = (shift, shift);
63 my $args = $class->$orig(@_);
64 $constructor_args{$class} = $args;
69 package App::DefaultConfigFile;
74 has '+configfile' => (
75 default => Path::Tiny::path('/notused/default')->stringify,
80 package App::DefaultConfigFileCodeRef;
85 has '+configfile' => (
86 default => sub { return Path::Tiny::path('/notused/default')->stringify },
91 package App::ConfigFileWrapped;
96 sub _get_default_configfile { '/notused/default' }
100 # No config specified
102 local @ARGV = qw( --required_from_argv 1 );
104 like exception { App->new_with_options },
105 ($Getopt::Long::Descriptive::VERSION >= 0.091
106 ? qr/Mandatory parameter 'required_from_config' missing/
107 : qr/Required option missing: required_from_config/);
110 my $app = App::DefaultConfigFile->new_with_options;
111 isa_ok( $app, 'App::DefaultConfigFile' );
114 ok( !$app->config_from_override,
115 '... config_from_override false as expected' );
117 is( $app->configfile, path('/notused/default'),
118 '... configfile is /notused/default as expected' );
121 $constructor_args{blessed($app)},
123 configfile => str(path('/notused/default')),
125 'correct constructor args passed',
130 my $app = App::DefaultConfigFileCodeRef->new_with_options;
131 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
134 ok( !$app->config_from_override,
135 '... config_from_override false as expected' );
137 is( $app->configfile, path('/notused/default'),
138 '... configfile is /notused/default as expected' );
141 $constructor_args{blessed $app},
143 configfile => str(path('/notused/default')),
145 'correct constructor args passed',
150 eval "use MooseX::ConfigFromFile 0.08 (); 1;";
151 diag("MooseX::ConfigFromFile 0.08 needed to test this use of configfile defaults"),
152 skip "MooseX::ConfigFromFile 0.08 needed to test this use of configfile defaults", 7 if $@;
154 my $app = App::ConfigFileWrapped->new_with_options;
155 isa_ok( $app, 'App::ConfigFileWrapped' );
158 ok( !$app->config_from_override,
159 '... config_from_override false as expected' );
161 is( $app->configfile, path('/notused/default'),
162 '... configfile is /notused/default as expected' );
165 $constructor_args{blessed $app},
167 configfile => str(path('/notused/default')),
169 'correct constructor args passed',
176 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 );
179 my $app = App->new_with_options;
180 isa_ok( $app, 'App' );
185 my $app = App::DefaultConfigFile->new_with_options;
186 isa_ok( $app, 'App::DefaultConfigFile' );
189 ok( $app->config_from_override,
190 '... config_from_override true as expected' );
192 is( $app->configfile, path('/notused/override'),
193 '... configfile is /notused/override as expected' );
196 $constructor_args{blessed $app},
198 configfile => str(path('/notused/override')),
200 'correct constructor args passed',
204 my $app = App::DefaultConfigFileCodeRef->new_with_options;
205 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
208 ok( $app->config_from_override,
209 '... config_from_override true as expected' );
211 is( $app->configfile, path('/notused/override'),
212 '... configfile is /notused/override as expected' );
215 $constructor_args{blessed $app},
217 configfile => str(path('/notused/override')),
219 'correct constructor args passed',
223 my $app = App::ConfigFileWrapped->new_with_options;
224 isa_ok( $app, 'App::ConfigFileWrapped' );
227 ok( $app->config_from_override,
228 '... config_from_override true as expected' );
230 is( $app->configfile, path('/notused/override'),
231 '... configfile is /notused as expected' );
234 $constructor_args{blessed $app},
236 configfile => str(path('/notused/override')),
238 'correct constructor args passed',
243 # Required arg not supplied from cmdline
245 local @ARGV = qw( --configfile /notused/override );
246 like exception { App->new_with_options },
247 ($Getopt::Long::Descriptive::VERSION >= 0.091
248 ? qr/Mandatory parameter 'required_from_argv' missing/
249 : qr/Required option missing: required_from_argv/);
252 # Config file value overriden from cmdline
254 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 --required_from_config override );
256 my $app = App->new_with_options;
257 isa_ok( $app, 'App' );
259 is( $app->required_from_config, 'override',
260 '... required_from_config is override as expected' );
262 is( $app->optional_from_config, 'from_config_2',
263 '... optional_from_config is from_config_2 as expected' );
268 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
270 my $app = App->new_with_options;
271 isa_ok( $app, 'App' );
273 is( $app->required_from_config, 'noconfig',
274 '... required_from_config is noconfig as expected' );
276 ok( !defined $app->optional_from_config,
277 '... optional_from_config is undef as expected' );
281 package BaseApp::WithConfig;
283 with 'MooseX::ConfigFromFile';
285 sub get_config_from_file { return {}; }
289 package DerivedApp::Getopt;
291 extends 'BaseApp::WithConfig';
292 with 'MooseX::Getopt';
295 # With DerivedApp, the Getopt role was applied at a different level
296 # than the ConfigFromFile role
298 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
304 is( $app->required_from_config, 'from_config_1',
305 '... required_from_config is from_config_1 as expected' );
307 is( $app->optional_from_config, 'from_config_2',
308 '... optional_from_config is from_config_2 as expected' );
310 is( $app->required_from_argv, '1',
311 '... required_from_argv is 1 as expected' );