Skip to content

Commit

Permalink
Merge pull request #2097 from haarg/fix-module-true-compat
Browse files Browse the repository at this point in the history
use do rather than require to load a mojo application
  • Loading branch information
mergify[bot] authored Aug 25, 2023
2 parents 058216d + 693fc6c commit d84c8a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Mojo/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ sub load_app {
local @ARGS_OVERRIDE = @args;

# Try to load application from script into sandbox
delete $INC{$path};
my $app = eval "package Mojo::Server::Sandbox::@{[md5_sum $path]}; require \$path";
die qq{Can't load application from file "$path": $@} if $@;
my $app = eval "package Mojo::Server::Sandbox::@{[md5_sum $path]}; do \$path";
my $err = $app ? undef : $@ || $! || "$path did not return a true value";
die qq{Can't load application from file "$path": $err} if $err;
die qq{File "$path" did not return an application object.\n} unless blessed $app && $app->can('handler');
$self->app($app);
};
Expand Down
9 changes: 9 additions & 0 deletions t/mojo/daemon.t
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ subtest 'Load broken app' => sub {
like $@, qr/^Can't load application/, 'right error';
};

subtest 'Load app using module_true' => sub {
plan skip_all => 'module_true feature requires perl 5.38' if $] < 5.038;
my $daemon = Mojo::Server::Daemon->new;
my $path = curfile->sibling('lib', '..', 'lib', 'myapp-module-true.pl');
my $app = eval { $daemon->load_app($path) };
is $@, '', 'no error loading app';
is ref $app, 'Mojolicious::Lite', 'right reference';
};

subtest 'Load missing application class' => sub {
eval { Mojo::Server::Daemon->new->build_app('Mojo::DoesNotExist') };
like $@, qr/^Can't find application class "Mojo::DoesNotExist" in \@INC/, 'right error';
Expand Down
15 changes: 15 additions & 0 deletions t/mojo/lib/myapp-module-true.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use v5.38;
use Mojolicious::Lite;

app->config(script => $0);

app->start;

=head1 SYNOPSIS
USAGE: myapp.pl daemon
test
123
=cut

0 comments on commit d84c8a8

Please sign in to comment.