#!/usr/bin/perl
#
#  This is a perl script test harness which will recursively search the
#  directory tree looking for files named UnitTest and then execute
#  the script
#
#  SYNOSIS :  FullUnitTest options arguements
#
#       where  options =
#             --verbose     Display extra information to user
#             --noverbose   Don't display extra information to user
#             --recursiv e  Recursively run tests in directory tree
#             --norecursive Test only the specified or current directory
#             --silent      Don't display any information to user
#             --nosilent    Display progress of script to user
#
#              arguements = directory(ies) to perform tests
#
#  RETURN : integer number of tests which failed
#
#  $Revision: 1.22 $  $Name: not supported by cvs2svn $
#  $Date: 2005-04-09 00:04:57 $
#
#  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
#
##############################################################################

use FindBin qw($Bin);

$runTest = "$Bin/runTest";

# Provide functions for determining the pathname of current working directory
use Cwd;

# Provides functions for handling psS64 command line options
use Getopt::Long;

# Assign variables based on the presence of command line options to the script
# The ! option allows for --nooption to be set to zero
# (e.g. --noverbose --recursive causes $verbose=0 and $recursive=1)
GetOptions(
    "verbose!"   => \$verbose,
    "recursive!" => \$recursive,
    "silent!"    => \$silent,
    "clean!"     => \$clean
);

# Check if both silent and verbose options are set and if so stop the script
die "Can't specify both verbose and silent options." if ( $verbose && $silent );

# Set up the PSLIB_ROOT environment variable if the user doesn't have
if ( !$ENV{'PSLIB_ROOT'} ) {

    # Use the directory directly above where FullUnitTest script resides
    $PSLIB_ROOT = `cd ..;pwd`;

    # Remove newline for the end of path returned
    chomp($PSLIB_ROOT);

    # Set the environment variable
    $ENV{'PSLIB_ROOT'} = $PSLIB_ROOT;
    print("PSLIB_ROOT not found: set to $PSLIB_ROOT.\n") if $verbose;
}

# add PSLIB_ROOT/lib to LD_LIBRARY_PATH and DYLD_LIBRARY_PATH environment
# variables
$ENV{'LD_LIBRARY_PATH'}   = "$ENV{'PSLIB_ROOT'}/src/.libs:$ENV{'LD_LIBRARY_PATH'}";
$ENV{'DYLD_LIBRARY_PATH'} = "$ENV{'PSLIB_ROOT'}/src/.libs:$ENV{'DYLD_LIBRARY_PATH'}";

# Initialize variables for counting the makes and test failures and the
# total makes and tests performed
$makeFailCount       = 0;
$testpointFailCount  = 0;
$testDriverFailCount = 0;
$totalTestpoints     = 0;
$totalTestDrivers    = 0;
$totalMakes          = 0;

# Initialize variable indicating how many arguements were passed
$args = 0;

# Loop through all the arguements passed to the script
foreach (@ARGV) {

    # Remove newline if there is one
    chomp;

    # Increment number of arguements found
    $args++;

    # Set variable to current working directory
    $cwd = cwd;

    # Change directory to the directory of the arguement
    chdir($_);

    # Check for the recursive option
    if ($recursive) {

        # Invoke subroutine to go to the lowest directory in tree
        # starting at the specified directory
        &worm($_);
    }
    else {

        # Invoke subroutines to run the test at the specified directory
        &makeTestDrivers($_);
        &executeTestDrivers($_);
    }

    # Change directory back to the directory where FullUnitTest was invoked
    chdir($cwd);
}

# Check if there were no arguements specified
if ( $args == 0 ) {

    # Display message to user that all directories under the current will be
    # tested
    print("Recursively testing current directory tree.\n") if $verbose;

    # Invoke subroutine to go to recursively test each directory in tree
    &worm( $ENV{"PWD"} );
}

# Check if there were any failures during make or testing
if ( $makeFailCount > 0 || $testDriverFailCount > 0 ) {

    # Display summary of failures
    print("\nMake Failures = $makeFailCount out of $totalMakes");
    print(
"\nTest Driver Failures = $testDriverFailCount out of $totalTestDrivers\n"
    );
    print( "\nMakes that failed:\n  " . join( "\n  ", @makesFailed ) . "\n" )
      if $makeFailCount;
    print( "\nTests that failed:\n  " . join( "\n  ", @testsFailed ) . "\n" )
      if $testDriverFailCount;
}
else {

    # Display message of all makes and tests pass to user if silent option
    # not specified
    print(
"\nAll $totalTestDrivers Test Drivers Passed with $totalTestpoints Testpoints.\n"
      )
      if ( !$silent );
}

# Exit with the number of tests that failed
exit($testDriverFailCount);

################################################################################
#
# SUBROUTINE: worm
#
#     Description:  This subroutine will perform the necessary unit tests be
#                   calling makeTestDrivers and executeTestDrivers  subroutines
#                   and then check each subdirectory below the base directory
#                   recursively.
#
#     Parameter(s):  base directory to start testing
#
#     Return:  None
#
################################################################################

sub worm {

    # Assign local variable to input parameter
    local ($base_dir) = @_;
    local ( @files, $i );

    # Invoke subroutine to make test driver in the base directory
    &makeTestDrivers($base_dir);

    # Invoke subroutine to execute tests in the base directory
    &executeTestDrivers($base_dir);

    # Create array of entries found in directory
    @files = <*>;

    # Loop through the file list looking for another directory that is not
    # labelled CVS and recursively invoke subroutine worm
    $i = 0;
    while ( $files[$i] ) {

        # Check for directory and directory not labelled CVS
        if (   -d $files[$i]
            && ( $files[$i] ne "CVS" )
            && ( $files[$i] ne "temp" )
            && ( $files[$i] ne "verified" ) )
        {

            # Change current directory to directory found
            chdir( $files[$i] );

            # Invoke subroutine worm again
            &worm( $files[$i] );

            # Change current directory back to parent
            chdir("..");
        }

        # Increment file list index
        $i++;
    }
}

################################################################################
#
#  SUBROUTINE: makeTestDrivers
#
#      Description:  This subroutine will perform a make for all the necessary
#                    test drivers.    This will occur in the specified
#                    base directory which is passed as the only parameter.
#
#      Parameter(s):  base directory where make and test drivers are located
#
#      Return:  None
#
################################################################################

sub makeTestDrivers {
    local ($base_dir) = @_;
    local ($pwd);

    # Set variable pwd to current working directory
    $pwd = cwd;

    # Display message to user in which directory testing is taken place only
    # if the silent command option was not specified
    print("---- Entering $pwd ----\n") if ( !$silent );

    # Check for the existence of a file name Makefile in current directory
    if ( -e "Makefile" ) {

        # Increment the total number of makes executed
        $totalMakes++;

        if ($clean) {

            # Execute the make clean
            `make clean`;

        }

        # Execute the make and save results
        $_ = join( "\n|| ", split( "\n", "\n" . `make tests` ) );

        # Check the output of make for return value != 0 or any of the
        # following words: FAILED, FAULT, ERROR, Not found, SIGNAL
        if ( $? != 0 ) {

            # Display the errored output of make if silent option not enabled
            print("$_\n") if ( !$silent );

            # Display the make failed in the current directory
            print("\nMake for $pwd Failed\n");

            # Increment the number of makes that have failed
            $makeFailCount++;

            # Push the current working directory onto the list of failed
            # make directories list
            push( @makesFailed, $pwd );
        }
        else {

            # Display the results of the successful make if verbose option set
            print("$_\n") if $verbose;

            # Display the make was successful if silent option not set
            print("\nMake successful.\n") if ( !$silent );
        }
    }
}

################################################################################
#
#  SUBROUTINE: executeTestDrivers
#
#      Description:  This subroutine will execute all the necessary
#                    test drivers.    This will occur in the specified
#                    base directory which is passed as the only parameter.
#
#      Parameter(s):  base directory where make and test drivers are located
#
################################################################################

sub executeTestDrivers {
    local ($base_dir) = @_;
    local ( @files, $j );
    local ($pwd);
    local ($exitValue) = 0;

    # Set variable to pwd to current test directory
    $pwd = cwd;

    # Create a list of all elements in base directory
    @files = <*>;

    # Loop through all the items in the files array
    $initialTest = 0;
    $j           = 0;
    while ( $files[$j] ) {

        # Check that the item is not a directory and is executable and
        # conforms to the test driver naming convention TST
        if (   !( -d $files[$j] )
            && ( -x $files[$j] )
            && ( $files[$j] =~ /^TST/i || $files[$j] =~ /^ATST/i ) )
        {

            # Increment total number of test drivers executed
            $totalTestDrivers++;

            # Display message to user of which test driver is being executed
            print("--- Executing test driver $files[$j]\n") if ( !$silent );

            # Execute the test driver
            if ($silent) {
                system("$runTest --quiet ./$files[$j]");
            } elsif ($verbose) {
                system("$runTest --printpassfail ./$files[$j]");
            } else {
                system("$runTest ./$files[$j]");
            }
            $retVal = $?;

            # Count testpoints
            $testPattern = "\"\\*\\*\\*\\* TESTPOINT \\*\\*\\*\\*\"";
            $totalPoints = `grep -c $testPattern temp/$files[$j].stdout`;
            $totalPoints += `grep -c $testPattern temp/$files[$j].stderr`;
            $failPoints =
              `grep "> TESTPOINT FAILED" temp/$files[$j].stdout | wc -l`;
            $failPoints +=
              `grep "> TESTPOINT FAILED" temp/$files[$j].stderr | wc -l`;
            $testpointFailCount += $failPoints;
            $totalTestpoints    += $totalPoints;

            # Check result of test driver
            if ( $retVal != 0 )
            {

                # Display test driver failed
                $failPoints++;
                print(
                    "Test driver: $files[$j] Failed (Return value $retVal).\n");

                # Increment the total number of test failed
                $testDriverFailCount++;

                # Push the current working directory on the list of failed
                push( @testsFailed, $pwd . "/" . $files[$j] );
            }
        }
        $j++;
    }
}

