Skip to content

Commit

Permalink
use Mojo::File in tests too
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 7, 2017
1 parent c6b1066 commit 47873c6
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 89 deletions.
13 changes: 5 additions & 8 deletions t/mojo/asset.t
@@ -1,11 +1,9 @@
use Mojo::Base -strict;

use Test::More;
use File::Basename 'dirname';
use File::Spec::Functions qw(catdir catfile);
use File::Temp 'tempdir';
use Mojo::Asset::File;
use Mojo::Asset::Memory;
use Mojo::File qw(path tempdir);

# File asset
my $file = Mojo::Asset::File->new;
Expand Down Expand Up @@ -201,19 +199,18 @@ ok !$asset->is_file, 'stored in memory';

# Temporary directory
{
my $tmpdir = tempdir CLEANUP => 1;
local $ENV{MOJO_TMPDIR} = $tmpdir;
local $ENV{MOJO_TMPDIR} = my $tmpdir = tempdir;
$file = Mojo::Asset::File->new;
is($file->tmpdir, $tmpdir, 'same directory');
$file->add_chunk('works!');
is $file->slurp, 'works!', 'right content';
is dirname($file->path), $tmpdir, 'same directory';
is path($file->path)->dirname, $tmpdir, 'same directory';
}

# Custom temporary file
{
my $tmpdir = tempdir CLEANUP => 1;
my $path = catfile $tmpdir, 'test.file';
my $tmpdir = tempdir;
my $path = $tmpdir->child('test.file');
ok !-e $path, 'file does not exist';
$file = Mojo::Asset::File->new(path => $path);
is $file->path, $path, 'right path';
Expand Down
10 changes: 4 additions & 6 deletions t/mojo/bytestream.t
@@ -1,11 +1,9 @@
use Mojo::Base -strict;

use Test::More;
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use FindBin;
use Mojo::ByteStream 'b';
use Mojo::File qw(path tempdir);

# Tap into method chain
is b('test')->tap(sub { $$_ .= '1' })->camelize, 'Test1', 'right result';
Expand Down Expand Up @@ -136,15 +134,15 @@ b('te', 'st')->say($handle);
is $buffer, "test\n123\n\"123\"\n", 'right output';

# slurp
my $file = catfile dirname(__FILE__), 'templates', 'exception.mt';
my $file = path(__FILE__)->dirname->child('templates', 'exception.mt');
$stream = b($file)->slurp;
is $stream, "test\n% die;\n123\n", 'right content';
$stream = b($file)->slurp->split("\n")->grep(qr/die/)->join;
is $stream, '% die;', 'right content';

# spurt
my $dir = tempdir CLEANUP => 1;
$file = catfile $dir, 'test.txt';
my $dir = tempdir;
$file = $dir->child('test.txt');
is b("just\nworks!")->spurt($file)->quote, qq{"just\nworks!"}, 'right result';
is b($file)->slurp, "just\nworks!", 'successful roundtrip';

Expand Down
9 changes: 4 additions & 5 deletions t/mojo/daemon.t
Expand Up @@ -3,10 +3,9 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Cwd 'abs_path';
use File::Spec::Functions 'catdir';
use FindBin;
use Mojo;
use Mojo::File 'path';
use Mojo::IOLoop;
use Mojo::Log;
use Mojo::Server::Daemon;
Expand Down Expand Up @@ -65,8 +64,8 @@ is $tx->res->body, 'Hello TestApp!', 'right content';

# Optional home detection
my @path = qw(th is mojo dir wil l never-ever exist);
my $app = Mojo->new(home => Mojo::Home->new(catdir @path));
is $app->home, catdir(@path), 'right home directory';
my $app = Mojo->new(home => Mojo::Home->new(path(@path)));
is $app->home, path(@path), 'right home directory';

# Config
is $app->config('foo'), undef, 'no value';
Expand All @@ -83,7 +82,7 @@ is_deeply $app->config, {foo => 'bar', baz => 'yada', test => 23},
my $daemon = Mojo::Server::Daemon->new;
my $path = "$FindBin::Bin/lib/../lib/myapp.pl";
is ref $daemon->load_app($path), 'Mojolicious::Lite', 'right reference';
is $daemon->app->config('script'), abs_path($path), 'right script name';
is $daemon->app->config('script'), path($path)->to_abs, 'right script name';
is ref $daemon->build_app('TestApp'), 'TestApp', 'right reference';
is ref $daemon->app, 'TestApp', 'right reference';

Expand Down
22 changes: 10 additions & 12 deletions t/mojo/hypnotoad.t
Expand Up @@ -7,14 +7,12 @@ use Test::More;
plan skip_all => 'set TEST_HYPNOTOAD to enable this test (developer only!)'
unless $ENV{TEST_HYPNOTOAD};

use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use FindBin;
use IO::Socket::INET;
use Mojo::File 'tempdir';
use Mojo::IOLoop::Server;
use Mojo::Server::Hypnotoad;
use Mojo::UserAgent;
use Mojo::Util qw(slurp spurt);

# Configure
{
Expand Down Expand Up @@ -54,12 +52,12 @@ use Mojo::Util qw(slurp spurt);
}

# Prepare script
my $dir = tempdir CLEANUP => 1;
my $script = catfile $dir, 'myapp.pl';
my $log = catfile $dir, 'mojo.log';
my $dir = tempdir;
my $script = $dir->child('myapp.pl');
my $log = $dir->child('mojo.log');
my $port1 = Mojo::IOLoop::Server->generate_port;
my $port2 = Mojo::IOLoop::Server->generate_port;
spurt <<EOF, $script;
$script->spurt(<<EOF);
use Mojolicious::Lite;
use Mojo::IOLoop;
Expand Down Expand Up @@ -135,7 +133,7 @@ is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Hello Hypnotoad!', 'right content';

# Update script (broken)
spurt <<'EOF', $script;
$script->spurt(<<'EOF');
use Mojolicious::Lite;
die if $ENV{HYPNOTOAD_PID};
Expand All @@ -146,7 +144,7 @@ open my $hot_deploy, '-|', $^X, "$prefix/hypnotoad", $script;

# Wait for hot deployment to fail
while (1) {
last if slurp($log) =~ qr/Zero downtime software upgrade failed/;
last if $log->slurp =~ qr/Zero downtime software upgrade failed/;
sleep 1;
}

Expand All @@ -172,7 +170,7 @@ $ua->start($tx => sub { });
Mojo::IOLoop->one_tick until $tx->req->is_finished;

# Update script
spurt <<EOF, $script;
$script->spurt(<<EOF);
use Mojolicious::Lite;
app->log->path('$log');
Expand Down Expand Up @@ -251,13 +249,13 @@ open my $stop, '-|', $^X, "$prefix/hypnotoad", $script, '-s';
sleep 1 while _port($port2);

# Check log
$log = slurp $log;
$log = $log->slurp;
like $log, qr/Worker \d+ started/, 'right message';
like $log, qr/Starting zero downtime software upgrade/, 'right message';
like $log, qr/Upgrade successful, stopping $old/, 'right message';

sub _pid {
return undef unless open my $file, '<', catfile($dir, 'hypnotoad.pid');
return undef unless open my $file, '<', $dir->child('hypnotoad.pid');
my $pid = <$file>;
chomp $pid;
return $pid;
Expand Down
9 changes: 4 additions & 5 deletions t/mojo/log.t
@@ -1,15 +1,14 @@
use Mojo::Base -strict;

use Test::More;
use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use Mojo::File 'tempdir';
use Mojo::Log;
use Mojo::Util qw(decode slurp);

# Logging to file
my $dir = tempdir CLEANUP => 1;
my $path = catfile $dir, 'test.log';
my $log = Mojo::Log->new(level => 'error', path => $path);
my $dir = tempdir;
my $path = $dir->child('test.log');
my $log = Mojo::Log->new(level => 'error', path => $path);
$log->error('Just works');
$log->fatal('I ♥ Mojolicious');
$log->debug('Does not work');
Expand Down
30 changes: 13 additions & 17 deletions t/mojo/morbo.t
Expand Up @@ -7,26 +7,22 @@ use Test::More;
plan skip_all => 'set TEST_MORBO to enable this test (developer only!)'
unless $ENV{TEST_MORBO};

use File::Path 'make_path';
use File::Spec::Functions qw(catdir catfile);
use File::Temp 'tempdir';
use FindBin;
use IO::Socket::INET;
use Mojo::File 'tempdir';
use Mojo::IOLoop::Server;
use Mojo::Server::Daemon;
use Mojo::Server::Morbo;
use Mojo::UserAgent;
use Mojo::Util 'spurt';
use Socket qw(SO_REUSEPORT SOL_SOCKET);

# Prepare script
my $dir = tempdir CLEANUP => 1;
my $script = catfile $dir, 'myapp.pl';
my $subdir = catdir $dir, 'test', 'stuff';
make_path $subdir;
my $morbo = Mojo::Server::Morbo->new(watch => [$subdir, $script]);
my $dir = tempdir;
my $script = $dir->child('myapp.pl');
my $subdir = $dir->child('test', 'stuff')->make_path;
my $morbo = Mojo::Server::Morbo->new(watch => [$subdir, $script]);
is_deeply $morbo->modified_files, [], 'no files have changed';
spurt <<EOF, $script;
$script->spurt(<<EOF);
use Mojolicious::Lite;
app->log->level('fatal');
Expand Down Expand Up @@ -58,7 +54,7 @@ is $tx->res->body, 'Hello Morbo!', 'right content';

# Update script without changing size
my ($size, $mtime) = (stat $script)[7, 9];
spurt <<EOF, $script;
$script->spurt(<<EOF);
use Mojolicious::Lite;
app->log->level('fatal');
Expand Down Expand Up @@ -87,7 +83,7 @@ is $tx->res->body, 'Hello World!', 'right content';
# Update script without changing mtime
($size, $mtime) = (stat $script)[7, 9];
is_deeply $morbo->modified_files, [], 'no files have changed';
spurt <<EOF, $script;
$script->spurt(<<EOF);
use Mojolicious::Lite;
app->log->level('fatal');
Expand Down Expand Up @@ -116,17 +112,17 @@ is $tx->res->body, 'Hello!', 'right content';

# New file(s)
is_deeply $morbo->modified_files, [], 'directory has not changed';
my @new = map { catfile $subdir, "$_.txt" } qw/test testing/;
spurt 'whatever', $_ for @new;
my @new = map { $subdir->child("$_.txt") } qw/test testing/;
$_->spurt('whatever') for @new;
is_deeply $morbo->modified_files, \@new, 'two files have changed';
spurt 'whatever', catfile($subdir, '.hidden.txt');
$subdir->child('.hidden.txt')->spurt('whatever');
is_deeply $morbo->modified_files, [], 'directory has not changed again';

# Broken symlink
SKIP: {
skip 'Symlink support required!', 4 unless eval { symlink '', ''; 1 };
my $missing = catfile $subdir, 'missing.txt';
my $broken = catfile $subdir, 'broken.txt';
my $missing = $subdir->child('missing.txt');
my $broken = $subdir->child('broken.txt');
symlink $missing, $broken;
ok -l $broken, 'symlink created';
ok !-f $broken, 'symlink target does not exist';
Expand Down
5 changes: 2 additions & 3 deletions t/mojo/prefork.t
Expand Up @@ -7,8 +7,7 @@ use Test::More;
plan skip_all => 'set TEST_PREFORK to enable this test (developer only!)'
unless $ENV{TEST_PREFORK};

use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use Mojo::File 'path';
use Mojo::IOLoop::Server;
use Mojo::Server::Prefork;
use Mojo::UserAgent;
Expand All @@ -31,7 +30,7 @@ undef $prefork;
ok !-e $file, 'file has been cleaned up';

# Bad PID file
my $bad = catfile dirname(__FILE__), 'does_not_exist', 'test.pid';
my $bad = path(__FILE__)->dirname->child('does_not_exist', 'test.pid');
$prefork = Mojo::Server::Prefork->new(pid_file => $bad);
$prefork->app->log->level('fatal');
my $log = '';
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/request.t
@@ -1,12 +1,11 @@
use Mojo::Base -strict;

use Test::More;
use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use IO::Compress::Gzip 'gzip';
use Mojo::Content::Single;
use Mojo::Content::MultiPart;
use Mojo::Cookie::Request;
use Mojo::File 'tempdir';
use Mojo::Message::Request;
use Mojo::URL;
use Mojo::Util 'encode';
Expand Down Expand Up @@ -760,7 +759,8 @@ is $req->body_params->to_hash->{text2}, '', 'right value';
is $req->upload('upload')->filename, 'hello.pl', 'right filename';
ok !$req->upload('upload')->asset->is_file, 'stored in memory';
is $req->upload('upload')->asset->size, 69, 'right size';
my $file = catfile(tempdir(CLEANUP => 1), ("MOJO_TMP." . time . ".txt"));
my $tempdir = tempdir;
my $file = $tempdir->child('MOJO_TMP.' . time . '.txt');
is $req->upload('upload')->move_to($file)->filename, 'hello.pl',
'right filename';
ok unlink($file), 'unlinked file';
Expand Down
27 changes: 13 additions & 14 deletions t/mojo/template.t
@@ -1,8 +1,7 @@
use Mojo::Base -strict;

use Test::More;
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use Mojo::File 'path';
use Mojo::Template;

package MyTemplateExporter;
Expand Down Expand Up @@ -651,21 +650,21 @@ EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/ohoh/, 'right message';
ok $output->verbose, 'verbose exception';
is $output->lines_before->[0][0], 15, 'right number';
is $output->lines_before->[0][0], 14, 'right number';
is $output->lines_before->[0][1], '}', 'right line';
is $output->lines_before->[1][0], 16, 'right number';
is $output->lines_before->[1][0], 15, 'right number';
is $output->lines_before->[1][1], '', 'right line';
is $output->lines_before->[2][0], 17, 'right number';
is $output->lines_before->[2][0], 16, 'right number';
is $output->lines_before->[2][1], 'package MyTemplateException;', 'right line';
is $output->lines_before->[3][0], 18, 'right number';
is $output->lines_before->[3][0], 17, 'right number';
is $output->lines_before->[3][1], 'use Mojo::Base -strict;', 'right line';
is $output->lines_before->[4][0], 19, 'right number';
is $output->lines_before->[4][0], 18, 'right number';
is $output->lines_before->[4][1], '', 'right line';
is $output->line->[0], 20, 'right number';
is $output->line->[0], 19, 'right number';
is $output->line->[1], "sub exception { die 'ohoh' }", 'right line';
is $output->lines_after->[0][0], 21, 'right number';
is $output->lines_after->[0][0], 20, 'right number';
is $output->lines_after->[0][1], '', 'right line';
is $output->lines_after->[1][0], 22, 'right number';
is $output->lines_after->[1][0], 21, 'right number';
is $output->lines_after->[1][1], 'package main;', 'right line';
like "$output", qr/ohoh/, 'right result';

Expand Down Expand Up @@ -1106,13 +1105,13 @@ EOF

# File
$mt = Mojo::Template->new;
my $file = catfile dirname(__FILE__), 'templates', 'test.mt';
my $file = path(__FILE__)->dirname->child('templates', 'test.mt');
$output = $mt->render_file($file, 3);
like $output, qr/23\nHello World!/, 'file';

# Exception in file
$mt = Mojo::Template->new;
$file = catfile dirname(__FILE__), 'templates', 'exception.mt';
$file = path(__FILE__)->dirname->child('templates', 'exception.mt');
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
Expand Down Expand Up @@ -1142,7 +1141,7 @@ like "$output", qr/foo\.mt from DATA section line 2/, 'right result';

# Exception with UTF-8 context
$mt = Mojo::Template->new;
$file = catfile dirname(__FILE__), 'templates', 'utf8_exception.mt';
$file = path(__FILE__)->dirname->child('templates', 'utf8_exception.mt');
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
ok $output->verbose, 'verbose exception';
Expand All @@ -1163,7 +1162,7 @@ is $output->lines_after->[0], undef, 'no lines after';

# Different encodings
$mt = Mojo::Template->new(encoding => 'shift_jis');
$file = catfile dirname(__FILE__), 'templates', 'utf8_exception.mt';
$file = path(__FILE__)->dirname->child('templates', 'utf8_exception.mt');
ok !eval { $mt->render_file($file) }, 'file not rendered';
like $@, qr/invalid encoding/, 'right error';

Expand Down

0 comments on commit 47873c6

Please sign in to comment.