File Coverage

blib/lib/App/Run/Command/ToFail.pm
Criterion Covered Total %
statement 48 49 97.9
branch 11 12 91.6
condition 3 3 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 70 72 97.2


line stmt bran cond sub pod time code
1             package App::Run::Command::ToFail;
2              
3 4     4   180809 use strict;
  4         9  
  4         147  
4 4     4   21 use warnings;
  4         8  
  4         217  
5              
6 4     4   2355 use Getopt::Std;
  4         9675  
  4         271  
7 4     4   2292 use Readonly;
  4         19487  
  4         3214  
8              
9             Readonly::Hash our %PRESETS => (
10             'blank' => [
11             0,
12             '',
13             ],
14             'perl' => [
15             1,
16             'perl %s',
17             ],
18             'strace_perl' => [
19             1,
20             'strace -ostrace.log -esignal,write perl -Ilib %s',
21             ],
22             );
23              
24             our $VERSION = 0.05;
25              
26             # Constructor.
27             sub new {
28 8     8 1 764368 my ($class, @params) = @_;
29              
30             # Create object.
31 8         27 my $self = bless {}, $class;
32              
33             # Object.
34 8         47 return $self;
35             }
36              
37             # Run.
38             sub run {
39 7     7 1 18 my $self = shift;
40              
41             # Process arguments.
42 7         55 $self->{'_opts'} = {
43             'h' => 0,
44             'l' => 0,
45             'n' => 100,
46             'p' => 'perl',
47             };
48 7 100 100     43 if (! getopts('hln:p:', $self->{'_opts'}) || $self->{'_opts'}->{'h'}) {
49 2         338 print STDERR "Usage: $0 [-h] [-l] [-n cycles] [-p preset] [--version]\n";
50 2         65 print STDERR "\t-h\t\tPrint help.\n";
51 2         32 print STDERR "\t-l\t\tList presets.\n";
52 2         26 print STDERR "\t-n cycles\tNumber of cycles (default is 100).\n";
53 2         23 print STDERR "\t-p preset\tPreset for run (default is perl).\n";
54 2         25 print STDERR "\t--version\tPrint version.\n";
55 2         15 return 1;
56             }
57              
58 5 100       522 if ($self->{'_opts'}->{'l'}) {
59 1         9 foreach my $key (sort keys %PRESETS) {
60 3         160 print $key.': '.$PRESETS{$key}[1]."\n";
61             }
62 1         40 return 0;
63             }
64              
65             # Check presets.
66 4 100       35 if (! exists $PRESETS{$self->{'_opts'}->{'p'}}) {
67 1         13 print STDERR 'Bad preset. Possible values are \''.(join "', '", (sort keys %PRESETS))."'.\n";
68 1         70 return 1;
69             }
70              
71 3 100       53 if ($PRESETS{$self->{'_opts'}->{'p'}}[0] > @ARGV) {
72             print STDERR 'Wrong number of arguments (need '.$PRESETS{$self->{'_opts'}->{'p'}}[0].
73 1         20 " for command '".$PRESETS{$self->{'_opts'}->{'p'}}[1]."').\n";
74 1         72 return 1;
75             }
76              
77 2         50 my @preset_args;
78             my @other_args;
79 2         18 for (my $i = 0; $i < @ARGV; $i++) {
80 3 50       16 if ($i + 1 <= $PRESETS{$self->{'_opts'}->{'p'}}[0]) {
81 0         0 push @preset_args, $ARGV[$i];
82             } else {
83 3         63 push @other_args, $ARGV[$i];
84             }
85             }
86 2         9 my $command = sprintf $PRESETS{$self->{'_opts'}->{'p'}}[1], @preset_args;
87 2         35 $command .= ' '.(join ' ', @other_args);
88 2         32 foreach my $i (1 .. $self->{'_opts'}->{'n'}) {
89 15         8297227 my $ret = system $command;
90 15 100       647 if ($ret > 1) {
91 1         100 print STDERR "Exited in $i round with exit code $ret.\n";
92 1         51 return 1;
93             }
94             }
95 1         94 print "Everything is ok.\n";
96            
97 1         50 return 0;
98             }
99              
100             1;
101              
102             __END__