Skip to content

Commit

Permalink
Add tests for new ConfigReader
Browse files Browse the repository at this point in the history
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
  • Loading branch information
mikkoi committed Feb 2, 2022
1 parent a087975 commit dfd9ced
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 8 deletions.
6 changes: 6 additions & 0 deletions t/app/t_config_file_extended/bin/app.psgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!perl

use Dancer2;
use App1;

start;
5 changes: 5 additions & 0 deletions t/app/t_config_file_extended/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app:
config: ok
extended:
one: ${ENV:DANCER_FILE_EXTENDED_ONE}
two: Begin ${ENV:DANCER_FILE_EXTENDED_TWO} End
6 changes: 6 additions & 0 deletions t/app/t_config_file_extended/lib/App1.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package App1;
use strict;
use warnings;
use Dancer2;

1;
34 changes: 34 additions & 0 deletions t/config_file_extended.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use strict;
use warnings;

use Test::More;
use File::Spec;

BEGIN {
# undefine ENV vars used as defaults for app environment in these tests
local $ENV{DANCER_ENVIRONMENT};
local $ENV{PLACK_ENV};
$ENV{DANCER_CONFIG_READERS} = 'Dancer2::ConfigReader::File::Extended';
$ENV{DANCER_FILE_EXTENDED_ONE} = 'Extended String';
$ENV{DANCER_FILE_EXTENDED_TWO} = 'ExtendedToo';
}
use lib '.';
use lib './t/lib';

use t::app::t_config_file_extended::lib::App1;

my $app = Dancer2->runner->apps->[0];

is_deeply $app->config_files,
[ File::Spec->rel2abs(File::Spec->catfile( 't', 'app',
't_config_file_extended', 'config.yml' )) ],
$app->name . ": config files found";

is $app->config->{app}->{config}, 'ok',
$app->name . ": config loaded properly";
is $app->config->{extended}->{one}, 'Extended String',
$app->name . ": extended config (extended:one) loaded properly";
is $app->config->{extended}->{two}, 'Begin ExtendedToo End',
$app->name . ": extended config (extended:two) loaded properly";

done_testing;
30 changes: 30 additions & 0 deletions t/config_many.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use strict;
use warnings;

use Test::More;
use File::Spec;

BEGIN {
# undefine ENV vars used as defaults for app environment in these tests
local $ENV{DANCER_ENVIRONMENT};
local $ENV{PLACK_ENV};
$ENV{DANCER_CONFIG_READERS}
= 'Dancer2::ConfigReader::File::Simple Dancer2::ConfigReader::TestDummy';
}
use lib '.';
use lib './t/lib';

use t::app::t1::lib::App1;

my $app = Dancer2->runner->apps->[0];

is_deeply $app->config_files,
[ File::Spec->rel2abs(File::Spec->catfile( 't', 'app', 't1', 'config.yml' )) ],
$app->name . ": config files found";

is $app->config->{app}->{config}, 'ok',
$app->name . ": config loaded properly";
is $app->config->{dummy}->{dummy_subitem}, 2,
$app->name . ": dummy config loaded properly";

done_testing;
16 changes: 8 additions & 8 deletions t/config_reader.t → t/config_role.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ my $location2 = File::Spec->rel2abs( path( dirname(__FILE__), 'config2' ) );

package Prod;
use Moo;
with 'Dancer2::Core::Role::ConfigReader';
with 'Dancer2::Core::Role::Config';

sub name {'Prod'}

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

package Dev;
use Moo;
with 'Dancer2::Core::Role::ConfigReader';
with 'Dancer2::Core::Role::Config';

sub _build_environment {'development'}
sub _build_location {$location};
sub _build_default_config {$runner->config}

package Failure;
use Moo;
with 'Dancer2::Core::Role::ConfigReader';
with 'Dancer2::Core::Role::Config';

sub _build_environment {'failure'}
sub _build_location {$location}
sub _build_default_config {$runner->config}

package Staging;
use Moo;
with 'Dancer2::Core::Role::ConfigReader';
with 'Dancer2::Core::Role::Config';

sub _build_environment {'staging'}
sub _build_location {$location}
sub _build_default_config {$runner->config}

package Merging;
use Moo;
with 'Dancer2::Core::Role::ConfigReader';
with 'Dancer2::Core::Role::Config';

sub name {'Merging'}

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

package LocalConfig;
use Moo;
with 'Dancer2::Core::Role::ConfigReader';
with 'Dancer2::Core::Role::Config';

sub name {'LocalConfig'}

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

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

is_deeply $f->config_files,
[ path( $location, 'config.yml' ),
Expand Down
66 changes: 66 additions & 0 deletions t/lib/Dancer2/ConfigReader/File/Extended.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package Dancer2::ConfigReader::File::Extended;

use Moo;
use Dancer2::Core::Types;

use Carp 'croak';

extends 'Dancer2::ConfigReader::File::Simple';

has name => (
is => 'ro',
isa => Str,
lazy => 0,
default => sub {'File::Extended'},
);

around read_config => sub {
my ($orig, $self) = @_;
my $config = $orig->($self, @_);
$self->_replace_env_vars($config);
return $config;
};

# Attn. We are traversing along the original data structure all the time,
# using references, and changing values on the spot, not returning anything.
sub _replace_env_vars {
my ( $self, $entry ) = @_;
if( ref $entry ne 'HASH' && ref $entry ne 'ARRAY' ) {
croak 'Param entry is not HASH or ARRAY';
}
if( ref $entry eq 'HASH' ) {
foreach my $value (values %{ $entry }) {
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
$self->_replace_env_vars( $value );
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
# Pretty much anything else except SCALAR. Do nothing
1;
} else {
if( $value ) {
while( my ($k, $v) = each %ENV) {
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
}
}
}
}
} else {
# ref $entry is 'ARRAY'
foreach my $value (@{ $entry }) {
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
$self->_replace_env_vars( $value );
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
# Pretty much anything else except SCALAR. Do nothing
1;
} else {
if( $value ) {
while( my ($k, $v) = each %ENV) {
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
}
}
}
}
}
return;
}

1;
36 changes: 36 additions & 0 deletions t/lib/Dancer2/ConfigReader/TestDummy.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package Dancer2::ConfigReader::TestDummy;
use Moo;
use Dancer2::Core::Factory;
use Dancer2::Core;
use Dancer2::Core::Types;
use Dancer2::FileUtils 'path';

with 'Dancer2::Core::Role::ConfigReader';

has name => (
is => 'ro',
isa => Str,
lazy => 0,
default => sub {'TestDummy'},
);

has config_files => (
is => 'ro',
lazy => 1,
isa => ArrayRef,
default => sub {
my ($self) = @_;
return [];
},
);

sub read_config {
my %config = (
dummy => {
dummy_subitem => 2,
}
);
return \%config;
}

1;

0 comments on commit dfd9ced

Please sign in to comment.