Skip to content

Commit

Permalink
add realpath method and make sibling method a bit faster
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 5, 2017
1 parent 09a566f commit 4d080d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

7.28 2017-03-05
- Added copy_to and sibling methods to Mojo::File.
- Added copy_to, realpath and sibling methods to Mojo::File.
- Added dir option to list_tree method in Mojo::File.
- Improved app generator command to generate a config file.
(tudorconstantin)
Expand Down
15 changes: 13 additions & 2 deletions lib/Mojo/File.pm
Expand Up @@ -91,13 +91,18 @@ sub open {

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

sub realpath { $_[0]->new(Cwd::realpath ${$_[0]}) }

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

sub sibling { shift->dirname->child(@_) }
sub sibling {
my $self = shift;
return $self->new(scalar File::Basename::dirname($self), @_);
}

sub slurp {
my $self = shift;
Expand Down Expand Up @@ -359,6 +364,12 @@ Open file with L<IO::File>.
use Fcntl qw(O_CREAT O_EXCL O_RDWR);
my $handle = path('/home/sri/test.pl')->open(O_RDWR | O_CREAT | O_EXCL);
=head2 realpath
my $realpath = $path->realpath;
Resolve the path with L<Cwd> and return the result as a L<Mojo::File> object.
=head2 remove_tree
$path = $path->remove_tree;
Expand Down Expand Up @@ -402,7 +413,7 @@ Alias for L<Mojo::Base/"tap">.
my $absolute = $path->to_abs;
Return the canonical path as a L<Mojo::File> object.
Return the absolute path as a L<Mojo::File> object.
=head2 to_array
Expand Down
11 changes: 8 additions & 3 deletions t/mojo/file.t
@@ -1,7 +1,7 @@
use Mojo::Base -strict;

use Test::More;
use Cwd 'getcwd';
use Cwd qw(getcwd realpath);
use Fcntl 'O_RDONLY';
use File::Basename qw(basename dirname);
use File::Spec::Functions qw(abs2rel canonpath catfile rel2abs splitdir);
Expand All @@ -26,7 +26,7 @@ is path('foo', 'bar')->child('baz', 'yada'),

# Siblings
is path('foo', 'bar')->sibling('baz', 'yada'),
catfile(dirname(catfile('foo', 'bar')), 'baz', 'yada'), 'same path';
catfile(scalar dirname(catfile('foo', 'bar')), 'baz', 'yada'), 'same path';

# Array
is_deeply path('foo', 'bar')->to_array, [splitdir catfile('foo', 'bar')],
Expand All @@ -41,13 +41,18 @@ is path('file.t')->to_abs, rel2abs('file.t'), 'same path';
is path('test.txt')->to_abs->to_rel(getcwd),
abs2rel(rel2abs('test.txt'), getcwd), 'same path';

# Resolved
is path(__FILE__)->sibling('..', 'lib')->realpath,
realpath(catfile(scalar dirname(__FILE__), '..', 'lib')), 'same path';

# Basename
is path('file.t')->to_abs->basename, basename(rel2abs 'file.t'), 'same path';
is path('file.t')->to_abs->basename('.t'), basename(rel2abs('file.t'), '.t'),
'same path';

# Dirname
is path('file.t')->to_abs->dirname, dirname(rel2abs 'file.t'), 'same path';
is path('file.t')->to_abs->dirname, scalar dirname(rel2abs 'file.t'),
'same path';

# Checks
ok path(__FILE__)->to_abs->is_abs, 'path is absolute';
Expand Down

0 comments on commit 4d080d5

Please sign in to comment.