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