Archive-TAP-Convert

 view release on metacpan or  search on metacpan

lib/Archive/TAP/Convert.pm  view on Meta::CPAN

use warnings;

use Capture::Tiny qw( capture_merged );
use TAP::Harness;
use TAP::Harness::Archive;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(convert_from_taparchive);

# one and only subroutine of this module
sub convert_from_taparchive {

    my %args = @_;

    # Input Arguments: archive, formatter, force_inline
    # Set default values:
    die 'no archive specified'
        unless (exists $args{archive});

    my $formatter;

    if ( exists $args{formatter} && ref ( $args{formatter} ) =~ /^TAP::Formatter::/ ) {
      $formatter = $args{formatter};
    }
    else {
      $args{formatter} = 'TAP::Formatter::HTML'
          unless (exists $args{formatter});
      $args{force_inline} = 0
          unless (exists $args{force_inline});

      # This is the complicate but flexible version to:
      #   use TAP::Formatter::HTML;
      #   my $formatter = TAP::Formatter::HTML->new;
      (my $require_name = $args{formatter} . ".pm") =~ s{::}{/}g;
      eval {
          require $require_name;
          $formatter = $args{formatter}->new();
      };
      die "Problems with formatter $args{formatter}"
        . " at $require_name: $@"
          if $@;
    }

    # if set, include all CSS and JS in HTML file
    if ($args{force_inline}) {
        $formatter->force_inline_css(1);
        $formatter->force_inline_js (1);
    }

    # Now we do a lot of magic to convert this stuff...

    my $harness = TAP::Harness->new({ formatter => $formatter });

    $formatter->really_quiet(1);
    $formatter->prepare;

    my $session;
    my $aggregator = TAP::Harness::Archive->aggregator_from_archive({
        archive          => $args{archive},
        parser_callbacks => {
            ALL => sub {
                $session->result( $_[0] );
            },
        },
        made_parser_callback => sub {
            $session = $formatter->open_test( $_[1], $_[0] );
        }
    });

    $aggregator->start;
    $aggregator->stop;

    # This code also prints to STDOUT but we will catch it!
    return capture_merged { $formatter->summary($aggregator) };

}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Archive::TAP::Convert - Read from a TAP archive and convert it for displaying

=head1 VERSION

version 0.007

=head1 SYNOPSIS

Either:

  use Archive::TAP::Convert qw(convert_from_taparchive);

  my $html = convert_from_taparchive(
                archive   => '/must/be/the/complete/path/to/test.tar.gz',
                formatter => 'TAP::Formatter::HTML',
             );

Or:

  use Archive::TAP::Convert qw(convert_from_taparchive);
  use TAP::Formatter::HTML; # or ::JUnit, ::Console, etc.

  my $formatter = TAP::Formatter::HTML->new;
  # possibly configure formatter

  my $html = convert_from_taparchive(
                archive   => '/must/be/the/complete/path/to/test.tar.gz',
                formatter => $formatter,
             );

=head1 ABOUT

This is a software library for the I<perl programming language>.



( run in 1.923 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )