Fixes for generated_app.t so it doesn't rely on finding catalyst.pl in the current...
[catagits/Catalyst-Devel.git] / t / generated_app.t
1 use strict;
2 use warnings;
3 use lib ();
4 use Cwd qw( abs_path );
5 use File::Temp qw/ tempdir /;
6 use File::Spec;
7 use FindBin qw/$Bin/;
8 use Catalyst::Devel;
9 use Catalyst::Helper;
10 use Test::More;
11
12 eval "use IPC::Run3";
13 plan skip_all => 'These tests require IPC::Run3' if $@;
14
15 my $share_dir = abs_path('share');
16 plan skip_all => "No share dir at $share_dir!"
17     unless -d $share_dir;
18
19 $ENV{CATALYST_DEVEL_SHAREDIR} = $share_dir;
20
21 my $dir = tempdir(CLEANUP => 1);
22 my $devnull = File::Spec->devnull;
23
24 diag "Generated app is in $dir";
25
26 chdir $dir or die "Cannot chdir to $dir: $!";
27
28 {
29     open my $fh, '>', $devnull or die "Cannot write to $devnull: $!";
30
31     local *STDOUT = $fh;
32
33     my $helper = Catalyst::Helper->new(
34         {
35             name => 'TestApp',
36         }
37     );
38
39     $helper->mk_app('TestApp');
40 }
41
42 my $app_dir = File::Spec->catdir($dir, 'TestApp');
43 chdir($app_dir) or die "Cannot chdir to $app_dir: $!";
44 lib->import(File::Spec->catdir($dir, 'TestApp', 'lib'));
45
46 my @files = qw|
47     Makefile.PL
48     testapp.conf
49     lib/TestApp.pm
50     lib/TestApp/Controller/Root.pm
51     README
52     Changes
53     t/01app.t
54     t/02pod.t
55     t/03podcoverage.t
56     root/static/images/catalyst_logo.png
57     root/static/images/btn_120x50_built.png
58     root/static/images/btn_120x50_built_shadow.png
59     root/static/images/btn_120x50_powered.png
60     root/static/images/btn_120x50_powered_shadow.png
61     root/static/images/btn_88x31_built.png
62     root/static/images/btn_88x31_built_shadow.png
63     root/static/images/btn_88x31_powered.png
64     root/static/images/btn_88x31_powered_shadow.png
65     root/favicon.ico
66     Makefile.PL
67     script/testapp_cgi.pl
68     script/testapp_fastcgi.pl
69     script/testapp_server.pl
70     script/testapp_test.pl
71     script/testapp_create.pl
72 |;
73
74 foreach my $fn (map { File::Spec->catdir(@$_) } map { [ split /\// ] } @files) {
75     test_fn($fn);
76 }
77 create_ok($_, 'My' . $_) for qw/Model View Controller/;
78
79 command_ok( [ $^X, 'Makefile.PL' ] );
80 ok -e "Makefile", "Makefile generated";
81 command_ok( [ 'make' ] );
82
83 run_generated_component_tests();
84
85 my $server_script = do {
86     open(my $fh, '<', File::Spec->catdir(qw/script testapp_server.pl/)) or fail $!;
87     local $/;
88     <$fh>;
89 };
90
91 ok $server_script;
92 ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
93     'SCRIPT_GEN found in generated output';
94 is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
95
96 chdir('/');
97 done_testing;
98
99 sub command_ok {
100     my $cmd = shift;
101     my $desc = shift;
102
103     my $stdout;
104     my $stderr;
105     run3( $cmd, \undef, \$stdout, \$stderr );
106
107     $desc ||= "Exit status ok for '@{$cmd}'";
108     unless ( is $? >> 8, 0, $desc ) {
109         diag "STDOUT:\n$stdout" if defined $stdout;
110         diag "STDERR:\n$stderr" if defined $stderr;
111     }
112 }
113
114 sub runperl {
115     my $comment = pop @_;
116     command_ok( [ $^X, '-I', File::Spec->catdir($Bin, '..', 'lib'), @_ ], $comment );
117 }
118
119 my @generated_component_tests;
120
121 sub test_fn {
122     my $fn = shift;
123     ok -r $fn, "Have $fn in generated app";
124     if ($fn =~ /script/) {
125         ok -x $fn, "$fn is executable";
126     }
127     if ($fn =~ /\.p[ml]$/) {
128         runperl( '-c', $fn, "$fn compiles" );
129     }
130     # Save these till later as Catalyst::Test will only be loaded once :-/
131     push @generated_component_tests, $fn
132         if $fn =~ /\.t$/;
133 }
134
135 sub run_generated_component_tests {
136     local $ENV{TEST_POD} = 1;
137     local $ENV{CATALYST_DEBUG} = 0;
138     foreach my $fn (@generated_component_tests) {
139         subtest "Generated app test: $fn", sub {
140             require $fn;
141         };
142     }
143 }
144
145 sub create_ok {
146     my ($type, $name) = @_;
147     runperl( File::Spec->catdir('script', 'testapp_create.pl'), $type, $name,
148         "'script/testapp_create.pl $type $name' ok");
149     test_fn(File::Spec->catdir('t', sprintf("%s_%s.t", lc $type, $name)));
150 }