| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Path::Tiny (); |
| 6 |
|
| 7 |
use Dancer2::ConfigReader; |
| 8 |
|
| 9 |
# How the config files combine. Dancer2 reads, in order: |
| 10 |
# |
| 11 |
# <location>/config.yml |
| 12 |
# <location>/config_local.yml |
| 13 |
# <location>/environments/<environment>.yml |
| 14 |
# <location>/environments/<environment>_local.yml |
| 15 |
# |
| 16 |
# and merges each over the last with Hash::Merge::Simple, which merges *into* |
| 17 |
# nested hashes rather than replacing them. That distinction is the point of |
| 18 |
# this file: an environment file that sets one key inside engines.session.Simple |
| 19 |
# must not wipe out the sibling keys the base config set there. |
| 20 |
|
| 21 |
my $DIR; |
| 22 |
BEGIN { |
| 23 |
$DIR = Path::Tiny->tempdir; |
| 24 |
$DIR->child('environments')->mkpath; |
| 25 |
|
| 26 |
$DIR->child('config.yml')->spew_utf8( <<'YML' ); |
| 27 |
appname: BaseApp |
| 28 |
layout: base_layout |
| 29 |
base_only: base_value |
| 30 |
strict_config: 0 |
| 31 |
engines: |
| 32 |
session: |
| 33 |
Simple: |
| 34 |
cookie_name: base.sid |
| 35 |
cookie_path: /base |
| 36 |
is_secure: 0 |
| 37 |
template: |
| 38 |
TemplateToolkit: |
| 39 |
start_tag: '[%' |
| 40 |
YML |
| 41 |
|
| 42 |
$DIR->child('config_local.yml')->spew_utf8( <<'YML' ); |
| 43 |
layout: local_layout |
| 44 |
local_only: local_value |
| 45 |
YML |
| 46 |
|
| 47 |
$DIR->child( 'environments', 'production.yml' )->spew_utf8( <<'YML' ); |
| 48 |
layout: prod_layout |
| 49 |
prod_only: prod_value |
| 50 |
engines: |
| 51 |
session: |
| 52 |
Simple: |
| 53 |
cookie_name: prod.sid |
| 54 |
YML |
| 55 |
|
| 56 |
$DIR->child( 'environments', 'development.yml' )->spew_utf8( <<'YML' ); |
| 57 |
layout: dev_layout |
| 58 |
YML |
| 59 |
} |
| 60 |
|
| 61 |
# DANCER_CONFDIR would override the location we pass in, so make sure a value |
| 62 |
# inherited from the surrounding environment cannot reach these tests. |
| 63 |
sub read_config_for { |
| 64 |
my $environment = shift; |
| 65 |
local $ENV{DANCER_CONFDIR}; |
| 66 |
delete local $ENV{DANCER_CONFDIR}; |
| 67 |
local $ENV{DANCER_ENVDIR}; |
| 68 |
delete local $ENV{DANCER_ENVDIR}; |
| 69 |
|
| 70 |
return Dancer2::ConfigReader->new( |
| 71 |
location => $DIR->stringify, |
| 72 |
environment => $environment, |
| 73 |
default_config => {}, |
| 74 |
)->config; |
| 75 |
} |
| 76 |
|
| 77 |
subtest 'each file layer overrides the one before it' => sub { |
| 78 |
my $config = read_config_for('production'); |
| 79 |
|
| 80 |
# The environment file is read last, so it wins outright. |
| 81 |
is( $config->{layout}, 'prod_layout', |
| 82 |
'the environment file beats config_local and config' ); |
| 83 |
|
| 84 |
# Keys only one layer sets all survive. |
| 85 |
is( $config->{appname}, 'BaseApp', 'a base-only key survives' ); |
| 86 |
is( $config->{base_only}, 'base_value', 'and so does another' ); |
| 87 |
is( $config->{local_only}, 'local_value', 'a config_local-only key survives' ); |
| 88 |
is( $config->{prod_only}, 'prod_value', 'an environment-only key survives' ); |
| 89 |
}; |
| 90 |
|
| 91 |
subtest 'a different environment picks up a different file' => sub { |
| 92 |
my $config = read_config_for('development'); |
| 93 |
|
| 94 |
is( $config->{layout}, 'dev_layout', |
| 95 |
'the development environment file is used instead' ); |
| 96 |
|
| 97 |
# config_local still applies - it is not environment-specific. |
| 98 |
is( $config->{local_only}, 'local_value', |
| 99 |
'config_local applies regardless of environment' ); |
| 100 |
is( $config->{base_only}, 'base_value', 'and so does the base config' ); |
| 101 |
|
| 102 |
# production.yml must not have leaked in. |
| 103 |
is( $config->{prod_only}, undef, |
| 104 |
'the production environment file is not read' ); |
| 105 |
}; |
| 106 |
|
| 107 |
subtest 'an environment file merges into a nested block, not over it' => sub { |
| 108 |
my $config = read_config_for('production'); |
| 109 |
|
| 110 |
my $simple = $config->{engines}{session}{Simple}; |
| 111 |
|
| 112 |
# This is the assertion this file exists for. production.yml sets only |
| 113 |
# cookie_name inside engines.session.Simple. If the merge replaced the |
| 114 |
# block instead of merging into it, cookie_path and is_secure would be gone |
| 115 |
# and the session engine would silently fall back to its defaults. |
| 116 |
is( $simple->{cookie_name}, 'prod.sid', |
| 117 |
'the environment file overrides the key it sets' ); |
| 118 |
is( $simple->{cookie_path}, '/base', |
| 119 |
'while a sibling key from the base config survives' ); |
| 120 |
is( $simple->{is_secure}, 0, |
| 121 |
'including one whose value is false' ); |
| 122 |
|
| 123 |
is_deeply( |
| 124 |
[ sort keys %$simple ], |
| 125 |
[ 'cookie_name', 'cookie_path', 'is_secure' ], |
| 126 |
'the block has every key from both files and nothing else', |
| 127 |
); |
| 128 |
|
| 129 |
# A sibling block the environment file never mentions is untouched. |
| 130 |
is_deeply( |
| 131 |
$config->{engines}{template}, |
| 132 |
{ TemplateToolkit => { start_tag => '[%' } }, |
| 133 |
'an engine block the environment file does not mention is unchanged', |
| 134 |
); |
| 135 |
}; |
| 136 |
|
| 137 |
subtest 'a missing config directory is not an error' => sub { |
| 138 |
my $empty = Path::Tiny->tempdir; |
| 139 |
|
| 140 |
local $ENV{DANCER_CONFDIR}; |
| 141 |
delete local $ENV{DANCER_CONFDIR}; |
| 142 |
local $ENV{DANCER_ENVDIR}; |
| 143 |
delete local $ENV{DANCER_ENVDIR}; |
| 144 |
|
| 145 |
# 'from_default' is not a key Dancer2 knows, and strict_config defaults to |
| 146 |
# on, so reading this warns. Captured and asserted rather than silenced, |
| 147 |
# because it pins something worth knowing: the strict-key check inspects |
| 148 |
# the whole merged config, including whatever arrived via default_config, |
| 149 |
# not only what was read from files. |
| 150 |
my @warnings; |
| 151 |
my $config = do { |
| 152 |
local $SIG{__WARN__} = sub { push @warnings, $_[0] }; |
| 153 |
Dancer2::ConfigReader->new( |
| 154 |
location => $empty->stringify, |
| 155 |
environment => 'production', |
| 156 |
default_config => { from_default => 'default_value' }, |
| 157 |
)->config; |
| 158 |
}; |
| 159 |
|
| 160 |
is( $config->{from_default}, 'default_value', |
| 161 |
'the default config comes through when there are no files' ); |
| 162 |
is( $config->{appname}, undef, 'and nothing is invented' ); |
| 163 |
|
| 164 |
is( scalar @warnings, 1, 'exactly one warning is emitted' ); |
| 165 |
like( $warnings[0], qr/Unknown configuration key 'from_default'/, |
| 166 |
'and it is the strict-key check reaching a default_config key' ); |
| 167 |
}; |
| 168 |
|
| 169 |
done_testing(); |