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