| 1 | package Poet::t::Conf; | ||||||||||
| 2 | 1 | $Poet::t::Conf::VERSION = '0.16'; | |||||||||
| 3 | 1 | ▶ | use Poet::Tools qw(read_file tempdir_simple write_file); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use IPC::System::Simple qw(run); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Test::Class::Most parent => 'Poet::Test::Class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | require Poet; | |||||||||
| 8 | |||||||||||
| 9 | 1 | my $conf_files = { | |||||||||
| 10 | 'global/first.cfg' => { a => 10, b => 6, c => 11, d => 12 }, | ||||||||||
| 11 | 'global/second.cfg' => { e => 20 }, | ||||||||||
| 12 | 'global/third-not-a-config' => { f => 30 }, | ||||||||||
| 13 | 'layer/personal.cfg' => { c => 40, g => '_${b}', h => 51 }, | ||||||||||
| 14 | 'layer/development.cfg' => { f => 30 }, | ||||||||||
| 15 | 'local.cfg' => { h => '__${e}', layer => 'personal' }, | ||||||||||
| 16 | }; | ||||||||||
| 17 | |||||||||||
| 18 | 1 | my $expected_values = { | |||||||||
| 19 | a => 10, | ||||||||||
| 20 | b => 6, | ||||||||||
| 21 | c => 40, | ||||||||||
| 22 | d => 12, | ||||||||||
| 23 | e => 20, | ||||||||||
| 24 | f => undef, | ||||||||||
| 25 | g => "_6", | ||||||||||
| 26 | h => "__20", | ||||||||||
| 27 | }; | ||||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | sub test_global : Tests { | ||||||||
|
Subroutine
test_global: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 30 | 1 | my $self = shift; | |||||||||
| 31 | 1 | my $poet = $self->temp_env( conf_files => $conf_files ); | |||||||||
| 32 | 1 | my $conf = $poet->conf(); | |||||||||
| 33 | 1 | ▶ | while ( my ( $key, $value ) = each(%$expected_values) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 8 | ▶ | is( $conf->get($key), $value, "$key = " . ( defined($value) ? $value : 'undef' ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | } | ||||||||||
| 36 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 1 | ▶ | sub test_duplicate : Tests { | ||||||||
|
Subroutine
test_duplicate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | 1 | my $self = shift; | |||||||||
| 40 | throws_ok( | ||||||||||
| 41 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | $self->temp_env( | |||||||||
| 43 | conf_files => { %$conf_files, 'global/fourth.cfg' => { j => 71, c => 72 } } ); | ||||||||||
| 44 | }, | ||||||||||
| 45 | 1 | qr/key 'c' defined in both '.*(first|fourth)\.cfg' and '.*(first|fourth)\.cfg'/, | |||||||||
| 46 | 'cannot define same key in multiple global config files' | ||||||||||
| 47 | ); | ||||||||||
| 48 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | sub test_set_local : Tests { | ||||||||
|
Subroutine
test_set_local: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | 1 | my $self = shift; | |||||||||
| 52 | 1 | my $poet = $self->temp_env( conf_files => { 'global/foo.cfg' => { a => 5, b => 6, c => 7 } } ); | |||||||||
| 53 | 1 | my $conf = $poet->conf(); | |||||||||
| 54 | |||||||||||
| 55 | 1 | is( $conf->get('a'), 5, 'a = 5' ); | |||||||||
| 56 | 1 | is( $conf->get( 'b' => 0 ), 6, 'b = 6' ); | |||||||||
| 57 | 1 | is( $conf->get('c'), 7, 'c = 7' ); | |||||||||
| 58 | |||||||||||
| 59 | { | ||||||||||
| 60 | 1 | my $lex = $conf->set_local( { a => 15, c => 17 } ); | |||||||||
| 61 | 1 | is( $conf->get('a'), 15, 'a = 15 (set_local)' ); | |||||||||
| 62 | 1 | is( $conf->get('b'), 6, 'b = 6 (set_local)' ); | |||||||||
| 63 | 1 | is( $conf->get('c'), 17, 'c = 17 (set_local)' ); | |||||||||
| 64 | { | ||||||||||
| 65 | 1 | my $lex = $conf->set_local( { c => 27 } ); | |||||||||
| 66 | 1 | is( $conf->get('c'), 27, 'c = 27 (set_local depth 2)' ); | |||||||||
| 67 | |||||||||||
| 68 | 1 | my $lex2 = $conf->set_local( { c => 37 } ); | |||||||||
| 69 | 1 | $lex2 = 'shiny'; | |||||||||
| 70 | 1 | is( $conf->get('c'), 27, 'c = 27 (did not hold on to lexical)' ); | |||||||||
| 71 | } | ||||||||||
| 72 | 1 | is( $conf->get('c'), 17, 'c = 17 (set_local)' ); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | is( $conf->get('a'), 5, 'a = 5 (restored)' ); | |||||||||
| 76 | 1 | is( $conf->get('b'), 6, 'b = 6 (restored)' ); | |||||||||
| 77 | 1 | is( $conf->get('c'), 7, 'c = 7 (restored)' ); | |||||||||
| 78 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | sub test_dot_notation : Tests { | ||||||||
|
Subroutine
test_dot_notation: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 81 | 1 | my $self = shift; | |||||||||
| 82 | 1 | my $conf_files = { | |||||||||
| 83 | 'layer/personal.cfg' => ' | ||||||||||
| 84 | a: | ||||||||||
| 85 | b: | ||||||||||
| 86 | c: 1 | ||||||||||
| 87 | d: 2 | ||||||||||
| 88 | e: | ||||||||||
| 89 | f: | ||||||||||
| 90 | g: 3 | ||||||||||
| 91 | h: 4 | ||||||||||
| 92 | ', | ||||||||||
| 93 | 'local.cfg' => ' | ||||||||||
| 94 | layer: personal | ||||||||||
| 95 | a.b.c: 6 | ||||||||||
| 96 | e: | ||||||||||
| 97 | f: | ||||||||||
| 98 | g: 7 | ||||||||||
| 99 | |||||||||||
| 100 | ' | ||||||||||
| 101 | }; | ||||||||||
| 102 | 1 | my $poet = $self->temp_env( conf_files => $conf_files ); | |||||||||
| 103 | 1 | my %expected_values = ( | |||||||||
| 104 | 'a' => { 'b' => { c => 6, d => 2 } }, | ||||||||||
| 105 | 'a.b' => { c => 6, d => 2 }, | ||||||||||
| 106 | 'a.b.c' => 6, | ||||||||||
| 107 | 'a.b.d' => 2, | ||||||||||
| 108 | 'e' => { f => { g => 7 } }, | ||||||||||
| 109 | 'e.f' => { g => 7 }, | ||||||||||
| 110 | 'e.f.g' => 7, | ||||||||||
| 111 | 'x.y.z' => undef | ||||||||||
| 112 | ); | ||||||||||
| 113 | 1 | my $conf = $poet->conf(); | |||||||||
| 114 | 3 | ▶ | my $check_expected = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 115 | 3 | my $desc = shift; | |||||||||
| 116 | 3 | foreach my $key ( sort( keys(%expected_values) ) ) { | |||||||||
| 117 | 24 | my $value = $expected_values{$key}; | |||||||||
| 118 | 24 | cmp_deeply( $conf->get($key), $value, "$key - $desc" ); | |||||||||
| 119 | } | ||||||||||
| 120 | 1 | }; | |||||||||
| 121 | |||||||||||
| 122 | 1 | $check_expected->('initial'); | |||||||||
| 123 | 1 | ▶ | throws_ok( sub { $conf->get('a.b.c.z') }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 1 | qr/hash value expected for conf key 'a.b.c', got non-hash '6'/, "a.b.c.z" ); | |||||||||
| 125 | |||||||||||
| 126 | 1 | $conf_files->{'layer/personal.cfg'} = { 'a' => { 'b' => 17 } }; | |||||||||
| 127 | throws_ok( | ||||||||||
| 128 | 1 | ▶ | sub { $poet = $self->temp_env( conf_files => $conf_files ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 129 | 1 | qr/error assigning to 'a.b.c' in .*; 'a.b' already has non-hash value/, | |||||||||
| 130 | "e.f: 17" | ||||||||||
| 131 | ); | ||||||||||
| 132 | |||||||||||
| 133 | { | ||||||||||
| 134 | 1 | my $lex = $conf->set_local( { 'a.b.c' => 16 } ); | |||||||||
| 135 | local $expected_values{'a'} = { 'b' => { c => 16, d => 2 } }, | ||||||||||
| 136 | 1 | local $expected_values{'a.b'} = { c => 16, d => 2 }; | |||||||||
| 137 | 1 | local $expected_values{'a.b.c'} = 16; | |||||||||
| 138 | 1 | $check_expected->('set_local'); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 1 | $check_expected->('after set_local'); | |||||||||
| 142 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 143 | |||||||||||
| 144 | 1 | ▶ | sub test_types : Tests { | ||||||||
|
Subroutine
test_types: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 145 | 1 | my $self = shift; | |||||||||
| 146 | 1 | my $truth = { c => 't', d => 'true', e => 'y', f => 'yes' }; | |||||||||
| 147 | 1 | my $falsity = { c => 'f', d => 'false', e => 'n', f => 'no' }; | |||||||||
| 148 | 1 | my $conf_files = { | |||||||||
| 149 | 'global.cfg' => { | ||||||||||
| 150 | scalar => 5, | ||||||||||
| 151 | list => [ 1, 2, 3 ], | ||||||||||
| 152 | hash => { size => 'large', flavor => 'chocolate' }, | ||||||||||
| 153 | truth => $truth, | ||||||||||
| 154 | falsity => $falsity, | ||||||||||
| 155 | } | ||||||||||
| 156 | }; | ||||||||||
| 157 | |||||||||||
| 158 | 1 | my $poet = $self->temp_env( conf_files => $conf_files ); | |||||||||
| 159 | 1 | my $conf = $poet->conf(); | |||||||||
| 160 | |||||||||||
| 161 | 1 | cmp_deeply( $conf->get_list('list'), [ 1, 2, 3 ], 'list ok' ); | |||||||||
| 162 | 1 | foreach my $key (qw(scalar hash)) { | |||||||||
| 163 | 2 | ▶ | throws_ok( sub { $conf->get_list($key) }, qr/list value expected/ ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 164 | } | ||||||||||
| 165 | 1 | cmp_deeply( $conf->get_hash('hash'), { size => 'large', flavor => 'chocolate' }, 'hash ok' ); | |||||||||
| 166 | 1 | foreach my $key (qw(scalar list)) { | |||||||||
| 167 | 2 | ▶ | throws_ok( sub { $conf->get_hash($key) }, qr/hash value expected/ ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 168 | } | ||||||||||
| 169 | 1 | foreach my $key (qw(c d e f)) { | |||||||||
| 170 | 4 | is( $conf->get_boolean("truth.$key"), 1, "$key = true" ); | |||||||||
| 171 | 4 | is( $conf->get_boolean("falsity.$key"), 0, "$key = false" ); | |||||||||
| 172 | } | ||||||||||
| 173 | 1 | foreach my $key (qw(scalar list hash)) { | |||||||||
| 174 | 3 | ▶ | throws_ok( sub { $conf->get_boolean($key) }, qr/boolean value expected/ ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 175 | } | ||||||||||
| 176 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 177 | |||||||||||
| 178 | 1 | ▶ | sub test_layer_required : Tests { | ||||||||
|
Subroutine
test_layer_required: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 179 | 1 | my $self = shift; | |||||||||
| 180 | throws_ok( | ||||||||||
| 181 | 1 | ▶ | sub { $self->temp_env( conf_files => { 'local.cfg' => {} } ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 182 | 1 | qr/must specify layer/, | |||||||||
| 183 | 'no layer' | ||||||||||
| 184 | ); | ||||||||||
| 185 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 186 | |||||||||||
| 187 | 1 | ▶ | sub test_interpolation : Tests { | ||||||||
|
Subroutine
test_interpolation: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 188 | 1 | my $self = shift; | |||||||||
| 189 | 1 | my $poet = $self->temp_env( | |||||||||
| 190 | conf => { | ||||||||||
| 191 | layer => 'development', | ||||||||||
| 192 | 'a.b' => 'bar', | ||||||||||
| 193 | 'c' => '/foo/${a.b}/baz', | ||||||||||
| 194 | 'd' => '/foo/${huh}/baz', | ||||||||||
| 195 | 'deep' => { 'e' => 5, 'f' => [ '${c}', ['${a.b}'] ] } | ||||||||||
| 196 | } | ||||||||||
| 197 | ); | ||||||||||
| 198 | 1 | my $conf = $poet->conf(); | |||||||||
| 199 | 1 | is( $conf->get('c'), '/foo/bar/baz', 'substitution' ); | |||||||||
| 200 | 1 | ▶ | throws_ok { $conf->get('d') } qr/could not get conf for 'huh'/, 'bad substitution'; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 201 | 1 | cmp_deeply( | |||||||||
| 202 | $conf->get('deep'), | ||||||||||
| 203 | { | ||||||||||
| 204 | e => 5, | ||||||||||
| 205 | f => [ '/foo/bar/baz', ['bar'] ] | ||||||||||
| 206 | }, | ||||||||||
| 207 | 'deep' | ||||||||||
| 208 | ); | ||||||||||
| 209 | |||||||||||
| 210 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 211 | |||||||||||
| 212 | 1 | ▶ | sub test_dynamic_conf : Tests { | ||||||||
|
Subroutine
test_dynamic_conf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 213 | 1 | my $self = shift; | |||||||||
| 214 | 1 | my $poet = $self->temp_env(); | |||||||||
| 215 | 1 | write_file( $poet->conf_path("dynamic/foo.mc"), "<% 2+2 %>" ); | |||||||||
| 216 | 1 | ok( !-d $poet->data_path("conf/dynamic"), "data/conf/dynamic does not exist" ); | |||||||||
| 217 | 1 | run( $poet->conf_path("dynamic/gen.pl") ); | |||||||||
| 218 | 1 | ok( -d $poet->data_path("conf/dynamic"), "data/conf/dynamic exists" ); | |||||||||
| 219 | 1 | ok( -f $poet->data_path("conf/dynamic/foo"), "conf/dynamic/foo exists" ); | |||||||||
| 220 | 1 | is( read_file( $poet->data_path("conf/dynamic/foo") ), 4, "foo has correct content" ); | |||||||||
| 221 | 1 | ok( !-f $poet->data_path("conf/dynamic/gen.pl"), "conf/dynamic/gen.pl does not exist" ); | |||||||||
| 222 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 223 | |||||||||||
| 224 | 1 | ▶ | sub test_get_secure : Tests { | ||||||||
|
Subroutine
test_get_secure: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 225 | 1 | my $self = shift; | |||||||||
| 226 | 1 | my $tempdir = tempdir_simple('poet-conf-XXXX'); | |||||||||
| 227 | 1 | my $secure_file = "$tempdir/supersecret.cfg"; | |||||||||
| 228 | 1 | write_file( $secure_file, "foo: 7\nbar: 8\n" ); | |||||||||
| 229 | 1 | my $poet = $self->temp_env( | |||||||||
| 230 | conf_files => { | ||||||||||
| 231 | 'local.cfg' => { | ||||||||||
| 232 | layer => 'development', | ||||||||||
| 233 | 'foo' => 0, | ||||||||||
| 234 | 'baz' => 9, | ||||||||||
| 235 | 'conf.secure_conf_file' => $secure_file | ||||||||||
| 236 | } | ||||||||||
| 237 | } | ||||||||||
| 238 | ); | ||||||||||
| 239 | 1 | my $conf = $poet->conf; | |||||||||
| 240 | |||||||||||
| 241 | 1 | is( $conf->get_secure('foo'), 7, "foo=0" ); | |||||||||
| 242 | 1 | is( $conf->get_secure('bar'), 8, "bar=8" ); | |||||||||
| 243 | 1 | is( $conf->get_secure('baz'), 9, "baz=9" ); | |||||||||
| 244 | 1 | is( $conf->get_secure('blargh'), undef, "blargh=undef" ); | |||||||||
| 245 | |||||||||||
| 246 | 1 | is( $conf->get('bar'), undef, "bar=undef" ); | |||||||||
| 247 | 1 | ok( ( !grep { /bar/ } $conf->get_keys() ), "no bar in keys" ); | |||||||||
| 248 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 249 | |||||||||||
| 250 | 1 | 1; | |||||||||