Debian Codegraph @

lib/Test2/Harness/Renderer/Formatter.pm

raw
1 package Test2::Harness::Renderer::Formatter;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Carp qw/croak/;
8
9 use File::Spec;
10
11 use Storable qw/dclone/;
12
13 use Test2::Harness::Util qw/fqmod mod2file/;
14 use Test2::Harness::Util::JSON qw/encode_pretty_json/;
15
16 BEGIN { require Test2::Harness::Renderer; our @ISA = ('Test2::Harness::Renderer') }
17 use Test2::Harness::Util::HashBase qw{
18 -io -io_err
19 -formatter
20 -show_run_info
21 -show_job_info
22 -show_job_launch
23 -show_job_end
24 -do_step
25 -interactive
26 };
27
28 sub init {
29 my $self = shift;
30
31 my $settings = $self->{+SETTINGS};
32
33 my $formatter = $self->{+FORMATTER} //= 'Test2';
34 my $f_class = fqmod('Test2::Formatter', $formatter);
35 my $f_file = mod2file($f_class);
36 require $f_file;
37
38 my $io = $self->{+IO} || $self->{output} || \*STDOUT;
39 unless (ref $io) {
40 open(my $fh, '>', $io) or die "Could not open file '$io' for writing: $!";
41 $self->{+IO} = $fh;
42 }
43
44 my $io_err = $self->{+IO_ERR} || $self->{output} || \*STDERR;
45 unless (ref $io_err) {
46 open(my $fh, '>', $io_err) or die "Could not open file '$io_err' for writing: $!";
47 $self->{+IO_ERR} = $fh;
48 }
49
50 $self->{+INTERACTIVE} = 1 if $settings->debug->interactive;
51 $self->{+INTERACTIVE} //= 1 if $ENV{YATH_INTERACTIVE};
52
53 $self->{+FORMATTER} = $f_class->new(
54 io => $self->{+IO},
55 progress => $self->{+PROGRESS},
56 handles => [$self->{+IO}, $self->{+IO_ERR}, $self->{+IO}],
57 verbose => $settings->display->verbose,
58 color => $settings->display->color,
59 no_wrap => $settings->display->no_wrap,
60 interactive => $self->{+INTERACTIVE},
61 is_persistent => $self->{+COMMAND_CLASS}->group eq 'persist' ? 1 : 0,
62 );
63
64 $self->{+DO_STEP} = $self->{+FORMATTER}->can('step') ? 1 : 0;
65
66 $self->{+SHOW_JOB_END} = 1 unless defined $self->{+SHOW_JOB_END};
67 }
68
69 sub step {
70 my $self = shift;
71 return unless $self->{+DO_STEP};
72 $self->{+FORMATTER}->step;
73 }
74
75 sub render_event {
76 my $self = shift;
77 my ($event) = @_;
78
79 # We modify the event, which would be bad if there were multiple renderers,
80 # so we deep clone it.
81 $event = dclone($event);
82
83 my $settings = $self->{+SETTINGS};
84
85 my $f = $event->{facet_data}; # Optimization
86
87 $f->{harness} = {%$event};
88 delete $f->{harness}->{facet_data};
89
90 if ($self->{+SHOW_RUN_INFO} && $f->{harness_run}) {
91 my $run = $f->{harness_run};
92
93 push @{$f->{info}} => {
94 tag => 'RUN INFO',
95 details => encode_pretty_json($run),
96 };
97 }
98
99 if ($f->{harness_job_launch}) {
100 my $job = $f->{harness_job};
101
102 $f->{harness}->{job_id} ||= $job->{job_id};
103
104 if ($self->{+SHOW_JOB_LAUNCH}) {
105 push @{$f->{info}} => {
106 tag => $f->{harness_job_launch}->{retry} ? 'RETRY' : 'LAUNCH',
107 debug => 0,
108 important => 1,
109 details => File::Spec->abs2rel($job->{file}),
110 };
111 }
112
113 if ($self->{+SHOW_JOB_INFO}) {
114 push @{$f->{info}} => {
115 tag => 'JOB INFO',
116 details => encode_pretty_json($job),
117 };
118 }
119 }
120
121 if ($f->{harness_job_end}) {
122 my $job = $f->{harness_job};
123 my $skip = $f->{harness_job_end}->{skip};
124 my $fail = $f->{harness_job_end}->{fail};
125 my $file = $f->{harness_job_end}->{file};
126 my $retry = $f->{harness_job_end}->{retry};
127
128 my $job_id = $f->{harness}->{job_id} ||= $job->{job_id};
129
130 # Make the times important if they were requested
131 if ($settings->display->show_times && $f->{info}) {
132 for my $info (@{$f->{info}}) {
133 next unless $info->{tag} eq 'TIME';
134 $info->{important} = 1;
135 }
136 }
137
138 if ($self->{+SHOW_JOB_END}) {
139 my $name = File::Spec->abs2rel($file);
140 $name .= " - $skip" if $skip;
141
142 my $tag = 'PASSED';
143 $tag = 'SKIPPED' if $skip;
144 $tag = 'FAILED' if $fail;
145 $tag = 'TO RETRY' if $retry;
146
147 unshift @{$f->{info}} => {
148 tag => $tag,
149 debug => $fail,
150 important => 1,
151 details => $name,
152 };
153 }
154 }
155
156 my $num = $f->{assert} && $f->{assert}->{number} ? $f->{assert}->{number} : undef;
157
158 $self->{+FORMATTER}->write($event, $num, $f);
159 }
160
161 sub finish {
162 my $self = shift;
163 $self->{+FORMATTER}->finalize();
164 }
165
166 1;
167
168 __END__
169
170 =pod
171
172 =encoding UTF-8
173
174 =head1 NAME
175
176 Test2::Harness::Renderer::Formatter - Renderer that uses any Test2::Formatter
177 for rendering.
178
179 =head1 DESCRIPTION
180
181 This renderer simply acts as a communication layer between the harness and any
182 Test2 formatter that you wish to use to display results. Not all formatters
183 will produce useful output for harness events.
184
185 =head1 SOURCE
186
187 The source code repository for Test2-Harness can be found at
188 F<http://github.com/Test-More/Test2-Harness/>.
189
190 =head1 MAINTAINERS
191
192 =over 4
193
194 =item Chad Granum E<lt>[email protected]<gt>
195
196 =back
197
198 =head1 AUTHORS
199
200 =over 4
201
202 =item Chad Granum E<lt>[email protected]<gt>
203
204 =back
205
206 =head1 COPYRIGHT
207
208 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
209
210 This program is free software; you can redistribute it and/or
211 modify it under the same terms as Perl itself.
212
213 See F<http://dev.perl.org/licenses/>
214
215 =cut