Skip to content

Commit

Permalink
add realpath method to Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 7, 2017
1 parent fd3cf43 commit d4eaf95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/Mojo/File.pm
Expand Up @@ -91,6 +91,8 @@ sub open {

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

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

sub remove_tree {
my $self = shift;
File::Path::remove_tree $$self, @_;
Expand Down Expand Up @@ -367,6 +369,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
5 changes: 4 additions & 1 deletion 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 Down Expand Up @@ -41,6 +41,9 @@ 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('.')->realpath, realpath('.'), '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'),
Expand Down

0 comments on commit d4eaf95

Please sign in to comment.