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