Adapt to recent -Runtime changes
[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;
99618c49 21
0d4859fc 22my $dir = tempdir(CLEANUP => 1);
165233a4 23my $devnull = File::Spec->devnull;
ad9d93fc 24
15f460a5 25diag "Generated app is in $dir";
91497b69 26
4c709311 27chdir $dir or die "Cannot chdir to $dir: $!";
28
ad9d93fc 29{
4c709311 30 open my $fh, '>', $devnull or die "Cannot write to $devnull: $!";
31
32 local *STDOUT = $fh;
33
34 my $helper = Catalyst::Helper->new(
35 {
36 name => 'TestApp',
37 }
38 );
39
40 $helper->mk_app('TestApp');
ad9d93fc 41}
0d4859fc 42
4c709311 43my $app_dir = File::Spec->catdir($dir, 'TestApp');
44chdir($app_dir) or die "Cannot chdir to $app_dir: $!";
0d4859fc 45lib->import(File::Spec->catdir($dir, 'TestApp', 'lib'));
ad9d93fc 46
ad9d93fc 47my @files = qw|
48 Makefile.PL
2cb998f9 49 testapp.conf
b769c101 50 testapp.psgi
91497b69 51 lib/TestApp.pm
52 lib/TestApp/Controller/Root.pm
53 README
54 Changes
55 t/01app.t
56 t/02pod.t
57 t/03podcoverage.t
58 root/static/images/catalyst_logo.png
59 root/static/images/btn_120x50_built.png
60 root/static/images/btn_120x50_built_shadow.png
61 root/static/images/btn_120x50_powered.png
62 root/static/images/btn_120x50_powered_shadow.png
63 root/static/images/btn_88x31_built.png
64 root/static/images/btn_88x31_built_shadow.png
65 root/static/images/btn_88x31_powered.png
66 root/static/images/btn_88x31_powered_shadow.png
67 root/favicon.ico
68 Makefile.PL
69 script/testapp_cgi.pl
70 script/testapp_fastcgi.pl
71 script/testapp_server.pl
72 script/testapp_test.pl
73 script/testapp_create.pl
ad9d93fc 74|;
75
a73b3971 76foreach my $fn (map { File::Spec->catdir(@$_) } map { [ split /\// ] } @files) {
77 test_fn($fn);
ad9d93fc 78}
a73b3971 79create_ok($_, 'My' . $_) for qw/Model View Controller/;
ad9d93fc 80
4c709311 81command_ok( [ $^X, 'Makefile.PL' ] );
15f460a5 82ok -e "Makefile", "Makefile generated";
29ac6b9f 83#NOTE: do not assume that 'make' is always 'make' as e.g. Win32/strawberry perl uses 'dmake'
84command_ok( [ ($Config{make} || 'make') ] );
91497b69 85
41cebeb1 86run_generated_component_tests();
87
5327ff9b 88my $server_script_file = File::Spec->catdir(qw/script testapp_server.pl/);
3d3e34a0 89my $server_script = do {
5327ff9b 90 open(my $fh, '<', $server_script_file) or fail $!;
3d3e34a0 91 local $/;
92 <$fh>;
93};
94
15f460a5 95ok $server_script;
3d3e34a0 96ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
97 'SCRIPT_GEN found in generated output';
98is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
99
5327ff9b 100{
101 open(my $fh, '>', $server_script_file) or fail $!;
102 print $fh "MOO\n";
103}
104my $helper = Catalyst::Helper->new(
105 {
106 '.newfiles' => 0,
107 'makefile' => 0,
108 'scripts' => 1,
109 name => '.',
110 }
111);
112$helper->mk_app( '.' ) or fail;
113
114my $server_script_new = do {
115 open(my $fh, '<', $server_script_file) or fail $!;
116 local $/;
117 <$fh>;
118};
119
120is $server_script, $server_script_new;
121
91497b69 122chdir('/');
3d3e34a0 123done_testing;
a73b3971 124
4c709311 125sub command_ok {
126 my $cmd = shift;
127 my $desc = shift;
128
129 my $stdout;
130 my $stderr;
131 run3( $cmd, \undef, \$stdout, \$stderr );
132
133 $desc ||= "Exit status ok for '@{$cmd}'";
134 unless ( is $? >> 8, 0, $desc ) {
135 diag "STDOUT:\n$stdout" if defined $stdout;
136 diag "STDERR:\n$stderr" if defined $stderr;
137 }
138}
139
a73b3971 140sub runperl {
141 my $comment = pop @_;
4c709311 142 command_ok( [ $^X, '-I', File::Spec->catdir($Bin, '..', 'lib'), @_ ], $comment );
a73b3971 143}
144
41cebeb1 145my @generated_component_tests;
146
a73b3971 147sub test_fn {
a73b3971 148 my $fn = shift;
149 ok -r $fn, "Have $fn in generated app";
150 if ($fn =~ /script/) {
29ac6b9f 151 SKIP: {
152 skip 'Executable file flag test does not make sense on Win32', 1 if ($^O eq 'MSWin32');
153 ok -x $fn, "$fn is executable";
154 }
a73b3971 155 }
156 if ($fn =~ /\.p[ml]$/) {
157 runperl( '-c', $fn, "$fn compiles" );
158 }
41cebeb1 159 # Save these till later as Catalyst::Test will only be loaded once :-/
160 push @generated_component_tests, $fn
161 if $fn =~ /\.t$/;
162}
163
164sub run_generated_component_tests {
e3eb9f88 165 local $ENV{TEST_POD} = 1;
166 local $ENV{CATALYST_DEBUG} = 0;
41cebeb1 167 foreach my $fn (@generated_component_tests) {
a73b3971 168 subtest "Generated app test: $fn", sub {
c632d293 169 do $fn;
a73b3971 170 };
171 }
172}
173
174sub create_ok {
175 my ($type, $name) = @_;
176 runperl( File::Spec->catdir('script', 'testapp_create.pl'), $type, $name,
177 "'script/testapp_create.pl $type $name' ok");
4c709311 178 test_fn(File::Spec->catdir('t', sprintf("%s_%s.t", lc $type, $name)));
a73b3971 179}