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