Skip to content

Commit

Permalink
use a slightly safer approach for detecting the home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 11, 2017
1 parent 6fcb8e1 commit 1c5ca37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/Mojo/Home.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ sub detect {
my ($self, $class) = @_;

# Environment variable
my $detected;
if ($ENV{MOJO_HOME}) { $detected = Mojo::File->new($ENV{MOJO_HOME})->to_abs }
my $home;
if ($ENV{MOJO_HOME}) { $home = Mojo::File->new($ENV{MOJO_HOME})->to_array }

# Location of the application class (Windows mixes backslash and slash)
elsif ($class && (my $path = $INC{my $file = class_to_path $class})) {
$path =~ s!\\!/!g;
$path =~ s!(?:(?:^|/)b?lib)?/\Q$file\E$!!;
$detected = Mojo::File->new($path)->to_abs;
$home = Mojo::File->new($path)->to_array;
splice @$home, split('/', $file) * -1;
pop @$home if @$home && ($home->[-1] eq 'blib' || $home->[-1] eq 'lib');
}

$$self = $detected->to_string if $detected;
$$self = Mojo::File->new(@$home)->to_abs->to_string if $home;
return $self;
}

Expand Down
7 changes: 7 additions & 0 deletions t/mojo/home.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ use Mojo::Home;
is_deeply $home->to_array, $fake->to_array, 'right path detected';
}

# Specific class detection (relative)
{
local $INC{'My/Class.pm'} = path('My', 'Class.pm')->to_string;
my $home = Mojo::Home->new->detect('My::Class');
is_deeply $home->to_array, path->to_array, 'right path detected';
}

# Specific class detection (relative "blib")
{
local $INC{'My/Class.pm'} = path('blib', 'My', 'Class.pm')->to_string;
Expand Down

0 comments on commit 1c5ca37

Please sign in to comment.