blib/lib/Tapper/CLI/API/Command/upload.pm

filter: Statementstmt 40.8% 29 / 71 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 52.9% 9 / 17 Podpod 83.3% 5 / 6 totaltotal 27.7% 43 / 155 CC 19 SCAR 43.3
CC
19
Cov
25%
validate_args
182.0
upload
42.0
_read_file
6.0
CRAP 75.7
« blib/lib/Tapper/CLI/Resource.pm ↑ Summary blib/lib/Tapper/CLI/Host.pm »
1 package Tapper::CLI::API::Command::upload;
2 2 our $AUTHORITY = 'cpan:TAPPER';
3 2 $Tapper::CLI::API::Command::upload::VERSION = '5.0.8';
4 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use parent 'App::Cmd::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Tapper::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Slurp 'slurp';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub abstract {
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
18 0 'Upload and attach a file to a report'
19 }
20  
21 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
22 return (
23 0 [ "verbose", "some more informational output" ],
24 [ "reportid=s", "INT; the testrun id to change", ],
25 [ "file=s", "STRING; the file to upload, use '-' for STDIN", ],
26 [ "filename=s", "STRING; alternate file name, especially when reading from STDIN", ],
27 [ "reportserver=s", "STRING; use this host for upload", ],
28 [ "reportport=s", "STRING; use this port for upload", ],
29 [ "contenttype=s", "STRING; content-type, default 'plain', use 'application/octet-stream' for binaries", ],
30 );
31 }
32  
33 sub usage_desc
34 0 ▶{
Subroutine usage_desc: not called CC=1 SCAR=6.9
Pod documented
35 0 my $allowed_opts = join ' ', map { '--'.$_ } _allowed_opts();
36 0 "tapper-api upload --reportid=s --file=s [ --contenttype=s ]";
37 }
38  
39 sub _allowed_opts
40 0 ▶{
Subroutine _allowed_opts: not called CC=1 SCAR=6.9
41 0 my @allowed_opts = map { $_->[0] } opt_spec();
42 }
43  
44 sub validate_args
45 0 ▶{
Subroutine validate_args: not called CC=13 SCAR=52.0
Pod documented
46 0 my ($self, $opt, $args) = @_;
47  
48 # -- file constraints --
49 0 my $file = $opt->{file};
50 0 ▶ my $file_ok = $file && ($file eq '-' || -r $file);
Condition0%0/6
$file && ($file eq '-' || -r $file)
!ll&&!rl&&r
000
$file eq "-" or -r $file
l!l&&r!l&&!r
000
MC/DC0%0/3
$file && ($file eq '-' || -r $file)
$file $file eq "-" -r $file
Truth table0%0/4
A && B or C
A= $fileB= $file eq "-"C= -r $file
ABCresult
0XX0
1000
11X1
1011
51 0 ▶ say "Missing argument --file" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
52 0 ▶ say "Error: file '".($file//"")."' must be readable or STDIN." unless $file_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_ok 0 0 0
Condition0%0/2
$file // ""
l!l
00
MC/DC0%0/1
$file // ""
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
53  
54 # -- report constraints --
55 0 my $reportid = $opt->{reportid};
56 0 ▶ my $report_ok = $reportid && $reportid =~ /^\d+$/;
Condition0%0/3
$reportid && $reportid =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$reportid && $reportid =~ /^\d+$/
$reportid $reportid =~ /^\d+$/
Truth table0%0/3
A && B
A= $reportidB= $reportid =~ /^\d+$/
ABresult
0X0
100
111
57 0 ▶ say "Missing argument --reportid" unless $reportid;
Branch: 1 branch
BranchTrueFalseTotal
unless $reportid 0 0 0
58 0 ▶ say "Error: Strange target report (id '".($reportid//"")."')." unless $report_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $report_ok 0 0 0
Condition0%0/2
$reportid // ""
l!l
00
MC/DC0%0/1
$reportid // ""
$reportid
Truth table0%0/2
A
A= $reportid
Aresult
11
00
59  
60 0 ▶ return 1 if $opt->{reportid} && $file_ok && $report_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"reportid"} and $file_ok and $report_ok 0 0 0
Condition0%0/6
$opt->{"reportid"} and $file_ok and $report_ok
!ll&&!rl&&r
000
$opt->{"reportid"} and $file_ok
!ll&&!rl&&r
000
MC/DC0%0/3
$opt->{"reportid"} and $file_ok and $report_ok
$opt->{"reportid"} $file_ok $report_ok
Truth table0%0/4
A and B and C
A= $opt->{"reportid"}B= $file_okC= $report_ok
ABCresult
0XX0
10X0
1100
1111
61 0 die $self->usage->text;
62 }
63  
64 sub execute
65 0 ▶{
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
66 0 my ($self, $opt, $args) = @_;
67  
68 0 $self->upload ($opt, $args);
69 }
70  
71 sub _read_file
72 0 ▶{
Subroutine _read_file: not called CC=2 SCAR=17.9
73 0 my ($self, $opt, $args) = @_;
74  
75 0 my $file = $opt->{file};
76 0 my $content;
77  
78 # read from file or STDIN if filename == '-'
79 0 ▶ if ($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 0 0
80 0 $content = slurp (\*STDIN);
81 } else {
82 0 $content = slurp ($file);
83 }
84 0 return $content;
85 }
86  
87  
88 sub upload
89 0 ▶{
Subroutine upload: not called CC=6 SCAR=37.4
Pod undocumented
90 0 my ($self, $opt, $args) = @_;
91  
92 0 ▶ my $host = $opt->{reportserver} || Tapper::Config->subconfig->{report_server};
Condition0%0/3
$opt->{'reportserver'} || 'Tapper::Config'->subconfig->{'report_server'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{'reportserver'} || 'Tapper::Config'->subconfig->{'report_server'}
$opt->{'reportserver'} 'Tapper::Config'->subconfig->{'report_server'}
Truth table0%0/3
A || B
A= $opt->{'reportserver'}B= 'Tapper::Config'->subconfig->{'report_server'}
ABresult
1X1
011
000
93 0 ▶ my $port = $opt->{reportport} || Tapper::Config->subconfig->{report_api_port};
Condition0%0/3
$opt->{'reportport'} || 'Tapper::Config'->subconfig->{'report_api_port'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{'reportport'} || 'Tapper::Config'->subconfig->{'report_api_port'}
$opt->{'reportport'} 'Tapper::Config'->subconfig->{'report_api_port'}
Truth table0%0/3
A || B
A= $opt->{'reportport'}B= 'Tapper::Config'->subconfig->{'report_api_port'}
ABresult
1X1
011
000
94  
95 0 my $reportid = $opt->{reportid};
96 0 my $file = $opt->{file};
97 0 my $filename = $opt->{filename};
98 0 ▶ my $contenttype = $opt->{contenttype} || 'plain';
Condition0%0/2
$opt->{'contenttype'} || 'plain'
l!l
00
MC/DC0%0/1
$opt->{'contenttype'} || 'plain'
$opt->{'contenttype'}
Truth table0%0/2
A
A= $opt->{'contenttype'}
Aresult
11
00
99 0 my $content = $self->_read_file($opt, $args);
100  
101 0 ▶ my $cmdline = "#! upload $reportid ".($filename || $file)." $contenttype\n";
Condition0%0/3
$filename || $file
l!l&&r!l&&!r
000
MC/DC0%0/2
$filename || $file
$filename $file
Truth table0%0/3
A || B
A= $filenameB= $file
ABresult
1X1
011
000
102  
103 0 my $REMOTEAPI = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port);
104 0 ▶ if ($REMOTEAPI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($REMOTEAPI) { } 0 0 0
105 #my $oldfh = select $REMOTEAPI;
106 0 print $REMOTEAPI $cmdline;
107 0 print $REMOTEAPI $content;
108 #select($oldfh);
109 0 close ($REMOTEAPI);
110 }
111 else {
112 0 say "Cannot open remote receiver $host:$port.";
113 }
114 }
115  
116 # perl -Ilib bin/tapper-api upload --reportid=552 --file ~/xyz
117 # perl -Ilib bin/tapper-api upload --reportid=552 --file=$HOME/xyz
118 # dmesg | perl -Ilib bin/tapper-api upload --reportid=552 --file=- --filename="dmesg"
119  
120 2 1;
121  
122 __END__
« blib/lib/Tapper/CLI/Resource.pm ↑ Summary blib/lib/Tapper/CLI/Host.pm »