cb54a1c9325bdda63a7c1ff9a7475be21a4f6b5e
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
1 use strict;
2 use warnings;
3
4 use Test::Requires { 'MooseX::ConfigFromFile' => '0.06' };    # skip all if not installed
5 use Test::More tests => 50;
6 use Test::Fatal;
7 use Path::Tiny;
8 use Test::NoWarnings 1.04 ':early';
9
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
50         if ( $file ne Path::Tiny::path('/notused/default') ) {
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' => (
65         default => Path::Tiny::path('/notused/default')->stringify,
66     );
67 }
68
69 {
70     package App::DefaultConfigFileCodeRef;
71
72     use Moose;
73     extends 'App';
74
75     has '+configfile' => (
76         default => sub { return Path::Tiny::path('/notused/default') },
77     );
78 }
79
80 {
81     package App::ConfigFileWrapped;
82
83     use Moose;
84     extends 'App';
85
86     around configfile => sub { '/notused/default' };
87 }
88
89
90 # No config specified
91 {
92     local @ARGV = qw( --required_from_argv 1 );
93
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/);
98
99     {
100         my $app = App::DefaultConfigFile->new_with_options;
101         isa_ok( $app, 'App::DefaultConfigFile' );
102         app_ok( $app );
103
104         ok(  !$app->config_from_override,
105             '... config_from_override false as expected' );
106
107         is( $app->configfile, path('/notused/default'),
108             '... configfile is /notused/default as expected' );
109     }
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
119         is( $app->configfile, path('/notused/default'),
120             '... configfile is /notused/default as expected' );
121     }
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     }
134 }
135
136 # Config specified
137 {
138     local @ARGV = qw( --configfile /notused/override --required_from_argv 1 );
139
140     {
141         my $app = App->new_with_options;
142         isa_ok( $app, 'App' );
143         app_ok( $app );
144     }
145
146     {
147         my $app = App::DefaultConfigFile->new_with_options;
148         isa_ok( $app, 'App::DefaultConfigFile' );
149         app_ok( $app );
150
151         ok( $app->config_from_override,
152              '... config_from_override true as expected' );
153
154         is( $app->configfile, path('/notused/override'),
155             '... configfile is /notused/override as expected' );
156     }
157     {
158         my $app = App::DefaultConfigFileCodeRef->new_with_options;
159         isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
160         app_ok( $app );
161
162         ok( $app->config_from_override,
163              '... config_from_override true as expected' );
164
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'),
181             '... configfile is /notused as expected' );
182     }
183 }
184
185 # Required arg not supplied from cmdline
186 {
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/);
192 }
193
194 # Config file value overriden from cmdline
195 {
196     local @ARGV = qw( --configfile /notused/override --required_from_argv 1 --required_from_config override );
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
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 {
240     ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
241 }
242
243 sub app_ok {
244     my $app = shift;
245
246     is( $app->required_from_config, 'from_config_1',
247         '... required_from_config is from_config_1 as expected' );
248
249     is( $app->optional_from_config, 'from_config_2',
250         '... optional_from_config is from_config_2 as expected' );
251
252     is( $app->required_from_argv, '1',
253         '... required_from_argv is 1 as expected' );
254 }