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 # No config specified
105 local @ARGV = qw( --required_from_argv 1 );
108 my $app = App::DefaultConfigFileCodeRef->new_with_options;
109 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
112 ok( !$app->config_from_override,
113 '... config_from_override false as expected' );
115 is( $app->configfile, path('/notused/default'),
116 '... configfile is /notused/default as expected' );
122 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
125 my $app = App->new_with_options;
126 isa_ok( $app, 'App' );
131 my $app = App::DefaultConfigFile->new_with_options;
132 isa_ok( $app, 'App::DefaultConfigFile' );
135 ok( $app->config_from_override,
136 '... config_from_override true as expected' );
138 is( $app->configfile, path('/notused'),
139 '... configfile is /notused as expected' );
142 my $app = App::DefaultConfigFileCodeRef->new_with_options;
143 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
146 ok( $app->config_from_override,
147 '... config_from_override true as expected' );
149 is( $app->configfile, path('/notused'),
150 '... configfile is /notused as expected' );
154 # Required arg not supplied from cmdline
156 local @ARGV = qw( --configfile /notused );
157 like exception { App->new_with_options },
158 ($Getopt::Long::Descriptive::VERSION >= 0.091
159 ? qr/Mandatory parameter 'required_from_argv' missing/
160 : qr/Required option missing: required_from_argv/);
163 # Config file value overriden from cmdline
165 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
167 my $app = App->new_with_options;
168 isa_ok( $app, 'App' );
170 is( $app->required_from_config, 'override',
171 '... required_from_config is override as expected' );
173 is( $app->optional_from_config, 'from_config_2',
174 '... optional_from_config is from_config_2 as expected' );
179 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
181 my $app = App->new_with_options;
182 isa_ok( $app, 'App' );
184 is( $app->required_from_config, 'noconfig',
185 '... required_from_config is noconfig as expected' );
187 ok( !defined $app->optional_from_config,
188 '... optional_from_config is undef as expected' );
192 package BaseApp::WithConfig;
194 with 'MooseX::ConfigFromFile';
196 sub get_config_from_file { return {}; }
200 package DerivedApp::Getopt;
202 extends 'BaseApp::WithConfig';
203 with 'MooseX::Getopt';
206 # With DerivedApp, the Getopt role was applied at a different level
207 # than the ConfigFromFile role
209 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
215 is( $app->required_from_config, 'from_config_1',
216 '... required_from_config is from_config_1 as expected' );
218 is( $app->optional_from_config, 'from_config_2',
219 '... optional_from_config is from_config_2 as expected' );
221 is( $app->required_from_argv, '1',
222 '... required_from_argv is 1 as expected' );