| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Test::Fatal qw<exception>; |
| 6 |
use Path::Tiny (); |
| 7 |
|
| 8 |
use Dancer2::ConfigReader; |
| 9 |
|
| 10 |
# A config can bootstrap further config readers: any reader whose config |
| 11 |
# contains 'additional_config_readers' has those readers instantiated and |
| 12 |
# unshifted onto the queue (Dancer2/ConfigReader.pm:254-259). That is a loop |
| 13 |
# that a config file gets to extend, so it needs a stop. |
| 14 |
# |
| 15 |
# The stop is a counter checked against $MAX_CONFIGS |
| 16 |
# (Dancer2/ConfigReader.pm:239-250), which dies with an explanation rather than |
| 17 |
# spinning forever. This file pins that it fires, that it honours the limit it |
| 18 |
# is given, and that a well-formed chain still works. |
| 19 |
# |
| 20 |
# The readers below are real classes consuming the real role, not stand-ins: |
| 21 |
# the whole mechanism is "instantiate this class and call read_config on it", |
| 22 |
# so there is nothing to fake. |
| 23 |
|
| 24 |
# Contributes one setting and stops. |
| 25 |
{ |
| 26 |
package TestReader::Leaf; |
| 27 |
use Moo; |
| 28 |
with 'Dancer2::Core::Role::ConfigReader'; |
| 29 |
sub name { 'TestReader::Leaf' } |
| 30 |
sub read_config { return { leaf_key => 'leaf_value', strict_config => 0 } } |
| 31 |
} |
| 32 |
|
| 33 |
# Contributes a setting and asks for Leaf as well. |
| 34 |
{ |
| 35 |
package TestReader::Chained; |
| 36 |
use Moo; |
| 37 |
with 'Dancer2::Core::Role::ConfigReader'; |
| 38 |
sub name { 'TestReader::Chained' } |
| 39 |
|
| 40 |
sub read_config { |
| 41 |
return { |
| 42 |
chained_key => 'chained_value', |
| 43 |
additional_config_readers => 'TestReader::Leaf', |
| 44 |
}; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
# Asks for another copy of itself, forever. |
| 49 |
{ |
| 50 |
package TestReader::SelfAdding; |
| 51 |
use Moo; |
| 52 |
with 'Dancer2::Core::Role::ConfigReader'; |
| 53 |
sub name { 'TestReader::SelfAdding' } |
| 54 |
|
| 55 |
# Counts how many times it was actually read, so a test can assert where |
| 56 |
# the guard cut in rather than only that it did. |
| 57 |
our $READS = 0; |
| 58 |
|
| 59 |
sub read_config { |
| 60 |
$READS++; |
| 61 |
return { additional_config_readers => 'TestReader::SelfAdding' }; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
# An entry with two keys, which is ambiguous: is it a class with args, or two |
| 66 |
# classes? |
| 67 |
{ |
| 68 |
package TestReader::TwoKeys; |
| 69 |
use Moo; |
| 70 |
with 'Dancer2::Core::Role::ConfigReader'; |
| 71 |
sub name { 'TestReader::TwoKeys' } |
| 72 |
|
| 73 |
sub read_config { |
| 74 |
return { |
| 75 |
additional_config_readers => { 'TestReader::Leaf' => {}, 'Other' => {} }, |
| 76 |
}; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
my $EMPTY; |
| 81 |
BEGIN { $EMPTY = Path::Tiny->tempdir } |
| 82 |
|
| 83 |
# DANCER_CONFIG_READERS is how the reader list is chosen, so each case sets it |
| 84 |
# for the duration of one read. |
| 85 |
sub read_with { |
| 86 |
my $readers = shift; |
| 87 |
|
| 88 |
local $ENV{DANCER_CONFIG_READERS} = $readers; |
| 89 |
local $ENV{DANCER_CONFDIR}; |
| 90 |
delete local $ENV{DANCER_CONFDIR}; |
| 91 |
local $ENV{DANCER_ENVDIR}; |
| 92 |
delete local $ENV{DANCER_ENVDIR}; |
| 93 |
|
| 94 |
return Dancer2::ConfigReader->new( |
| 95 |
location => $EMPTY->stringify, |
| 96 |
environment => 'production', |
| 97 |
default_config => {}, |
| 98 |
)->config; |
| 99 |
} |
| 100 |
|
| 101 |
subtest 'a reader named in additional_config_readers is also read' => sub { |
| 102 |
my $config = read_with('TestReader::Chained'); |
| 103 |
|
| 104 |
is( $config->{chained_key}, 'chained_value', |
| 105 |
'the reader that was asked for contributed its setting' ); |
| 106 |
is( $config->{leaf_key}, 'leaf_value', |
| 107 |
'and so did the reader it pulled in' ); |
| 108 |
|
| 109 |
# The key that drives the mechanism is consumed, not left in the config. |
| 110 |
is( $config->{additional_config_readers}, undef, |
| 111 |
'additional_config_readers is removed once it has been acted on' ); |
| 112 |
}; |
| 113 |
|
| 114 |
subtest 'the recursion guard stops a reader that adds itself forever' => sub { |
| 115 |
my $err = exception { read_with('TestReader::SelfAdding') }; |
| 116 |
|
| 117 |
ok( defined $err, 'reading does not hang - it dies' ); |
| 118 |
like( $err, qr/MAX_CONFIGS exceeded/, |
| 119 |
'naming the limit that was hit' ); |
| 120 |
like( $err, qr/read over 100 configurations/, |
| 121 |
'and how many configs were read, from the default limit' ); |
| 122 |
|
| 123 |
# The message has to be usable, not just fatal: it says what is probably |
| 124 |
# wrong and what to do about it. |
| 125 |
like( $err, qr/infinite recursion in your configuration system/, |
| 126 |
'the message explains the likely cause' ); |
| 127 |
like( $err, qr/DANCER_CONFIG_VERBOSE/, |
| 128 |
'and points at the way to see which readers ran' ); |
| 129 |
like( $err, qr/DANCER_MAX_CONFIGS/, |
| 130 |
'and at the way to raise the limit deliberately' ); |
| 131 |
}; |
| 132 |
|
| 133 |
subtest 'the guard honours the limit it is given' => sub { |
| 134 |
# Proves the count is what stops it, rather than some unrelated failure |
| 135 |
# that happens to look like the guard. |
| 136 |
no warnings 'once'; |
| 137 |
local $Dancer2::ConfigReader::MAX_CONFIGS = 5; |
| 138 |
|
| 139 |
local $TestReader::SelfAdding::READS = 0; |
| 140 |
my $err = exception { read_with('TestReader::SelfAdding') }; |
| 141 |
|
| 142 |
ok( defined $err, 'it still dies with a lower limit' ); |
| 143 |
like( $err, qr/read over 5 configurations/, |
| 144 |
'and reports the lowered limit, not the default' ); |
| 145 |
unlike( $err, qr/read over 100 configurations/, |
| 146 |
'so the default is genuinely not in play' ); |
| 147 |
|
| 148 |
# The message alone cannot show *where* the guard cut in: it interpolates |
| 149 |
# the limit, so it reads the same whether the guard stops at the limit or |
| 150 |
# one past it. Counting the reads is what pins the boundary. |
| 151 |
is( $TestReader::SelfAdding::READS, 5, |
| 152 |
'exactly MAX_CONFIGS configs are read, not one more' ); |
| 153 |
}; |
| 154 |
|
| 155 |
subtest 'the guard leaves a chain just under the limit alone' => sub { |
| 156 |
# The other side of the boundary: a chain that reads exactly MAX_CONFIGS |
| 157 |
# configs must succeed. Together with the subtest above this fixes the |
| 158 |
# guard at one specific count. |
| 159 |
no warnings 'once'; |
| 160 |
local $Dancer2::ConfigReader::MAX_CONFIGS = 5; |
| 161 |
|
| 162 |
local $TestReader::SelfAdding::READS = 0; |
| 163 |
my $config; |
| 164 |
my $err = exception { |
| 165 |
$config = read_with('TestReader::Chained,TestReader::Leaf'); |
| 166 |
}; |
| 167 |
|
| 168 |
is( $err, undef, 'a short chain is read without complaint' ); |
| 169 |
is( $config->{chained_key}, 'chained_value', 'and its settings arrive' ); |
| 170 |
is( $config->{leaf_key}, 'leaf_value', 'from every reader in it' ); |
| 171 |
}; |
| 172 |
|
| 173 |
subtest 'an ambiguous additional_config_readers entry is refused' => sub { |
| 174 |
my $err = exception { read_with('TestReader::TwoKeys') }; |
| 175 |
|
| 176 |
ok( defined $err, 'an entry with two keys is fatal' ); |
| 177 |
like( $err, qr/additional_config_readers entry must have exactly one key/, |
| 178 |
'and says what the requirement is' ); |
| 179 |
}; |
| 180 |
|
| 181 |
subtest 'an unloadable reader fails loudly' => sub { |
| 182 |
my $err = exception { read_with('TestReader::NoSuchThing') }; |
| 183 |
|
| 184 |
ok( defined $err, 'naming a reader that does not exist is fatal' ); |
| 185 |
like( $err, qr/TestReader::NoSuchThing|Can't locate/, |
| 186 |
'and the error names what could not be loaded' ); |
| 187 |
}; |
| 188 |
|
| 189 |
done_testing(); |