From af66d2cae19067d1f1add2c236feefa3bc55e8b4 Mon Sep 17 00:00:00 2001 From: Chris Kirke Date: Tue, 24 Jan 2017 20:52:18 -0600 Subject: [PATCH] increase test coverage --- Makefile.PL | 1 + t/02_sleep_perl.t | 9 ++++-- t/02_sleep_perl_array.t | 3 ++ t/02_sleep_perl_foreground.t | 46 +++++++++++++++++++++++++++ t/02_sleep_perl_single.t | 49 +++++++++++++++++++++++++++++ t/02_sleep_system.t | 3 ++ t/03_perl_gets_control.t | 3 ++ t/06_stderr_stdout.t | 3 ++ t/bin/02_sleep_perl_foreground.pl | 26 +++++++++++++++ t/bin/02_sleep_perl_single.pl | 28 +++++++++++++++++ t/lib/Daemon/Control/Plugin/Help.pm | 15 +++++++++ 11 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 t/02_sleep_perl_foreground.t create mode 100644 t/02_sleep_perl_single.t create mode 100644 t/bin/02_sleep_perl_foreground.pl create mode 100644 t/bin/02_sleep_perl_single.pl create mode 100644 t/lib/Daemon/Control/Plugin/Help.pm diff --git a/Makefile.PL b/Makefile.PL index 0d1a9cc..6542675 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -17,5 +17,6 @@ requires 'File::Path' => '2.08'; test_requires 'Test::More' => '0.88'; test_requires 'File::Temp' => '0.14'; +test_requires 'Devel::Cover' => '1.23'; WriteAll; diff --git a/t/02_sleep_perl.t b/t/02_sleep_perl.t index e25dfef..6f98c03 100644 --- a/t/02_sleep_perl.t +++ b/t/02_sleep_perl.t @@ -3,6 +3,9 @@ use warnings; use strict; use Test::More; +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + my ( $file, $ilib ); # Let's make it so people can test in t/ or in the dist directory. @@ -43,8 +46,10 @@ ok $out = get_command_output( "$^X -I$ilib $file start" ), "Started system daemo like $out, qr/\[Started\]/, "Daemon started for restarting."; ok $out = get_command_output( "$^X -I$ilib $file status" ), "Get status of system daemon."; like $out, qr/\[Running\]/, "Daemon running for restarting."; -ok $out = get_command_output( "$^X -I$ilib $file restart" ), "Get status of system daemon."; -like $out, qr/\[Stopped\].*\[Started\]/s, "Daemon restarted."; +ok $out = get_command_output( "$^X -I$ilib $file reload" ), "Reload system daemon."; +like $out, qr/\[Reloaded\]/, "Daemon reloaded."; +ok $out = get_command_output( "$^X -I$ilib $file restart" ), "Restart of system daemon."; +like $out, qr/\[Started\]/, "Daemon restarted."; ok $out = get_command_output( "$^X -I$ilib $file status" ), "Get status of system daemon."; like $out, qr/\[Running\]/, "Daemon running after restart."; ok $out = get_command_output( "$^X -I$ilib $file stop" ), "Get status of system daemon."; diff --git a/t/02_sleep_perl_array.t b/t/02_sleep_perl_array.t index 6c75f00..f2b526f 100644 --- a/t/02_sleep_perl_array.t +++ b/t/02_sleep_perl_array.t @@ -3,6 +3,9 @@ use warnings; use strict; use Test::More; +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + my ( $file, $ilib ); # Let's make it so people can test in t/ or in the dist directory. diff --git a/t/02_sleep_perl_foreground.t b/t/02_sleep_perl_foreground.t new file mode 100644 index 0000000..a0dcd54 --- /dev/null +++ b/t/02_sleep_perl_foreground.t @@ -0,0 +1,46 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More; + +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + +my ( $file, $ilib ); + +# Let's make it so people can test in t/ or in the dist directory. +if ( -f 't/bin/02_sleep_perl_foreground.pl' ) { # Dist Directory. + $file = "t/bin/02_sleep_perl_foreground.pl"; + $ilib = "lib"; +} elsif ( -f 'bin/02_sleep_perl_foreground.pl' ) { + $file = "bin/02_sleep_perl_foreground.pl"; + $ilib = "../lib"; +} else { + die "Tests should be run in the dist directory or t/"; +} + + +sub get_command_output { + my ( @command ) = @_; + open my $lf, "-|", @command + or die "Couldn't get pipe to '@command': $!"; + my $content = do { local $/; <$lf> }; + close $lf; + return $content; +} + +my $out; + +$out = get_command_output( "$^X -I$ilib $file foreground" ); +like $out, qr//, "Daemon started quiet."; + +sleep 10; + +ok $out = get_command_output( "$^X -I$ilib $file status" ), "Get status of perl daemon."; +like $out, qr/\[Not Running\]/, "Daemon not running"; + +# Testing restart. +ok $out = get_command_output( "$^X -I$ilib $file start" ), "Started system daemon"; +like $out, qr/\[Started\]/, "Daemon started for restarting."; + +done_testing; diff --git a/t/02_sleep_perl_single.t b/t/02_sleep_perl_single.t new file mode 100644 index 0000000..102e35b --- /dev/null +++ b/t/02_sleep_perl_single.t @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More; + +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + +my ( $file, $ilib ); + +# Let's make it so people can test in t/ or in the dist directory. +if ( -f 't/bin/02_sleep_perl_single.pl' ) { # Dist Directory. + $file = "t/bin/02_sleep_perl_single.pl"; + $ilib = "lib"; +} elsif ( -f 'bin/02_sleep_perl_single.pl' ) { + $file = "bin/02_sleep_perl_single.pl"; + $ilib = "../lib"; +} else { + die "Tests should be run in the dist directory or t/"; +} + + +sub get_command_output { + my ( @command ) = @_; + open my $lf, "-|", @command + or die "Couldn't get pipe to '@command': $!"; + my $content = do { local $/; <$lf> }; + close $lf; + return $content; +} + +my $out; + +ok $out = get_command_output( "$^X -I$ilib $file start" ), "Started perl daemon"; +like $out, qr/\[Started\]/, "Daemon started."; + +sleep 10; + +ok $out = get_command_output( "$^X -I$ilib $file status" ), "Get status of perl daemon."; +like $out, qr/\[Not Running\]/, "Daemon not running"; + +ok $out = get_command_output( "$^X -I$ilib $file help" ), "Get help for perl daemon."; +like $out, qr/Some test help$/, "Daemon help"; + +# Testing restart. +ok $out = get_command_output( "$^X -I$ilib $file start" ), "Started system daemon"; +like $out, qr/\[Started\]/, "Daemon started for restarting."; + +done_testing; diff --git a/t/02_sleep_system.t b/t/02_sleep_system.t index 8849877..c7c4819 100644 --- a/t/02_sleep_system.t +++ b/t/02_sleep_system.t @@ -3,6 +3,9 @@ use warnings; use strict; use Test::More; +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + my ( $file, $ilib ); # Let's make it so people can test in t/ or in the dist directory. diff --git a/t/03_perl_gets_control.t b/t/03_perl_gets_control.t index 3c5cf06..140910b 100644 --- a/t/03_perl_gets_control.t +++ b/t/03_perl_gets_control.t @@ -3,6 +3,9 @@ use warnings; use strict; use Test::More; +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + my ( $file, $ilib ); # Let's make it so people can test in t/ or in the dist directory. diff --git a/t/06_stderr_stdout.t b/t/06_stderr_stdout.t index d4c62af..b7b2a1b 100644 --- a/t/06_stderr_stdout.t +++ b/t/06_stderr_stdout.t @@ -4,6 +4,9 @@ use strict; use Test::More; use File::Temp; +# enable coverage for get_command_output() commands +$ENV{'PERL5OPT'} = '-MDevel::Cover'; + my ( $file, $ilib ); # Let's make it so people can test in t/ or in the dist directory. diff --git a/t/bin/02_sleep_perl_foreground.pl b/t/bin/02_sleep_perl_foreground.pl new file mode 100644 index 0000000..6d9e14e --- /dev/null +++ b/t/bin/02_sleep_perl_foreground.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Daemon::Control; + +Daemon::Control->new({ + name => "My Daemon", + lsb_start => '$syslog $remote_fs', + lsb_stop => '$syslog', + lsb_sdesc => 'My Daemon Short', + lsb_desc => 'My Daemon controls the My Daemon daemon.', + path => '/usr/sbin/mydaemon/init.pl', + + program => sub { sleep $_[1] }, + program_args => [ 10 ], + + pid_file => 'pid_tmp', + stderr_file => '/dev/null', + stdout_file => '/dev/null', + + user => (getpwuid($>))[0], + prereq_no_process => 1, + + fork => 0, + +})->run; diff --git a/t/bin/02_sleep_perl_single.pl b/t/bin/02_sleep_perl_single.pl new file mode 100644 index 0000000..e675002 --- /dev/null +++ b/t/bin/02_sleep_perl_single.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use lib './t/lib'; + +use Daemon::Control; + +Daemon::Control->with_plugins( qw( +Daemon::Control::Plugin::Help ) )->new({ + name => "My Daemon", + lsb_start => '$syslog $remote_fs', + lsb_stop => '$syslog', + lsb_sdesc => 'My Daemon Short', + lsb_desc => 'My Daemon controls the My Daemon daemon.', + path => '/usr/sbin/mydaemon/init.pl', + + program => sub { sleep $_[1] }, + program_args => [ 10 ], + + pid_file => 'pid_tmp', + stderr_file => '/dev/null', + stdout_file => '/dev/null', + + fork => 1, + + help => 'some test help', + +})->run; diff --git a/t/lib/Daemon/Control/Plugin/Help.pm b/t/lib/Daemon/Control/Plugin/Help.pm new file mode 100644 index 0000000..3658095 --- /dev/null +++ b/t/lib/Daemon/Control/Plugin/Help.pm @@ -0,0 +1,15 @@ +package Daemon::Control::Plugin::Help; + +use strict; +use warnings; + +use Class::Method::Modifiers qw(); +use Role::Tiny; + +around 'help' => sub { + my $orig = shift; + print ucfirst( $orig->(@_) ); + return 0; +}; + +1;