4 use Test::Requires { 'MooseX::ConfigFromFile' => '0.06' }; # skip all if not installed
5 use Test::More tests => 38;
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') },
82 local @ARGV = qw( --required_from_argv 1 );
84 like exception { App->new_with_options },
85 ($Getopt::Long::Descriptive::VERSION >= 0.091
86 ? qr/Mandatory parameter 'required_from_config' missing/
87 : qr/Required option missing: required_from_config/);
90 my $app = App::DefaultConfigFile->new_with_options;
91 isa_ok( $app, 'App::DefaultConfigFile' );
94 ok( !$app->config_from_override,
95 '... config_from_override false as expected' );
97 is( $app->configfile, path('/notused/default'),
98 '... configfile is /notused/default as expected' );
102 my $app = App::DefaultConfigFileCodeRef->new_with_options;
103 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
106 ok( !$app->config_from_override,
107 '... config_from_override false as expected' );
109 is( $app->configfile, path('/notused/default'),
110 '... configfile is /notused/default as expected' );
116 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 );
119 my $app = App->new_with_options;
120 isa_ok( $app, 'App' );
125 my $app = App::DefaultConfigFile->new_with_options;
126 isa_ok( $app, 'App::DefaultConfigFile' );
129 ok( $app->config_from_override,
130 '... config_from_override true as expected' );
132 is( $app->configfile, path('/notused/override'),
133 '... configfile is /notused/override as expected' );
136 my $app = App::DefaultConfigFileCodeRef->new_with_options;
137 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
140 ok( $app->config_from_override,
141 '... config_from_override true as expected' );
143 is( $app->configfile, path('/notused'),
144 '... configfile is /notused as expected' );
148 # Required arg not supplied from cmdline
150 local @ARGV = qw( --configfile /notused/override );
151 like exception { App->new_with_options },
152 ($Getopt::Long::Descriptive::VERSION >= 0.091
153 ? qr/Mandatory parameter 'required_from_argv' missing/
154 : qr/Required option missing: required_from_argv/);
157 # Config file value overriden from cmdline
159 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 --required_from_config override );
161 my $app = App->new_with_options;
162 isa_ok( $app, 'App' );
164 is( $app->required_from_config, 'override',
165 '... required_from_config is override as expected' );
167 is( $app->optional_from_config, 'from_config_2',
168 '... optional_from_config is from_config_2 as expected' );
173 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
175 my $app = App->new_with_options;
176 isa_ok( $app, 'App' );
178 is( $app->required_from_config, 'noconfig',
179 '... required_from_config is noconfig as expected' );
181 ok( !defined $app->optional_from_config,
182 '... optional_from_config is undef as expected' );
186 package BaseApp::WithConfig;
188 with 'MooseX::ConfigFromFile';
190 sub get_config_from_file { return {}; }
194 package DerivedApp::Getopt;
196 extends 'BaseApp::WithConfig';
197 with 'MooseX::Getopt';
200 # With DerivedApp, the Getopt role was applied at a different level
201 # than the ConfigFromFile role
203 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
209 is( $app->required_from_config, 'from_config_1',
210 '... required_from_config is from_config_1 as expected' );
212 is( $app->optional_from_config, 'from_config_2',
213 '... optional_from_config is from_config_2 as expected' );
215 is( $app->required_from_argv, '1',
216 '... required_from_argv is 1 as expected' );