remove useless temp variable
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
1 use strict;
2 use warnings;
3
4 use Test::Requires { 'MooseX::ConfigFromFile' => '0.06' };    # skip all if not installed
5 use Test::More tests => 38;
6 use Test::Fatal;
7 use Path::Tiny;
8 use Test::NoWarnings 1.04 ':early';
9
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
50         if ( $file ne Path::Tiny::path('/notused/default') ) {
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' => (
65         default => Path::Tiny::path('/notused/default')->stringify,
66     );
67 }
68
69 {
70     package App::DefaultConfigFileCodeRef;
71
72     use Moose;
73     extends 'App';
74
75     has '+configfile' => (
76         default => sub { return Path::Tiny::path('/notused/default') },
77     );
78 }
79
80 # No config specified
81 {
82     local @ARGV = qw( --required_from_argv 1 );
83
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/);
88
89     {
90         my $app = App::DefaultConfigFile->new_with_options;
91         isa_ok( $app, 'App::DefaultConfigFile' );
92         app_ok( $app );
93
94         ok(  !$app->config_from_override,
95             '... config_from_override false as expected' );
96
97         is( $app->configfile, path('/notused/default'),
98             '... configfile is /notused/default as expected' );
99     }
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
109         is( $app->configfile, path('/notused/default'),
110             '... configfile is /notused/default as expected' );
111     }
112 }
113
114 # Config specified
115 {
116     local @ARGV = qw( --configfile /notused/override --required_from_argv 1 );
117
118     {
119         my $app = App->new_with_options;
120         isa_ok( $app, 'App' );
121         app_ok( $app );
122     }
123
124     {
125         my $app = App::DefaultConfigFile->new_with_options;
126         isa_ok( $app, 'App::DefaultConfigFile' );
127         app_ok( $app );
128
129         ok( $app->config_from_override,
130              '... config_from_override true as expected' );
131
132         is( $app->configfile, path('/notused/override'),
133             '... configfile is /notused/override as expected' );
134     }
135     {
136         my $app = App::DefaultConfigFileCodeRef->new_with_options;
137         isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
138         app_ok( $app );
139
140         ok( $app->config_from_override,
141              '... config_from_override true as expected' );
142
143         is( $app->configfile, path('/notused'),
144             '... configfile is /notused as expected' );
145     }
146 }
147
148 # Required arg not supplied from cmdline
149 {
150     local @ARGV = qw( --configfile /notused/override );
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/);
155 }
156
157 # Config file value overriden from cmdline
158 {
159     local @ARGV = qw( --configfile /notused/override --required_from_argv 1 --required_from_config override );
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
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 {
203     ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
204 }
205
206 sub app_ok {
207     my $app = shift;
208
209     is( $app->required_from_config, 'from_config_1',
210         '... required_from_config is from_config_1 as expected' );
211
212     is( $app->optional_from_config, 'from_config_2',
213         '... optional_from_config is from_config_2 as expected' );
214
215     is( $app->required_from_argv, '1',
216         '... required_from_argv is 1 as expected' );
217 }