Tests and fixes for conditional inclusion
Tomas Doran [Thu, 16 Feb 2012 21:41:12 +0000 (21:41 +0000)]
lib/Catalyst/ScriptRunner.pm
t/aggregate/unit_core_scriptrunner_home.t [new file with mode: 0644]
t/aggregate/unit_utils_home.t [new file with mode: 0644]

index 1b52e13..7b80377 100644 (file)
@@ -35,7 +35,7 @@ sub subclass_with_traits {
 sub run {
     my ($self, $appclass, $scriptclass) = @_;
 
-    if (my $home = Catalyst::Utils::home($appclass)) {
+    if (my $home = Catalyst::Utils::find_home_unloaded_in_checkout()) {
         lib->import(File::Spec->catdir($home, 'lib'));
     }
 
diff --git a/t/aggregate/unit_core_scriptrunner_home.t b/t/aggregate/unit_core_scriptrunner_home.t
new file mode 100644 (file)
index 0000000..0816d25
--- /dev/null
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+use Test::More;
+use FindBin qw/$Bin/;
+use Test::Exception;
+use lib "$Bin/../lib";
+use File::Temp qw/ tempdir /;
+use Cwd;
+
+use_ok('Catalyst::ScriptRunner');
+
+my $cwd = cwd();
+
+my $d = tempdir(); #CLEANUP => 1);
+chdir($d) or die;
+mkdir("lib") or die;
+mkdir(File::Spec->catdir("lib", "MyApp")) or die;
+mkdir(File::Spec->catdir("lib", "MyApp", "Script")) or die;
+
+open(my $fh, '>', 'Makefile.PL') or die;
+close($fh) or die;
+
+open($fh, '>', File::Spec->catdir("lib", "MyApp", "Script", "Foo.pm")) or die;
+print $fh q{package MyApp::Script::Foo;
+use Moose;
+use namespace::autoclean;
+
+with 'Catalyst::ScriptRole';
+
+sub run { __PACKAGE__ }
+
+1;
+};
+close($fh) or die;
+
+use_ok 'Catalyst::ScriptRunner';
+
+is Catalyst::ScriptRunner->run('MyApp', 'Foo'), 'MyApp::Script::Foo';
+
+chdir($cwd) or die;
+
+done_testing;
diff --git a/t/aggregate/unit_utils_home.t b/t/aggregate/unit_utils_home.t
new file mode 100644 (file)
index 0000000..587f618
--- /dev/null
@@ -0,0 +1,45 @@
+use strict;
+use warnings;
+
+use Test::More;
+use File::Temp qw/ tempdir /;
+use Catalyst::Utils;
+use File::Spec;
+use Path::Class qw/ dir /;
+use Cwd qw/ cwd /;
+
+my @dists = Catalyst::Utils::dist_indicator_file_list();
+is(scalar(@dists), 3, 'Makefile.PL Build.PL dist.ini');
+
+my $cwd = cwd();
+foreach my $inc ('', 'lib', 'blib'){
+    my $d = tempdir(CLEANUP => 1);
+    chdir($d);
+    local $INC{'MyApp.pm'} = File::Spec->catdir($d, $inc, 'MyApp.pm');
+    ok !Catalyst::Utils::home('MyApp'), "No files found inc $inc";
+    open(my $fh, '>', "Makefile.PL");
+    close($fh);
+    is Catalyst::Utils::home('MyApp'), dir($d)->absolute->cleanup, "Did find inc '$inc'";
+}
+
+{
+    my $d = tempdir(CLEANUP => 1);
+    local $INC{'MyApp.pm'} = File::Spec->catdir($d, 'MyApp.pm');
+    ok !Catalyst::Utils::home('MyApp'), 'No files found';
+    mkdir File::Spec->catdir($d, 'MyApp');
+    is Catalyst::Utils::home('MyApp'), dir($d, 'MyApp')->absolute->cleanup;
+}
+
+{
+    my $d = tempdir(CLEANUP => 1);
+    chdir($d);
+    ok !Catalyst::Utils::find_home_unloaded_in_checkout();
+    open(my $fh, '>', "Makefile.PL");
+    close($fh);
+    is Catalyst::Utils::find_home_unloaded_in_checkout(), cwd(), "Did find home_unloaded_in_checkout"
+}
+
+chdir($cwd);
+
+done_testing;
+