4 use Test::Requires { 'MooseX::ConfigFromFile' => '0.06' }; # skip all if not installed
5 use Test::More tests => 50;
8 use Test::NoWarnings 1.04 ':early';
14 with 'MooseX::Getopt';
15 with 'MooseX::ConfigFromFile';
17 has 'config_from_override' => (
23 has 'optional_from_config' => (
29 has 'required_from_config' => (
35 has 'required_from_argv' => (
41 sub get_config_from_file
43 my ( $class, $file ) = @_;
46 required_from_config => 'from_config_1',
47 optional_from_config => 'from_config_2',
50 if ( $file ne Path::Tiny::path('/notused/default') ) {
51 $config{config_from_override} = 1;
59 package App::DefaultConfigFile;
64 has '+configfile' => (
65 default => Path::Tiny::path('/notused/default')->stringify,
70 package App::DefaultConfigFileCodeRef;
75 has '+configfile' => (
76 default => sub { return Path::Tiny::path('/notused/default') },
81 package App::ConfigFileWrapped;
86 around configfile => sub { '/notused/default' };
92 local @ARGV = qw( --required_from_argv 1 );
94 like exception { App->new_with_options },
95 ($Getopt::Long::Descriptive::VERSION >= 0.091
96 ? qr/Mandatory parameter 'required_from_config' missing/
97 : qr/Required option missing: required_from_config/);
100 my $app = App::DefaultConfigFile->new_with_options;
101 isa_ok( $app, 'App::DefaultConfigFile' );
104 ok( !$app->config_from_override,
105 '... config_from_override false as expected' );
107 is( $app->configfile, path('/notused/default'),
108 '... configfile is /notused/default as expected' );
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, path('/notused/default'),
120 '... configfile is /notused/default as expected' );
124 my $app = App::ConfigFileWrapped->new_with_options;
125 isa_ok( $app, 'App::ConfigFileWrapped' );
128 ok( !$app->config_from_override,
129 '... config_from_override false as expected' );
131 is( $app->configfile, path('/notused/default'),
132 '... configfile is /notused/default as expected' );
138 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 );
141 my $app = App->new_with_options;
142 isa_ok( $app, 'App' );
147 my $app = App::DefaultConfigFile->new_with_options;
148 isa_ok( $app, 'App::DefaultConfigFile' );
151 ok( $app->config_from_override,
152 '... config_from_override true as expected' );
154 is( $app->configfile, path('/notused/override'),
155 '... configfile is /notused/override as expected' );
158 my $app = App::DefaultConfigFileCodeRef->new_with_options;
159 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
162 ok( $app->config_from_override,
163 '... config_from_override true as expected' );
165 is( $app->configfile, path('/notused/override'),
166 '... configfile is /notused/override as expected' );
169 my $app = App::ConfigFileWrapped->new_with_options;
170 isa_ok( $app, 'App::ConfigFileWrapped' );
173 ok( $app->config_from_override,
174 '... config_from_override true as expected' );
176 # FIXME - in order for this to work, we need to fix CFF so the
177 # configfile method always returns the actual value of the attribute,
178 # not the default sub thingy.
179 local $TODO = 'MooseX::ConfigFromFile needs fixes';
180 is( $app->configfile, path('/notused/override'),
181 '... configfile is /notused as expected' );
185 # Required arg not supplied from cmdline
187 local @ARGV = qw( --configfile /notused/override );
188 like exception { App->new_with_options },
189 ($Getopt::Long::Descriptive::VERSION >= 0.091
190 ? qr/Mandatory parameter 'required_from_argv' missing/
191 : qr/Required option missing: required_from_argv/);
194 # Config file value overriden from cmdline
196 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 --required_from_config override );
198 my $app = App->new_with_options;
199 isa_ok( $app, 'App' );
201 is( $app->required_from_config, 'override',
202 '... required_from_config is override as expected' );
204 is( $app->optional_from_config, 'from_config_2',
205 '... optional_from_config is from_config_2 as expected' );
210 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
212 my $app = App->new_with_options;
213 isa_ok( $app, 'App' );
215 is( $app->required_from_config, 'noconfig',
216 '... required_from_config is noconfig as expected' );
218 ok( !defined $app->optional_from_config,
219 '... optional_from_config is undef as expected' );
223 package BaseApp::WithConfig;
225 with 'MooseX::ConfigFromFile';
227 sub get_config_from_file { return {}; }
231 package DerivedApp::Getopt;
233 extends 'BaseApp::WithConfig';
234 with 'MooseX::Getopt';
237 # With DerivedApp, the Getopt role was applied at a different level
238 # than the ConfigFromFile role
240 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
246 is( $app->required_from_config, 'from_config_1',
247 '... required_from_config is from_config_1 as expected' );
249 is( $app->optional_from_config, 'from_config_2',
250 '... optional_from_config is from_config_2 as expected' );
252 is( $app->required_from_argv, '1',
253 '... required_from_argv is 1 as expected' );