Crawl/Chromium.pm

filter: Statementstmt 25.7% 17 / 66 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 71.4% 5 / 7 Podpod n/a totaltotal 17.4% 22 / 126 CC 22 SCAR 48.2
CC
22
Cov
16%
_fetch_page
182.0
_execute_chromium
110.0
BEGIN
1.0
CRAP 124.0
« Crawl/Auto.pm ↑ Summary test_compile.pl »
1 package WWW::Crawl::Chromium;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶use parent 'WWW::Crawl';
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IPC::Run qw(start timeout);
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 1 our $VERSION = '0.5';
12 # $VERSION = eval $VERSION;
13
14 0 ▶sub _fetch_page {
Subroutine _fetch_page: not called CC=13 SCAR=52.0
15 0 my ($self, $url) = @_;
16
17 my $chromium_path = $self->{'chromium_path'}
18 || $self->{'chrome_path'}
19 0 ▶ || $self->{'chromium'}
Condition0%0/8
$self->{'chromium_path'} || $self->{'chrome_path'} || $self->{'chromium'} || 'chromium'
l!l
00
$self->{"chromium_path"} or $self->{"chrome_path"} or $self->{"chromium"}
l!l&&r!l&&!r
000
$self->{"chromium_path"} or $self->{"chrome_path"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{'chromium_path'} || $self->{'chrome_path'} || $self->{'chromium'} || 'chromium'
$self->{"chromium_path"} $self->{"chrome_path"} $self->{"chromium"}
Truth table0%0/4
A or B or C
A= $self->{"chromium_path"}B= $self->{"chrome_path"}C= $self->{"chromium"}
ABCresult
1XX1
01X1
0011
0000
20 || 'chromium';
21 0 ▶ my $timeout = $self->{'chromium_timeout'} // 120;
Condition0%0/2
$self->{"chromium_timeout"} // 120
l!l
00
MC/DC0%0/1
$self->{"chromium_timeout"} // 120
$self->{"chromium_timeout"}
Truth table0%0/2
A
A= $self->{"chromium_timeout"}
Aresult
11
00
22 0 ▶ my $virtual_time_budget = $self->{'chromium_time_budget'} // 20000;
Condition0%0/2
$self->{"chromium_time_budget"} // 20000
l!l
00
MC/DC0%0/1
$self->{"chromium_time_budget"} // 20000
$self->{"chromium_time_budget"}
Truth table0%0/2
A
A= $self->{"chromium_time_budget"}
Aresult
11
00
23 0 ▶ my $proxy = $self->{'proxy'} // 'direct://';
Condition0%0/2
$self->{"proxy"} // "direct://"
l!l
00
MC/DC0%0/1
$self->{"proxy"} // "direct://"
$self->{"proxy"}
Truth table0%0/2
A
A= $self->{"proxy"}
Aresult
11
00
24
25 0 ▶ my $chrome_ua = $self->{'agent'} // "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36";
Condition0%0/2
$self->{"agent"} // "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
l!l
00
MC/DC0%0/1
$self->{"agent"} // "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
$self->{"agent"}
Truth table0%0/2
A
A= $self->{"agent"}
Aresult
11
00
26 0 ▶ my $retry_count = $self->{'retry_count'} // 0;
Condition0%0/2
$self->{"retry_count"} // 0
l!l
00
MC/DC0%0/1
$self->{"retry_count"} // 0
$self->{"retry_count"}
Truth table0%0/2
A
A= $self->{"retry_count"}
Aresult
11
00
27
28 0 my @base_command = (
29 $chromium_path,
30 '--headless=new',
31 qq{--proxy-server=$proxy},
32 qq{--user-agent=$chrome_ua},
33 '--window-size=1920,1080',
34 '--no-sandbox',
35 '--disable-gpu',
36 '--disable-dev-shm-usage',
37 '--disable-software-rasterizer',
38 '--disable-features=VaapiVideoDecoder',
39 '--disable-site-isolation-trials',
40 '--password-store=basic',
41 '--use-mock-keychain',
42 '--log-level=3',
43 '--no-first-run',
44 '--no-default-browser-check',
45 '--run-all-compositor-stages-before-draw',
46 "--virtual-time-budget=$virtual_time_budget",
47 '--dump-dom',
48 $url,
49 );
50
51 0 my $result;
52 0 for my $attempt (0 .. $retry_count) {
53 0 ▶ if ($attempt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt > 0) 0 0 0
54 0 ▶ print STDERR "DEBUG: Retry attempt $attempt for $url\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
55 0 sleep 1;
56 }
57
58 0 $result = $self->_execute_chromium(\@base_command, $timeout);
59 0 ▶ last if $result->{'success'};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"success"} 0 0 0
60 }
61
62 0 return $result;
63 }
64
65 0 ▶sub _execute_chromium {
Subroutine _execute_chromium: not called CC=10 SCAR=47.0
66 0 my ($self, $command, $timeout) = @_;
67 0 my @command = @$command;
68
69 0 my $content = '';
70 0 my $error_output = '';
71 0 my $timed_out = 0;
72 0 my $launch_error = '';
73
74 0 my $exit_code = 0;
75 0 my $harness = eval {
76 0 start \@command, '<', \undef, '>', \$content, '2>', \$error_output, timeout($timeout);
77 };
78 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not $harness) { } 0 0 0
79 0 $launch_error = $@;
80 } elsif (!$harness) {
81 0 $launch_error = 'Unknown failure starting Chromium';
82 } else {
83 0 eval { $harness->finish; 1 };
84 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
85 0 ▶ if ($@ =~ /timeout/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /timeout/i) { } 0 0 0
86 0 $timed_out = 1;
87 0 $harness->kill_kill;
88 } else {
89 0 $launch_error = $@;
90 0 $harness->kill_kill;
91 }
92 } else {
93 0 $exit_code = $? >> 8;
94 0 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 0 0
95 0 $launch_error = "Chromium exited with status $exit_code";
96 }
97 }
98 }
99
100 0 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 0 0 0
101 return {
102 0 'success' => 0,
103 'status' => 599,
104 'reason' => "Chromium timeout after ${timeout}s",
105 'content' => '',
106 };
107 }
108
109 0 ▶ if ($launch_error ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($launch_error ne "") 0 0 0
110 0 $launch_error =~ s/\s+$//;
111 0 ▶ my $reason = $error_output || $launch_error;
Condition0%0/3
$error_output || $launch_error
l!l&&r!l&&!r
000
MC/DC0%0/2
$error_output || $launch_error
$error_output $launch_error
Truth table0%0/3
A || B
A= $error_outputB= $launch_error
ABresult
1X1
011
000
112 return {
113 0 'success' => 0,
114 'status' => 599,
115 'reason' => $reason,
116 'content' => $content,
117 };
118 }
119
120 0 ▶ if ($content eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq "") 0 0 0
121 return {
122 0 'success' => 0,
123 'status' => 599,
124 'reason' => 'Chromium returned empty content',
125 'content' => '',
126 };
127 }
128
129 return {
130 0 'success' => 1,
131 'status' => 200,
132 'reason' => 'OK',
133 'content' => $content,
134 };
135 }
136
137 1 1;
138
139 __END__
« Crawl/Auto.pm ↑ Summary test_compile.pl »