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