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 | |
498f0ffc |
9 | use Test::Requires 'MooseX::ConfigFromFile'; |
62c4891a |
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 | |
d1e5e425 |
51 | my $cpath = File::Spec->canonpath('/notused/default'); |
52 | if ( $file ne $cpath ) { |
62c4891a |
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' => ( |
d1e5e425 |
67 | default => File::Spec->canonpath('/notused/default'), |
62c4891a |
68 | ); |
69 | } |
70 | |
aa03fdad |
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 | |
62c4891a |
82 | # No config specified |
83 | { |
84 | local @ARGV = qw( --required_from_argv 1 ); |
85 | |
b3bb80f8 |
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/); |
62c4891a |
90 | |
4e086633 |
91 | { |
92 | my $app = App::DefaultConfigFile->new_with_options; |
62c4891a |
93 | isa_ok( $app, 'App::DefaultConfigFile' ); |
94 | app_ok( $app ); |
95 | |
4e086633 |
96 | ok( !$app->config_from_override, |
62c4891a |
97 | '... config_from_override false as expected' ); |
98 | |
d1e5e425 |
99 | is( $app->configfile, File::Spec->canonpath('/notused/default'), |
62c4891a |
100 | '... configfile is /notused/default as expected' ); |
101 | } |
102 | } |
103 | |
aa03fdad |
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 | |
62c4891a |
121 | # Config specified |
122 | { |
123 | local @ARGV = qw( --configfile /notused --required_from_argv 1 ); |
124 | |
4e086633 |
125 | { |
126 | my $app = App->new_with_options; |
62c4891a |
127 | isa_ok( $app, 'App' ); |
128 | app_ok( $app ); |
129 | } |
130 | |
4e086633 |
131 | { |
132 | my $app = App::DefaultConfigFile->new_with_options; |
62c4891a |
133 | isa_ok( $app, 'App::DefaultConfigFile' ); |
134 | app_ok( $app ); |
aa03fdad |
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 ); |
62c4891a |
146 | |
4e086633 |
147 | ok( $app->config_from_override, |
62c4891a |
148 | '... config_from_override true as expected' ); |
149 | |
d1e5e425 |
150 | is( $app->configfile, File::Spec->canonpath('/notused'), |
62c4891a |
151 | '... configfile is /notused as expected' ); |
152 | } |
153 | } |
154 | |
155 | # Required arg not supplied from cmdline |
156 | { |
157 | local @ARGV = qw( --configfile /notused ); |
b3bb80f8 |
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/); |
62c4891a |
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 | |
9f1ec7c0 |
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 | { |
aabf4179 |
210 | ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp'; |
9f1ec7c0 |
211 | } |
212 | |
62c4891a |
213 | sub app_ok { |
214 | my $app = shift; |
215 | |
4e086633 |
216 | is( $app->required_from_config, 'from_config_1', |
62c4891a |
217 | '... required_from_config is from_config_1 as expected' ); |
218 | |
4e086633 |
219 | is( $app->optional_from_config, 'from_config_2', |
62c4891a |
220 | '... optional_from_config is from_config_2 as expected' ); |
221 | |
4e086633 |
222 | is( $app->required_from_argv, '1', |
62c4891a |
223 | '... required_from_argv is 1 as expected' ); |
224 | } |