Mojolicious
view release on metacpan or search on metacpan
t/mojolicious/commands.t view on Meta::CPAN
use Mojo::Base -strict;
BEGIN {
$ENV{PLACK_ENV} = undef;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Test::More;
use Mojo::File qw(curfile);
use lib curfile->sibling('lib')->to_string;
use Mojo::File qw(path tempdir);
package Mojolicious::Command::my_fake_test_command;
package Mojolicious::Command::my_test_command;
use Mojo::Base 'Mojolicious::Command';
has description => 'See, it works';
package main;
# Make sure @ARGV is not changed
{
local $ENV{MOJO_MODE};
local @ARGV = qw(-m production -x whatever);
require Mojolicious::Commands;
is $ENV{MOJO_MODE}, 'production', 'right mode';
is_deeply \@ARGV, [qw(-m production -x whatever)], 'unchanged';
}
# Environment detection
my $commands = Mojolicious::Commands->new;
{
local $ENV{PLACK_ENV} = 'production';
is $commands->detect, 'psgi', 'right environment';
}
{
local $ENV{PATH_INFO} = '/test';
is $commands->detect, 'cgi', 'right environment';
}
{
local $ENV{GATEWAY_INTERFACE} = 'CGI/1.1';
is $commands->detect, 'cgi', 'right environment';
}
{
local @ENV{qw(PLACK_ENV PATH_INFO GATEWAY_INTERFACE)};
is $commands->detect, undef, 'no environment';
}
{
local $ENV{PLACK_ENV} = 'production';
is ref Mojolicious::Commands->new->run, 'CODE', 'right reference';
local $ENV{MOJO_NO_DETECT} = 1;
isnt ref Mojolicious::Commands->new->run, 'CODE', 'not a CODE reference';
}
# Run command
is ref Mojolicious::Commands->new->run('psgi'), 'CODE', 'right reference';
# Start application
{
local $ENV{MOJO_APP_LOADER} = 1;
is ref Mojolicious::Commands->start_app('MojoliciousTest'), 'MojoliciousTest', 'right class';
}
# Start application with command
{
is ref Mojolicious::Commands->start_app(MojoliciousTest => 'psgi'), 'CODE', 'right reference';
}
# Start application with application specific commands
my $app;
{
local $ENV{MOJO_APP_LOADER} = 1;
$app = Mojolicious::Commands->start_app('MojoliciousTest');
}
is $app->start('test_command'), 'works!', 'right result';
is $app->start('test-command'), 'works!', 'right result';
is $app->start('_test2-command'), 'works 2!', 'right result';
{
is(Mojolicious::Commands->start_app(MojoliciousTest => 'test_command'), 'works!', 'right result');
is(Mojolicious::Commands->start_app(MojoliciousTest => 'test-command'), 'works!', 'right result');
is(Mojolicious::Commands->start_app(MojoliciousTest => '_test2-command'), 'works 2!', 'right result');
}
# Application specific help
my $buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
local $ENV{HARNESS_ACTIVE} = 0;
$app->start;
}
like $buffer, qr/Usage: APPLICATION COMMAND \[OPTIONS\].*_test2-command.*cgi.*test-comm/s, 'right output';
subtest 'Commands starting with a dash are not allowed' => sub {
local $ENV{HARNESS_ACTIVE} = 0;
eval { $app->start('-test2-command') };
like $@, qr/Invalid command "-test2-command"\./, 'not allowed';
};
# Do not pick up options for detected environments
{
local $ENV{MOJO_MODE};
local $ENV{PLACK_ENV} = 'testing';
local @ARGV = qw(psgi -m production);
is ref Mojolicious::Commands->start_app('MojoliciousTest'), 'CODE', 'right reference';
is $ENV{MOJO_MODE}, undef, 'no mode';
}
# mojo
is_deeply $commands->namespaces, ['Mojolicious::Command::Author', 'Mojolicious::Command'], 'right namespaces';
ok $commands->description, 'has a description';
like $commands->message, qr/COMMAND/, 'has a message';
like $commands->hint, qr/help/, 'has a hint';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
local $ENV{HARNESS_ACTIVE} = 0;
$commands->run;
}
like $buffer, qr/Usage: APPLICATION COMMAND \[OPTIONS\].*daemon.*my-test-command.*version/s, 'right output';
like $buffer, qr/See, it works/, 'description has been picked up';
unlike $buffer, qr/my-fake-test-command/, 'fake command has been ignored';
# help
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$commands->run('help', 'generate', 'lite-app');
}
like $buffer, qr/Usage: APPLICATION generate lite-app \[OPTIONS\] \[NAME\]/, 'right output';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$commands->run('generate', 'help', 'lite-app');
}
like $buffer, qr/Usage: APPLICATION generate lite-app \[OPTIONS\] \[NAME\]/, 'right output';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$commands->run('generate', 'app', '-h');
}
like $buffer, qr/Usage: APPLICATION generate app \[OPTIONS\] \[NAME\]/, 'right output';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$commands->run('generate', 'lite-app', '--help');
}
like $buffer, qr/Usage: APPLICATION generate lite-app \[OPTIONS\] \[NAME\]/, 'right output';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
local $ENV{HARNESS_ACTIVE} = 0;
$commands->run('help');
}
like $buffer, qr/Usage: APPLICATION COMMAND \[OPTIONS\]/, 'right output';
$buffer = '';
{
open my $handle, '>', \$buffer;
( run in 1.641 second using v1.01-cache-2.11-cpan-800906f7e73 )