support the configfile sub being totally overridden
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
CommitLineData
62c4891a 1use strict;
2use warnings;
3
14cb82ca 4use Test::Requires { 'MooseX::ConfigFromFile' => '0.06' }; # skip all if not installed
cd4283e8 5use Test::More tests => 50;
aabf4179 6use Test::Fatal;
e202fd48 7use Path::Tiny;
9fbb5be9 8use Test::NoWarnings 1.04 ':early';
62c4891a 9
62c4891a 10{
11 package App;
12
13 use Moose;
14 with 'MooseX::Getopt';
15 with 'MooseX::ConfigFromFile';
16
17 has 'config_from_override' => (
18 is => 'ro',
19 isa => 'Bool',
20 default => 0,
21 );
22
23 has 'optional_from_config' => (
24 is => 'ro',
25 isa => 'Str',
26 required => 0,
27 );
28
29 has 'required_from_config' => (
30 is => 'ro',
31 isa => 'Str',
32 required => 1,
33 );
34
35 has 'required_from_argv' => (
36 is => 'ro',
37 isa => 'Str',
38 required => 1,
39 );
40
41 sub get_config_from_file
42 {
43 my ( $class, $file ) = @_;
44
45 my %config = (
46 required_from_config => 'from_config_1',
47 optional_from_config => 'from_config_2',
48 );
49
01788da4 50 if ( $file ne Path::Tiny::path('/notused/default') ) {
62c4891a 51 $config{config_from_override} = 1;
52 }
53
54 return \%config;
55 }
56}
57
58{
59 package App::DefaultConfigFile;
60
61 use Moose;
62 extends 'App';
63
64 has '+configfile' => (
e202fd48 65 default => Path::Tiny::path('/notused/default')->stringify,
62c4891a 66 );
67}
68
aa03fdad 69{
70 package App::DefaultConfigFileCodeRef;
71
72 use Moose;
73 extends 'App';
74
75 has '+configfile' => (
e202fd48 76 default => sub { return Path::Tiny::path('/notused/default') },
aa03fdad 77 );
78}
79
cd4283e8 80{
81 package App::ConfigFileWrapped;
82
83 use Moose;
84 extends 'App';
85
86 around configfile => sub { '/notused/default' };
87}
88
89
62c4891a 90# No config specified
91{
92 local @ARGV = qw( --required_from_argv 1 );
93
b3bb80f8 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/);
62c4891a 98
4e086633 99 {
100 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 101 isa_ok( $app, 'App::DefaultConfigFile' );
102 app_ok( $app );
103
4e086633 104 ok( !$app->config_from_override,
62c4891a 105 '... config_from_override false as expected' );
106
e202fd48 107 is( $app->configfile, path('/notused/default'),
62c4891a 108 '... configfile is /notused/default as expected' );
109 }
aa03fdad 110
111 {
112 my $app = App::DefaultConfigFileCodeRef->new_with_options;
113 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
114 app_ok( $app );
115
116 ok( !$app->config_from_override,
117 '... config_from_override false as expected' );
118
e202fd48 119 is( $app->configfile, path('/notused/default'),
aa03fdad 120 '... configfile is /notused/default as expected' );
121 }
cd4283e8 122
123 {
124 my $app = App::ConfigFileWrapped->new_with_options;
125 isa_ok( $app, 'App::ConfigFileWrapped' );
126 app_ok( $app );
127
128 ok( !$app->config_from_override,
129 '... config_from_override false as expected' );
130
131 is( $app->configfile, path('/notused/default'),
132 '... configfile is /notused/default as expected' );
133 }
aa03fdad 134}
135
62c4891a 136# Config specified
137{
c6858912 138 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 );
62c4891a 139
4e086633 140 {
141 my $app = App->new_with_options;
62c4891a 142 isa_ok( $app, 'App' );
143 app_ok( $app );
144 }
145
4e086633 146 {
147 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 148 isa_ok( $app, 'App::DefaultConfigFile' );
149 app_ok( $app );
aa03fdad 150
151 ok( $app->config_from_override,
152 '... config_from_override true as expected' );
153
c6858912 154 is( $app->configfile, path('/notused/override'),
155 '... configfile is /notused/override as expected' );
aa03fdad 156 }
157 {
158 my $app = App::DefaultConfigFileCodeRef->new_with_options;
159 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
160 app_ok( $app );
62c4891a 161
4e086633 162 ok( $app->config_from_override,
62c4891a 163 '... config_from_override true as expected' );
164
cd4283e8 165 is( $app->configfile, path('/notused/override'),
166 '... configfile is /notused/override as expected' );
167 }
168 TODO: {
169 my $app = App::ConfigFileWrapped->new_with_options;
170 isa_ok( $app, 'App::ConfigFileWrapped' );
171 app_ok( $app );
172
173 ok( $app->config_from_override,
174 '... config_from_override true as expected' );
175
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'),
62c4891a 181 '... configfile is /notused as expected' );
182 }
183}
184
185# Required arg not supplied from cmdline
186{
c6858912 187 local @ARGV = qw( --configfile /notused/override );
b3bb80f8 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/);
62c4891a 192}
193
194# Config file value overriden from cmdline
195{
c6858912 196 local @ARGV = qw( --configfile /notused/override --required_from_argv 1 --required_from_config override );
62c4891a 197
198 my $app = App->new_with_options;
199 isa_ok( $app, 'App' );
200
201 is( $app->required_from_config, 'override',
202 '... required_from_config is override as expected' );
203
204 is( $app->optional_from_config, 'from_config_2',
205 '... optional_from_config is from_config_2 as expected' );
206}
207
208# No config file
209{
210 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
211
212 my $app = App->new_with_options;
213 isa_ok( $app, 'App' );
214
215 is( $app->required_from_config, 'noconfig',
216 '... required_from_config is noconfig as expected' );
217
218 ok( !defined $app->optional_from_config,
219 '... optional_from_config is undef as expected' );
220}
221
9f1ec7c0 222{
223 package BaseApp::WithConfig;
224 use Moose;
225 with 'MooseX::ConfigFromFile';
226
227 sub get_config_from_file { return {}; }
228}
229
230{
231 package DerivedApp::Getopt;
232 use Moose;
233 extends 'BaseApp::WithConfig';
234 with 'MooseX::Getopt';
235}
236
237# With DerivedApp, the Getopt role was applied at a different level
238# than the ConfigFromFile role
239{
aabf4179 240 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
9f1ec7c0 241}
242
62c4891a 243sub app_ok {
244 my $app = shift;
245
4e086633 246 is( $app->required_from_config, 'from_config_1',
62c4891a 247 '... required_from_config is from_config_1 as expected' );
248
4e086633 249 is( $app->optional_from_config, 'from_config_2',
62c4891a 250 '... optional_from_config is from_config_2 as expected' );
251
4e086633 252 is( $app->required_from_argv, '1',
62c4891a 253 '... required_from_argv is 1 as expected' );
254}