blib/lib/Test/Smoke/Util/Execute.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 12 / 12 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 100% 70 / 70 CC 8 SCAR 7.2
CC
8
Cov
100%
full_command
3.0
arguments
3.0
new
2.0
CRAP 2.1
« blib/lib/Test/Smoke/App/AppOptionCollection.pm ↑ Summary blib/lib/Test/Smoke/Util/LoadAJSON.pm »
1 package Test::Smoke::Util::Execute;
2 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 our $VERSION = '0.002';
6  
7 17 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use Test::Smoke::LogMixin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Test::Smoke::Util::Execute - Run a command and return its output.
14  
15 =head1 SYNOPSIS
16  
17 use Test::Smoke::Util::Execute;
18  
19 my $ex = Test::Smoke::Execute->new(
20 verbose => $level,
21 command => $command,
22 arguments => [@arguments],
23 );
24 my $output = eval { $ex->run() };
25 if (my $error = $@) {
26 croak("Error running $command: $error");
27 }
28  
29 =head1 DESCRIPTION
30  
31 =head2 Test::Smoke::Util::Execute->new(%arguments)
32  
33 Instantiate an object of this class
34  
35 =head3 Arguments
36  
37 =over
38  
39 =item * verbose => [0, 1, 2]
40  
41 =item * command => $command_to_pass_to_qx
42  
43 =back
44  
45 =head3 Returns
46  
47 The instantiated object.
48  
49 =cut
50  
51 18 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
52 18 my $class = shift;
53  
54 18 my %args = @_;
55  
56 my $self = {
57 verbose => $args{verbose} || 0,
58 command => $args{command},
59 18 ▶ exitcode => undef,
Condition100%2/2
$args{'verbose'} || 0
l!l
216
MC/DC100%1/1
$args{'verbose'} || 0
$args{'verbose'}
Truth table100%2/2
A
A= $args{'verbose'}
Aresult
11
00
60 };
61 18 return bless $self, $class;
62 }
63  
64 =head2 $executer->full_command()
65  
66 Create the full command as pass to C<qx()>.
67  
68 =head3 Arguments
69  
70 None
71  
72 =head3 Returns
73  
74 A string with quotes around the elements/arguments that need them.
75  
76 =cut
77  
78 76 ▶sub full_command {
Subroutine full_command: called CC=3 SCAR=11.0
Pod documented
79 76 my $self = shift;
80  
81 my $command = join(
82 " ",
83 map {
84 316 ▶ /^(["'])(.*)\1$/
Branch: 2 branches
BranchTrueFalseTotal
/^(["'])(.*)\1$/ ? : 7 309 316
/\s/ ? : 13 296 309
85 ? qq/"$2"/
86 : /\s/
87 ? qq/"$_"/
88 : $_
89 76 } $self->{command}, $self->arguments(@_)
90 );
91 76 return $command;
92 }
93  
94 =head2 $executer->run()
95  
96 Run the command with backticks.
97  
98 =head3 Arguments
99  
100 None
101  
102 =head3 Returns
103  
104 Context aware list or scalar.
105  
106 If any error occured, C<< $self->exitcode >> is set.
107  
108 =cut
109  
110 71 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod documented
111 71 my $self = shift;
112  
113 71 my $command = $self->full_command(@_);
114 71 $self->log_debug("In pwd(%s) running:", cwd());
115 71 $self->log_info("qx[%s]\n", $command);
116  
117 71 my @output = qx/$command/;
118 71 $self->{exitcode} = $? >> 8;
119  
120 71 ▶ return wantarray ? @output : join("", @output);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 70 71
121 }
122  
123 =head2 $executer->exitcode
124  
125 Getter that returns the exitcode.
126  
127 =cut
128  
129 31 ▶sub exitcode { return $_[0]->{exitcode} }
Subroutine exitcode: called CC=1 SCAR=0.0
Pod documented
130  
131 =head2 $executer->verbose
132  
133 Accessor that returns the verbose.
134  
135 =cut
136  
137 147 ▶sub verbose {
Subroutine verbose: called CC=2 SCAR=6.9
Pod documented
138 147 my $self = shift;
139 147 ▶ if (@_) { $self->{verbose} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 146 147
140  
141 return $self->{verbose}
142 147 }
143  
144 =head2 $executer->arguments
145  
146 Accessor that returns the arguments.
147  
148 =cut
149  
150 76 ▶sub arguments {
Subroutine arguments: called CC=3 SCAR=11.0
Pod documented
151 76 my $self = shift;
152 76 ▶ if (@_) { $self->{arguments} = [@_]; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 74 2 76
153  
154 76 ▶ return $self->{arguments} ? @{ $self->{arguments} } : ()
Branch: 1 branch
BranchTrueFalseTotal
$self->{'arguments'} ? : 74 2 76
155 }
156  
157 17 1;
158  
159 =head1 STUFF
160  
161 (c) MMXIII - Abe Timmerman <abeltje@cpan.org>
162  
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself.
165  
166 See:
167  
168 =over 4
169  
170 =item * http://www.perl.com/perl/misc/Artistic.html
171  
172 =item * http://www.gnu.org/copyleft/gpl.html
173  
174 =back
175  
176 This program is distributed in the hope that it will be useful,
177 but WITHOUT ANY WARRANTY; without even the implied warranty of
178 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
179  
180 =cut
« blib/lib/Test/Smoke/App/AppOptionCollection.pm ↑ Summary blib/lib/Test/Smoke/Util/LoadAJSON.pm »