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