Skip to content

Commit

Permalink
add a few new features to Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 25, 2017
1 parent fece5de commit bed4003
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@

7.22 2017-01-25
- Added ports method to Mojo::Server::Daemon.
- Added remove_tree method to Mojo::File.
- Improved spurt method in Mojo::File with support for writing multiple chunks
at once.

7.21 2017-01-21
- Added extract_usage function to Mojo::Util.
Expand Down
24 changes: 20 additions & 4 deletions lib/Mojo/File.pm
Expand Up @@ -78,6 +78,12 @@ sub new {

sub path { __PACKAGE__->new(@_) }

sub remove_tree {
my $self = shift;
File::Path::remove_tree $$self, @_;
return $self;
}

sub slurp {
my $self = shift;

Expand All @@ -90,7 +96,7 @@ sub slurp {
}

sub spurt {
my ($self, $content) = @_;
my ($self, $content) = (shift, join '', @_);
open my $file, '>', $$self or croak qq{Can't open file "$$self": $!};
($file->syswrite($content) // -1) == length $content
or croak qq{Can't write to file "$$self": $!};
Expand Down Expand Up @@ -224,7 +230,8 @@ Check if the path is absolute.
my $collection = $path->list({hidden => 1});
List all files in the directory and return a L<Mojo::Collection> object
containing the results as L<Mojo::File> objects.
containing the results as L<Mojo::File> objects. The list does not include C<.>
and C<..>.
# List files
say for Mojo::File->new('/home/sri/myapp')->list->each;
Expand Down Expand Up @@ -253,7 +260,8 @@ Include hidden files.
my $collection = $path->list_tree({hidden => 1});
List all files recursively in the directory and return a L<Mojo::Collection>
object containing the results as L<Mojo::File> objects.
object containing the results as L<Mojo::File> objects. The list does not
include C<.> and C<..>.
# List all templates
say for Mojo::File->new('/home/sri/myapp/templates')->list_tree->each;
Expand All @@ -280,7 +288,7 @@ Create the directories if they don't already exist with L<File::Path>.
$path = $path->move_to('/home/sri/.vimrc.backup');
Move the file.
Move the file with L<File::Copy>.
=head2 new
Expand All @@ -295,6 +303,13 @@ directory.
# "foo/bar/baz.txt" (on UNIX)
Mojo::File->new('foo', 'bar', 'baz.txt');
=head2 remove_tree
$path = $path->remove_tree;
Delete this directory and any files and subdirectories it may contain with
L<File::Path>.
=head2 slurp
my $bytes = $path->slurp;
Expand All @@ -304,6 +319,7 @@ Read all data at once from the file.
=head2 spurt
$path = $path->spurt($bytes);
$path = $path->spurt(@chunks_of_bytes);
Write all data at once to the file.
Expand Down
10 changes: 5 additions & 5 deletions t/mojo/asset.t
Expand Up @@ -149,11 +149,11 @@ undef $tmp;
ok !-e $path, 'file has been cleaned up';
is $mem->move_to($path)->slurp, 'abc', 'right content';
ok -e $path, 'file exists';
unlink $path;
ok unlink($path), 'unlinked file';
ok !-e $path, 'file has been cleaned up';
is(Mojo::Asset::Memory->new->move_to($path)->slurp, '', 'no content');
ok -e $path, 'file exists';
unlink $path;
ok unlink($path), 'unlinked file';
ok !-e $path, 'file has been cleaned up';

# Move file asset to file
Expand All @@ -169,11 +169,11 @@ ok !-e $path, 'file has been cleaned up';
is $file->move_to($path)->slurp, 'bcd', 'right content';
undef $file;
ok -e $path, 'file exists';
unlink $path;
ok unlink($path), 'unlinked file';
ok !-e $path, 'file has been cleaned up';
is(Mojo::Asset::File->new->move_to($path)->slurp, '', 'no content');
ok -e $path, 'file exists';
unlink $path;
ok unlink($path), 'unlinked file';
ok !-e $path, 'file has been cleaned up';

# Upgrade
Expand Down Expand Up @@ -232,7 +232,7 @@ is $file->contains('es'), 1, '"es" at position 1';
$path = $file->path;
undef $file;
ok -e $path, 'file exists';
unlink $path;
ok unlink($path), 'unlinked file';
ok !-e $path, 'file has been cleaned up';

# Incomplete write
Expand Down
7 changes: 7 additions & 0 deletions t/mojo/file.t
Expand Up @@ -71,6 +71,12 @@ ok !-d $subdir, 'directory does not exist anymore';
$subdir->make_path;
ok -d $subdir, 'directory exists';

# Remove tree
$dir = tempdir;
$dir->child('foo', 'bar')->make_path->child('test.txt')->spurt('test!');
is $dir->child('foo', 'bar', 'test.txt')->slurp, 'test!', 'right content';
ok !-e $dir->child('foo')->remove_tree->to_string, 'tree has been removed';

# Move to
$dir = tempdir;
my $destination = $dir->child('dest.txt');
Expand Down Expand Up @@ -123,6 +129,7 @@ is_deeply path($lib)->list_tree({hidden => 1})->map('to_string')->to_array,
$dir = tempdir;
my $file = $dir->child('test.txt')->spurt('just works!');
is $file->slurp, 'just works!', 'right content';
is $file->spurt('w', 'orks', ' too!')->slurp, 'works too!', 'right content';
{
no warnings 'redefine';
local *IO::Handle::syswrite = sub { $! = 0; 5 };
Expand Down

0 comments on commit bed4003

Please sign in to comment.