Debian Codegraph @

t/config_reader.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Fatal;
5 use Carp 'croak';
6
7 use Path::Tiny qw< path >;
8 use Dancer2::Core::Runner;
9 use Dancer2::ConfigReader;
10 use Dancer2::ConfigReader::Config::Any;
11
12 # undefine ENV vars used as defaults for app environment in these tests
13 local $ENV{DANCER_ENVIRONMENT};
14 local $ENV{PLACK_ENV};
15
16 my $runner = Dancer2::Core::Runner->new();
17 my $location = path( __FILE__() )->sibling('config');
18 my $location2 = path( __FILE__() )->sibling('config2');
19
20 {
21
22 package ConfigUser;
23 use Moo;
24 with 'Dancer2::Core::Role::HasConfig';
25
26 has environment => ( is => 'ro', required => 1 );
27 has location => ( is => 'ro', required => 1 );
28 has default_config => ( is => 'ro', required => 1 );
29
30 sub _build_config {
31 my $self = shift;
32 return Dancer2::ConfigReader->new(
33 environment => $self->environment,
34 location => $self->location,
35 default_config => $self->default_config,
36 )->config;
37 }
38 }
39
40 sub config_any {
41 my ( $environment, $location ) = @_;
42 return Dancer2::ConfigReader::Config::Any->new(
43 environment => $environment,
44 location => $location,
45 );
46 }
47
48 sub config_reader {
49 my ( $environment, $location ) = @_;
50 return Dancer2::ConfigReader->new(
51 environment => $environment,
52 location => $location,
53 default_config => $runner->config,
54 );
55 }
56
57 sub config_user {
58 my ( $environment, $location ) = @_;
59 return ConfigUser->new(
60 environment => $environment,
61 location => $location,
62 default_config => $runner->config,
63 );
64 }
65
66 my $d = config_any( 'development', $location );
67 is_deeply $d->config_files,
68 [ path( $location, 'config.yml' ), ],
69 "config_files() only sees existing files";
70
71 my $f_any = config_any( 'production', $location );
72 is $f_any->does('Dancer2::Core::Role::ConfigReader'), 1,
73 "role Dancer2::Core::Role::ConfigReader is consumed";
74
75 is_deeply $f_any->config_files,
76 [ path( $location, 'config.yml' ),
77 path( $location, 'environments', 'production.yml' ),
78 ],
79 "config_files() works";
80
81 my $j = config_any( 'staging', $location );
82 is_deeply $j->config_files,
83 [ path( $location, 'config.yml' ),
84 path( $location, 'environments', 'staging.json' ),
85 ],
86 "config_files() does JSON too!";
87
88 note "bad YAML file";
89 my $fail_any = config_any( 'failure', $location );
90 is $fail_any->environment, 'failure';
91
92 is_deeply $fail_any->config_files,
93 [ path( $location, 'config.yml' ),
94 path( $location, 'environments', 'failure.yml' ),
95 ],
96 "config_files() works";
97
98 like(
99 exception { config_reader( 'failure', $location->stringify )->config },
100 qr{Unable to parse the configuration file}, 'Configuration file parsing failure',
101 );
102
103 note "config merging";
104 my $m = config_reader( 'merging', $location->stringify );
105
106 # Check the 'application' top-level key; its the only key that
107 # is currently a HoH in the test configurations
108 is_deeply $m->config->{application},
109 { some_feature => 'bar',
110 another_setting => 'baz',
111 },
112 "full merging of configuration hashes";
113
114 {
115 my $l_any = config_any( 'lconfig', $location2->stringify );
116
117 is_deeply $l_any->config_files,
118 [ path( $location2, 'config.yml' ),
119 path( $location2, 'config_local.yml' ),
120 path( $location2, 'environments', 'lconfig.yml' ),
121 path( $location2, 'environments', 'lconfig_local.yml' ),
122 ],
123 "config_files() with local config works";
124
125 my $l = config_reader( 'lconfig', $location2->stringify );
126 is_deeply $l->config->{application},
127 { feature_1 => 'foo',
128 feature_2 => 'alpha',
129 feature_3 => 'replacement',
130 feature_4 => 'blat',
131 feature_5 => 'beta',
132 feature_6 => 'bar',
133 feature_7 => 'baz',
134 feature_8 => 'goober',
135 },
136 "full merging of local configuration hashes";
137
138 }
139
140 note "config parsing";
141
142 my $f = config_user( 'production', $location->stringify );
143 is $f->config->{main}, 1;
144 is $f->config->{charset}, 'utf-8', "normalized UTF-8 to utf-8";
145
146 ok( $f->has_setting('charset') );
147 ok( !$f->has_setting('foobarbaz') );
148
149 note "default values";
150 is $f->setting('apphandler'), 'Standalone';
151
152 like(
153 exception {
154 config_reader( 'production', $location->stringify )
155 ->_normalize_config( { charset => 'BOGUS' } );
156 },
157 qr{Charset defined in configuration is wrong : couldn't identify 'BOGUS'},
158 'Configuration file charset failure',
159 );
160
161 {
162
163 package Foo;
164 use Carp 'croak';
165 sub foo { croak "foo" }
166 }
167
168 is $f->setting('traces'), 0;
169 unlike( exception { Foo->foo() }, qr{Foo::foo}, "traces are not enabled", );
170
171 $f->setting( traces => 1 );
172 like( exception { Foo->foo() }, qr{Foo::foo}, "traces are enabled", );
173
174 {
175 my $tmpdir = Path::Tiny->tempdir( CLEANUP => 1, TMPDIR => 1 );
176 $ENV{DANCER_CONFDIR} = $tmpdir;
177 my $f = config_any( 'production', $location->stringify );
178 is $f->config_location, $tmpdir;
179 }
180
181 done_testing;