Debian Codegraph @

t/strict_config.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Dancer2::ConfigReader;
6 use Dancer2::FileUtils qw/dirname path/;
7 use File::Spec;
8
9 {
10 package Dancer2::ConfigReader::TestWarn;
11 use Moo;
12 with 'Dancer2::Core::Role::ConfigReader';
13
14 has name => (
15 is => 'ro',
16 default => sub { 'TestWarn' },
17 );
18
19 has config_data => (
20 is => 'ro',
21 default => sub { {} },
22 );
23
24 sub read_config {
25 my ($self) = @_;
26 return $self->config_data;
27 }
28 }
29
30 my $location = File::Spec->rel2abs( path( dirname(__FILE__), 'config' ) );
31
32 sub _read_config_with_warnings {
33 my ($config_data) = @_;
34
35 my $reader = Dancer2::ConfigReader::TestWarn->new(
36 environment => 'test',
37 location => $location,
38 config_data => $config_data,
39 );
40
41 my $cfgr = Dancer2::ConfigReader->new(
42 environment => 'test',
43 location => $location,
44 default_config => {},
45 config_readers => [$reader],
46 );
47
48 my @warnings;
49 local $SIG{__WARN__} = sub { push @warnings, @_ };
50
51 $cfgr->config;
52
53 return join q{}, @warnings;
54 }
55
56 subtest 'warns on unknown keys' => sub {
57 my $warnings = _read_config_with_warnings({
58 strict_config => 1,
59 typo => 1,
60 engines => {
61 logger => {
62 File => {
63 log_dir => '/tmp',
64 log_level => 'debug',
65 extra => 1,
66 },
67 },
68 template => {
69 template_toolkit => {
70 foo => 1,
71 },
72 },
73 serializer => {
74 JSON => {
75 allow_nonref => 1,
76 foo => 2,
77 },
78 },
79 },
80 });
81
82 like(
83 $warnings,
84 qr/Unknown configuration key 'typo'/,
85 'warns for unknown top-level key',
86 );
87 like(
88 $warnings,
89 qr/Unknown configuration key 'extra' for engine 'logger\/File'/,
90 'warns for unknown engine key',
91 );
92 unlike(
93 $warnings,
94 qr/template_toolkit/,
95 'does not warn for template_toolkit keys',
96 );
97 unlike(
98 $warnings,
99 qr/serializer\/JSON/,
100 'does not warn for JSON serializer keys',
101 );
102 };
103
104 subtest 'can disable warnings' => sub {
105 my $warnings = _read_config_with_warnings({
106 strict_config => 0,
107 typo => 1,
108 engines => {
109 logger => {
110 File => {
111 extra => 1,
112 },
113 },
114 },
115 });
116
117 is( $warnings, q{}, 'warnings silenced' );
118 };
119
120 subtest 'can allow specific top-level keys' => sub {
121 my $warnings = _read_config_with_warnings({
122 strict_config => 1,
123 strict_config_allow => [ 'typo', 'extra_top_level' ],
124 typo => 1,
125 extra_top_level => 1,
126 nope => 1,
127 engines => {
128 logger => {
129 File => {
130 extra => 1,
131 },
132 },
133 },
134 });
135
136 unlike(
137 $warnings,
138 qr/Unknown configuration key 'typo'/,
139 'does not warn for allowlisted keys',
140 );
141 unlike(
142 $warnings,
143 qr/Unknown configuration key 'extra_top_level'/,
144 'does not warn for allowlisted keys',
145 );
146 like(
147 $warnings,
148 qr/Unknown configuration key 'nope'/,
149 'still warns for other top-level keys',
150 );
151 like(
152 $warnings,
153 qr/Unknown configuration key 'extra' for engine 'logger\/File'/,
154 'still warns for unknown engine keys',
155 );
156 };
157
158 done_testing;