use IPC::Open3 rather than IPC::Run3
[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 use IPC::Open3 qw( open3 );
12
13 my $helper_lib = abs_path(catdir(dirname($INC{'Catalyst/Helper.pm'}), updir));
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 my $MAKE = $Config{make} || 'make';
23
24 my $escaped_path = $instdir;
25 $escaped_path =~ s/\\/\\\\/g;
26 if ($escaped_path =~ s/ /\\ /g) {
27   $escaped_path = qq{"$escaped_path"};
28 }
29
30 $ENV{PERL_MM_OPT} = "INSTALL_BASE=$escaped_path";
31
32 if ($ENV{MAKEFLAGS}) {
33     $ENV{MAKEFLAGS} =~ s/PREFIX=[^\s]+//;
34     $ENV{MAKEFLAGS} =~ s/INSTALL_BASE=[^\s]+//;
35 }
36
37 my $dir = tempdir(CLEANUP => 1);
38 my $devnull = devnull;
39
40 note "Generated app is in $dir";
41
42 chdir $dir or die "Cannot chdir to $dir: $!";
43
44 {
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');
56 }
57
58 my $app_dir = catdir($dir, 'TestApp');
59 chdir($app_dir) or die "Cannot chdir to $app_dir: $!";
60 lib->import(catdir($dir, 'TestApp', 'lib'));
61
62 my @files = qw|
63     Makefile.PL
64     testapp.conf
65     testapp.psgi
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
89 |;
90
91 foreach my $fn (map { catdir(@$_) } map { [ split qr{/}, $_ ] } @files) {
92     test_fn($fn);
93 }
94 create_ok($_, 'My' . $_) for qw/Model View Controller/;
95
96 command_ok( [ $^X, 'Makefile.PL' ] );
97 ok -e "Makefile", "Makefile generated";
98 command_ok( [ $MAKE ] );
99
100 run_generated_component_tests();
101
102 my $server_script_file = catdir(qw/script testapp_server.pl/);
103 my $server_script = do {
104     open(my $fh, '<', $server_script_file) or fail $!;
105     local $/;
106     <$fh>;
107 };
108
109 ok $server_script;
110 ok $server_script =~ qr/CATALYST_SCRIPT_GEN}\s+=\s+(\d+)/,
111     'SCRIPT_GEN found in generated output';
112 is $1, $Catalyst::Devel::CATALYST_SCRIPT_GEN, 'Script gen correct';
113
114 {
115     open(my $fh, '>', $server_script_file) or fail $!;
116     print $fh "MOO\n";
117 }
118 my $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
128 my $server_script_new = do {
129     open(my $fh, '<', $server_script_file) or fail $!;
130     local $/;
131     <$fh>;
132 };
133
134 is $server_script, $server_script_new;
135
136 note "Installed app is in $instdir";
137 command_ok( [ $MAKE, 'install' ] );
138
139 my $inst_app_dir = catdir($instdir);
140 chdir($inst_app_dir) or die "Cannot chdir to $inst_app_dir: $!";
141 lib->import(catdir($instdir, 'lib', 'perl5'));
142
143 my @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
164 foreach my $fn (map { catdir(@$_) } map { [ split qr{/}, $_ ] } @installed_files) {
165     my $ffn = catfile($inst_app_dir, $fn);
166     ok -r $ffn, "'$fn' installed in correct location";
167 }
168
169 chdir('/');
170 done_testing;
171
172 sub command_ok {
173     my $cmd = shift;
174     my $desc = shift || "Exit status ok for '@{$cmd}'";
175
176     open my $stdin, '<', $devnull
177         or die "Cannot read $devnull: $!";
178
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;
189 }
190
191 sub runperl {
192     my $comment = pop @_;
193     command_ok( [ $^X, '-I', $helper_lib, @_ ], $comment );
194 }
195
196 my @generated_component_tests;
197
198 sub test_fn {
199     my $fn = shift;
200     ok -r $fn, "Have $fn in generated app";
201     if ($fn =~ /script/) {
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        }
206     }
207     if ($fn =~ /\.p[ml]$/) {
208         runperl( '-c', $fn, "$fn compiles" );
209     }
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
215 sub run_generated_component_tests {
216     local $ENV{TEST_POD} = 1;
217     local $ENV{CATALYST_DEBUG} = 0;
218     foreach my $fn (@generated_component_tests) {
219         my $full_name = rel2abs($fn);
220         subtest "Generated app test: $fn", sub {
221             do $full_name;
222         };
223     }
224 }
225
226 sub create_ok {
227     my ($type, $name) = @_;
228     runperl( catdir('script', 'testapp_create.pl'), $type, $name,
229         "'script/testapp_create.pl $type $name' ok");
230     test_fn( catdir('t', sprintf("%s_%s.t", lc $type, $name)));
231 }