f6b3990bb1e78836b74decbee3202941e5d99cd4
[catagits/Catalyst-Devel.git] / t / generated_app.t
1 use strict;
2 use warnings;
3
4 use File::Temp qw/ tempdir tmpnam /;
5 use File::Spec;
6 use Test::WWW::Mechanize;
7 use Catalyst::Devel;
8
9 my $dir = tempdir();
10 my $devnull = File::Spec->devnull;
11
12 use Test::More;
13
14 diag "In $dir";
15
16 {
17     my $exit;
18     if ($^O eq 'MSWin32') {
19       $exit = system("cd $dir & catalyst TestApp > $devnull 2>&1");
20     }
21     else {
22       $exit = system("cd $dir; catalyst.pl TestApp > $devnull 2>&1");
23     }
24     is $exit, 0, 'Exit status ok';
25 }
26 # FIXME paths / nl work on win32
27 chdir("$dir/TestApp/");
28
29 my @files = qw|
30     Makefile.PL
31     testapp.conf
32     lib/TestApp.pm
33     lib/TestApp/Controller/Root.pm
34     README
35     Changes
36     t/01app.t
37     t/02pod.t
38     t/03podcoverage.t
39     root/static/images/catalyst_logo.png
40     root/static/images/btn_120x50_built.png
41     root/static/images/btn_120x50_built_shadow.png
42     root/static/images/btn_120x50_powered.png
43     root/static/images/btn_120x50_powered_shadow.png
44     root/static/images/btn_88x31_built.png
45     root/static/images/btn_88x31_built_shadow.png
46     root/static/images/btn_88x31_powered.png
47     root/static/images/btn_88x31_powered_shadow.png
48     root/favicon.ico
49     Makefile.PL
50     script/testapp_cgi.pl
51     script/testapp_fastcgi.pl
52     script/testapp_server.pl
53     script/testapp_test.pl
54     script/testapp_create.pl
55 |;
56
57 foreach my $fn (@files) {
58     ok -r $fn, "Have $fn in generated app";
59     if ($fn =~ /script/) {
60         ok -x $fn, "$fn is executable";
61     }
62 }
63
64 ## Makefile stuff
65 my $makefile_status = `$^X Makefile.PL`;
66 ok $makefile_status, "Makefile ran okay";
67 ok -e "Makefile", "Makefile exists";
68
69 is system("make"), 0, 'Run make';
70
71 {
72     local $ENV{TEST_POD} = 1;
73
74     foreach my $test (grep { m|^t/| } @files) {
75         subtest "Generated app test: $test", sub {
76             require $test;
77         }
78     }
79 }
80
81 ## Moosey server tests - kmx++
82 my $server_path   = File::Spec->catfile('script', 'testapp_server.pl');
83 my $port = int(rand(10000)) + 40000; # get random port between 40000-50000
84
85 my $childpid = fork();
86 die "fork() error, cannot continue" unless defined($childpid);
87
88 if ($childpid == 0) {
89   system("$^X $server_path -p $port > $devnull 2>&1");
90   exit; # just for sure; we should never got here
91 }
92
93 sleep 10; #wait for catalyst application to start
94 my $mech = Test::WWW::Mechanize->new;
95 $mech->get_ok( "http://localhost:" . $port );
96
97 kill 'KILL', $childpid;
98
99 my $server_script = do {
100     open(my $fh, '<', 'script/testapp_server.pl') or die $!;
101     local $/;
102     <$fh>;
103 };
104
105 ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
106     'SCRIPT_GEN found in generated output';
107 is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
108
109 chdir('/');
110
111 done_testing;
112