Fix RT#65907: missing build require: Test::Exception
[catagits/Catalyst-Devel.git] / t / generated_app.t
CommitLineData
ad9d93fc 1use strict;
2use warnings;
0d4859fc 3use lib ();
4c709311 4use Cwd qw( abs_path );
5use File::Temp qw/ tempdir /;
0f89baa1 6use File::Spec;
a73b3971 7use FindBin qw/$Bin/;
3d3e34a0 8use Catalyst::Devel;
4c709311 9use Catalyst::Helper;
10use Test::More;
29ac6b9f 11use Config;
4c709311 12
13eval "use IPC::Run3";
14plan skip_all => 'These tests require IPC::Run3' if $@;
15
16my $share_dir = abs_path('share');
17plan skip_all => "No share dir at $share_dir!"
18 unless -d $share_dir;
19
20$ENV{CATALYST_DEVEL_SHAREDIR} = $share_dir;
62bb1d93 21my $instdir = tempdir(CLEANUP => 1);
636c7d94 22
62bb1d93 23$ENV{PERL_MM_OPT} = "INSTALL_BASE=$instdir";
636c7d94 24$ENV{INSTALL_BASE} = $instdir;
25if ($ENV{MAKEFLAGS}) {
26 $ENV{MAKEFLAGS} =~ s/PREFIX=[^\s]+//;
27 $ENV{MAKEFLAGS} =~ s/INSTALL_BASE=[^\s]+//;
28}
99618c49 29
0d4859fc 30my $dir = tempdir(CLEANUP => 1);
165233a4 31my $devnull = File::Spec->devnull;
ad9d93fc 32
15f460a5 33diag "Generated app is in $dir";
91497b69 34
4c709311 35chdir $dir or die "Cannot chdir to $dir: $!";
36
ad9d93fc 37{
4c709311 38 open my $fh, '>', $devnull or die "Cannot write to $devnull: $!";
39
40 local *STDOUT = $fh;
41
42 my $helper = Catalyst::Helper->new(
43 {
44 name => 'TestApp',
45 }
46 );
47
48 $helper->mk_app('TestApp');
ad9d93fc 49}
0d4859fc 50
4c709311 51my $app_dir = File::Spec->catdir($dir, 'TestApp');
52chdir($app_dir) or die "Cannot chdir to $app_dir: $!";
0d4859fc 53lib->import(File::Spec->catdir($dir, 'TestApp', 'lib'));
ad9d93fc 54
ad9d93fc 55my @files = qw|
56 Makefile.PL
2cb998f9 57 testapp.conf
91497b69 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
ad9d93fc 81|;
82
62bb1d93 83foreach my $fn (map { File::Spec->catdir(@$_) } map { [ File::Spec::Unix->splitdir($_) ] } @files) {
a73b3971 84 test_fn($fn);
ad9d93fc 85}
a73b3971 86create_ok($_, 'My' . $_) for qw/Model View Controller/;
ad9d93fc 87
4c709311 88command_ok( [ $^X, 'Makefile.PL' ] );
15f460a5 89ok -e "Makefile", "Makefile generated";
29ac6b9f 90#NOTE: do not assume that 'make' is always 'make' as e.g. Win32/strawberry perl uses 'dmake'
91command_ok( [ ($Config{make} || 'make') ] );
91497b69 92
41cebeb1 93run_generated_component_tests();
94
5327ff9b 95my $server_script_file = File::Spec->catdir(qw/script testapp_server.pl/);
3d3e34a0 96my $server_script = do {
5327ff9b 97 open(my $fh, '<', $server_script_file) or fail $!;
3d3e34a0 98 local $/;
99 <$fh>;
100};
101
15f460a5 102ok $server_script;
3d3e34a0 103ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
104 'SCRIPT_GEN found in generated output';
105is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
106
5327ff9b 107{
108 open(my $fh, '>', $server_script_file) or fail $!;
109 print $fh "MOO\n";
110}
111my $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
121my $server_script_new = do {
122 open(my $fh, '<', $server_script_file) or fail $!;
123 local $/;
124 <$fh>;
125};
126
127is $server_script, $server_script_new;
128
62bb1d93 129diag "Installed app is in $instdir";
130command_ok( [ ($Config{make} || 'make', 'install') ] );
131
132my $inst_app_dir = File::Spec->catdir($instdir);
133chdir($inst_app_dir) or die "Cannot chdir to $inst_app_dir: $!";
134lib->import(File::Spec->catdir($instdir, 'lib', 'perl5'));
135
136my @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
157foreach my $fn (map { File::Spec->catdir(@$_) } map { [ File::Spec::Unix->splitdir($_) ] } @installed_files) {
158 my $ffn = File::Spec->catfile($inst_app_dir, $fn);
159 ok -r $ffn, "'$fn' installed in correct location";
160}
161
91497b69 162chdir('/');
3d3e34a0 163done_testing;
a73b3971 164
4c709311 165sub 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
a73b3971 180sub runperl {
181 my $comment = pop @_;
4c709311 182 command_ok( [ $^X, '-I', File::Spec->catdir($Bin, '..', 'lib'), @_ ], $comment );
a73b3971 183}
184
41cebeb1 185my @generated_component_tests;
186
a73b3971 187sub test_fn {
a73b3971 188 my $fn = shift;
189 ok -r $fn, "Have $fn in generated app";
190 if ($fn =~ /script/) {
29ac6b9f 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 }
a73b3971 195 }
196 if ($fn =~ /\.p[ml]$/) {
197 runperl( '-c', $fn, "$fn compiles" );
198 }
41cebeb1 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
204sub run_generated_component_tests {
e3eb9f88 205 local $ENV{TEST_POD} = 1;
206 local $ENV{CATALYST_DEBUG} = 0;
41cebeb1 207 foreach my $fn (@generated_component_tests) {
a73b3971 208 subtest "Generated app test: $fn", sub {
c632d293 209 do $fn;
a73b3971 210 };
211 }
212}
213
214sub create_ok {
215 my ($type, $name) = @_;
216 runperl( File::Spec->catdir('script', 'testapp_create.pl'), $type, $name,
217 "'script/testapp_create.pl $type $name' ok");
4c709311 218 test_fn(File::Spec->catdir('t', sprintf("%s_%s.t", lc $type, $name)));
a73b3971 219}