Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add copy_to method to Mojo::File
  • Loading branch information
kraih committed Mar 4, 2017
1 parent 45f90bd commit bd9288c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

7.28 2017-02-28
7.28 2017-03-04
- Added copy_to method to Mojo::File.

7.27 2017-02-27
- Added support for UNIX domain sockets. (sri, salva)
Expand Down
13 changes: 13 additions & 0 deletions lib/Mojo/File.pm
Expand Up @@ -25,6 +25,13 @@ sub basename { scalar File::Basename::basename ${$_[0]}, @_ }

sub child { $_[0]->new(@_) }

sub copy_to {
my ($self, $to) = @_;
File::Copy::copy($$self, $to)
or croak qq{Can't copy file "$$self" to "$to": $!};
return $self;
}

sub dirname { $_[0]->new(scalar File::Basename::dirname ${$_[0]}) }

sub is_abs { file_name_is_absolute ${shift()} }
Expand Down Expand Up @@ -223,6 +230,12 @@ Return a new L<Mojo::File> object relative to the path.
# "/home/sri/.vimrc" (on UNIX)
path('/home')->child('sri', '.vimrc');
=head2 copy_to
$path = $path->copy_to('/home/sri/.vimrc.backup');
Copy the file with L<File::Copy>.
=head2 dirname
my $name = $path->dirname;
Expand Down
11 changes: 11 additions & 0 deletions t/mojo/file.t
Expand Up @@ -120,6 +120,17 @@ ok !-f $source->move_to($destination), 'file no longer exists';
ok -f $destination, 'file exists';
is $destination->slurp, 'works!', 'right content';

# Copy to
$dir = tempdir;
$destination = $dir->child('dest.txt');
$source = $dir->child('src.txt')->spurt('works!');
ok -f $source, 'file exists';
ok !-f $destination, 'file does not exists';
ok -f $source->copy_to($destination), 'file still exists';
ok -f $destination, 'file also exists now';
is $source->slurp, 'works!', 'right content';
is $destination->slurp, 'works!', 'right content';

# List
is_deeply path('does_not_exist')->list->to_array, [], 'no files';
is_deeply path(__FILE__)->list->to_array, [], 'no files';
Expand Down

0 comments on commit bd9288c

Please sign in to comment.