Skip to content

Commit 37f06f0

Browse files
authored
Merge pull request #17 from GrantStreetGroup/Support-hub-format-set_no_numbers
Allow for set_no_numbers format
2 parents 6ac86ae + 181919a commit 37f06f0

3 files changed

Lines changed: 105 additions & 18 deletions

File tree

lib/Test2/Harness/Renderer/JUnit.pm

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ sub render_event {
176176
# Ignore subtests
177177
return if ( $f->{'hubs'} && $f->{'hubs'}->[0]->{'nested'} );
178178

179-
my $test_num = sprintf( "%04d", $event->{'assert_count'} || $f->{'assert'}->{'number'} || die Dumper $event);
179+
my $test_num = $event->{'assert_count'} || $f->{'assert'}->{'number'};
180+
$test_num = sprintf "%04d", $test_num if defined $test_num;
180181
my $test_name = _squeaky_clean( $f->{'assert'}->{'details'} // 'UNKNOWN_TEST?' );
181-
my $test_string = defined $test_name ? "$test_num - $test_name" : $test_num;
182+
$test_name = join " - ", grep { defined } $test_num, $test_name;
182183
$test->{'testsuite'}->{'tests'}++;
183184

184185
$self->close_open_failure_testcase( $test, $test_num );
@@ -190,10 +191,10 @@ sub render_event {
190191

191192
if ( $f->{'amnesty'} && grep { ( $_->{'tag'} // '' ) eq 'TODO' } @{ $f->{'amnesty'} } ) { # All TODO Tests
192193
if ( !$f->{'assert'}->{'pass'} ) { # Failing TODO
193-
push @{ $test->{'testcase'} }, $self->xml->testcase( { 'name' => "$test_string (TODO)", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} }, "" );
194+
push @{ $test->{'testcase'} }, $self->xml->testcase( { 'name' => "$test_name (TODO)", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} }, "" );
194195
}
195196
elsif ( $self->{'allow_passing_todos'} ) { # junit parsers don't like passing TODO tests. Let's just not tell them about it if $ENV{ALLOW_PASSING_TODOS} is set.
196-
push @{ $test->{'testcase'} }, $self->xml->testcase( { 'name' => "$test_string (PASSING TODO)", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} }, "" );
197+
push @{ $test->{'testcase'} }, $self->xml->testcase( { 'name' => "$test_name (PASSING TODO)", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} }, "" );
197198
}
198199
else { # Passing TODO (Failure) when not allowed.
199200

@@ -204,31 +205,34 @@ sub render_event {
204205
my ($todo_message) = map { $_->{'details'} } grep { $_->{'tag'} // '' eq 'TODO' } @{ $f->{'amnesty'} };
205206

206207
push @{ $test->{'testcase'} }, $self->xml->testcase(
207-
{ 'name' => "$test_string (TODO)", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} },
208+
{ 'name' => "$test_name (TODO)", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} },
208209
$self->xml->error(
209210
{ 'message' => $todo_message, 'type' => "TodoTestSucceeded" },
210-
$self->_cdata("ok $test_string")
211+
$self->_cdata("ok $test_name")
211212
)
212213
);
213214

214215
}
215216
}
216217
elsif ( $f->{'assert'}->{'pass'} ) { # Passing test
217218
push @{ $test->{'testcase'} }, $self->xml->testcase(
218-
{ 'name' => "$test_string", 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} },
219+
{ 'name' => $test_name, 'time' => $run_time, 'classname' => $test->{'testsuite'}->{'name'} },
219220
""
220221
);
221222
}
222223
else { # Failing Test.
223224
$test->{'testsuite'}->{'failures'}++;
224225
$test->{'testsuite'}->{'errors'}++;
225226

227+
my $message = "not ok" . ( $test_name ? " $test_name" : "" );
228+
226229
# Trap the test information. We can't generate the XML for this test until we get all the diag information.
227230
$test->{'last_failure'} = {
228-
'test_num' => $test_num,
229-
'test_name' => $test_name,
230-
'time' => $run_time,
231-
'message' => "not ok $test_string\n",
231+
'test_num' => $test_num,
232+
'test_name' => $test_name,
233+
'time' => $run_time,
234+
'message' => $message,
235+
'full_message' => "$message\n",
232236
};
233237
}
234238

@@ -240,7 +244,7 @@ sub render_event {
240244
foreach my $line ( @{ $f->{'info'} } ) {
241245
next unless $line->{'details'};
242246
chomp $line->{'details'};
243-
$test->{'last_failure'}->{'message'} .= "# $line->{details}\n";
247+
$test->{'last_failure'}->{'full_message'} .= "# $line->{details}\n";
244248
}
245249
return;
246250
}
@@ -293,17 +297,20 @@ sub close_open_failure_testcase {
293297
# This causes the entire suite to choke. We don't want this.
294298
# If we're here already, we've already failed the test. let's just make sure the person reviewing
295299
# it knows the test count was messed up.
296-
if ( $fail->{'test_num'} == $new_test_number ) {
297-
$fail->{'message'} .= "# WARNING This test number has already been seen. Duplicate TEST # in output!\n";
300+
if ( defined $fail->{'test_num'}
301+
&& defined $new_test_number
302+
&& $fail->{'test_num'} == $new_test_number )
303+
{
304+
$fail->{'message'}
305+
.= "# WARNING This test number has already been seen. Duplicate TEST # in output!\n";
298306
}
299307

300308
my $xml = $self->xml;
301309
push @{ $test->{'testcase'} }, $xml->testcase(
302-
{ 'name' => "$fail->{test_num} - $fail->{test_name}", 'time' => $fail->{'time'}, 'classname' => $test->{'testsuite'}->{'name'} },
310+
{ 'name' => $fail->{'test_name'}, 'time' => $fail->{'time'}, 'classname' => $test->{'testsuite'}->{'name'} },
303311
$xml->failure(
304-
{ 'message' => "not ok $fail->{test_num} - $fail->{test_name}", 'type' => 'TestFailed' },
305-
$self->_cdata( $fail->{'message'} )
306-
)
312+
{ 'message' => $fail->{message}, 'type' => 'TestFailed' },
313+
$self->_cdata( $fail->{'full_message'} ) )
307314
);
308315

309316
delete $test->{'last_failure'};

t/test-no-numbers/pass-1.tx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use Test2::V0;
2+
3+
sub pass_ok {
4+
my $ctx = context();
5+
$ctx->hub->format->set_no_numbers(1);
6+
7+
pass("pass");
8+
9+
$ctx->release;
10+
}
11+
12+
pass_ok();
13+
14+
done_testing;

t/test.t

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,72 @@ my $env = { # env passed to yath
211211
}, 'junit output' or diag explain $junit;
212212
}
213213

214+
{
215+
note "all tests are ok without numbers";
216+
217+
my $sdir = $dir . '-no-numbers';
218+
219+
$env->{JUNIT_TEST_FILE} = "$tmpdir/no-numbers.xml";
220+
221+
yath(
222+
command => 'test',
223+
args => [ $sdir, '--ext=tx', @renderers, '-v' ],
224+
exit => 0,
225+
env => $env,
226+
test => sub {
227+
my $out = shift;
228+
229+
like(
230+
$out->{output},
231+
qr{\Q( PASSED )\E.*\Qt/test-no-numbers/pass-1.tx\E},
232+
"t/test-no-numbers/pass-1.tx"
233+
);
234+
235+
like( $out->{output}, qr/Result: PASSED/, "Result: PASSED" );
236+
},
237+
);
238+
239+
# checking xml file
240+
ok -e $env->{JUNIT_TEST_FILE}, 'junit file exists';
241+
242+
my $junit = XML::Simple::XMLin( $env->{JUNIT_TEST_FILE} );
243+
like $junit => hash {
244+
field testsuite => hash {
245+
field errors => 0;
246+
field failures => 0;
247+
field id => D();
248+
field name => 'test-no-numbers_pass-1_tx';
249+
250+
field 'system-err' => hash { end; };
251+
field 'system-out' => hash { end; };
252+
253+
field 'testcase' => hash {
254+
field 'pass' => hash {
255+
field classname => 'test-no-numbers_pass-1_tx';
256+
field time => D();
257+
end;
258+
};
259+
field 'Tear down.' => hash {
260+
field classname => 'test-no-numbers_pass-1_tx';
261+
field time => D();
262+
end;
263+
};
264+
265+
end;
266+
};
267+
268+
field tests => 1;
269+
field time => D();
270+
field timestamp => D();
271+
272+
end;
273+
};
274+
275+
end;
276+
277+
}, 'junit output' or diag explain $junit;
278+
}
279+
214280
{
215281
note "plan ok - one failure";
216282

0 commit comments

Comments
 (0)