Skip to content

Commit

Permalink
added test for invalid config file
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 23, 2014
1 parent 686418e commit 8881290
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

5.54 2014-10-22
5.54 2014-10-23
- Added auto_decompress attribute to Mojo::Content.
- Fixed bug where Mojo::UserAgent would try to follow redirects for
protocols other than HTTP and HTTPS.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Plugin/JSONConfig.pm
Expand Up @@ -8,11 +8,10 @@ use Mojo::Util 'encode';
sub parse {
my ($self, $content, $file, $conf, $app) = @_;

my $json = Mojo::JSON->new;
my $json = Mojo::JSON->new;
my $config = $json->decode($self->render($content, $file, $conf, $app));
my $err = $json->error;
die qq{Can't parse config "$file": $err} if !$config && $err;
die qq{Invalid config "$file"} if !$config || ref $config ne 'HASH';
if (my $err = $json->error) { die qq{Can't parse config "$file": $err} }
die qq{Invalid config "$file"} unless ref $config eq 'HASH';

return $config;
}
Expand Down
8 changes: 6 additions & 2 deletions t/mojolicious/json_config_lite_app.t
Expand Up @@ -17,13 +17,17 @@ use Test::Mojo;
app->config(it => 'works');
is_deeply app->config, {it => 'works'}, 'right value';

# Invalid config file
my $path = abs_path catfile(dirname(__FILE__), 'public', 'hello.txt');
eval { plugin JSONConfig => {file => $path}; };
like $@, qr/Malformed JSON/, 'right error';

# Load plugins
my $config
= plugin j_s_o_n_config => {default => {foo => 'baz', hello => 'there'}};
my $log = '';
my $cb = app->log->on(message => sub { $log .= pop });
my $path
= abs_path(catfile(dirname(__FILE__), 'json_config_lite_app_abs.json'));
$path = abs_path catfile(dirname(__FILE__), 'json_config_lite_app_abs.json');
plugin JSONConfig => {file => $path};
like $log, qr/Reading configuration file "\Q$path\E"\./, 'right message';
app->log->unsubscribe(message => $cb);
Expand Down

0 comments on commit 8881290

Please sign in to comment.