]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Action/Captor.pm
This is 1.01
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Action / Captor.pm
1 package Test::Valgrind::Action::Captor;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Action::Captor - Mock Test::Valgrind::Action for capturing output.
9
10 =head1 VERSION
11
12 Version 1.01
13
14 =cut
15
16 our $VERSION = '1.01';
17
18 =head1 DESCRIPTION
19
20 This class provides helpers for saving, redirecting and restoring filehandles.
21
22 It's not meant to be used directly as an action.
23
24 =cut
25
26 use File::Spec ();
27
28 use base qw/Test::Valgrind::Carp/;
29
30 =head1 METHODS
31
32 =head2 C<new>
33
34 Just a croaking stub to remind you not to use this class as a real action.
35
36 =cut
37
38 sub new { shift->_croak('This mock action isn\'t meant to be used directly') }
39
40 # Widely inspired from Capture::Tiny
41
42 sub _redirect_fh {
43  open $_[1], $_[2], $_[3]
44           or $_[0]->_croak('open(' . fileno($_[1]) . ", '$_[2]', '$_[3]'): $!");
45 }
46
47 sub _dup_fh {
48  my $fd = fileno $_[3];
49  open $_[1], $_[2] . '&' . $fd
50              or $_[0]->_croak('open(' . fileno($_[1]) . ", '$_[2]&', $fd): $!");
51 }
52
53 =head2 C<save_fh $from, $mode [, $to ]>
54
55 Save the original filehandle C<$from> opened with mode C<$mode>, and redirect it to C<$to> if it's defined or to F</dev/null> otherwise.
56
57 =cut
58
59 sub save_fh {
60  my ($self, $from, $mode, $to) = @_;
61
62  unless (defined fileno $from) {
63   $self->_redirect_fh($from, $mode, File::Spec->devnull);
64   push @{$self->{proxies}}, $from;
65  }
66
67  $self->_dup_fh(my $save, $mode, $from);
68  push @{$self->{saves}}, [ $save, $mode, $from ];
69
70  if ($to and ref $to eq 'GLOB') {
71   $self->_dup_fh($from, $mode, $to);
72  } else {
73   $self->_redirect_fh($from, $mode, defined $to ? $to : File::Spec->devnull);
74  }
75
76  return;
77 }
78
79 =head2 C<restore_all_fh>
80
81 Restore all the filehandles that were saved with L</save_fh> to their original state.
82
83 The redirections aren't closed.
84
85 =cut
86
87 sub restore_all_fh {
88  my ($self) = @_;
89
90  for (@{$self->{saves}}) {
91   my ($save, $mode, $from) = @$_;
92   $self->_dup_fh($from, $mode, $save);
93   close $save or $self->_croak('close(saved[' . fileno($save) . "]): $!");
94  }
95  delete $self->{saves};
96
97  for (@{$self->{proxies}}) {
98   close $_ or $self->_croak('close(proxy[' . fileno($_) . "]): $!");
99  }
100  delete $self->{proxies};
101
102  return;
103 }
104
105 =head1 SEE ALSO
106
107 L<Test::Valgrind>, L<Test::Valgrind::Action>.
108
109 L<Capture::Tiny>.
110
111 =head1 AUTHOR
112
113 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
114
115 You can contact me by mail or on C<irc.perl.org> (vincent).
116
117 =head1 BUGS
118
119 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
120 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
121
122 =head1 SUPPORT
123
124 You can find documentation for this module with the perldoc command.
125
126     perldoc Test::Valgrind::Action::Captor
127
128 =head1 COPYRIGHT & LICENSE
129
130 Copyright 2009 Vincent Pit, all rights reserved.
131
132 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
133
134 =cut
135
136 1; # End of Test::Valgrind::Action::Captor