Skip to content

Commit

Permalink
add support for overriding configuration files in applications tested…
Browse files Browse the repository at this point in the history
… with Test::Mojo
  • Loading branch information
kraih committed Mar 12, 2017
1 parent 852a71a commit 528bfd9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

7.29 2017-03-09
7.29 2017-03-12
- Added support for overriding configuration files in applications tested with
Test::Mojo.
- Added html_attr_unescape function to Mojo::Util.
- Fixed unescaping of HTML5 attribute values in Mojo::DOM::HTML.

Expand Down
6 changes: 4 additions & 2 deletions lib/Mojo/Server.pm
Expand Up @@ -12,9 +12,9 @@ has app => sub { shift->build_app('Mojo::HelloWorld') };
has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };

sub build_app {
my ($self, $app) = @_;
my ($self, $app) = (shift, shift);
local $ENV{MOJO_EXE};
return $self->app($app->new)->app unless my $e = load_class $app;
return $self->app($app->new(@_))->app unless my $e = load_class $app;
die ref $e ? $e : qq{Can't find application class "$app" in \@INC. (@INC)\n};
}

Expand Down Expand Up @@ -150,6 +150,8 @@ the following new ones.
=head2 build_app
my $app = $server->build_app('MyApp');
my $app = $server->build_app('MyApp', log => Mojo::Log->new);
my $app = $server->build_app('MyApp', {log => Mojo::Log->new});
Build application from class and assign it to L</"app">.
Expand Down
6 changes: 6 additions & 0 deletions lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -22,6 +22,9 @@ sub parse {
sub register {
my ($self, $app, $conf) = @_;

# Override
return $app->config if $app->config->{config_override};

# Config file
my $file = $conf->{file} || $ENV{MOJO_CONFIG};
$file ||= $app->moniker . '.' . ($conf->{ext} || 'conf');
Expand Down Expand Up @@ -101,6 +104,9 @@ directory will be generated from the value of L<Mojolicious/"moniker">
C<$moniker.conf> with C<mode> specific ones like C<$moniker.$mode.conf>, which
will be detected automatically.
If the configuration value C<config_override> has been set in L<Mojo/"config">
when this plugin is loaded, it will not do anything.
The code of this plugin is a good example for learning to build new plugins,
you're welcome to fork it.
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Plugin/JSONConfig.pm
Expand Up @@ -80,6 +80,9 @@ generated from the value of L<Mojolicious/"moniker"> (C<$moniker.json>). You can
extend the normal configuration file C<$moniker.json> with C<mode> specific ones
like C<$moniker.$mode.json>, which will be detected automatically.
If the configuration value C<config_override> has been set in L<Mojo/"config">
when this plugin is loaded, it will not do anything.
The code of this plugin is a good example for learning to build new plugins,
you're welcome to fork it.
Expand Down
14 changes: 12 additions & 2 deletions lib/Test/Mojo.pm
Expand Up @@ -240,8 +240,12 @@ sub message_unlike {

sub new {
my $self = shift->SUPER::new;

return $self unless my $app = shift;
return $self->app(ref $app ? $app : Mojo::Server->new->build_app($app));

my @args = @_ ? {config => {config_override => 1, %{shift()}}} : ();
return $self->app(
ref $app ? $app : Mojo::Server->new->build_app($app, @args));
}

sub options_ok { shift->_build_ok(OPTIONS => @_) }
Expand Down Expand Up @@ -876,9 +880,15 @@ Opposite of L</"message_like">.
my $t = Test::Mojo->new;
my $t = Test::Mojo->new('MyApp');
my $t = Test::Mojo->new(MyApp => {test => 'configuration', hello => 'Mojo!'});
my $t = Test::Mojo->new(MyApp->new);
Construct a new L<Test::Mojo> object.
Construct a new L<Test::Mojo> object. In addition to a class name, you can pass
along a hash reference with configuration values that will be used to
instantiate the application. The special configuration value C<config_override>
will be set in L<Mojo/"config"> as well, which is used to disable configuration
plugins like L<Mojolicious::Plugin::Config> and
L<Mojolicious::Plugin::JSONConfig> for tests.
=head2 options_ok
Expand Down
14 changes: 14 additions & 0 deletions t/mojolicious/external_app.t
Expand Up @@ -24,4 +24,18 @@ $t->get_ok('/index.html')->status_is(200)
# More text from config file
$t->get_ok('/test')->status_is(200)->content_is('works%21');

# Config override
$t = Test::Mojo->new(
MyApp => {whatever => 'override!', works => 'override two!'});

# Text from config override
$t->get_ok('/')->status_is(200)->content_is('override two!');

# Static file again
$t->get_ok('/index.html')->status_is(200)
->content_is("External static file!\n");

# More text from config override
$t->get_ok('/test')->status_is(200)->content_is('override!');

done_testing();

0 comments on commit 528bfd9

Please sign in to comment.