Fix big bug in test
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_script_test.t
1 use strict;
2 use warnings;
3
4 use FindBin qw/$Bin/;
5 use lib "$Bin/../lib";
6
7 use Test::More;
8 use Test::Exception;
9
10 use Catalyst::Script::Test;
11 use File::Temp qw/tempfile/;
12 use IO::Handle;
13
14 is run_test('/'), "root index\n", 'correct content printed';
15 is run_test('/moose/get_attribute'), "42\n", 'Correct content printed for non root action';
16
17 done_testing;
18
19 sub run_test {
20     my $url = shift;
21
22     my ($fh, $fn) = tempfile();
23
24     binmode( $fh );
25     binmode( STDOUT );
26
27     {
28         local @ARGV = ($url);
29         my $i;
30         lives_ok {
31             $i = Catalyst::Script::Test->new_with_options(application_name => 'TestApp');
32         } "new_with_options";
33         ok $i;
34         my $saved;
35         open( $saved, '<&'. STDIN->fileno )
36               or croak("Can't dup stdin: $!");
37         open( STDOUT, '>&='. $fh->fileno )
38             or croak("Can't open stdout: $!");
39         eval { $i->run };
40         ok !$@, 'Ran ok';
41
42         STDOUT->flush
43             or croak("Can't flush stdout: $!");
44
45         open( STDOUT, '>&'. fileno($saved) )
46             or croak("Can't restore stdout: $!");
47     }
48
49     my $data = do { my $fh; open($fh, '<', $fn) or die $!; local $/; <$fh>; };
50     $fh = undef;
51     unlink $fn if -r $fn;
52
53     return $data;
54 }