blib/lib/Test/MooseX/Daemonize.pm

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