canonicalize changelog entries
[gitmo/MooseX-Daemonize.git] / lib / Test / MooseX / Daemonize.pm
CommitLineData
3c3db18c 1package Test::MooseX::Daemonize;
fe0eeebc 2use strict;
7d7115e5 3
69186a48 4our $AUTHORITY = 'cpan:PERIGRIN';
5
7d7115e5 6# BEGIN CARGO CULTING
7use Sub::Exporter;
8use Test::Builder;
fe0eeebc 9
7d7115e5 10
b3cd9b56 11{
12 my @exports = qw[
13 daemonize_ok
14 check_test_output
15 ];
16
17 Sub::Exporter::setup_exporter(
18 {
19 exports => \@exports,
20 groups => { default => \@exports }
21 }
22 );
23}
7d7115e5 24
25our $Test = Test::Builder->new;
26
27sub daemonize_ok {
28 my ( $daemon, $msg ) = @_;
d02fc704 29 unless ( my $pid = fork ) {
7d7115e5 30 $daemon->start();
31 exit;
32 }
33 else {
3543c999 34 sleep(1); # Punt on sleep time, 1 seconds should be enough
fe0eeebc 35 $Test->ok( $daemon->pidfile->does_file_exist, $msg )
b3cd9b56 36 || $Test->diag(
37 'Pidfile (' . $daemon->pidfile->file . ') not found.' );
3543c999 38 }
39}
40
41sub check_test_output {
42 my ($app) = @_;
43 open( my $stdout_in, '<', $app->test_output )
44 or die "can't open test output: $!";
45 while ( my $line = <$stdout_in> ) {
46 $line =~ s/\s+\z//;
47 my $label;
48 if ( $line =~ /\A((not\s+)?ok)(?:\s+-)(?:\s+(.*))\z/ ) {
49 my ( $status, $not, $text ) = ( $1, $2, $3 );
50 $text ||= '';
51
52 # We don't just call ok(!$not), because that generates diagnostics of
53 # its own for failures. We only want the diagnostics from the child.
cc4826cc 54 my $orig_no_diag = $Test->no_diag;
55 $Test->no_diag(1);
56 $Test->ok(!$not, $text);
57 $Test->no_diag($orig_no_diag);
3543c999 58 }
59 elsif ( $line =~ s/\A#\s?// ) {
60 $Test->diag($line);
61 }
62 else {
cc4826cc 63 $Test->diag("$label: $line (unrecognised)\n");
3543c999 64 }
7d7115e5 65 }
66}
67
3c3db18c 68package Test::MooseX::Daemonize::Testable;
69use Moose::Role;
70
71has test_output => (
3543c999 72 isa => 'Str',
73 is => 'ro',
3c3db18c 74 required => 1,
75);
76
77after daemonize => sub {
78 $Test->use_numbers(0);
79 $Test->no_ending(1);
3543c999 80 open my $out, '>', $_[0]->test_output or die "Cannot open test output: $!";
81 my $fileno = fileno $out;
82 open STDERR, ">&=", $fileno
3c3db18c 83 or die "Can't redirect STDERR";
84
3543c999 85 open STDOUT, ">&=", $fileno
3c3db18c 86 or die "Can't redirect STDOUT";
87
3543c999 88 $Test->output($out);
89 $Test->failure_output($out);
90 $Test->todo_output($out);
3c3db18c 91};
92
7d7115e5 931;
94__END__
95
fe0eeebc 96=pod
7d7115e5 97
98=head1 NAME
99
fe0eeebc 100Test::MooseX::Daemonize - Tool to help test MooseX::Daemonize applications
7d7115e5 101
7d7115e5 102=head1 SYNOPSIS
1d85c76d 103
380acf65 104 use File::Spec::Functions;
105 use File::Temp qw(tempdir);
106
107 my $dir = tempdir( CLEANUP => 1 );
7d7115e5 108
109 ## Try to make sure we are in the test directory
7d7115e5 110
380acf65 111 my $file = catfile( $dir, "im_alive" );
112 my $daemon = FileMaker->new( pidbase => $dir, filename => $file );
7d7115e5 113
114 daemonize_ok( $daemon, 'child forked okay' );
115 ok( -e $file, "$file exists" );
7d7115e5 116
117=head1 DESCRIPTION
118
1d85c76d 119This module provides some basic Test::Builder compatible test methods to
120use when writing tests for you MooseX::Daemonize based modules.
7d7115e5 121
fe0eeebc 122=head1 EXPORTED FUNCTIONS
7d7115e5 123
fe0eeebc 124=over 4
7d7115e5 125
fe0eeebc 126=item B<daemonize_ok ( $daemon, ?$msg )>
7d7115e5 127
1d85c76d 128This will attempt to daemonize your C<$daemon> returning ok on
fe0eeebc 129success and not ok on failure.
7d7115e5 130
fe0eeebc 131=item B<check_test_output ( $daemon )>
7d7115e5 132
1d85c76d 133This is expected to be used with a C<$daemon> which does the
fe0eeebc 134B<Test::MooseX::Daemonize::Testable> role (included in this package
1d85c76d 135see the source for more info). It will collect the test output
136from your daemon and apply it in the parent process by mucking
137around with L<Test::Builder> stuff, again, read the source for
fe0eeebc 138more info. If we get time we will document this more thoroughly.
3543c999 139
7d7115e5 140=back
141
7d7115e5 142=head1 INCOMPATIBILITIES
143
7d7115e5 144None reported.
145
7d7115e5 146=head1 BUGS AND LIMITATIONS
147
7d7115e5 148No bugs have been reported.
149
150Please report any bugs or feature requests to
151C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
152L<http://rt.cpan.org>.
153
154=head1 SEE ALSO
155
fe0eeebc 156L<MooseX::Daemonize>
7d7115e5 157
158=head1 AUTHOR
159
160Chris Prather C<< <perigrin@cpan.org> >>
161
7d7115e5 162=head1 LICENCE AND COPYRIGHT
163
05b96f4d 164Copyright (c) 2007-2011, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
7d7115e5 165
166This module is free software; you can redistribute it and/or
167modify it under the same terms as Perl itself. See L<perlartistic>.
168
7d7115e5 169=head1 DISCLAIMER OF WARRANTY
170
171BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
172FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
173OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
174PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
175EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
176WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
177ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
178YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
179NECESSARY SERVICING, REPAIR, OR CORRECTION.
180
181IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
182WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
183REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
184LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
185OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
186THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
187RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
188FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
189SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
3543c999 190SUCH DAMAGES.
fe0eeebc 191
192=cut