revert d08ef824 - test_requires wrong
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 37;
7 use Test::Fatal;
8 use File::Spec;
9
10 use Test::Requires {
11     'MooseX::ConfigFromFile' => 0.01, # skip all if not installed
12 };
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
54         my $cpath = File::Spec->canonpath('/notused/default');
55         if ( $file ne $cpath ) {
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' => (
70         default => File::Spec->canonpath('/notused/default'),
71     );
72 }
73
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
85 # No config specified
86 {
87     local @ARGV = qw( --required_from_argv 1 );
88
89     like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
90
91     {
92         my $app = App::DefaultConfigFile->new_with_options;
93         isa_ok( $app, 'App::DefaultConfigFile' );
94         app_ok( $app );
95
96         ok(  !$app->config_from_override,
97             '... config_from_override false as expected' );
98
99         is( $app->configfile, File::Spec->canonpath('/notused/default'),
100             '... configfile is /notused/default as expected' );
101     }
102 }
103
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
121 # Config specified
122 {
123     local @ARGV = qw( --configfile /notused --required_from_argv 1 );
124
125     {
126         my $app = App->new_with_options;
127         isa_ok( $app, 'App' );
128         app_ok( $app );
129     }
130
131     {
132         my $app = App::DefaultConfigFile->new_with_options;
133         isa_ok( $app, 'App::DefaultConfigFile' );
134         app_ok( $app );
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 );
146
147         ok( $app->config_from_override,
148              '... config_from_override true as expected' );
149
150         is( $app->configfile, File::Spec->canonpath('/notused'),
151             '... configfile is /notused as expected' );
152     }
153 }
154
155 # Required arg not supplied from cmdline
156 {
157     local @ARGV = qw( --configfile /notused );
158     like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
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
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 {
207     ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
208 }
209
210 sub app_ok {
211     my $app = shift;
212
213     is( $app->required_from_config, 'from_config_1',
214         '... required_from_config is from_config_1 as expected' );
215
216     is( $app->optional_from_config, 'from_config_2',
217         '... optional_from_config is from_config_2 as expected' );
218
219     is( $app->required_from_argv, '1',
220         '... required_from_argv is 1 as expected' );
221 }