ArduinoBuilder/System.pm

filter: Statementstmt 47.1% 33 / 70 Branchbran 9.0% 2 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 10 / 15 Podpod 0.0% 0 / 5 totaltotal 37.1% 45 / 121 CC 14 SCAR 30.3
CC
14
Cov
36%
find_arduino_dir
56.0
execute_cmd
30.0
system_cwd
6.0
CRAP 20.8
« ArduinoBuilder/Builder.pm ↑ Summary ArduinoBuilder/FilePath.pm »
1 package App::ArduinoBuilder::System;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec::Functions 'catdir', 'rel2abs', 'canonpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Log::Any::Simple ':default';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Win32::ShellQuote 'quote_native';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our @EXPORT_OK = qw(find_arduino_dir system_cwd system_canonpath execute_cmd split_cmd);
15  
16 0 ▶sub find_arduino_dir {
Subroutine find_arduino_dir: not called CC=7 SCAR=40.3
Pod undocumented
17 0 my @tests;
18 0 ▶ if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "cygwin" or $^O eq "msys") 0 0 0
Condition0%0/6
$^O eq "MSWin32" or $^O eq "cygwin" or $^O eq "msys"
l!l&&r!l&&!r
000
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
000
MC/DC0%0/3
$^O eq "MSWin32" or $^O eq "cygwin" or $^O eq "msys"
$^O eq "MSWin32" $^O eq "cygwin" $^O eq "msys"
Truth table0%0/4
A or B or C
A= $^O eq "MSWin32"B= $^O eq "cygwin"C= $^O eq "msys"
ABCresult
1XX1
01X1
0011
0000
19 0 ▶ if (exists $ENV{LOCALAPPDATA}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"LOCALAPPDATA"}) 0 0 0
20 0 push @tests, catdir($ENV{LOCALAPPDATA}, 'Arduino15');
21 }
22 }
23 0 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 0 0 0
24 0 push @tests, '/usr/share/arduino', '/usr/local/share/arduino';
25 0 ▶ if (`which arduino 2>/dev/null` =~ m{^(.*)/bin/arduino}) {
Branch: 1 branch
BranchTrueFalseTotal
if (`which arduino 2>/dev/null` =~ m[^(.*)/bin/arduino]) 0 0 0
26 0 push @tests, catdir($1, 'share/arduino');
27 }
28 }
29 0 ▶ return first { -d } @tests;
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 }
31  
32 0 ▶sub system_cwd {
Subroutine system_cwd: not called CC=2 SCAR=17.9
Pod undocumented
33 0 my $cwd = getcwd();
34 # Todo: we could have a "use_native_cygwin" option somewhere in the improbable
35 # case of a native toolchain to deactivate this logic (as well as using
36 # /dev/null insted of nul in the builder).
37 0 ▶ if ($^O eq 'cygwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "cygwin") 0 0 0
38 0 $cwd = `cygpath -w '${cwd}'`;
39 0 chomp($cwd);
40 }
41 0 return $cwd;
42 }
43  
44 # Canonicalize a file path to be used to compare file paths (can’t be fed to
45 # external utilities).
46 0 ▶sub system_canonpath {
Subroutine system_canonpath: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my ($path) = @_;
48 0 my $canon = canonpath(rel2abs($path));
49 0 ▶ if ($^O eq 'cygwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "cygwin") 0 0 0
50 0 $canon = `cygpath '$canon'`;
51 0 chomp($canon);
52 }
53 0 return $canon;
54 }
55  
56 # Splits a given command line string into individual arguments.
57 # Returns the array of individual arguments.
58 #
59 # This approach is very primitive. However both Parse::CommandLine and
60 # Text::ParseWords have the same issue that they consider that a backslash can
61 # escape any character, which is wrong on Windows (C:\foo is not C:foo).
62 # Also Text::Balanced, is not well suited for this case where we can have
63 # unquoted pieces of text.
64 #
65 # Ideally, we would use whatever Perl uses to split a command into word as
66 # per https://perldoc.perl.org/functions/exec, but this does not seem to be
67 # exposed
68 #
69 # TODO: support escaped quotes (that are not quoting arguments) as well as,
70 # maybe, quotes interrupting unquoted arguments.
71 3 ▶sub split_cmd {
Subroutine split_cmd: called CC=2 SCAR=6.9
Pod undocumented
72 3 my ($cmd) = @_;
73 3 my @cmd;
74 3 ▶ while ($cmd =~ m/ \G \s* (?: (['"])(?<p>.*?)\1 | (?<p>[^ ]+) ) /gx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ / \G \s* (?: (['"])(?<p>.*?)\1 | (?<p>[^ ]+) ) /gx) 6 3 9
75 6 push @cmd, $+{p};
76 }
77 3 return @cmd;
78 }
79  
80 0 ▶sub execute_cmd {
Subroutine execute_cmd: not called CC=5 SCAR=34.0
Pod undocumented
81 0 my ($cmd, %options) = @_;
82 0 trace $cmd;
83 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
84 # This fix cases where the command looks like: foo '--bar="baz"'
85 #
86 # TODO: Possibly we could just split the command using split_cmd
87 # and then pass the list to system (and find something for the `` case).
88 0 $cmd = quote_native(split_cmd($cmd));
89 }
90 0 my $failed;
91 0 ▶ if (exists $options{capture_output}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{'capture_output'}) { } 0 0 0
92 0 my $out = `${cmd}`;
93 0 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 0 0
94 0 ${$options{capture_output}} = $out;
95 } else {
96 0 $failed = 1;
97 }
98 } else {
99 0 $failed = system($cmd);
100 }
101 0 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 0 0 0
102 0 debug "Can’t execute the following command: $!\n\t${cmd}";
103 0 fatal "External command failed";
104 }
105 0 return 1;
106 }
« ArduinoBuilder/Builder.pm ↑ Summary ArduinoBuilder/FilePath.pm »