Commit | Line | Data |
62c4891a |
1 | use strict; |
2 | use warnings; |
3 | |
9fbb5be9 |
4 | use Test::More tests => 38; |
aabf4179 |
5 | use Test::Fatal; |
d1e5e425 |
6 | use File::Spec; |
9fbb5be9 |
7 | use Test::NoWarnings 1.04 ':early'; |
62c4891a |
8 | |
7d634446 |
9 | use Test::Requires { |
6a378fbd |
10 | 'MooseX::ConfigFromFile' => 0.01, # skip all if not installed |
11 | }; |
62c4891a |
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 | |
d1e5e425 |
53 | my $cpath = File::Spec->canonpath('/notused/default'); |
54 | if ( $file ne $cpath ) { |
62c4891a |
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' => ( |
d1e5e425 |
69 | default => File::Spec->canonpath('/notused/default'), |
62c4891a |
70 | ); |
71 | } |
72 | |
aa03fdad |
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 | |
62c4891a |
84 | # No config specified |
85 | { |
86 | local @ARGV = qw( --required_from_argv 1 ); |
87 | |
99d37f16 |
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 | } |
62c4891a |
94 | |
4e086633 |
95 | { |
96 | my $app = App::DefaultConfigFile->new_with_options; |
62c4891a |
97 | isa_ok( $app, 'App::DefaultConfigFile' ); |
98 | app_ok( $app ); |
99 | |
4e086633 |
100 | ok( !$app->config_from_override, |
62c4891a |
101 | '... config_from_override false as expected' ); |
102 | |
d1e5e425 |
103 | is( $app->configfile, File::Spec->canonpath('/notused/default'), |
62c4891a |
104 | '... configfile is /notused/default as expected' ); |
105 | } |
106 | } |
107 | |
aa03fdad |
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 | |
62c4891a |
125 | # Config specified |
126 | { |
127 | local @ARGV = qw( --configfile /notused --required_from_argv 1 ); |
128 | |
4e086633 |
129 | { |
130 | my $app = App->new_with_options; |
62c4891a |
131 | isa_ok( $app, 'App' ); |
132 | app_ok( $app ); |
133 | } |
134 | |
4e086633 |
135 | { |
136 | my $app = App::DefaultConfigFile->new_with_options; |
62c4891a |
137 | isa_ok( $app, 'App::DefaultConfigFile' ); |
138 | app_ok( $app ); |
aa03fdad |
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 ); |
62c4891a |
150 | |
4e086633 |
151 | ok( $app->config_from_override, |
62c4891a |
152 | '... config_from_override true as expected' ); |
153 | |
d1e5e425 |
154 | is( $app->configfile, File::Spec->canonpath('/notused'), |
62c4891a |
155 | '... configfile is /notused as expected' ); |
156 | } |
157 | } |
158 | |
159 | # Required arg not supplied from cmdline |
160 | { |
161 | local @ARGV = qw( --configfile /notused ); |
99d37f16 |
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 | } |
62c4891a |
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 | |
9f1ec7c0 |
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 | { |
aabf4179 |
216 | ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp'; |
9f1ec7c0 |
217 | } |
218 | |
62c4891a |
219 | sub app_ok { |
220 | my $app = shift; |
221 | |
4e086633 |
222 | is( $app->required_from_config, 'from_config_1', |
62c4891a |
223 | '... required_from_config is from_config_1 as expected' ); |
224 | |
4e086633 |
225 | is( $app->optional_from_config, 'from_config_2', |
62c4891a |
226 | '... optional_from_config is from_config_2 as expected' ); |
227 | |
4e086633 |
228 | is( $app->required_from_argv, '1', |
62c4891a |
229 | '... required_from_argv is 1 as expected' ); |
230 | } |