Skip to content

Commit

Permalink
fix Mojo::Home tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 8, 2017
1 parent 010d7bd commit cbfb8c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
13 changes: 3 additions & 10 deletions lib/Mojo/Home.pm
Expand Up @@ -2,9 +2,8 @@ package Mojo::Home;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Mojo::Util qw(class_to_path deprecated);
use Mojo::File 'path';
use Mojo::Util 'class_to_path';
use Mojo::Util qw(class_to_path deprecated);

has parts => sub { [] };

Expand All @@ -17,14 +16,8 @@ sub detect {

# Location of the application class
if ($class && (my $path = $INC{my $file = class_to_path $class})) {
$path =~ s/\Q$file\E$//;
my @home = @{path($path)};

# Remove "lib" and "blib"
pop @home while @home && ($home[-1] =~ /^b?lib$/ || !length $home[-1]);

# Turn into absolute path
return $self->parts(path(@home)->to_abs->to_array);
$path =~ s/(?:\/b?lib)?\/\Q$file\E$//;
return $self->parts(path($path)->to_abs->to_array);
}

# Current working directory
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -522,7 +522,7 @@ Convert a class name to a file.
my $path = class_to_path 'Foo::Bar';
Convert class name to path.
Convert class name to path, as used by C<%INC>.
# "Foo/Bar.pm"
class_to_path 'Foo::Bar';
Expand Down
33 changes: 26 additions & 7 deletions t/mojo/home.t
Expand Up @@ -9,25 +9,44 @@ use Mojo::HelloWorld;
use Mojo::Home;

# ENV detection
my $cwd = path->to_abs;
{
local $ENV{MOJO_HOME} = '.';
my $fake = path->to_abs->child('does_not_exist');
local $ENV{MOJO_HOME} = $fake->to_string;
my $home = Mojo::Home->new->detect;
is_deeply path($home->to_string)->to_abs->to_array, $cwd->to_array,
is_deeply path($home->to_string)->to_abs->to_array, $fake->to_array,
'right path detected';
}

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

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

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

# Current working directory
my $home = Mojo::Home->new->detect;
is_deeply path($home->to_string)->to_array, $cwd->to_array,
is_deeply path($home->to_string)->to_array, path->to_abs->to_array,
'right path detected';

# Path generation
Expand Down

0 comments on commit cbfb8c1

Please sign in to comment.