MooseX-Getopt-0.51
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
CommitLineData
62c4891a 1use strict;
2use warnings;
3
44f514c7 4use Test::Requires 'MooseX::ConfigFromFile'; # skip all if not installed
9fbb5be9 5use Test::More tests => 38;
aabf4179 6use Test::Fatal;
d1e5e425 7use File::Spec;
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
d1e5e425 50 my $cpath = File::Spec->canonpath('/notused/default');
51 if ( $file ne $cpath ) {
62c4891a 52 $config{config_from_override} = 1;
53 }
54
55 return \%config;
56 }
57}
58
59{
60 package App::DefaultConfigFile;
61
62 use Moose;
63 extends 'App';
64
65 has '+configfile' => (
d1e5e425 66 default => File::Spec->canonpath('/notused/default'),
62c4891a 67 );
68}
69
aa03fdad 70{
71 package App::DefaultConfigFileCodeRef;
72
73 use Moose;
74 extends 'App';
75
76 has '+configfile' => (
77 default => sub { return File::Spec->canonpath('/notused/default') },
78 );
79}
80
62c4891a 81# No config specified
82{
83 local @ARGV = qw( --required_from_argv 1 );
84
b3bb80f8 85 like exception { App->new_with_options },
86 ($Getopt::Long::Descriptive::VERSION >= 0.091
87 ? qr/Mandatory parameter 'required_from_config' missing/
88 : qr/Required option missing: required_from_config/);
62c4891a 89
4e086633 90 {
91 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 92 isa_ok( $app, 'App::DefaultConfigFile' );
93 app_ok( $app );
94
4e086633 95 ok( !$app->config_from_override,
62c4891a 96 '... config_from_override false as expected' );
97
d1e5e425 98 is( $app->configfile, File::Spec->canonpath('/notused/default'),
62c4891a 99 '... configfile is /notused/default as expected' );
100 }
101}
102
aa03fdad 103# No config specified
104{
105 local @ARGV = qw( --required_from_argv 1 );
106
107 {
108 my $app = App::DefaultConfigFileCodeRef->new_with_options;
109 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
110 app_ok( $app );
111
112 ok( !$app->config_from_override,
113 '... config_from_override false as expected' );
114
115 is( $app->configfile, File::Spec->canonpath('/notused/default'),
116 '... configfile is /notused/default as expected' );
117 }
118}
119
62c4891a 120# Config specified
121{
122 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
123
4e086633 124 {
125 my $app = App->new_with_options;
62c4891a 126 isa_ok( $app, 'App' );
127 app_ok( $app );
128 }
129
4e086633 130 {
131 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 132 isa_ok( $app, 'App::DefaultConfigFile' );
133 app_ok( $app );
aa03fdad 134
135 ok( $app->config_from_override,
136 '... config_from_override true as expected' );
137
138 is( $app->configfile, File::Spec->canonpath('/notused'),
139 '... configfile is /notused as expected' );
140 }
141 {
142 my $app = App::DefaultConfigFileCodeRef->new_with_options;
143 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
144 app_ok( $app );
62c4891a 145
4e086633 146 ok( $app->config_from_override,
62c4891a 147 '... config_from_override true as expected' );
148
d1e5e425 149 is( $app->configfile, File::Spec->canonpath('/notused'),
62c4891a 150 '... configfile is /notused as expected' );
151 }
152}
153
154# Required arg not supplied from cmdline
155{
156 local @ARGV = qw( --configfile /notused );
b3bb80f8 157 like exception { App->new_with_options },
158 ($Getopt::Long::Descriptive::VERSION >= 0.091
159 ? qr/Mandatory parameter 'required_from_argv' missing/
160 : qr/Required option missing: required_from_argv/);
62c4891a 161}
162
163# Config file value overriden from cmdline
164{
165 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
166
167 my $app = App->new_with_options;
168 isa_ok( $app, 'App' );
169
170 is( $app->required_from_config, 'override',
171 '... required_from_config is override as expected' );
172
173 is( $app->optional_from_config, 'from_config_2',
174 '... optional_from_config is from_config_2 as expected' );
175}
176
177# No config file
178{
179 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
180
181 my $app = App->new_with_options;
182 isa_ok( $app, 'App' );
183
184 is( $app->required_from_config, 'noconfig',
185 '... required_from_config is noconfig as expected' );
186
187 ok( !defined $app->optional_from_config,
188 '... optional_from_config is undef as expected' );
189}
190
9f1ec7c0 191{
192 package BaseApp::WithConfig;
193 use Moose;
194 with 'MooseX::ConfigFromFile';
195
196 sub get_config_from_file { return {}; }
197}
198
199{
200 package DerivedApp::Getopt;
201 use Moose;
202 extends 'BaseApp::WithConfig';
203 with 'MooseX::Getopt';
204}
205
206# With DerivedApp, the Getopt role was applied at a different level
207# than the ConfigFromFile role
208{
aabf4179 209 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
9f1ec7c0 210}
211
62c4891a 212sub app_ok {
213 my $app = shift;
214
4e086633 215 is( $app->required_from_config, 'from_config_1',
62c4891a 216 '... required_from_config is from_config_1 as expected' );
217
4e086633 218 is( $app->optional_from_config, 'from_config_2',
62c4891a 219 '... optional_from_config is from_config_2 as expected' );
220
4e086633 221 is( $app->required_from_argv, '1',
62c4891a 222 '... required_from_argv is 1 as expected' );
223}