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