tests for the config( ...->{file} ) option. addresses bug when ->{file} is a dir...
Torbjørn Lindahl [Thu, 9 Jan 2014 15:42:08 +0000 (16:42 +0100)]
one TestApp per case because config cannot be changed after ->setup

27 files changed:
t/25-setting-config-file.t [new file with mode: 0644]
t/lib/TestApp1.pm [new file with mode: 0644]
t/lib/TestApp1/Controller/Config.pm [new file with mode: 0644]
t/lib/TestApp1/Controller/Root.pm [new file with mode: 0644]
t/lib/TestApp1/customconfig.conf [new file with mode: 0644]
t/lib/TestApp1/testapp.pl [new file with mode: 0644]
t/lib/TestApp2.pm [new file with mode: 0644]
t/lib/TestApp2/Controller/Config.pm [new file with mode: 0644]
t/lib/TestApp2/Controller/Root.pm [new file with mode: 0644]
t/lib/TestApp2/customconfig.conf [new file with mode: 0644]
t/lib/TestApp2/testapp.pl [new file with mode: 0644]
t/lib/TestApp3.pm [new file with mode: 0644]
t/lib/TestApp3/Controller/Config.pm [new file with mode: 0644]
t/lib/TestApp3/Controller/Root.pm [new file with mode: 0644]
t/lib/TestApp3/config/another_file.pl [new file with mode: 0644]
t/lib/TestApp3/config/testapp3.conf [new file with mode: 0644]
t/lib/TestApp3/config/testapp3.pl [new file with mode: 0644]
t/lib/TestApp3/config/testapp3.yml [new file with mode: 0644]
t/lib/TestApp3/testapp.pl [new file with mode: 0644]
t/lib/TestApp4.pm [new file with mode: 0644]
t/lib/TestApp4/Controller/Config.pm [new file with mode: 0644]
t/lib/TestApp4/Controller/Root.pm [new file with mode: 0644]
t/lib/TestApp4/config.d/another_file.pl [new file with mode: 0644]
t/lib/TestApp4/config.d/testapp4.conf [new file with mode: 0644]
t/lib/TestApp4/config.d/testapp4.pl [new file with mode: 0644]
t/lib/TestApp4/config.d/testapp4.yml [new file with mode: 0644]
t/lib/TestApp4/testapp.pl [new file with mode: 0644]

diff --git a/t/25-setting-config-file.t b/t/25-setting-config-file.t
new file mode 100644 (file)
index 0000000..6e4e9dc
--- /dev/null
@@ -0,0 +1,119 @@
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More;
+
+BEGIN {
+
+    # Remove all relevant env variables to avoid accidental fail
+    foreach my $name ( grep { m{^(CATALYST|MOCKAPP)} } keys %ENV ) {
+        delete $ENV{ $name };
+    }
+
+    eval { require Catalyst; Catalyst->VERSION( '5.80001' ); };
+
+    plan skip_all => 'Catalyst 5.80001 required' if $@;
+    # plan tests => 3;
+
+    require_ok 'Catalyst::Test';
+
+}
+
+## TestApp1: a .conf config file exists but should not be loaded
+{
+
+    Catalyst::Test->import('TestApp1');
+
+    note( "TestApp1" );
+
+    ok my ( $res, $c ) = ctx_request( '/' ), 'context object';
+
+    isa_ok( $c, "TestApp1" );
+
+    subtest "normal config loaded" => sub {
+
+        is( get( '/appconfig/foo' ), "bar1", "config var foo ok" );
+
+        ## a config var not set will give a blank web page hence ""
+        is( get( '/appconfig/bar' ), "", "config var in custom config" );
+
+    };
+    is( get( '/appconfig/bar' ), "", "custom config not loaded" );
+}
+
+## TestApp2: config points to a file in addition to normal config and
+## should get loaded
+{
+    Catalyst::Test->import('TestApp2');
+
+    note( "TestApp2" );
+
+    ok my ( $res, $c ) = ctx_request( '/' ), 'context object';
+
+    isa_ok( $c, "TestApp2" );
+
+    subtest "normal config loaded" => sub {
+
+        is( get( '/appconfig/foo' ), "bar2", "config var foo" );
+
+        is( get( '/appconfig/unspecified_variable' ), "", "unknown config var" );
+
+    };
+
+    is( get( '/appconfig/bar' ), "baz2", "custom config loaded" );
+}
+
+## TestApp3: config points to a directory
+{
+    Catalyst::Test->import('TestApp3');
+
+    note( "TestApp3" );
+
+    ok my ( $res, $c ) = ctx_request( '/' ), 'context object';
+
+    isa_ok( $c, "TestApp3" );
+
+    subtest "normal config loaded" => sub {
+
+        is( get( '/appconfig/foo' ), "bar3", "config var foo" );
+
+        is( get( '/appconfig/unspecified_variable' ), "", "unknown config var" );
+
+    };
+
+    is( get( '/appconfig/test3_conf1' ), "a_value", "custom config var1 set" );
+    is( get( '/appconfig/test3_conf2' ), "a_value", "custom config var2 set" );
+    is( get( '/appconfig/test3_conf3' ), "a_value", "custom config var3 set" );
+    is( get( '/appconfig/test3_conf4' ), "", "custom config var4 not set" );
+
+}
+
+## TestApp4: config points to a directory with a suffix
+{
+    Catalyst::Test->import('TestApp4');
+
+    note( "TestApp4" );
+
+    ok my ( $res, $c ) = ctx_request( '/' ), 'context object';
+
+    isa_ok( $c, "TestApp4" );
+
+    subtest "normal config loaded" => sub {
+
+        is( get( '/appconfig/foo' ), "bar4", "config var foo" );
+
+        is( get( '/appconfig/unspecified_variable' ), "", "unknown config var" );
+
+    };
+
+    is( get( '/appconfig/test4_conf1' ), "a_value", "custom config var1 set" );
+    is( get( '/appconfig/test4_conf2' ), "a_value", "custom config var2 set" );
+    is( get( '/appconfig/test4_conf3' ), "a_value", "custom config var3 set" );
+    is( get( '/appconfig/test4_conf4' ), "", "custom config var4 not set" );
+
+}
+
+done_testing;
diff --git a/t/lib/TestApp1.pm b/t/lib/TestApp1.pm
new file mode 100644 (file)
index 0000000..dd15629
--- /dev/null
@@ -0,0 +1,20 @@
+package TestApp1;
+
+use strict;
+use warnings;
+
+use MRO::Compat;
+
+use Catalyst qw/ConfigLoader/;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->setup;
+
+sub finalize_config {
+    my $c = shift;
+    $c->config( foo => 'bar1' );
+    $c->next::method( @_ );
+}
+
+1;
diff --git a/t/lib/TestApp1/Controller/Config.pm b/t/lib/TestApp1/Controller/Config.pm
new file mode 100644 (file)
index 0000000..95ddd93
--- /dev/null
@@ -0,0 +1,19 @@
+package TestApp1::Controller::Config;
+
+use strict;
+use warnings;
+
+use base qw( Catalyst::Controller );
+
+sub index : Private {
+    my ( $self, $c ) = @_;
+    $c->res->output( $self->{ foo } );
+}
+
+sub appconfig : Global {
+    my ( $self, $c, $var ) = @_;
+
+    $c->res->body( $c->config->{ $var } );
+}
+
+1;
diff --git a/t/lib/TestApp1/Controller/Root.pm b/t/lib/TestApp1/Controller/Root.pm
new file mode 100644 (file)
index 0000000..24f6d76
--- /dev/null
@@ -0,0 +1,16 @@
+package TestApp1::Controller::Root;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default :Path {
+    my ( $self, $c ) = @_;
+    $c->response->body( 'Page not found' );
+    $c->response->status(404);
+}
+
+1;
diff --git a/t/lib/TestApp1/customconfig.conf b/t/lib/TestApp1/customconfig.conf
new file mode 100644 (file)
index 0000000..3503495
--- /dev/null
@@ -0,0 +1 @@
+bar = baz
\ No newline at end of file
diff --git a/t/lib/TestApp1/testapp.pl b/t/lib/TestApp1/testapp.pl
new file mode 100644 (file)
index 0000000..70dc31d
--- /dev/null
@@ -0,0 +1,5 @@
+{   name               => 'TestApp2',
+    Controller::Config => { foo => 'foo' },
+    cache              => '__HOME__/cache',
+    multi              => '__HOME__,__path_to(x)__,__HOME__,__path_to(y)__',
+}
diff --git a/t/lib/TestApp2.pm b/t/lib/TestApp2.pm
new file mode 100644 (file)
index 0000000..ab6c914
--- /dev/null
@@ -0,0 +1,26 @@
+package TestApp2;
+
+use strict;
+use warnings;
+
+use MRO::Compat;
+
+use Catalyst qw/ConfigLoader/;
+
+__PACKAGE__->config( "Plugin::ConfigLoader",
+                     {
+                         file => __PACKAGE__->path_to( "customconfig.conf" )
+                     }
+                 );
+
+our $VERSION = '0.01';
+
+__PACKAGE__->setup;
+
+sub finalize_config {
+    my $c = shift;
+    $c->config( foo => 'bar2' );
+    $c->next::method( @_ );
+}
+
+1;
diff --git a/t/lib/TestApp2/Controller/Config.pm b/t/lib/TestApp2/Controller/Config.pm
new file mode 100644 (file)
index 0000000..ab2323d
--- /dev/null
@@ -0,0 +1,19 @@
+package TestApp2::Controller::Config;
+
+use strict;
+use warnings;
+
+use base qw( Catalyst::Controller );
+
+sub index : Private {
+    my ( $self, $c ) = @_;
+    $c->res->output( $self->{ foo } );
+}
+
+sub appconfig : Global {
+    my ( $self, $c, $var ) = @_;
+
+    $c->res->body( $c->config->{ $var } );
+}
+
+1;
diff --git a/t/lib/TestApp2/Controller/Root.pm b/t/lib/TestApp2/Controller/Root.pm
new file mode 100644 (file)
index 0000000..0b0257d
--- /dev/null
@@ -0,0 +1,16 @@
+package TestApp2::Controller::Root;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default :Path {
+    my ( $self, $c ) = @_;
+    $c->response->body( 'Page not found' );
+    $c->response->status(404);
+}
+
+1;
diff --git a/t/lib/TestApp2/customconfig.conf b/t/lib/TestApp2/customconfig.conf
new file mode 100644 (file)
index 0000000..57ac742
--- /dev/null
@@ -0,0 +1 @@
+bar = baz2
\ No newline at end of file
diff --git a/t/lib/TestApp2/testapp.pl b/t/lib/TestApp2/testapp.pl
new file mode 100644 (file)
index 0000000..70dc31d
--- /dev/null
@@ -0,0 +1,5 @@
+{   name               => 'TestApp2',
+    Controller::Config => { foo => 'foo' },
+    cache              => '__HOME__/cache',
+    multi              => '__HOME__,__path_to(x)__,__HOME__,__path_to(y)__',
+}
diff --git a/t/lib/TestApp3.pm b/t/lib/TestApp3.pm
new file mode 100644 (file)
index 0000000..0324c95
--- /dev/null
@@ -0,0 +1,26 @@
+package TestApp3;
+
+use strict;
+use warnings;
+
+use MRO::Compat;
+
+use Catalyst qw/ConfigLoader/;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->config(
+    "Plugin::ConfigLoader" => {
+        file => __PACKAGE__->path_to( "config" )
+    }
+);
+
+__PACKAGE__->setup;
+
+sub finalize_config {
+    my $c = shift;
+    $c->config( foo => 'bar3' );
+    $c->next::method( @_ );
+}
+
+1;
diff --git a/t/lib/TestApp3/Controller/Config.pm b/t/lib/TestApp3/Controller/Config.pm
new file mode 100644 (file)
index 0000000..a19be2f
--- /dev/null
@@ -0,0 +1,18 @@
+package TestApp3::Controller::Config;
+
+use strict;
+use warnings;
+
+use base qw( Catalyst::Controller );
+
+sub index : Private {
+    my ( $self, $c ) = @_;
+    $c->res->output( $self->{ foo } );
+}
+
+sub appconfig : Global {
+    my ( $self, $c, $var ) = @_;
+    $c->res->body( $c->config->{ $var } );
+}
+
+1;
diff --git a/t/lib/TestApp3/Controller/Root.pm b/t/lib/TestApp3/Controller/Root.pm
new file mode 100644 (file)
index 0000000..a682eaa
--- /dev/null
@@ -0,0 +1,16 @@
+package TestApp3::Controller::Root;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default :Path {
+    my ( $self, $c ) = @_;
+    $c->response->body( 'Page not found' );
+    $c->response->status(404);
+}
+
+1;
diff --git a/t/lib/TestApp3/config/another_file.pl b/t/lib/TestApp3/config/another_file.pl
new file mode 100644 (file)
index 0000000..9644bf2
--- /dev/null
@@ -0,0 +1,3 @@
+{
+    test4_conf => "this is not set"
+}
diff --git a/t/lib/TestApp3/config/testapp3.conf b/t/lib/TestApp3/config/testapp3.conf
new file mode 100644 (file)
index 0000000..c7f8d7c
--- /dev/null
@@ -0,0 +1 @@
+test3_conf2 = a_value
diff --git a/t/lib/TestApp3/config/testapp3.pl b/t/lib/TestApp3/config/testapp3.pl
new file mode 100644 (file)
index 0000000..b224221
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  test3_conf3 => "a_value"
+}
diff --git a/t/lib/TestApp3/config/testapp3.yml b/t/lib/TestApp3/config/testapp3.yml
new file mode 100644 (file)
index 0000000..6d8ad68
--- /dev/null
@@ -0,0 +1 @@
+test3_conf1: a_value
diff --git a/t/lib/TestApp3/testapp.pl b/t/lib/TestApp3/testapp.pl
new file mode 100644 (file)
index 0000000..a872b6a
--- /dev/null
@@ -0,0 +1,5 @@
+{   name               => 'TestApp',
+    Controller::Config => { foo => 'foo' },
+    cache              => '__HOME__/cache',
+    multi              => '__HOME__,__path_to(x)__,__HOME__,__path_to(y)__',
+}
diff --git a/t/lib/TestApp4.pm b/t/lib/TestApp4.pm
new file mode 100644 (file)
index 0000000..99fd3df
--- /dev/null
@@ -0,0 +1,26 @@
+package TestApp4;
+
+use strict;
+use warnings;
+
+use MRO::Compat;
+
+use Catalyst qw/ConfigLoader/;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->config(
+    "Plugin::ConfigLoader" => {
+        file => __PACKAGE__->path_to( "config.d" )
+    }
+);
+
+__PACKAGE__->setup;
+
+sub finalize_config {
+    my $c = shift;
+    $c->config( foo => 'bar4' );
+    $c->next::method( @_ );
+}
+
+1;
diff --git a/t/lib/TestApp4/Controller/Config.pm b/t/lib/TestApp4/Controller/Config.pm
new file mode 100644 (file)
index 0000000..8f1d944
--- /dev/null
@@ -0,0 +1,18 @@
+package TestApp4::Controller::Config;
+
+use strict;
+use warnings;
+
+use base qw( Catalyst::Controller );
+
+sub index : Private {
+    my ( $self, $c ) = @_;
+    $c->res->output( $self->{ foo } );
+}
+
+sub appconfig : Global {
+    my ( $self, $c, $var ) = @_;
+    $c->res->body( $c->config->{ $var } );
+}
+
+1;
diff --git a/t/lib/TestApp4/Controller/Root.pm b/t/lib/TestApp4/Controller/Root.pm
new file mode 100644 (file)
index 0000000..bf232ba
--- /dev/null
@@ -0,0 +1,16 @@
+package TestApp4::Controller::Root;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default :Path {
+    my ( $self, $c ) = @_;
+    $c->response->body( 'Page not found' );
+    $c->response->status(404);
+}
+
+1;
diff --git a/t/lib/TestApp4/config.d/another_file.pl b/t/lib/TestApp4/config.d/another_file.pl
new file mode 100644 (file)
index 0000000..9644bf2
--- /dev/null
@@ -0,0 +1,3 @@
+{
+    test4_conf => "this is not set"
+}
diff --git a/t/lib/TestApp4/config.d/testapp4.conf b/t/lib/TestApp4/config.d/testapp4.conf
new file mode 100644 (file)
index 0000000..c7395eb
--- /dev/null
@@ -0,0 +1 @@
+test4_conf2 = a_value
diff --git a/t/lib/TestApp4/config.d/testapp4.pl b/t/lib/TestApp4/config.d/testapp4.pl
new file mode 100644 (file)
index 0000000..f870add
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  test4_conf3 => "a_value"
+}
diff --git a/t/lib/TestApp4/config.d/testapp4.yml b/t/lib/TestApp4/config.d/testapp4.yml
new file mode 100644 (file)
index 0000000..f6bcea2
--- /dev/null
@@ -0,0 +1 @@
+test4_conf1: a_value
diff --git a/t/lib/TestApp4/testapp.pl b/t/lib/TestApp4/testapp.pl
new file mode 100644 (file)
index 0000000..a872b6a
--- /dev/null
@@ -0,0 +1,5 @@
+{   name               => 'TestApp',
+    Controller::Config => { foo => 'foo' },
+    cache              => '__HOME__/cache',
+    multi              => '__HOME__,__path_to(x)__,__HOME__,__path_to(y)__',
+}