GitHubActions.pm

filter: Statementstmt 95.3% 102 / 107 Branchbran 69.4% 25 / 36 Conditioncond 37.5% 9 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 94.1% 16 / 17 Podpod 66.6% 2 / 3 totaltotal 77.6% 156 / 201 CC 32 SCAR 20.3
CC
32
Cov
81%
_dump_test_parser
19.2
summary
10.1
_normalize_stash
7.1
CRAP 7.6
↑ Summary GitHubActions/ErrorGroup.pm »
1 package TAP::Formatter::GitHubActions;
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 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'TAP::Formatter::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.3.4';
9  
10 1 ▶use TAP::Formatter::GitHubActions::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use TAP::Formatter::GitHubActions::ErrorGroup;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use TAP::Formatter::GitHubActions::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use TAP::Formatter::GitHubActions::ErrorAggregate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my $MONOPHASIC_REGEX = qr/^Failed\stest .+\nat/;
16 1 my $RUNNING_IN_GHA = $ENV{GITHUB_ACTIONS};
17 1 my $GHA_SKIP_SUMMARY = $ENV{GHA_SKIP_SUMMARY};
18  
19 77 ▶sub _stash_line_for_current_test {
Subroutine _stash_line_for_current_test: called CC=2 SCAR=6.9
20 77 my ($self, $parser, $line) = @_;
21 77 my $test_num = $parser->{tests_run};
22 77 ▶ my $stash = ($parser->{_msgs_for_test}{$test_num} //= []);
Condition100%2/2
$parser->{"_msgs_for_test"}{$test_num} //= []
l!l
5918
MC/DC100%1/1
$parser->{"_msgs_for_test"}{$test_num} //= []
$parser->{"_msgs_for_test"}{$test_num}
Truth table100%2/2
A
A= $parser->{"_msgs_for_test"}{$test_num}
Aresult
11
00
23 77 push @{$stash}, $line;
24 }
25  
26 18 ▶sub _normalize_stash {
Subroutine _normalize_stash: called CC=7 SCAR=19.6
27 18 my $stash = shift;
28 18 my @new_stash;
29  
30 # First phase: Join all output, and segment it by "Failed test".
31 18 my @chunks = split(/(Failed test)/, join("\n", @{$stash}));
32  
33 # Second phase, heal the splits so we end up with every string in the stash
34 # beginning with "Failed test".
35 18 ▶ while (scalar @chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @chunks) 49 18 67
36 49 my $chunk = shift @chunks;
37 # skip empty lines (consequence of split)
38 49 ▶ next unless $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk 18 31 49
39  
40 # if we hit the separator
41 31 ▶ if ($chunk eq 'Failed test' && scalar @chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk eq "Failed test" and scalar @chunks) 31 0 31
Condition50%1/2
$chunk eq "Failed test" and scalar @chunks
!ll
031
MC/DC0%0/1
$chunk eq "Failed test" and scalar @chunks
$chunk eq "Failed test"
Truth table50%1/2
A
A= $chunk eq "Failed test"
Aresult
00
11
42 # Grab a new chunk
43 31 $chunk .= shift @chunks;
44 # Cleanup the newlines for named tests.
45 31 ▶ $chunk =~ s/\n/ / if $chunk =~ qr/$MONOPHASIC_REGEX/;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk =~ qr/$MONOPHASIC_REGEX/u 21 10 31
46 }
47  
48 # kill off any rouge newlines
49 31 chomp($chunk);
50 31 my $error = TAP::Formatter::GitHubActions::Error->from_output($chunk);
51 31 ▶ next unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 31 31
52  
53 31 push @new_stash, $error;
54 }
55  
56 # Return it
57 18 return @new_stash;
58 }
59  
60 5 ▶sub open_test {
Subroutine open_test: called CC=1 SCAR=0.0
Pod documented
61 5 my ($self, $test, $parser) = @_;
62 5 my $session = $self->SUPER::open_test($test, $parser);
63  
64 # force verbosity to be able to read comments, yamls & unknowns.
65 5 $self->verbosity(1);
66  
67 # We'll use the parser as a vessel, afaics there's one parser instance per
68 # parallel job.
69  
70 # We'll keep track of all output of a test with this.
71 5 $parser->{_msgs_for_test} = {};
72  
73 # In an ideal world, we'd just need to listen to `comment` and that should
74 # suffice, but `throws_ok` & `lives_ok` report via `unknown`...
75 # But this is real life...
76 # so...
77 159 ▶ my $handler = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
78 159 my $result = shift->raw;
79 # Skip Subtests
80 159 ▶ return if $result =~ /Subtest/;
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /Subtest/u 5 154 159
81 # Ignore anything that's not a comment (plans & tests)
82 154 ▶ return unless $result =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /^\s*#/u 87 67 154
83 # Skip trailing end of tests/subtests
84 67 ▶ return if $result =~ m/Looks like you/;
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /Looks like you/u 9 58 67
85 # Cleanup comment start
86 58 $result =~ s/\s*# //;
87 # Test::Exception doens't indent the messages 🤡, excluding those keywords
88 # and anything that doesn't begin with a space.
89 58 ▶ return unless $result =~ m/^( |died|found|expecting)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /^( |died|found|expecting)/u 0 58 58
90 # Skip lines only having whitespaces
91 58 ▶ return if $result =~ m/^ +$/;
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /^ +$/u 0 58 58
92 # Cleanup indent (2 spaces)
93 58 $result =~ s/^ //;
94 58 $self->_stash_line_for_current_test($parser, $result);
95 5 };
96  
97 5 $parser->callback(comment => $handler);
98 5 $parser->callback(unknown => $handler);
99  
100 # Enable YAML Support
101 5 $parser->version(13);
102 27 ▶ $parser->callback(yaml => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
103 27 my $yaml = shift->data;
104 # skip notes.
105 27 ▶ return if grep { $yaml->{emitter} eq $_ } qw(Test::More::note Test::More::diag);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$yaml->{"emitter"} eq $_;} "Test::More::note", "Test::More::diag" 8 19 27
106 # skip empty messages (prolly never happens?)
107 19 ▶ return unless $yaml->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml->{"message"} 0 19 19
108 19 $self->_stash_line_for_current_test($parser, $yaml->{message});
109 5 });
110  
111 5 return $session;
112 }
113  
114 0 ▶sub header { }
Subroutine header: not called CC=1 SCAR=0.0
Pod undocumented
115  
116 4 ▶sub _output_report_notice {
Subroutine _output_report_notice: called CC=3 SCAR=11.8
117 4 my ($self, $test) = @_;
118 4 my $workflow_url = '%WORKFLOW_URL%';
119  
120 12 my @workflow_vars = grep { $_ } @ENV{
121 4 qw(
122 GITHUB_SERVER_URL
123 GITHUB_REPOSITORY
124 GITHUB_RUN_ID
125 )
126 };
127  
128 4 ▶ if (!$GHA_SKIP_SUMMARY && !!@workflow_vars) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($GHA_SKIP_SUMMARY or not @workflow_vars) 0 4 4
Condition33%1/3
$GHA_SKIP_SUMMARY or not @workflow_vars
l!l&&r!l&&!r
400
MC/DC0%0/2
$GHA_SKIP_SUMMARY or not @workflow_vars
$GHA_SKIP_SUMMARY not @workflow_vars
Truth table33%1/3
A or not(B)
A= $GHA_SKIP_SUMMARYB= not @workflow_vars
ABresult
1X1
011
000
129 0 $workflow_url = sprintf("%s/%s/actions/runs/%s", @workflow_vars);
130 }
131  
132 4 my $file_marker_line = TAP::Formatter::GitHubActions::Utils::log_annotation_line(
133 type => 'notice',
134 filename => $test,
135 line => 1,
136 title => 'More details',
137 body => "See the full report in: $workflow_url"
138 );
139  
140 4 $self->_output("$file_marker_line\n");
141 }
142  
143 5 ▶sub summary {
Subroutine summary: called CC=9 SCAR=23.2
Pod documented
144 5 my ($self, $aggregate, $interrupted) = @_;
145 5 $self->SUPER::summary($aggregate, $interrupted);
146  
147 5 my $total = $aggregate->total;
148 5 my $passed = $aggregate->passed;
149  
150 5 ▶ return if ($total == $passed && !$aggregate->has_problems);
Branch: 1 branch
BranchTrueFalseTotal
if $total == $passed and not $aggregate->has_problems 1 4 5
Condition66%2/3
$total == $passed and not $aggregate->has_problems
!ll&&!rl&&r
401
MC/DC50%1/2
$total == $passed and not $aggregate->has_problems
$total == $passed not $aggregate->has_problems
Truth table66%2/3
A and not(B)
A= $total == $passedB= not $aggregate->has_problems
ABresult
0X0
100
111
151  
152 4 $self->_output("\n= GitHub Actions Report =\n");
153  
154 # First print a mark at the beginning of the files reporting errors with a
155 # link to the workflow run for the full report.
156 #
157 # This is a workaround due to the fact that there's a hard limit on the
158 # amount of annotations rendered by GitHub on Pull requests.
159 #
160 # As of writting is a max of 10 annotations per step, 50 per workflow.
161 # To overcome this we'll write here to the Workflow Summary File.
162 # and link back in an anotation.
163 # see [0] & [1] & [2].
164 4 foreach my $test ($aggregate->descriptions) {
165 4 my ($parser) = $aggregate->parsers($test);
166 4 ▶ next if $parser->passed == $parser->tests_run && !$parser->exit;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->passed == $parser->tests_run and not $parser->exit 0 4 4
Condition33%1/3
$parser->passed == $parser->tests_run and not $parser->exit
!ll&&!rl&&r
400
MC/DC0%0/2
$parser->passed == $parser->tests_run and not $parser->exit
$parser->passed == $parser->tests_run not $parser->exit
Truth table33%1/3
A and not(B)
A= $parser->passed == $parser->tests_runB= not $parser->exit
ABresult
0X0
100
111
167  
168 4 $self->_output_report_notice($test);
169 }
170  
171 # Now print all error annotations
172 4 foreach my $test ($aggregate->descriptions) {
173 4 my ($parser) = $aggregate->parsers($test);
174 4 ▶ next if $parser->passed == $parser->tests_run && !$parser->exit;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->passed == $parser->tests_run and not $parser->exit 0 4 4
Condition33%1/3
$parser->passed == $parser->tests_run and not $parser->exit
!ll&&!rl&&r
400
MC/DC0%0/2
$parser->passed == $parser->tests_run and not $parser->exit
$parser->passed == $parser->tests_run not $parser->exit
Truth table33%1/3
A and not(B)
A= $parser->passed == $parser->tests_runB= not $parser->exit
ABresult
0X0
100
111
175  
176 4 $self->_dump_test_parser($test, $parser);
177 }
178 }
179  
180 4 ▶sub _dump_test_parser {
Subroutine _dump_test_parser: called CC=8 SCAR=29.6
181 4 my ($self, $test, $parser) = @_;
182  
183 4 my $error_aggregate = TAP::Formatter::GitHubActions::ErrorAggregate->new();
184  
185 # Transform messages into error objects and feed them into the error
186 # aggregate.
187 4 foreach my $test_num (sort keys %{$parser->{_msgs_for_test}}) {
188 18 my $stash = $parser->{_msgs_for_test}->{$test_num};
189 18 $error_aggregate->add(_normalize_stash($stash));
190 }
191  
192 4 my @error_groups = $error_aggregate->as_sorted_array();
193 # Print in GHA Annotation format
194 4 $self->_output($_->as_gha_summary_for($test)) for (@error_groups);
195  
196 # Skip if not running in GitHub Actions
197 4 ▶ return if !($RUNNING_IN_GHA && !$GHA_SKIP_SUMMARY);
Branch: 1 branch
BranchTrueFalseTotal
unless $RUNNING_IN_GHA and not $GHA_SKIP_SUMMARY 4 0 4
Condition33%1/3
$RUNNING_IN_GHA and not $GHA_SKIP_SUMMARY
!ll&&!rl&&r
400
MC/DC0%0/2
$RUNNING_IN_GHA and not $GHA_SKIP_SUMMARY
$RUNNING_IN_GHA not $GHA_SKIP_SUMMARY
Truth table33%1/3
A and not(B)
A= $RUNNING_IN_GHAB= not $GHA_SKIP_SUMMARY
ABresult
0X0
100
111
198  
199 # Write full report on GHA Step Summary [2]
200 0 ▶ open(my $summary_fd, '>>', $ENV{GITHUB_STEP_SUMMARY}) or die "Unable to open " . $ENV{GITHUB_STEP_SUMMARY} . ": $!" if $RUNNING_IN_GHA;
Branch: 1 branch
BranchTrueFalseTotal
if $RUNNING_IN_GHA 0 0 0
Condition0%0/5
$RUNNING_IN_GHA && (open(my $summary_fd, '>>', $ENV{'GITHUB_STEP_SUMMARY'}) || die('Unable to open ' . $ENV{'GITHUB_STEP_SUMMARY'} . ": $!"))
!ll&&!rl&&r
000
open my $summary_fd, ">>", $ENV{"GITHUB_STEP_SUMMARY"} or die 'Unable to open ' . $ENV{"GITHUB_STEP_SUMMARY"} . ": $!"
l!l
00
MC/DC0%0/2
$RUNNING_IN_GHA && (open(my $summary_fd, '>>', $ENV{'GITHUB_STEP_SUMMARY'}) || die('Unable to open ' . $ENV{'GITHUB_STEP_SUMMARY'} . ": $!"))
$RUNNING_IN_GHA open my $summary_fd, ">>", $ENV{"GITHUB_STEP_SUMMARY"}
Truth table0%0/3
A && B
A= $RUNNING_IN_GHAB= open my $summary_fd, ">>", $ENV{"GITHUB_STEP_SUMMARY"}
ABresult
0X0
100
111
201 0 print $summary_fd "## Failures in `$test`\n";
202 0 print $summary_fd $_->as_markdown_summary() for (@error_groups);
203 0 print $summary_fd "\n\n";
204 }
205  
206 # [0]: https://github.com/orgs/community/discussions/26680#discussioncomment-3252835
207 # [1]: https://github.com/orgs/community/discussions/68471
208 # [2]: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
209  
210 1 1;
211 __END__
↑ Summary GitHubActions/ErrorGroup.pm »