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