testing stderr and stdin closed
David Golden
15 years ago
43 | 43 | |
44 | 44 | sub _proxy_std { |
45 | 45 | my %proxies; |
46 | if ( ! defined fileno STDIN ) { | |
47 | open STDIN, "<", File::Spec->devnull; | |
48 | $proxies{stdin} = \*STDIN; | |
49 | } | |
46 | 50 | if ( ! defined fileno STDOUT ) { |
47 | 51 | open STDOUT, ">", File::Spec->devnull; |
48 | 52 | $proxies{stdout} = \*STDOUT; |
49 | 53 | } |
54 | if ( ! defined fileno STDERR ) { | |
55 | open STDERR, ">", File::Spec->devnull; | |
56 | $proxies{stderr} = \*STDERR; | |
57 | } | |
50 | 58 | return %proxies; |
51 | 59 | } |
52 | 60 | |
53 | 61 | sub _copy_std { |
54 | 62 | 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"; | |
58 | 66 | return \%handles; |
59 | 67 | } |
60 | 68 | |
171 | 179 | $code->(); |
172 | 180 | # restore prior filehandles and shut down tees |
173 | 181 | _open_std( $stash->{old} ); |
182 | close $_ for values %{$stash->{old}}; # don't leak fds | |
174 | 183 | close $_ for values %proxy_std; |
175 | 184 | _kill_tees( $stash ) if $tee_stdout || $tee_stderr; |
176 | 185 | # 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 |