Skip to content

Commit d9901f7

Browse files
committed
Add tests for new ConfigReader
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
1 parent 868a846 commit d9901f7

File tree

8 files changed

+191
-8
lines changed

8 files changed

+191
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!perl
2+
3+
use Dancer2;
4+
use App1;
5+
6+
start;
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
app:
2+
config: ok
3+
extended:
4+
one: ${ENV:DANCER_FILE_EXTENDED_ONE}
5+
two: Begin ${ENV:DANCER_FILE_EXTENDED_TWO} End
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package App1;
2+
use strict;
3+
use warnings;
4+
use Dancer2;
5+
6+
1;

t/config_file_extended.t

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More;
5+
use File::Spec;
6+
7+
BEGIN {
8+
# undefine ENV vars used as defaults for app environment in these tests
9+
local $ENV{DANCER_ENVIRONMENT};
10+
local $ENV{PLACK_ENV};
11+
$ENV{DANCER_CONFIG_READERS} = 'Dancer2::ConfigReader::File::Extended';
12+
$ENV{DANCER_FILE_EXTENDED_ONE} = 'Extended String';
13+
$ENV{DANCER_FILE_EXTENDED_TWO} = 'ExtendedToo';
14+
}
15+
use lib '.';
16+
use lib './t/lib';
17+
18+
use t::app::t_config_file_extended::lib::App1;
19+
20+
my $app = Dancer2->runner->apps->[0];
21+
22+
is_deeply $app->config_files,
23+
[ File::Spec->rel2abs(File::Spec->catfile( 't', 'app',
24+
't_config_file_extended', 'config.yml' )) ],
25+
$app->name . ": config files found";
26+
27+
is $app->config->{app}->{config}, 'ok',
28+
$app->name . ": config loaded properly";
29+
is $app->config->{extended}->{one}, 'Extended String',
30+
$app->name . ": extended config (extended:one) loaded properly";
31+
is $app->config->{extended}->{two}, 'Begin ExtendedToo End',
32+
$app->name . ": extended config (extended:two) loaded properly";
33+
34+
done_testing;

t/config_many.t

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More;
5+
use File::Spec;
6+
7+
BEGIN {
8+
# undefine ENV vars used as defaults for app environment in these tests
9+
local $ENV{DANCER_ENVIRONMENT};
10+
local $ENV{PLACK_ENV};
11+
$ENV{DANCER_CONFIG_READERS}
12+
= 'Dancer2::ConfigReader::File::Simple Dancer2::ConfigReader::TestDummy';
13+
}
14+
use lib '.';
15+
use lib './t/lib';
16+
17+
use t::app::t1::lib::App1;
18+
19+
my $app = Dancer2->runner->apps->[0];
20+
21+
is_deeply $app->config_files,
22+
[ File::Spec->rel2abs(File::Spec->catfile( 't', 'app', 't1', 'config.yml' )) ],
23+
$app->name . ": config files found";
24+
25+
is $app->config->{app}->{config}, 'ok',
26+
$app->name . ": config loaded properly";
27+
is $app->config->{dummy}->{dummy_subitem}, 2,
28+
$app->name . ": dummy config loaded properly";
29+
30+
done_testing;

t/config_reader.t renamed to t/config_role.t

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ my $location2 = File::Spec->rel2abs( path( dirname(__FILE__), 'config2' ) );
2121

2222
package Prod;
2323
use Moo;
24-
with 'Dancer2::Core::Role::ConfigReader';
24+
with 'Dancer2::Core::Role::Config';
2525

2626
sub name {'Prod'}
2727

@@ -31,31 +31,31 @@ my $location2 = File::Spec->rel2abs( path( dirname(__FILE__), 'config2' ) );
3131

3232
package Dev;
3333
use Moo;
34-
with 'Dancer2::Core::Role::ConfigReader';
34+
with 'Dancer2::Core::Role::Config';
3535

3636
sub _build_environment {'development'}
3737
sub _build_location {$location};
3838
sub _build_default_config {$runner->config}
3939

4040
package Failure;
4141
use Moo;
42-
with 'Dancer2::Core::Role::ConfigReader';
42+
with 'Dancer2::Core::Role::Config';
4343

4444
sub _build_environment {'failure'}
4545
sub _build_location {$location}
4646
sub _build_default_config {$runner->config}
4747

4848
package Staging;
4949
use Moo;
50-
with 'Dancer2::Core::Role::ConfigReader';
50+
with 'Dancer2::Core::Role::Config';
5151

5252
sub _build_environment {'staging'}
5353
sub _build_location {$location}
5454
sub _build_default_config {$runner->config}
5555

5656
package Merging;
5757
use Moo;
58-
with 'Dancer2::Core::Role::ConfigReader';
58+
with 'Dancer2::Core::Role::Config';
5959

6060
sub name {'Merging'}
6161

@@ -65,7 +65,7 @@ my $location2 = File::Spec->rel2abs( path( dirname(__FILE__), 'config2' ) );
6565

6666
package LocalConfig;
6767
use Moo;
68-
with 'Dancer2::Core::Role::ConfigReader';
68+
with 'Dancer2::Core::Role::Config';
6969

7070
sub name {'LocalConfig'}
7171

@@ -81,8 +81,8 @@ is_deeply $d->config_files,
8181
"config_files() only sees existing files";
8282

8383
my $f = Prod->new;
84-
is $f->does('Dancer2::Core::Role::ConfigReader'), 1,
85-
"role Dancer2::Core::Role::ConfigReader is consumed";
84+
is $f->does('Dancer2::Core::Role::Config'), 1,
85+
"role Dancer2::Core::Role::Config is consumed";
8686

8787
is_deeply $f->config_files,
8888
[ path( $location, 'config.yml' ),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package Dancer2::ConfigReader::File::Extended;
2+
3+
use Moo;
4+
use Dancer2::Core::Types;
5+
6+
use Carp 'croak';
7+
8+
extends 'Dancer2::ConfigReader::File::Simple';
9+
10+
has name => (
11+
is => 'ro',
12+
isa => Str,
13+
lazy => 0,
14+
default => sub {'File::Extended'},
15+
);
16+
17+
around read_config => sub {
18+
my ($orig, $self) = @_;
19+
my $config = $orig->($self, @_);
20+
$self->_replace_env_vars($config);
21+
return $config;
22+
};
23+
24+
# Attn. We are traversing along the original data structure all the time,
25+
# using references, and changing values on the spot, not returning anything.
26+
sub _replace_env_vars {
27+
my ( $self, $entry ) = @_;
28+
if( ref $entry ne 'HASH' && ref $entry ne 'ARRAY' ) {
29+
croak 'Param entry is not HASH or ARRAY';
30+
}
31+
if( ref $entry eq 'HASH' ) {
32+
foreach my $value (values %{ $entry }) {
33+
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
34+
$self->_replace_env_vars( $value );
35+
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
36+
# Pretty much anything else except SCALAR. Do nothing
37+
1;
38+
} else {
39+
if( $value ) {
40+
while( my ($k, $v) = each %ENV) {
41+
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
42+
}
43+
}
44+
}
45+
}
46+
} else {
47+
# ref $entry is 'ARRAY'
48+
foreach my $value (@{ $entry }) {
49+
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
50+
$self->_replace_env_vars( $value );
51+
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
52+
# Pretty much anything else except SCALAR. Do nothing
53+
1;
54+
} else {
55+
if( $value ) {
56+
while( my ($k, $v) = each %ENV) {
57+
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
58+
}
59+
}
60+
}
61+
}
62+
}
63+
return;
64+
}
65+
66+
1;
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Dancer2::ConfigReader::TestDummy;
2+
use Moo;
3+
use Dancer2::Core::Factory;
4+
use Dancer2::Core;
5+
use Dancer2::Core::Types;
6+
use Dancer2::FileUtils 'path';
7+
8+
with 'Dancer2::Core::Role::ConfigReader';
9+
10+
has name => (
11+
is => 'ro',
12+
isa => Str,
13+
lazy => 0,
14+
default => sub {'TestDummy'},
15+
);
16+
17+
has config_files => (
18+
is => 'ro',
19+
lazy => 1,
20+
isa => ArrayRef,
21+
default => sub {
22+
my ($self) = @_;
23+
return [];
24+
},
25+
);
26+
27+
sub read_config {
28+
my %config = (
29+
dummy => {
30+
dummy_subitem => 2,
31+
}
32+
);
33+
return \%config;
34+
}
35+
36+
1;

0 commit comments

Comments
 (0)