Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use do rather than require to load a mojo application #2097

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading