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