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 | |
aabf4179 |
89 | like exception { App->new_with_options }, 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 ); |
aabf4179 |
158 | like exception { App->new_with_options }, qr/Required option missing: required_from_argv/; |
62c4891a |
159 | } |
160 | |
161 | # Config file value overriden from cmdline |
162 | { |
163 | local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override ); |
164 | |
165 | my $app = App->new_with_options; |
166 | isa_ok( $app, 'App' ); |
167 | |
168 | is( $app->required_from_config, 'override', |
169 | '... required_from_config is override as expected' ); |
170 | |
171 | is( $app->optional_from_config, 'from_config_2', |
172 | '... optional_from_config is from_config_2 as expected' ); |
173 | } |
174 | |
175 | # No config file |
176 | { |
177 | local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig ); |
178 | |
179 | my $app = App->new_with_options; |
180 | isa_ok( $app, 'App' ); |
181 | |
182 | is( $app->required_from_config, 'noconfig', |
183 | '... required_from_config is noconfig as expected' ); |
184 | |
185 | ok( !defined $app->optional_from_config, |
186 | '... optional_from_config is undef as expected' ); |
187 | } |
188 | |
9f1ec7c0 |
189 | { |
190 | package BaseApp::WithConfig; |
191 | use Moose; |
192 | with 'MooseX::ConfigFromFile'; |
193 | |
194 | sub get_config_from_file { return {}; } |
195 | } |
196 | |
197 | { |
198 | package DerivedApp::Getopt; |
199 | use Moose; |
200 | extends 'BaseApp::WithConfig'; |
201 | with 'MooseX::Getopt'; |
202 | } |
203 | |
204 | # With DerivedApp, the Getopt role was applied at a different level |
205 | # than the ConfigFromFile role |
206 | { |
aabf4179 |
207 | ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp'; |
9f1ec7c0 |
208 | } |
209 | |
62c4891a |
210 | sub app_ok { |
211 | my $app = shift; |
212 | |
4e086633 |
213 | is( $app->required_from_config, 'from_config_1', |
62c4891a |
214 | '... required_from_config is from_config_1 as expected' ); |
215 | |
4e086633 |
216 | is( $app->optional_from_config, 'from_config_2', |
62c4891a |
217 | '... optional_from_config is from_config_2 as expected' ); |
218 | |
4e086633 |
219 | is( $app->required_from_argv, '1', |
62c4891a |
220 | '... required_from_argv is 1 as expected' ); |
221 | } |