#! perl
# $Id: /mirror/trunk/languages/pipp/t/harness 34226 2008-12-21T12:51:56.513040Z bernhard  $

=head1 NAME

languages/pipp/t/harness - A harness for Pipp

=head1 SYNOPSIS

  cd languages && perl pipp/t/harness --files --master

  cd languages/pipp && perl t/harness 

  cd languages/pipp && perl t/harness --verbose t/hello.t 

=head1 DESCRIPTION

If I'm called with a single
argument of "--files", I just return a list of files to process.
This list is one per line, and is relative to the languages dir.

If I'm called with no args, I run the complete suite.

Otherwise I run the tests that were passed on the command line.

=cut

# pragmata
use strict;
use warnings;
use FindBin ();
use lib "$FindBin::Bin/../../../lib", "$FindBin::Bin/../lib";

use Cwd                     ();
use File::Spec              ();
use TAP::Harness            3.12;     # support closures for the 'exec' option
use TAP::Harness::Archive   0.12; 
use Parrot::Config          qw( %PConfig );
use Getopt::Long;
use Parrot::Harness::Smoke;
use Parrot::Test;

my ( $files_flag, $master_flag, $send_to_smolder_flag, $archive_flag, $verbose_flag );
my ( $php_flag, $pct_flag);
GetOptions(
    'files'           => \$files_flag,
    'master'          => \$master_flag,          # unused, but passed by languages/t/harness
    'send-to-smolder' => \$send_to_smolder_flag,
    'archive'         => \$archive_flag,
    'verbose'         => \$verbose_flag,
    'with-pct'        => \$pct_flag,
    'with-php'        => \$php_flag,
);

my $hll = 'pipp';
my $verbosity = $verbose_flag ? 1 : $ENV{HARNESS_VERBOSE};
$verbosity ||= 0;

if ( $files_flag ) {
    # Only the Makefile in 'parrot/languages' uses --files for unified testing
    my $dir = File::Spec->catfile( $hll, 't' );
    # unified testing can't handle test scripts written in PHP
    my @files = grep { ! m!in_php|pmc|embed! } glob( File::Spec->catfile( $dir, '*/*.t' ) );
    print join( "\n", @files );
    print "\n" if scalar(@files);
}
else { 
    my $path_to_parrot = Parrot::Test::path_to_parrot();
    my @cmd = ( "$path_to_parrot/parrot$PConfig{exe}", "$path_to_parrot/languages/pipp/pipp.pbc" );
    $ENV{PARROT_PIPP_TEST_MODULE} = 'Parrot::Test::Pipp::PCT';

    if ( $php_flag ) { 
         $ENV{PARROT_PIPP_TEST_MODULE} = 'Parrot::Test::Pipp::PHP';
         @cmd = qw{ php-cgi -q -C -n } ;
    }
    elsif ( $pct_flag ) {
        $ENV{PARROT_PIPP_TEST_MODULE} = 'Parrot::Test::Pipp::PCT';
    }

    my @files;
    if ( scalar(@ARGV) ) {
        # Someone specified tests for me to run.
        @files = grep { -f $_ } @ARGV
    }
    else {
        ( undef, undef, my $current_dir ) = File::Spec->splitpath( Cwd::getcwd() );
        if ( $current_dir eq 'languages' ) {
            @files = glob( File::Spec->catfile( $hll, 't', '*/*.t' ) );
        }
        elsif ( $current_dir eq $hll ) {
            @files = glob( File::Spec->catfile( 't', '*/*.t' ) );
        }
        else {
            die "Where am I?";
        }
    }

    my $exec_sub
        = sub {
              my ( $harness, $test_file ) = @_;

              # the directory t/embed contains only PIR test files
              return [ "$path_to_parrot/parrot$PConfig{exe}", $test_file ] if $test_file =~ m!t/embed/.*[.]t$!;

              # the directory t/pmc contains only PIR test files
              return [ "$path_to_parrot/parrot$PConfig{exe}", $test_file ] if $test_file =~ m!t/pmc/.*[.]t$!;

              # the directory t/in_php contains only test scripts written in PHP
              return [ @cmd, $test_file ] if $test_file =~ m!t/in_php/.*[.]t$!;

              # all other directories contain test scripts written in Perl
              return [ $PConfig{perl}, $test_file ];
          };  
    if ( $archive_flag ) { 
        my %env_data = Parrot::Harness::Smoke::collect_test_environment_data();
       
        my $report_file = ['pipp_test_run.tar.gz'];
        my $harness = TAP::Harness::Archive->new(
            {
                exec             => $exec_sub,
                verbosity        => $verbosity,
                archive          => $report_file->[0],
                merge            => 1,
                extra_properties => \%env_data,
            }
        );
        $harness->runtests(@files);

        if ( $send_to_smolder_flag ) {
            $env_data{report_file} = $report_file;
            $env_data{project_id}  = 10;
            Parrot::Harness::Smoke::send_archive_to_smolder(%env_data);
        }
    } else {
       my $harness = TAP::Harness->new(
           {
               exec       => $exec_sub,
               verbosity  => $verbosity,
           }
       );
        $harness->runtests(@files);
    }
}

=head1 SEE ALSO

  F<languages/perl6/t/harness>

=head1 AUTHOR

Bernhard Schmalhofer - <Bernhard.Schmalhofer@gmx.de>

=cut

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
