Skip to content

Commit

Permalink
formalize home detection strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 13, 2016
1 parent 2f15e46 commit 4ae568a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
13 changes: 6 additions & 7 deletions lib/Mojo/Home.pm
Expand Up @@ -2,10 +2,9 @@ package Mojo::Home;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Cwd 'abs_path';
use Cwd qw(abs_path getcwd);
use File::Basename 'dirname';
use File::Spec::Functions qw(abs2rel catdir catfile splitdir);
use FindBin;
use Mojo::Util qw(class_to_path files);

has parts => sub { [] };
Expand All @@ -16,7 +15,7 @@ sub detect {
# Environment variable
return $self->parts([splitdir abs_path $ENV{MOJO_HOME}]) if $ENV{MOJO_HOME};

# Try to find home from lib directory
# Location of the application class
if ($class && (my $path = $INC{my $file = class_to_path $class})) {
$path =~ s/\Q$file\E$//;
my @home = splitdir $path;
Expand All @@ -28,8 +27,8 @@ sub detect {
return $self->parts([splitdir abs_path catdir(@home) || '.']);
}

# FindBin fallback
return $self->parts([split '/', $FindBin::Bin]);
# Current working directory
return $self->parts([splitdir getcwd]);
}

sub lib_dir {
Expand Down Expand Up @@ -98,8 +97,8 @@ following new ones.
$home = $home->detect;
$home = $home->detect('My::App');
Detect home directory from the value of the C<MOJO_HOME> environment variable
or application class.
Detect home directory from the value of the C<MOJO_HOME> environment variable,
location of the application class, or the current working directory.
=head2 lib_dir
Expand Down
20 changes: 7 additions & 13 deletions t/mojo/home.t
Expand Up @@ -15,26 +15,20 @@ my $target = canonpath realpath getcwd;
{
local $ENV{MOJO_HOME} = '.';
my $home = Mojo::Home->new->detect;
is_deeply [split /\\|\//, canonpath($home->to_string)],
[split /\\|\//, $target], 'right path detected';
is_deeply [splitdir canonpath($home->to_string)], [splitdir $target],
'right path detected';
}

# Class detection
my $original = catdir splitdir $FindBin::Bin;
# Current working directory
my $original = catdir splitdir getcwd;
my $home = Mojo::Home->new->detect;
is_deeply [split /\\|\//, realpath $original], [split /\\|\//, $home],
'right path detected';
is_deeply [splitdir realpath getcwd], [splitdir $home], 'right path detected';

# Specific class detection
$INC{'MyClass.pm'} = 'MyClass.pm';
$home = Mojo::Home->new->detect('MyClass');
is_deeply [split /\\|\//, canonpath($home->to_string)],
[split /\\|\//, $target], 'right path detected';

# FindBin detection
$home = Mojo::Home->new->detect(undef);
is_deeply [split /\\|\//, catdir(splitdir($FindBin::Bin))],
[split /\\|\//, $home], 'right path detected';
is_deeply [splitdir canonpath($home->to_string)], [splitdir $target],
'right path detected';

# Path generation
$home = Mojo::Home->new($FindBin::Bin);
Expand Down

0 comments on commit 4ae568a

Please sign in to comment.