Skip to content

Commit

Permalink
improved Mojolicious::Plugin::Config to accept mode specific config f…
Browse files Browse the repository at this point in the history
…iles without a normal config file (closes #377)
  • Loading branch information
kraih committed Aug 27, 2012
1 parent 7460208 commit ba08389
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@

3.35 2012-08-27
- Improved Mojolicious::Plugin::Config to accept mode specific config files
without a normal config file. (alexbyk, sri)
- Improved documentation.
- Improved tests.

3.34 2012-08-24
- Improved documentation.
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -56,20 +56,19 @@ sub register {
my $home = $app->home;
$file = $home->rel_file($file) unless file_name_is_absolute $file;
$mode = $home->rel_file($mode) if $mode && !file_name_is_absolute $mode;
$mode = undef unless $mode && -e $mode;

# Read config file
my $config = {};
if (-e $file) { $config = $self->load($file, $conf, $app) }

# Check for default
elsif ($conf->{default}) {
$app->log->debug(qq{Config file "$file" not found, using default config.});
# Check for default and mode specific config file
elsif (!$conf->{default} && !$mode) {
die qq{Config file "$file" missing, maybe you need to create it?\n};
}
else { die qq{Config file "$file" missing, maybe you need to create it?\n} }

# Merge everything
$config = {%$config, %{$self->load($mode, $conf, $app)}}
if $mode && -e $mode;
$config = {%$config, %{$self->load($mode, $conf, $app)}} if $mode;
$config = {%{$conf->{default}}, %$config} if $conf->{default};
my $current = $app->defaults(config => $app->config)->config;
%$current = (%$current, %$config);
Expand Down
1 change: 0 additions & 1 deletion lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -115,7 +115,6 @@ sub _perldoc {
$self->res->headers->content_type('text/html;charset="UTF-8"');
}

# "Aw, he looks like a little insane drunken angel."
sub _pod_to_html {
return unless defined(my $pod = shift);

Expand Down
15 changes: 12 additions & 3 deletions t/mojolicious/app.t
Expand Up @@ -7,19 +7,28 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 337;
use Test::More tests => 339;

use FindBin;
use lib "$FindBin::Bin/lib";

# "Congratulations Fry, you've snagged the perfect girlfriend.
# Amy's rich, she's probably got other characteristics..."
use File::Spec::Functions 'catdir';
use Mojo::Date;
use Mojo::Transaction::HTTP;
use Mojolicious;
use Test::Mojo;

# "Congratulations Fry, you've snagged the perfect girlfriend.
# Amy's rich, she's probably got other characteristics..."
# Missing config file
{
eval { Test::Mojo->new('MojoliciousConfigTest')->app };
like $@, qr/mojolicious_config_test.conf" missing/, 'right error';
local $ENV{MOJO_MODE} = 'whatever';
is(Test::Mojo->new('MojoliciousConfigTest')->app->config->{it},
'works', 'right result');
}

my $t = Test::Mojo->new('MojoliciousTest');

# Application is already available
Expand Down
7 changes: 7 additions & 0 deletions t/mojolicious/lib/MojoliciousConfigTest.pm
@@ -0,0 +1,7 @@
package MojoliciousConfigTest;
use Mojo::Base 'Mojolicious';

# "Aw, he looks like a little insane drunken angel."
sub startup { shift->plugin('Config') }

1;
2 changes: 1 addition & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -27,7 +27,7 @@ use Test::Mojo;

# Missing plugin
eval { plugin 'does_not_exist' };
is $@, "Plugin \"does_not_exist\" missing, maybe you need to install it?\n",
is $@, qq{Plugin "does_not_exist" missing, maybe you need to install it?\n},
'right error';

# Default
Expand Down
1 change: 1 addition & 0 deletions t/mojolicious/mojolicious_config_test.whatever.conf
@@ -0,0 +1 @@
{it => 'works'};

0 comments on commit ba08389

Please sign in to comment.