File Coverage

blib/lib/Test/MooseX/Daemonize.pm
Criterion Covered Total %
statement 33 35 94.2
branch 5 10 50.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 2 2 100.0
total 48 56 85.7


line stmt bran cond sub pod time code
1 4     4   192248 use strict;
  4         8  
  4         88  
2 4     4   12 use warnings;
  4         4  
  4         240  
3             package Test::MooseX::Daemonize;
4             # ABSTRACT: Tool to help test MooseX::Daemonize applications
5              
6             our $VERSION = '0.21';
7              
8             # BEGIN CARGO CULTING
9 4         24 use Sub::Exporter -setup => {
10             exports => [ qw(daemonize_ok check_test_output) ],
11             groups => { default => [ qw(daemonize_ok check_test_output) ] },
12 4     4   1748 };
  4         34768  
13              
14 4     4   1084 use Test::Builder;
  4         4  
  4         1148  
15              
16             our $Test = Test::Builder->new;
17              
18             sub daemonize_ok {
19 4     4 1 2736 my ( $daemon, $msg ) = @_;
20 4 100       4775 unless ( my $pid = fork ) {
21 3         147 $daemon->start();
22 1         48 exit;
23             }
24             else {
25 1         1000168 sleep(1); # Punt on sleep time, 1 seconds should be enough
26 1 50       149 $Test->ok( $daemon->pidfile->does_file_exist, $msg )
27             || $Test->diag(
28             'Pidfile (' . $daemon->pidfile->file . ') not found.' );
29             }
30             }
31              
32             sub check_test_output {
33 1     1 1 858 my ($app) = @_;
34 1 50       38 open( my $stdout_in, '<', $app->test_output )
35             or die "can't open test output: $!";
36 1         21 while ( my $line = <$stdout_in> ) {
37 4         266 $line =~ s/\s+\z//;
38 4         5 my $label;
39 4 50       17 if ( $line =~ /\A(?:(not\s+)?ok)(?:\s+-)(?:\s+(.*))\z/ ) {
    0          
40 4         9 my ( $not, $text ) = ( $1, $2, $3 );
41 4   50     8 $text ||= '';
42              
43             # We don't just call ok(!$not), because that generates diagnostics of
44             # its own for failures. We only want the diagnostics from the child.
45 4         12 my $orig_no_diag = $Test->no_diag;
46 4         351 $Test->no_diag(1);
47 4         280 $Test->ok(!$not, $text);
48 4         575 $Test->no_diag($orig_no_diag);
49             }
50             elsif ( $line =~ s/\A#\s?// ) {
51 0           $Test->diag($line);
52             }
53             else {
54 0           $Test->diag("$label: $line (unrecognised)\n");
55             }
56             }
57             }
58              
59             package # hide from PAUSE
60             Test::MooseX::Daemonize::Testable;
61              
62 4     4   1412 use Moose::Role;
  4         1102900  
  4         16  
63              
64             has test_output => (
65             isa => 'Str',
66             is => 'ro',
67             required => 1,
68             );
69              
70             after daemonize => sub {
71             $Test->use_numbers(0);
72             $Test->no_ending(1);
73             open my $out, '>', $_[0]->test_output or die "Cannot open test output: $!";
74             my $fileno = fileno $out;
75             open STDERR, ">&=", $fileno
76             or die "Can't redirect STDERR";
77              
78             open STDOUT, ">&=", $fileno
79             or die "Can't redirect STDOUT";
80              
81             $Test->output($out);
82             $Test->failure_output($out);
83             $Test->todo_output($out);
84             };
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             Test::MooseX::Daemonize - Tool to help test MooseX::Daemonize applications
97              
98             =head1 VERSION
99              
100             version 0.21
101              
102             =head1 SYNOPSIS
103              
104             use File::Spec::Functions;
105             use File::Temp qw(tempdir);
106              
107             my $dir = tempdir( CLEANUP => 1 );
108              
109             ## Try to make sure we are in the test directory
110              
111             my $file = catfile( $dir, "im_alive" );
112             my $daemon = FileMaker->new( pidbase => $dir, filename => $file );
113              
114             daemonize_ok( $daemon, 'child forked okay' );
115             ok( -e $file, "$file exists" );
116              
117             =head1 DESCRIPTION
118              
119             This module provides some basic L<Test::Builder>-compatible test methods to
120             use when writing tests for your L<MooseX::Daemonize>-based modules.
121              
122             =head1 EXPORTED FUNCTIONS
123              
124             =over 4
125              
126             =item B<daemonize_ok ( $daemon, ?$msg )>
127              
128             This will attempt to daemonize your C<$daemon> returning ok on
129             success and not ok on failure.
130              
131             =item B<check_test_output ( $daemon )>
132              
133             This is expected to be used with a C<$daemon> which does the
134             B<Test::MooseX::Daemonize::Testable> role (included in this package --
135             see the source for more info). It will collect the test output
136             from your daemon and apply it in the parent process by mucking
137             around with L<Test::Builder> stuff, again, read the source for
138             more info. If we get time we will document this more thoroughly.
139              
140             =back
141              
142             =head1 SEE ALSO
143              
144             L<MooseX::Daemonize>
145              
146             =head1 SUPPORT
147              
148             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Daemonize>
149             (or L<bug-MooseX-Daemonize@rt.cpan.org|mailto:bug-MooseX-Daemonize@rt.cpan.org>).
150              
151             There is also a mailing list available for users of this distribution, at
152             L<http://lists.perl.org/list/moose.html>.
153              
154             There is also an irc channel available for users of this distribution, at
155             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
156              
157             =head1 AUTHORS
158              
159             =over 4
160              
161             =item *
162              
163             Stevan Little <stevan.little@iinteractive.com>
164              
165             =item *
166              
167             Chris Prather <chris@prather.org>
168              
169             =back
170              
171             =head1 COPYRIGHT AND LICENCE
172              
173             This software is copyright (c) 2007 by Chris Prather.
174              
175             This is free software; you can redistribute it and/or modify it under
176             the same terms as the Perl 5 programming language system itself.
177              
178             =cut