Skip to content

Commit

Permalink
fix bugs in Mojo::Util and Mojo::Asset::File where incomplete writes …
Browse files Browse the repository at this point in the history
…would not be recognized as errors (closes #1020)
  • Loading branch information
kraih committed Nov 29, 2016
1 parent 02049dd commit 903feb8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Changes
Expand Up @@ -5,8 +5,10 @@
to the start method.
- Improved log attribute in Mojolicious making it easier to override default
settings. (jberger)
- Fixed a bug in Mojo::Server::Prefork where workers would accept keep-alive
- Fixed bug in Mojo::Server::Prefork where workers would accept keep-alive
requests after a graceful shutdown had already been initiated.
- Fixed bugs in Mojo::Util and Mojo::Asset::File where incomplete writes would
not be recognized as errors. (bobkare, sri)

7.10 2016-11-01
- Added getopt function to Mojo::Util.
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Asset/File.pm
Expand Up @@ -46,7 +46,8 @@ sub DESTROY {

sub add_chunk {
my ($self, $chunk) = @_;
defined $self->handle->syswrite($chunk) or croak "Can't write to asset: $!";
($self->handle->syswrite($chunk) // -1) == length $chunk
or croak "Can't write to asset: $!";
return $self;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -269,7 +269,7 @@ sub split_header { _header(shift, 0) }
sub spurt {
my ($content, $path) = @_;
open my $file, '>', $path or croak qq{Can't open file "$path": $!};
defined $file->syswrite($content)
($file->syswrite($content) // -1) == length $content
or croak qq{Can't write to file "$path": $!};
return $content;
}
Expand Down
8 changes: 8 additions & 0 deletions t/mojo/asset.t
Expand Up @@ -238,6 +238,14 @@ ok -e $path, 'file exists';
unlink $path;
ok !-e $path, 'file has been cleaned up';

# Incomplete write
{
no warnings 'redefine';
local *IO::Handle::syswrite = sub { $! = 0; 2 };
eval { Mojo::Asset::File->new->add_chunk('test') };
like $@, qr/Can't write to asset: .*/, 'right error';
}

# Abstract methods
eval { Mojo::Asset->add_chunk };
like $@, qr/Method "add_chunk" not implemented by subclass/, 'right error';
Expand Down
8 changes: 8 additions & 0 deletions t/mojo/util.t
Expand Up @@ -422,6 +422,14 @@ my $file = catfile $dir, 'test.txt';
spurt "just\nworks!", $file;
is slurp($file), "just\nworks!", 'successful roundtrip';

# spurt (incomplete write)
{
no warnings 'redefine';
local *IO::Handle::syswrite = sub { $! = 0; 5 };
eval { spurt "just\nworks!", $file };
like $@, qr/Can't write to file ".*/, 'right error';
}

# files
is_deeply [files 'does_not_exist'], [], 'no files';
is_deeply [files __FILE__], [], 'no files';
Expand Down

0 comments on commit 903feb8

Please sign in to comment.