# Copyright (C) 2005-2008, The Perl Foundation.
# $Id: /mirror/trunk/languages/lua/t/harness 35745 2009-01-18T12:09:17.170228Z bernhard  $

=head1 NAME

languages/lua/t/harness - A harness for Parrot Lua

=head1 SYNOPSIS

    perl t/harness --files

    perl t/harness

    perl t/harness --archive --send-to-smolder

    perl t/harness lua/t/examples.t

    perl t/harness lua/t/pmc/nil.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.

If I'm called with "--use-lua", I run with the original C<lua>
in order to valid of the test suite.

If I'm called with "--use-luac-pl", I run with C<luac.pl> (the Lua to PIR
compiler written in Perl5).

If I'm called with "--use-lua-pbc", I run with C<lua.pbc> (the Lua interpreter
written in PIR).

If I'm called with "--use-luap-pir", I run with C<luap.pir>.

If I'm called with "--use-luac2pir-pir", I run with C<luac2pir.pir>.

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

=cut

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

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 $language = 'lua';

my ( $files_flag, $master_flag, $send_to_smolder_flag, $archive_flag, $verbose_flag );
my ( $use_orig_lua, $use_luac_pl, $use_lua_pbc, $use_luap_pir, $use_luac2pir_pir );
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,
    # Lua specific part
    'use-lua' => \$use_orig_lua,
    'use-luac-pl' => \$use_luac_pl,
    'use-lua-pbc' => \$use_lua_pbc,
    'use-luap-pir' => \$use_luap_pir,
    'use-luac2pir-pir' => \$use_luac2pir_pir,
);

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

if ( $files_flag ) {

    # Only the Makefile in 'parrot/languages' uses --files
    my $dir = File::Spec->catfile( $language, 't' );
    my @files;
    push @files, glob( File::Spec->catfile( $dir, 'pmc', '*.t' ) );
    push @files, glob( File::Spec->catfile( $dir, '*.t' ) );
    print join( "\n", @files );
    print "\n" if scalar(@files);
}
else {
    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() );
        my $dir;
        if ( $current_dir eq 'languages' ) {
            $dir = File::Spec->catfile( $language, 't' );
        }
        elsif ( $current_dir eq $language ) {
            $dir = 't';
        }
        if ($dir) {
            push @files, glob( File::Spec->catfile( $dir, 'pmc', '*.t' ) )
                unless ($use_orig_lua);
            push @files, glob( File::Spec->catfile( $dir, '*.t' ) );
        }
    }

    if ($use_orig_lua) {
        $ENV{PARROT_LUA_TEST_PROG} = 'lua';
    }
    elsif ($use_luac_pl) {
        $ENV{PARROT_LUA_TEST_PROG} = 'luac.pl';
    }
    elsif ($use_lua_pbc) {
        $ENV{PARROT_LUA_TEST_PROG} = 'lua.pbc';
    }
    elsif ($use_luap_pir) {
        $ENV{PARROT_LUA_TEST_PROG} = 'luap.pir';
    }
    elsif ($use_luac2pir_pir) {
        $ENV{PARROT_LUA_TEST_PROG} = 'luac2pir.pir';
    }

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

              # 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 = ['lua_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}  = 999;
            $env_data{test_prog}   = $ENV{PARROT_LUA_TEST_PROG};
            Parrot::Harness::Smoke::send_archive_to_smolder(%env_data);
        }
    }
    else {
        my $harness = TAP::Harness->new(
            {
                exec       => $exec_sub,
                verbosity  => $verbosity,
            }
        );
        $harness->runtests(@files);
    }
}

=head1 HISTORY

Mostly taken from F<languages/bc/t/harness>.

=head1 SEE ALSO

F<languages/scheme/t/harness>, F<languages/python/t/harness>

=cut

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

