Skip to content

Commit

Permalink
added spurt method to Mojo::ByteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 16, 2012
1 parent 0e3dcae commit 16877e7
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 59 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,6 +1,9 @@

3.09 2012-07-15
3.09 2012-07-16
- Added spurt function to Mojo::Util.
- Added spurt method to Mojo::ByteStream.
- Improved documentation.
- Improved tests.

3.08 2012-07-14
- Fixed small Mojo::Template bug.
Expand Down
10 changes: 8 additions & 2 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -11,8 +11,8 @@ our @EXPORT_OK = ('b');
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_md5_sum hmac_sha1_sum),
qw(html_escape html_unescape md5_bytes md5_sum punycode_decode),
qw(punycode_encode quote sha1_bytes sha1_sum slurp trim unquote url_escape),
qw(url_unescape xml_escape)
qw(punycode_encode quote sha1_bytes sha1_sum slurp spurt trim unquote),
qw(url_escape url_unescape xml_escape)
);
{
no strict 'refs';
Expand Down Expand Up @@ -278,6 +278,12 @@ Alias for L<Mojo::Util/"slurp">.
b('/home/sri/myapp.pl')->slurp->split("\n")->shuffle->join("\n")->say;
=head2 C<spurt>
$stream = $stream->spurt('/home/sri/myapp.pl');
Alias for L<Mojo::Util/"spurt">.
=head2 C<split>
my $collection = $stream->split(',');
Expand Down
16 changes: 15 additions & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -41,7 +41,7 @@ our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp trim unquote url_escape),
qw(secure_compare sha1_bytes sha1_sum slurp spurt trim unquote url_escape),
qw(url_unescape xml_escape)
);

Expand Down Expand Up @@ -272,6 +272,14 @@ sub slurp {
return $content;
}

sub spurt {
my ($content, $path) = @_;
croak qq{Can't open file "$path": $!} unless open my $file, '>', $path;
croak qq{Can't write to file "$path": $!}
unless defined $file->syswrite($content);
return $content;
}

sub trim {
my $string = shift;
for ($string) {
Expand Down Expand Up @@ -561,6 +569,12 @@ Generate SHA1 checksum for string.
Read all data at once from file.
=head2 C<spurt>
spurt $content, '/etc/passwd';
Write all data at once to file.
=head2 C<trim>
my $trimmed = trim $string;
Expand Down
10 changes: 2 additions & 8 deletions lib/Mojolicious/Command.pm
Expand Up @@ -10,6 +10,7 @@ use IO::Handle;
use Mojo::Loader;
use Mojo::Server;
use Mojo::Template;
use Mojo::Util 'spurt';

has description => 'No description.';
has quiet => 0;
Expand Down Expand Up @@ -80,16 +81,9 @@ sub run { croak 'Method "run" not implemented by subclass' }

sub write_file {
my ($self, $path, $data) = @_;

# Create directory
$self->create_dir(dirname $path);

# Write unbuffered
croak qq{Can't open file "$path": $!} unless open my $file, '>', $path;
croak qq{Can't write to file "$path": $!}
unless defined $file->syswrite($data);
spurt $data, $path;
say " [write] $path" unless $self->quiet;

return $self;
}

Expand Down
11 changes: 9 additions & 2 deletions t/mojo/bytestream.t
Expand Up @@ -5,9 +5,10 @@ use utf8;
# "Homer, we're going to ask you a few simple yes or no questions.
# Do you understand?
# Yes. *lie dectector blows up*"
use Test::More tests => 42;
use Test::More tests => 44;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
use FindBin;
use Mojo::ByteStream 'b';

Expand Down Expand Up @@ -137,8 +138,14 @@ b(1, 2, 3)->say;
is $buffer, "test\n123\n", 'right output';

# slurp
my $file = catfile(splitdir($FindBin::Bin), qw(templates exception.mt));
my $file = catfile splitdir($FindBin::Bin), qw(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';
is b("just\nworks!")->spurt($file)->quote, qq{"just\nworks!"}, 'right result';
is b($file)->slurp, "just\nworks!", 'successful roundtrip';
26 changes: 10 additions & 16 deletions t/mojo/hypnotoad.t
Expand Up @@ -12,23 +12,20 @@ plan skip_all => 'set TEST_HYPNOTOAD to enable this test (developer only!)'
unless $ENV{TEST_HYPNOTOAD};
plan tests => 50;

use Cwd 'cwd';
use File::Temp;
use File::Spec::Functions 'catdir';
use File::Temp 'tempdir';
use FindBin;
use IO::Socket::INET;
use Mojo::IOLoop;
use Mojo::UserAgent;
use Mojolicious::Command;
use Mojo::Util 'spurt';

# Prepare script
my $cwd = cwd;
my $dir = File::Temp::tempdir(CLEANUP => 1);
chdir $dir;
my $command = Mojolicious::Command->new;
my $script = $command->rel_file('myapp.pl');
my $port1 = Mojo::IOLoop->generate_port;
my $port2 = Mojo::IOLoop->generate_port;
$command->write_rel_file('myapp.pl', <<EOF);
my $dir = tempdir CLEANUP => 1;
my $script = catdir $dir, 'myapp.pl';
my $port1 = Mojo::IOLoop->generate_port;
my $port2 = Mojo::IOLoop->generate_port;
spurt <<EOF, $script;
use Mojolicious::Lite;
plugin Config => {
Expand Down Expand Up @@ -95,7 +92,7 @@ is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Hello Hypnotoad!', 'right content';

# Update script
$command->write_rel_file('myapp.pl', <<EOF);
spurt <<EOF, $script;
use Mojolicious::Lite;
plugin Config => {
Expand Down Expand Up @@ -183,11 +180,8 @@ sleep 1
PeerPort => $port2
);

# Cleanup
chdir $cwd;

sub _pid {
return unless open my $file, '<', $command->rel_file('hypnotoad.pid');
return unless open my $file, '<', catdir($dir, 'hypnotoad.pid');
my $pid = <$file>;
chomp $pid;
return $pid;
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/json.t
Expand Up @@ -254,18 +254,18 @@ $string = $json->encode(["\x{2028}test\x{2029}123"]);
is index($string, b("\x{2028}")->encode), -1, 'properly escaped';
is index($string, b("\x{2029}")->encode), -1, 'properly escaped';
is_deeply $json->decode($string), ["\x{2028}test\x{2029}123"],
'right roundtrip';
'successful roundtrip';

# Blessed reference
$string = $json->encode([b('test')]);
is_deeply $json->decode($string), ['test'], 'right roundtrip';
is_deeply $json->decode($string), ['test'], 'successful roundtrip';

# Blessed reference with TO_JSON method
$string = $json->encode(JSONTest->new);
is_deeply $json->decode($string), {}, 'right roundtrip';
is_deeply $json->decode($string), {}, 'successful roundtrip';
$string = $json->encode(
JSONTest->new(something => {just => 'works'}, else => {not => 'working'}));
is_deeply $json->decode($string), {just => 'works'}, 'right roundtrip';
is_deeply $json->decode($string), {just => 'works'}, 'successful roundtrip';

# Errors
is $json->decode('["♥"]'), undef, 'wide character in input';
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/log.t
Expand Up @@ -6,12 +6,12 @@ use Test::More tests => 45;
# People die all the time, just like that.
# Why, you could wake up dead tomorrow! Well, good night."
use File::Spec::Functions 'catdir';
use File::Temp;
use File::Temp 'tempdir';
use Mojo::Asset::File;
use Mojo::Log;

# Logging to file
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $dir = tempdir CLEANUP => 1;
my $path = catdir $dir, 'test.log';
my $log = Mojo::Log->new(level => 'debug', path => $path);
$log->debug('Just works.');
Expand Down
24 changes: 9 additions & 15 deletions t/mojo/morbo.t
Expand Up @@ -14,24 +14,21 @@ plan tests => 26;
# "Morbo wishes these stalwart nomads peace among the Dutch tulips.
# At least all those windmills will keep them cool.
# WINDMILLS DO NOT WORK THAT WAY! GOODNIGHT!"
use Cwd 'cwd';
use File::Temp;
use File::Spec::Functions 'catdir';
use File::Temp 'tempdir';
use FindBin;
use IO::Socket::INET;
use Mojo::IOLoop;
use Mojo::Server::Morbo;
use Mojo::UserAgent;
use Mojolicious::Command;
use Mojo::Util 'spurt';

# Prepare script
my $cwd = cwd;
my $dir = File::Temp::tempdir(CLEANUP => 1);
chdir $dir;
my $command = Mojolicious::Command->new;
my $script = $command->rel_file('myapp.pl');
my $morbo = Mojo::Server::Morbo->new;
my $dir = tempdir CLEANUP => 1;
my $script = catdir $dir, 'myapp.pl';
my $morbo = Mojo::Server::Morbo->new;
ok !$morbo->check_file($script), 'file has not changed';
$command->write_rel_file('myapp.pl', <<EOF);
spurt <<EOF, $script;
use Mojolicious::Lite;
app->log->level('fatal');
Expand Down Expand Up @@ -70,7 +67,7 @@ is $tx->res->body, 'Hello Morbo!', 'right content';

# Update script without changing size
my ($size, $mtime) = (stat $script)[7, 9];
$command->write_rel_file('myapp.pl', <<EOF);
spurt <<EOF, $script;
use Mojolicious::Lite;
app->log->level('fatal');
Expand Down Expand Up @@ -105,7 +102,7 @@ is $tx->res->body, 'Hello World!', 'right content';
# Update script without changing mtime
($size, $mtime) = (stat $script)[7, 9];
ok !$morbo->check_file($script), 'file has not changed';
$command->write_rel_file('myapp.pl', <<EOF);
spurt <<EOF, $script;
use Mojolicious::Lite;
app->log->level('fatal');
Expand Down Expand Up @@ -146,6 +143,3 @@ sleep 1
PeerAddr => '127.0.0.1',
PeerPort => $port
);

# Cleanup
chdir $cwd;
5 changes: 2 additions & 3 deletions t/mojo/request.t
Expand Up @@ -8,7 +8,7 @@ use Test::More tests => 989;
# The answer to life's problems aren't at the bottom of a bottle,
# they're on TV!"
use File::Spec::Functions 'catfile';
use File::Temp;
use File::Temp 'tempdir';
use Mojo::Content::Single;
use Mojo::Content::MultiPart;
use Mojo::Cookie::Request;
Expand Down Expand Up @@ -817,8 +817,7 @@ is $req->body_params->to_hash->{text2}, '', 'right value';
is $req->upload('upload')->filename, 'hello.pl', 'right filename';
isa_ok $req->upload('upload')->asset, 'Mojo::Asset::Memory', 'right file';
is $req->upload('upload')->asset->size, 69, 'right size';
my $file
= catfile(File::Temp::tempdir(CLEANUP => 1), ("MOJO_TMP." . time . ".txt"));
my $file = catfile(tempdir(CLEANUP => 1), ("MOJO_TMP." . time . ".txt"));
ok $req->upload('upload')->move_to($file), 'moved file';
ok unlink($file), 'unlinked file';
is $req->content->boundary, '----------0xKhTmLbOuNdArY', 'right boundary';
Expand Down
20 changes: 17 additions & 3 deletions t/mojo/util.t
Expand Up @@ -2,15 +2,19 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 141;
use Test::More tests => 143;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
use FindBin;

# "If he is so smart, how come he is dead?"
use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(trim unquote secure_compare sha1_bytes sha1_sum url_escape url_unescape),
qw(xml_escape);
qw(trim unquote secure_compare sha1_bytes sha1_sum slurp spurt url_escape),
qw(url_unescape xml_escape);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -354,3 +358,13 @@ ok secure_compare('♥1', '♥1'), 'values are equal';
ok !secure_compare('', '♥0'), 'values are not equal';
ok !secure_compare('0♥', ''), 'values are not equal';
ok !secure_compare('0♥1', '1♥0'), 'values are not equal';

# slurp
is slurp(catfile(splitdir($FindBin::Bin), qw(templates exception.mt))),
"test\n% die;\n123\n", 'right content';

# spurt
my $dir = tempdir CLEANUP => 1;
my $file = catfile $dir, 'test.txt';
spurt "just\nworks!", $file;
is slurp($file), "just\nworks!", 'successful roundtrip';
4 changes: 2 additions & 2 deletions t/mojolicious/command.t
Expand Up @@ -8,7 +8,7 @@ use Test::More tests => 5;
# "Robot 1-X, save my friends! And Zoidberg!"
use Cwd 'cwd';
use File::Spec::Functions 'catdir';
use File::Temp;
use File::Temp 'tempdir';
use Mojolicious::Command;

# Application
Expand All @@ -17,7 +17,7 @@ isa_ok $command->app, 'Mojo', 'right application';

# Generating files
my $cwd = cwd;
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $dir = tempdir CLEANUP => 1;
chdir $dir;
$command->create_rel_dir('foo/bar');
ok -d catdir($dir, qw(foo bar)), 'directory exists';
Expand Down

0 comments on commit 16877e7

Please sign in to comment.