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