provide proper debug output in test if compiling psgi fails
[catagits/Catalyst-Runtime.git] / t / aggregate / psgi_file.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use FindBin;
5 use lib "$FindBin::Bin/../lib";
6 use File::Temp qw/ tempdir /;
7 use TestApp;
8 use File::Spec;
9 use Carp qw/croak/;
10 use IPC::Open3 qw(open3);
11
12 my $home = tempdir( CLEANUP => 1 );
13 my $path = File::Spec->catfile($home, 'testapp.psgi');
14 open(my $psgi, '>', $path)
15     or die;
16 print $psgi q{
17 use strict;
18 use warnings;
19 use TestApp;
20
21 TestApp->psgi_app;
22 };
23 close($psgi);
24
25 open my $stdin, '<', File::Spec->devnull;
26 my $pid = open3 $stdin, my $stdout, undef, $^X, '-I', "$FindBin::Bin/../lib", '-c', $path;
27 my $output = do { local $/; <$stdout> };
28 waitpid $pid, 0;
29
30 ok $? == 0, '.psgi compiles'
31   or diag $output;
32
33 # NOTE - YOU *CANNOT* do something like:
34 #my $psgi_ref = require $path;
35 # otherwise this test passes!
36 # I don't exactly know why that is yet, however, to be safe for future, that
37 # is why this test writes out its own .psgi file in a temp directory - so that that
38 # path has never been require'd before, and will never be require'd again..
39
40 local TestApp->config->{home} = $home;
41
42 my $failed = 0;
43 eval {
44     # Catch infinite recursion (or anything else)
45     local $SIG{__WARN__} = sub { warn(@_); $failed = 1; die; };
46     TestApp->_finalized_psgi_app;
47 };
48 ok(!$@, 'No exception')
49     or diag $@;
50 ok(!$failed, 'TestApp->_finalized_psgi_app works');
51
52 done_testing;