| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use TAP::Parser; |
| 6 |
use TAP::Parser::Aggregator; |
| 7 |
use TAP::Formatter::JUnit; |
| 8 |
use Getopt::Long; |
| 9 |
use Pod::Usage; |
| 10 |
use IO::File; |
| 11 |
use File::Slurp qw(slurp); |
| 12 |
|
| 13 |
############################################################################### |
| 14 |
# Read in our command line options. |
| 15 |
my ($help, $man); |
| 16 |
my $name; |
| 17 |
my $verbose; |
| 18 |
my $suffix = '.xml'; |
| 19 |
GetOptions( |
| 20 |
'suffix=s' => \$suffix, |
| 21 |
'verbose' => \$verbose, |
| 22 |
'name|junit_name=s' => \$name, |
| 23 |
'help|?' => \$help, |
| 24 |
'man' => \$man, |
| 25 |
) || pod2usage(1); |
| 26 |
pod2usage(1) if ($help); |
| 27 |
pod2usage( -exitstatus=>0, -verbose=>2 ) if ($man); |
| 28 |
|
| 29 |
############################################################################### |
| 30 |
# Get the names of all of the TAP files we're supposed to convert. |
| 31 |
my @tap_files = @ARGV; |
| 32 |
pod2usage(1) unless (@tap_files); |
| 33 |
|
| 34 |
############################################################################### |
| 35 |
# Convert all of the TAP files to JUnit. |
| 36 |
foreach my $file (@tap_files) { |
| 37 |
verbose( "converting TAP to JUnit: $file" ); |
| 38 |
|
| 39 |
# Slurp in the TAP. |
| 40 |
my $tap = ($file eq '-') |
| 41 |
? do { local $/; <STDIN>; } |
| 42 |
: slurp($file); |
| 43 |
|
| 44 |
# Open up a FH for where we're going to dump the JUnit |
| 45 |
my $fout; |
| 46 |
if ($file eq '-') { |
| 47 |
open $fout, '>&STDOUT' |
| 48 |
|| die "can't dup STDOUT; $!\n"; |
| 49 |
} |
| 50 |
else { |
| 51 |
my $junit_file = "${file}${suffix}"; |
| 52 |
$fout = IO::File->new( $junit_file, '>' ) |
| 53 |
|| die "can't open '$junit_file' for writing; $!"; |
| 54 |
} |
| 55 |
|
| 56 |
# Name the test; if one wasn't provided, name it after the file itself. |
| 57 |
my $test_name = $name || $file; |
| 58 |
|
| 59 |
# Convert the TAP to JUnit |
| 60 |
eval { |
| 61 |
# Create the TAP formatter, aggregator, and parser that we're going to |
| 62 |
# use to convert the TAP to JUnit. |
| 63 |
my $formatter = TAP::Formatter::JUnit->new( { stdout => $fout } ); |
| 64 |
my $aggregator = TAP::Parser::Aggregator->new(); |
| 65 |
my $parser = TAP::Parser->new( { tap => $tap } ); |
| 66 |
|
| 67 |
# Parse all of the TAP in this file. |
| 68 |
$aggregator->start(); |
| 69 |
|
| 70 |
my $session = $formatter->open_test($test_name, $parser); |
| 71 |
while (my $result = $parser->next()) { |
| 72 |
$session->result($result); |
| 73 |
} |
| 74 |
$session->close_test(); |
| 75 |
$aggregator->add($file, $parser); |
| 76 |
|
| 77 |
$aggregator->stop(); |
| 78 |
|
| 79 |
# Summarize the results (in JUnit) |
| 80 |
$formatter->summary; |
| 81 |
}; |
| 82 |
if ($@) { |
| 83 |
warn $@; |
| 84 |
} |
| 85 |
|
| 86 |
$fout->close(); |
| 87 |
} |
| 88 |
|
| 89 |
############################################################################### |
| 90 |
# All done; exit peacefully. |
| 91 |
exit; |
| 92 |
|
| 93 |
sub verbose { |
| 94 |
print "$_[0]\n" if ($verbose); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
=head1 NAME |
| 99 |
|
| 100 |
tap2junit - Converts TAP output to JUnit |
| 101 |
|
| 102 |
=head1 SYNOPSIS |
| 103 |
|
| 104 |
tap2junit [options] <filename> <filename> ... |
| 105 |
|
| 106 |
Options: |
| 107 |
--suffix <suffix> Suffix for JUnit output files (default ".xml") |
| 108 |
--verbose Display verbose status during conversion |
| 109 |
--name <name> Provide explicit name for the JUnit test |
| 110 |
--help/-? Display brief help message |
| 111 |
--man Display full documentation |
| 112 |
|
| 113 |
=head1 DESCRIPTION |
| 114 |
|
| 115 |
C<tap2junit> converts TAP output to JUnit. |
| 116 |
|
| 117 |
Give it a list of files containing TAP results and it will create a series of |
| 118 |
F<????.junit.xml> output files containing the JUnit representations of that TAP |
| 119 |
contained in the files. |
| 120 |
|
| 121 |
If you specify F<-> as the filename, C<tap2junit> will read from STDIN and write |
| 122 |
to STDOUT. You may also want to use the C<--name> option to name the test |
| 123 |
explicitly (as the default name of "-" isn't going to make much sense). |
| 124 |
|
| 125 |
=head1 OPTIONS |
| 126 |
|
| 127 |
=over |
| 128 |
|
| 129 |
=item B<--suffix E<lt>suffixE<gt>> |
| 130 |
|
| 131 |
Specifies the suffix which is appended to all of the input files, in order to |
| 132 |
generate the filename for the JUnit XML file that is being output. |
| 133 |
|
| 134 |
If you want to live dangerously and over-write your original TAP files, you can |
| 135 |
set this to "" and your original TAP files will be over-written. |
| 136 |
|
| 137 |
Defaults to F<.xml> |
| 138 |
|
| 139 |
=item B<--verbose> |
| 140 |
|
| 141 |
Display verbose status information during the conversion (telling you which TAP |
| 142 |
file its working on). |
| 143 |
|
| 144 |
=item B<--name E<lt>nameE<gt>> |
| 145 |
|
| 146 |
Specifies an explicit name for the JUnit test. If no name is provided, a |
| 147 |
default name will be constructed based on the full path of the TAP file being |
| 148 |
processed. |
| 149 |
|
| 150 |
This option has also been aliased as C<--junit_name> to provide compatibility |
| 151 |
with a patch from Joe McMahon. |
| 152 |
|
| 153 |
=item B<--help/-?> |
| 154 |
|
| 155 |
Display brief help message. |
| 156 |
|
| 157 |
=item B<--man> |
| 158 |
|
| 159 |
Displays the full documentation. |
| 160 |
|
| 161 |
=back |
| 162 |
|
| 163 |
=head1 AUTHOR |
| 164 |
|
| 165 |
Graham TerMarsch <[email protected]> |
| 166 |
|
| 167 |
=head1 COPYRIGHT |
| 168 |
|
| 169 |
Copyright 2008-2010, Graham TerMarsch. All Rights Reserved. |
| 170 |
|
| 171 |
This is free software; you can redistribute it and/or modify it under the same |
| 172 |
terms as Perl itself. |
| 173 |
|
| 174 |
=head1 SEE ALSO |
| 175 |
|
| 176 |
L<TAP::Formatter::JUnit>. |
| 177 |
|
| 178 |
=cut |