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 my $cpath = Path::Tiny::path('/notused/default');
51 if ( $file ne $cpath ) {
52 $config{config_from_override} = 1;
60 package App::DefaultConfigFile;
65 has '+configfile' => (
66 default => Path::Tiny::path('/notused/default')->stringify,
71 package App::DefaultConfigFileCodeRef;
76 has '+configfile' => (
77 default => sub { return Path::Tiny::path('/notused/default') },
83 local @ARGV = qw( --required_from_argv 1 );
85 like exception { App->new_with_options },
86 ($Getopt::Long::Descriptive::VERSION >= 0.091
87 ? qr/Mandatory parameter 'required_from_config' missing/
88 : qr/Required option missing: required_from_config/);
91 my $app = App::DefaultConfigFile->new_with_options;
92 isa_ok( $app, 'App::DefaultConfigFile' );
95 ok( !$app->config_from_override,
96 '... config_from_override false as expected' );
98 is( $app->configfile, path('/notused/default'),
99 '... configfile is /notused/default as expected' );
103 my $app = App::DefaultConfigFileCodeRef->new_with_options;
104 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
107 ok( !$app->config_from_override,
108 '... config_from_override false as expected' );
110 is( $app->configfile, path('/notused/default'),
111 '... configfile is /notused/default as expected' );
117 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
120 my $app = App->new_with_options;
121 isa_ok( $app, 'App' );
126 my $app = App::DefaultConfigFile->new_with_options;
127 isa_ok( $app, 'App::DefaultConfigFile' );
130 ok( $app->config_from_override,
131 '... config_from_override true as expected' );
133 is( $app->configfile, path('/notused'),
134 '... configfile is /notused as expected' );
137 my $app = App::DefaultConfigFileCodeRef->new_with_options;
138 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
141 ok( $app->config_from_override,
142 '... config_from_override true as expected' );
144 is( $app->configfile, path('/notused'),
145 '... configfile is /notused as expected' );
149 # Required arg not supplied from cmdline
151 local @ARGV = qw( --configfile /notused );
152 like exception { App->new_with_options },
153 ($Getopt::Long::Descriptive::VERSION >= 0.091
154 ? qr/Mandatory parameter 'required_from_argv' missing/
155 : qr/Required option missing: required_from_argv/);
158 # Config file value overriden from cmdline
160 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
162 my $app = App->new_with_options;
163 isa_ok( $app, 'App' );
165 is( $app->required_from_config, 'override',
166 '... required_from_config is override as expected' );
168 is( $app->optional_from_config, 'from_config_2',
169 '... optional_from_config is from_config_2 as expected' );
174 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
176 my $app = App->new_with_options;
177 isa_ok( $app, 'App' );
179 is( $app->required_from_config, 'noconfig',
180 '... required_from_config is noconfig as expected' );
182 ok( !defined $app->optional_from_config,
183 '... optional_from_config is undef as expected' );
187 package BaseApp::WithConfig;
189 with 'MooseX::ConfigFromFile';
191 sub get_config_from_file { return {}; }
195 package DerivedApp::Getopt;
197 extends 'BaseApp::WithConfig';
198 with 'MooseX::Getopt';
201 # With DerivedApp, the Getopt role was applied at a different level
202 # than the ConfigFromFile role
204 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
210 is( $app->required_from_config, 'from_config_1',
211 '... required_from_config is from_config_1 as expected' );
213 is( $app->optional_from_config, 'from_config_2',
214 '... optional_from_config is from_config_2 as expected' );
216 is( $app->required_from_argv, '1',
217 '... required_from_argv is 1 as expected' );