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