ee359a91aa660413a998818606752c6baa39924f
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 37;
7 use Test::Fatal;
8 use File::Spec;
9
10 use Test::Requires {
11     'MooseX::ConfigFromFile' => 0.01, # skip all if not installed
12 };
13
14 {
15     package App;
16
17     use Moose;
18     with 'MooseX::Getopt';
19     with 'MooseX::ConfigFromFile';
20
21     has 'config_from_override' => (
22         is       => 'ro',
23         isa      => 'Bool',
24         default  => 0,
25     );
26
27     has 'optional_from_config' => (
28         is        => 'ro',
29         isa       => 'Str',
30         required  => 0,
31     );
32
33     has 'required_from_config' => (
34         is        => 'ro',
35         isa       => 'Str',
36         required  => 1,
37     );
38
39     has 'required_from_argv' => (
40         is        => 'ro',
41         isa       => 'Str',
42         required  => 1,
43     );
44
45     sub get_config_from_file
46     {
47         my ( $class, $file ) = @_;
48
49         my %config = (
50             required_from_config => 'from_config_1',
51             optional_from_config => 'from_config_2',
52         );
53
54         my $cpath = File::Spec->canonpath('/notused/default');
55         if ( $file ne $cpath ) {
56             $config{config_from_override} = 1;
57         }
58
59         return \%config;
60     }
61 }
62
63 {
64     package App::DefaultConfigFile;
65
66     use Moose;
67     extends 'App';
68
69     has '+configfile' => (
70         default => File::Spec->canonpath('/notused/default'),
71     );
72 }
73
74 {
75     package App::DefaultConfigFileCodeRef;
76
77     use Moose;
78     extends 'App';
79
80     has '+configfile' => (
81         default => sub { return File::Spec->canonpath('/notused/default') },
82     );
83 }
84
85 # No config specified
86 {
87     local @ARGV = qw( --required_from_argv 1 );
88
89     if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
90         like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_config' missing/;
91     }
92     else {
93         like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
94     }
95
96     {
97         my $app = App::DefaultConfigFile->new_with_options;
98         isa_ok( $app, 'App::DefaultConfigFile' );
99         app_ok( $app );
100
101         ok(  !$app->config_from_override,
102             '... config_from_override false as expected' );
103
104         is( $app->configfile, File::Spec->canonpath('/notused/default'),
105             '... configfile is /notused/default as expected' );
106     }
107 }
108
109 # No config specified
110 {
111     local @ARGV = qw( --required_from_argv 1 );
112
113     {
114         my $app = App::DefaultConfigFileCodeRef->new_with_options;
115         isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
116         app_ok( $app );
117
118         ok(  !$app->config_from_override,
119             '... config_from_override false as expected' );
120
121         is( $app->configfile, File::Spec->canonpath('/notused/default'),
122             '... configfile is /notused/default as expected' );
123     }
124 }
125
126 # Config specified
127 {
128     local @ARGV = qw( --configfile /notused --required_from_argv 1 );
129
130     {
131         my $app = App->new_with_options;
132         isa_ok( $app, 'App' );
133         app_ok( $app );
134     }
135
136     {
137         my $app = App::DefaultConfigFile->new_with_options;
138         isa_ok( $app, 'App::DefaultConfigFile' );
139         app_ok( $app );
140
141         ok( $app->config_from_override,
142              '... config_from_override true as expected' );
143
144         is( $app->configfile, File::Spec->canonpath('/notused'),
145             '... configfile is /notused as expected' );
146     }
147     {
148         my $app = App::DefaultConfigFileCodeRef->new_with_options;
149         isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
150         app_ok( $app );
151
152         ok( $app->config_from_override,
153              '... config_from_override true as expected' );
154
155         is( $app->configfile, File::Spec->canonpath('/notused'),
156             '... configfile is /notused as expected' );
157     }
158 }
159
160 # Required arg not supplied from cmdline
161 {
162     local @ARGV = qw( --configfile /notused );
163     if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
164         like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_argv' missing/;
165     }
166     else {
167         like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
168     }
169 }
170
171 # Config file value overriden from cmdline
172 {
173     local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
174
175     my $app = App->new_with_options;
176     isa_ok( $app, 'App' );
177
178     is( $app->required_from_config, 'override',
179         '... required_from_config is override as expected' );
180
181     is( $app->optional_from_config, 'from_config_2',
182         '... optional_from_config is from_config_2 as expected' );
183 }
184
185 # No config file
186 {
187     local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
188
189     my $app = App->new_with_options;
190     isa_ok( $app, 'App' );
191
192     is( $app->required_from_config, 'noconfig',
193         '... required_from_config is noconfig as expected' );
194
195     ok( !defined $app->optional_from_config,
196         '... optional_from_config is undef as expected' );
197 }
198
199 {
200     package BaseApp::WithConfig;
201     use Moose;
202     with 'MooseX::ConfigFromFile';
203
204     sub get_config_from_file { return {}; }
205 }
206
207 {
208     package DerivedApp::Getopt;
209     use Moose;
210     extends 'BaseApp::WithConfig';
211     with 'MooseX::Getopt';
212 }
213
214 # With DerivedApp, the Getopt role was applied at a different level
215 # than the ConfigFromFile role
216 {
217     ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
218 }
219
220 sub app_ok {
221     my $app = shift;
222
223     is( $app->required_from_config, 'from_config_1',
224         '... required_from_config is from_config_1 as expected' );
225
226     is( $app->optional_from_config, 'from_config_2',
227         '... optional_from_config is from_config_2 as expected' );
228
229     is( $app->required_from_argv, '1',
230         '... required_from_argv is 1 as expected' );
231 }