Skip to content

Commit

Permalink
just return the destination
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 5, 2017
1 parent 4d080d5 commit 253ab3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
21 changes: 13 additions & 8 deletions lib/Mojo/File.pm
Expand Up @@ -21,14 +21,14 @@ use Mojo::Collection;

our @EXPORT_OK = ('path', 'tempdir', 'tempfile');

sub basename { scalar File::Basename::basename ${$_[0]}, @_ }
sub basename { File::Basename::basename ${$_[0]}, @_ }

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

sub copy_to {
my ($self, $to) = @_;
copy($$self, $to) or croak qq{Can't copy file "$$self" to "$to": $!};
return $self;
return $self->new(-d $to ? ($to, File::Basename::basename $self) : $to);
}

sub dirname { $_[0]->new(scalar File::Basename::dirname ${$_[0]}) }
Expand Down Expand Up @@ -73,7 +73,7 @@ sub make_path {
sub move_to {
my ($self, $to) = @_;
move($$self, $to) or croak qq{Can't move file "$$self" to "$to": $!};
return $self;
return $self->new(-d $to ? ($to, File::Basename::basename $self) : $to);
}

sub new {
Expand Down Expand Up @@ -237,9 +237,11 @@ Return a new L<Mojo::File> object relative to the path.
=head2 copy_to
$path = $path->copy_to('/home/sri/.vimrc.backup');
my $destination = $path->copy_to('/home/sri');
my $destination = $path->copy_to('/home/sri/.vimrc.backup');
Copy the file with L<File::Copy>.
Copy file with L<File::Copy> and return the destination as a L<Mojo::File>
object.
=head2 dirname
Expand Down Expand Up @@ -333,9 +335,11 @@ passed through to L<File::Path>.
=head2 move_to
$path = $path->move_to('/home/sri/.vimrc.backup');
my $destination = $path->move_to('/home/sri');
my $destination = $path->move_to('/home/sri/.vimrc.backup');
Move the file with L<File::Copy>.
Move file with L<File::Copy> and return the destination as a L<Mojo::File>
object.
=head2 new
Expand Down Expand Up @@ -413,7 +417,8 @@ Alias for L<Mojo::Base/"tap">.
my $absolute = $path->to_abs;
Return the absolute path as a L<Mojo::File> object.
Return absolute path as a L<Mojo::File> object, the path does not need to exist
on the file system.
=head2 to_array
Expand Down
19 changes: 17 additions & 2 deletions t/mojo/file.t
Expand Up @@ -125,20 +125,35 @@ my $destination = $dir->child('dest.txt');
my $source = $dir->child('src.txt')->spurt('works!');
ok -f $source, 'file exists';
ok !-f $destination, 'file does not exists';
ok !-f $source->move_to($destination), 'file no longer exists';
is $source->move_to($destination)->to_string, $destination, 'same path';
ok !-f $source, 'file no longer exists';
ok -f $destination, 'file exists';
is $destination->slurp, 'works!', 'right content';
$subdir = $dir->child('test')->make_path;
my $destination2 = $destination->move_to($subdir);
is $destination2, $subdir->child($destination->basename), 'same path';
ok !-f $destination, 'file no longer exists';
ok -f $destination2, 'file exists';
is $destination2->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';
is $source->copy_to($destination)->to_string, $destination, 'same path';
ok -f $source, 'file still exists';
ok -f $destination, 'file also exists now';
is $source->slurp, 'works!', 'right content';
is $destination->slurp, 'works!', 'right content';
$subdir = $dir->child('test')->make_path;
$destination2 = $destination->copy_to($subdir);
is $destination2, $subdir->child($destination->basename), 'same path';
ok -f $destination, 'file still exists';
ok -f $destination2, 'file also exists now';
is $destination->slurp, 'works!', 'right content';
is $destination2->slurp, 'works!', 'right content';

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

0 comments on commit 253ab3d

Please sign in to comment.