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