Fix bug with generated component classes and an issue with the test
[catagits/Catalyst-Devel.git] / t / generated_app.t
index f6b3990..2a46078 100644 (file)
@@ -1,17 +1,17 @@
 use strict;
 use warnings;
-
+use lib ();
 use File::Temp qw/ tempdir tmpnam /;
 use File::Spec;
-use Test::WWW::Mechanize;
+use FindBin qw/$Bin/;
 use Catalyst::Devel;
 
-my $dir = tempdir();
+my $dir = tempdir(CLEANUP => 1);
 my $devnull = File::Spec->devnull;
 
 use Test::More;
 
-diag "In $dir";
+diag "Generated app is in $dir";
 
 {
     my $exit;
@@ -23,8 +23,9 @@ diag "In $dir";
     }
     is $exit, 0, 'Exit status ok';
 }
-# FIXME paths / nl work on win32
-chdir("$dir/TestApp/");
+
+chdir(File::Spec->catdir($dir, 'TestApp'));
+lib->import(File::Spec->catdir($dir, 'TestApp', 'lib'));
 
 my @files = qw|
     Makefile.PL
@@ -54,59 +55,66 @@ my @files = qw|
     script/testapp_create.pl
 |;
 
-foreach my $fn (@files) {
-    ok -r $fn, "Have $fn in generated app";
-    if ($fn =~ /script/) {
-        ok -x $fn, "$fn is executable";
-    }
+foreach my $fn (map { File::Spec->catdir(@$_) } map { [ split /\// ] } @files) {
+    test_fn($fn);
 }
+create_ok($_, 'My' . $_) for qw/Model View Controller/;
 
-## Makefile stuff
-my $makefile_status = `$^X Makefile.PL`;
-ok $makefile_status, "Makefile ran okay";
-ok -e "Makefile", "Makefile exists";
-
+is system($^X, 'Makefile.PL'), 0, 'Ran Makefile.PL';
+ok -e "Makefile", "Makefile generated";
 is system("make"), 0, 'Run make';
 
-{
-    local $ENV{TEST_POD} = 1;
-
-    foreach my $test (grep { m|^t/| } @files) {
-        subtest "Generated app test: $test", sub {
-            require $test;
-        }
-    }
-}
-
-## Moosey server tests - kmx++
-my $server_path   = File::Spec->catfile('script', 'testapp_server.pl');
-my $port = int(rand(10000)) + 40000; # get random port between 40000-50000
-
-my $childpid = fork();
-die "fork() error, cannot continue" unless defined($childpid);
-
-if ($childpid == 0) {
-  system("$^X $server_path -p $port > $devnull 2>&1");
-  exit; # just for sure; we should never got here
-}
-
-sleep 10; #wait for catalyst application to start
-my $mech = Test::WWW::Mechanize->new;
-$mech->get_ok( "http://localhost:" . $port );
-
-kill 'KILL', $childpid;
+run_generated_component_tests();
 
 my $server_script = do {
-    open(my $fh, '<', 'script/testapp_server.pl') or die $!;
+    open(my $fh, '<', File::Spec->catdir(qw/script testapp_server.pl/)) or fail $!;
     local $/;
     <$fh>;
 };
 
+ok $server_script;
 ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
     'SCRIPT_GEN found in generated output';
 is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
 
 chdir('/');
-
 done_testing;
 
+sub runperl {
+    my $comment = pop @_;
+    is system($^X, '-I', File::Spec->catdir($Bin, '..', 'lib'), @_), 0, $comment;
+}
+
+my @generated_component_tests;
+
+sub test_fn {
+    local $ENV{TEST_POD} = 1;
+    local $ENV{CATALYST_DEBUG} = 0;
+
+    my $fn = shift;
+    ok -r $fn, "Have $fn in generated app";
+    if ($fn =~ /script/) {
+        ok -x $fn, "$fn is executable";
+    }
+    if ($fn =~ /\.p[ml]$/) {
+        runperl( '-c', $fn, "$fn compiles" );
+    }
+    # Save these till later as Catalyst::Test will only be loaded once :-/
+    push @generated_component_tests, $fn
+        if $fn =~ /\.t$/;
+}
+
+sub run_generated_component_tests {
+    foreach my $fn (@generated_component_tests) {
+        subtest "Generated app test: $fn", sub {
+            require $fn;
+        };
+    }
+}
+
+sub create_ok {
+    my ($type, $name) = @_;
+    runperl( File::Spec->catdir('script', 'testapp_create.pl'), $type, $name,
+        "'script/testapp_create.pl $type $name' ok");
+    test_fn(File::Spec->catdir('t', sprintf("%s_%s.t", $type, $name)));
+}