Debian Codegraph @

t/unit/configreader/strict.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Path::Tiny ();
6
7 use Dancer2::ConfigReader;
8
9 # strict_config warns about configuration keys Dancer2 does not recognise, so a
10 # typo in config.yml is noticed rather than silently ignored.
11 #
12 # Its scope is deliberately narrower than "everything", and the documentation
13 # says so (Dancer2::Manual::Config, "strict_config"): it warns about unknown
14 # keys "at the top level and about unknown keys in built-in engine sections
15 # where the options are known". The last subtest pins that boundary, because it
16 # is the part that looks like a gap until you read the sentence: a mistyped
17 # *engine name* produces no warning at all, which is what makes third-party
18 # engines usable.
19 #
20 # strict_config defaults to on. Every fixture that does not mean to be warned
21 # about therefore has to be clean.
22
23 my %DIR;
24
25 BEGIN {
26 my %fixture = (
27 # Two bogus top-level keys and one bogus key inside a known engine.
28 bogus_keys => <<'YML',
29 appname: X
30 not_a_real_key: 1
31 another_bogus: 2
32 engines:
33 session:
34 Simple:
35 cookie_name: ok.sid
36 bogus_engine_key: 1
37 YML
38
39 # One key allowed, one not.
40 with_allow => <<'YML',
41 appname: X
42 strict_config_allow:
43 - not_a_real_key
44 not_a_real_key: 1
45 still_bogus: 2
46 YML
47
48 # Warnings switched off entirely.
49 switched_off => <<'YML',
50 appname: X
51 strict_config: 0
52 not_a_real_key: 1
53 engines:
54 session:
55 Simple:
56 bogus_engine_key: 1
57 YML
58
59 # Nothing wrong at all.
60 clean => <<'YML',
61 appname: X
62 layout: main
63 engines:
64 session:
65 Simple:
66 cookie_name: ok.sid
67 YML
68
69 # An engine name that does not exist, and an engine *type* that does
70 # not exist.
71 unknown_engine => <<'YML',
72 appname: X
73 engines:
74 session:
75 NoSuchEngine:
76 whatever_key: 1
77 nosuchtype:
78 Foo:
79 bar: 1
80 YML
81
82 # An engine key listed in strict_config_allow, which documents itself
83 # as covering top-level keys only.
84 allow_engine_key => <<'YML',
85 appname: X
86 strict_config_allow:
87 - bogus_engine_key
88 engines:
89 session:
90 Simple:
91 bogus_engine_key: 1
92 YML
93 );
94
95 for my $name ( keys %fixture ) {
96 my $dir = Path::Tiny->tempdir;
97 $dir->child('config.yml')->spew_utf8( $fixture{$name} );
98 $DIR{$name} = $dir;
99 }
100 }
101
102 # Returns the warning lines emitted while reading the named fixture.
103 sub warnings_for {
104 my $name = shift;
105
106 local $ENV{DANCER_CONFDIR};
107 delete local $ENV{DANCER_CONFDIR};
108 local $ENV{DANCER_ENVDIR};
109 delete local $ENV{DANCER_ENVDIR};
110
111 my @warnings;
112 {
113 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
114 Dancer2::ConfigReader->new(
115 location => $DIR{$name}->stringify,
116 environment => 'production',
117 default_config => {},
118 )->config;
119 }
120
121 # The warnings arrive as one multi-line string; split so each complaint can
122 # be asserted on individually.
123 return [ grep { length } map { split /\n/ } @warnings ];
124 }
125
126 subtest 'unknown top-level keys are warned about' => sub {
127 my $warnings = warnings_for('bogus_keys');
128
129 ok( scalar @$warnings, 'reading the config warns' );
130
131 my $all = join "\n", @$warnings;
132 like( $all, qr/Unknown configuration key 'not_a_real_key'/,
133 'the first bogus key is named' );
134 like( $all, qr/Unknown configuration key 'another_bogus'/,
135 'and so is the second' );
136 like( $all, qr/Set strict_config => 0 to silence these warnings/,
137 'and the message says how to turn it off' );
138
139 # A key that is real must not be complained about.
140 unlike( $all, qr/'appname'/, 'a recognised key is not warned about' );
141 };
142
143 subtest 'unknown keys inside a known engine are warned about' => sub {
144 my $all = join "\n", @{ warnings_for('bogus_keys') };
145
146 like(
147 $all,
148 qr/Unknown configuration key 'bogus_engine_key' for engine 'session\/Simple'/,
149 'the engine key is named, with the engine it belongs to',
150 );
151 unlike( $all, qr/'cookie_name'/,
152 'a real engine key is not warned about' );
153 };
154
155 subtest 'strict_config_allow suppresses only the keys it lists' => sub {
156 my $all = join "\n", @{ warnings_for('with_allow') };
157
158 unlike( $all, qr/'not_a_real_key'/,
159 'the allowed key produces no warning' );
160 like( $all, qr/Unknown configuration key 'still_bogus'/,
161 'while a key not on the list still does' );
162
163 # strict_config_allow itself is a known key and must not warn.
164 unlike( $all, qr/'strict_config_allow'/,
165 'the allow list itself is not flagged' );
166 };
167
168 subtest 'strict_config => 0 silences everything' => sub {
169 my $warnings = warnings_for('switched_off');
170
171 is_deeply( $warnings, [],
172 'neither the top-level nor the engine key is warned about' );
173 };
174
175 subtest 'a clean config warns about nothing' => sub {
176 # Without this, every assertion above could be passing because *something*
177 # always warns.
178 my $warnings = warnings_for('clean');
179
180 is_deeply( $warnings, [], 'a config with no unknown keys is silent' );
181 };
182
183 subtest 'an unrecognised engine name is accepted in silence' => sub {
184 # Documented behavior, not an oversight: Dancer2::Manual::Config scopes
185 # strict_config to "built-in engine sections where the options are known".
186 # A third-party engine - Dancer2::Session::Redis, say - has options Dancer2
187 # cannot know, so it is skipped rather than flagged.
188 #
189 # The cost is that a *mistyped* engine name is equally silent, and the
190 # engine config is then quietly ignored. Pinned so that if anyone tightens
191 # this, they do it deliberately.
192 my $warnings = warnings_for('unknown_engine');
193
194 is_deeply( $warnings, [],
195 'neither an unknown engine name nor an unknown engine type warns' );
196 };
197
198 subtest 'strict_config_allow does not cover engine keys' => sub {
199 # Also documented: the allow list is described as a list of *top-level*
200 # keys. Listing an engine key there does not suppress the engine warning.
201 my $all = join "\n", @{ warnings_for('allow_engine_key') };
202
203 like(
204 $all,
205 qr/Unknown configuration key 'bogus_engine_key' for engine 'session\/Simple'/,
206 'the engine key is still warned about despite being on the allow list',
207 );
208 };
209
210 done_testing();