76a422222df7f5ec42a76c92b4ca1f044c0d1954
[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::Spec::Functions qw( devnull catdir catfile updir rel2abs );
6 use File::Temp qw( tempdir );
7 use FindBin qw( $Bin );
8 use Catalyst::Helper;
9 use Test::More;
10 use Config;
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 my $instdir = tempdir(CLEANUP => 1);
21
22 $ENV{PERL_MM_OPT} = "INSTALL_BASE=$instdir";
23 $ENV{INSTALL_BASE} = $instdir;
24 if ($ENV{MAKEFLAGS}) {
25     $ENV{MAKEFLAGS} =~ s/PREFIX=[^\s]+//;
26     $ENV{MAKEFLAGS} =~ s/INSTALL_BASE=[^\s]+//;
27 }
28
29 my $dir = tempdir(CLEANUP => 1);
30 my $devnull = devnull;
31
32 diag "Generated app is in $dir";
33
34 chdir $dir or die "Cannot chdir to $dir: $!";
35
36 {
37     open my $fh, '>', $devnull or die "Cannot write to $devnull: $!";
38
39     local *STDOUT = $fh;
40
41     my $helper = Catalyst::Helper->new(
42         {
43             name => 'TestApp',
44         }
45     );
46
47     $helper->mk_app('TestApp');
48 }
49
50 my $app_dir = catdir($dir, 'TestApp');
51 chdir($app_dir) or die "Cannot chdir to $app_dir: $!";
52 lib->import(catdir($dir, 'TestApp', 'lib'));
53
54 my @files = qw|
55     Makefile.PL
56     testapp.conf
57     testapp.psgi
58     lib/TestApp.pm
59     lib/TestApp/Controller/Root.pm
60     README
61     Changes
62     t/01app.t
63     t/02pod.t
64     t/03podcoverage.t
65     root/static/images/catalyst_logo.png
66     root/static/images/btn_120x50_built.png
67     root/static/images/btn_120x50_built_shadow.png
68     root/static/images/btn_120x50_powered.png
69     root/static/images/btn_120x50_powered_shadow.png
70     root/static/images/btn_88x31_built.png
71     root/static/images/btn_88x31_built_shadow.png
72     root/static/images/btn_88x31_powered.png
73     root/static/images/btn_88x31_powered_shadow.png
74     root/favicon.ico
75     Makefile.PL
76     script/testapp_cgi.pl
77     script/testapp_fastcgi.pl
78     script/testapp_server.pl
79     script/testapp_test.pl
80     script/testapp_create.pl
81 |;
82
83 foreach my $fn (map { catdir(@$_) } map { [ split qr{/}, $_ ] } @files) {
84     test_fn($fn);
85 }
86 create_ok($_, 'My' . $_) for qw/Model View Controller/;
87
88 command_ok( [ $^X, 'Makefile.PL' ] );
89 ok -e "Makefile", "Makefile generated";
90 #NOTE: do not assume that 'make' is always 'make' as e.g. Win32/strawberry perl uses 'dmake'
91 command_ok( [ ($Config{make} || 'make') ] );
92
93 run_generated_component_tests();
94
95 my $server_script_file = catdir(qw/script testapp_server.pl/);
96 my $server_script = do {
97     open(my $fh, '<', $server_script_file) or fail $!;
98     local $/;
99     <$fh>;
100 };
101
102 ok $server_script;
103 ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
104     'SCRIPT_GEN found in generated output';
105 is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
106
107 {
108     open(my $fh, '>', $server_script_file) or fail $!;
109     print $fh "MOO\n";
110 }
111 my $helper = Catalyst::Helper->new(
112     {
113         '.newfiles' => 0,
114         'makefile'  => 0,
115         'scripts'   => 1,
116         name => '.',
117     }
118 );
119 $helper->mk_app( '.' ) or fail;
120
121 my $server_script_new = do {
122     open(my $fh, '<', $server_script_file) or fail $!;
123     local $/;
124     <$fh>;
125 };
126
127 is $server_script, $server_script_new;
128
129 diag "Installed app is in $instdir";
130 command_ok( [ ($Config{make} || 'make', 'install') ] );
131
132 my $inst_app_dir = catdir($instdir);
133 chdir($inst_app_dir) or die "Cannot chdir to $inst_app_dir: $!";
134 lib->import(catdir($instdir, 'lib', 'perl5'));
135
136 my @installed_files = qw|
137     lib/perl5/TestApp.pm
138     lib/perl5/TestApp/testapp.conf
139     lib/perl5/TestApp/Controller/Root.pm
140     lib/perl5/TestApp/root/static/images/catalyst_logo.png
141     lib/perl5/TestApp/root/static/images/btn_120x50_built.png
142     lib/perl5/TestApp/root/static/images/btn_120x50_built_shadow.png
143     lib/perl5/TestApp/root/static/images/btn_120x50_powered.png
144     lib/perl5/TestApp/root/static/images/btn_120x50_powered_shadow.png
145     lib/perl5/TestApp/root/static/images/btn_88x31_built.png
146     lib/perl5/TestApp/root/static/images/btn_88x31_built_shadow.png
147     lib/perl5/TestApp/root/static/images/btn_88x31_powered.png
148     lib/perl5/TestApp/root/static/images/btn_88x31_powered_shadow.png
149     lib/perl5/TestApp/root/favicon.ico
150     bin/testapp_cgi.pl
151     bin/testapp_fastcgi.pl
152     bin/testapp_server.pl
153     bin/testapp_test.pl
154     bin/testapp_create.pl
155 |;
156
157 foreach my $fn (map { catdir(@$_) } map { [ split qr{/}, $_ ] } @installed_files) {
158     my $ffn = catfile($inst_app_dir, $fn);
159     ok -r $ffn, "'$fn' installed in correct location";
160 }
161
162 chdir('/');
163 done_testing;
164
165 sub command_ok {
166     my $cmd = shift;
167     my $desc = shift;
168
169     my $stdout;
170     my $stderr;
171     run3( $cmd, \undef, \$stdout, \$stderr );
172
173     $desc ||= "Exit status ok for '@{$cmd}'";
174     unless ( is $? >> 8, 0, $desc ) {
175         diag "STDOUT:\n$stdout" if defined $stdout;
176         diag "STDERR:\n$stderr" if defined $stderr;
177     }
178 }
179
180 sub runperl {
181     my $comment = pop @_;
182     command_ok( [ $^X, '-I', catdir($Bin, '..', 'lib'), @_ ], $comment );
183 }
184
185 my @generated_component_tests;
186
187 sub test_fn {
188     my $fn = shift;
189     ok -r $fn, "Have $fn in generated app";
190     if ($fn =~ /script/) {
191         SKIP: {
192             skip 'Executable file flag test does not make sense on Win32', 1 if ($^O eq 'MSWin32');
193             ok -x $fn, "$fn is executable";
194        }
195     }
196     if ($fn =~ /\.p[ml]$/) {
197         runperl( '-c', $fn, "$fn compiles" );
198     }
199     # Save these till later as Catalyst::Test will only be loaded once :-/
200     push @generated_component_tests, $fn
201         if $fn =~ /\.t$/;
202 }
203
204 sub run_generated_component_tests {
205     local $ENV{TEST_POD} = 1;
206     local $ENV{CATALYST_DEBUG} = 0;
207     foreach my $fn (@generated_component_tests) {
208         my $full_name = rel2abs($fn);
209         subtest "Generated app test: $fn", sub {
210             do $full_name;
211         };
212     }
213 }
214
215 sub create_ok {
216     my ($type, $name) = @_;
217     runperl( catdir('script', 'testapp_create.pl'), $type, $name,
218         "'script/testapp_create.pl $type $name' ok");
219     test_fn( catdir('t', sprintf("%s_%s.t", lc $type, $name)));
220 }