Codebase list libcapture-tiny-perl / e7dcf71
testing stderr and stdin closed David Golden 15 years ago
3 changed file(s) with 94 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
4343
4444 sub _proxy_std {
4545 my %proxies;
46 if ( ! defined fileno STDIN ) {
47 open STDIN, "<", File::Spec->devnull;
48 $proxies{stdin} = \*STDIN;
49 }
4650 if ( ! defined fileno STDOUT ) {
4751 open STDOUT, ">", File::Spec->devnull;
4852 $proxies{stdout} = \*STDOUT;
4953 }
54 if ( ! defined fileno STDERR ) {
55 open STDERR, ">", File::Spec->devnull;
56 $proxies{stderr} = \*STDERR;
57 }
5058 return %proxies;
5159 }
5260
5361 sub _copy_std {
5462 my %handles = map { $_, IO::Handle->new } qw/stdout stderr stdin/;
55 _open $handles{stdout}, ">&STDOUT" if defined fileno STDOUT;
56 _open $handles{stderr}, ">&STDERR" if defined fileno STDERR;
57 _open $handles{stdin}, "<&STDIN" if defined fileno STDIN;
63 _open $handles{stdin}, "<&STDIN";
64 _open $handles{stdout}, ">&STDOUT";
65 _open $handles{stderr}, ">&STDERR";
5866 return \%handles;
5967 }
6068
171179 $code->();
172180 # restore prior filehandles and shut down tees
173181 _open_std( $stash->{old} );
182 close $_ for values %{$stash->{old}}; # don't leak fds
174183 close $_ for values %proxy_std;
175184 _kill_tees( $stash ) if $tee_stdout || $tee_stderr;
176185 # return captured output
0 # Copyright (c) 2009 by David Golden. All rights reserved.
1 # Licensed under Apache License, Version 2.0 (the "License").
2 # You may not use this file except in compliance with the License.
3 # A copy of the License was distributed with this file or you may obtain a
4 # copy of the License from http://www.apache.org/licenses/LICENSE-2.0
5
6 use strict;
7 use warnings;
8 use Test::More;
9 use Config;
10 use t::lib::Utils qw/save_std restore_std/;
11 use t::lib::Tests qw(
12 capture_tests capture_count
13 capture_merged_tests capture_merged_count
14 tee_tests tee_count
15 tee_merged_tests tee_merged_count
16 );
17
18 #--------------------------------------------------------------------------#
19
20 plan tests => 1 + capture_count() + capture_merged_count()
21 + tee_count() + tee_merged_count();
22
23 #--------------------------------------------------------------------------#
24
25 save_std(qw/stderr/);
26 ok( close STDERR, "closed STDERR" );
27
28 capture_tests();
29 capture_merged_tests();
30
31 SKIP: {
32 if ( $^O ne 'MSWin32' && ! $Config{d_fork} ) {
33 skip tee_count() + tee_merged_count, "requires working fork()";
34 }
35 tee_tests();
36 tee_merged_tests();
37 }
38
39 restore_std(qw/stderr/);
40
0 # Copyright (c) 2009 by David Golden. All rights reserved.
1 # Licensed under Apache License, Version 2.0 (the "License").
2 # You may not use this file except in compliance with the License.
3 # A copy of the License was distributed with this file or you may obtain a
4 # copy of the License from http://www.apache.org/licenses/LICENSE-2.0
5
6 use strict;
7 use warnings;
8 use Test::More;
9 use Config;
10 use t::lib::Utils qw/save_std restore_std/;
11 use t::lib::Tests qw(
12 capture_tests capture_count
13 capture_merged_tests capture_merged_count
14 tee_tests tee_count
15 tee_merged_tests tee_merged_count
16 );
17
18 #--------------------------------------------------------------------------#
19
20 plan tests => 1 + capture_count() + capture_merged_count()
21 + tee_count() + tee_merged_count();
22
23 #--------------------------------------------------------------------------#
24
25 save_std(qw/stdin/);
26 ok( close STDIN, "closed STDIN" );
27
28 capture_tests();
29 capture_merged_tests();
30
31 SKIP: {
32 if ( $^O ne 'MSWin32' && ! $Config{d_fork} ) {
33 skip tee_count() + tee_merged_count, "requires working fork()";
34 }
35 tee_tests();
36 tee_merged_tests();
37 }
38
39 restore_std(qw/stdin/);
40