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