#!/usr/bin/perl -w

# Run each of the shell tests, capturing the output, and reporting passed 
# or failed.

use Cwd;

# Place to put test output to avoid cluttering test/
mkdir 'test_output', 0750;

# Intervene the users default .darcs settings
$ENV{HOME} = cwd();

# Put a false darcs first in PATH
$ENV{PATH} = "$ENV{HOME}/bin:$ENV{PATH}";
chmod 0755, 'bin/darcs';

# These two environment variables will turn off darcs' "Christmas mode".
# It will make the tests run a tad faster, and make darcs' output
# independent of the testing systems locale and environment.
$ENV{DARCS_DONT_COLOR} = 1;
$ENV{DARCS_DONT_ESCAPE_ANYTHING} = 1;

my $OK = 1;
my @Failures;

foreach my $test (@ARGV) {
    my $test_out = "test_output/$test.out";

    printf "Running %-40s", "$test ...";

    my $output = `sh $test 2>&1`;

    if( $? == 0 ) {
	print " passed.\n";
    }
    else {
	$OK = 0;
	push @Failures, $test;

	print " FAILED!\n";
	print "Output from failed $test:\n$output";
    }
}

if( $OK ) {
    print "All tests successful!\n";
}
else {
    print "TESTS FAILED!\n";
    print "\t$_\n" for @Failures;
}

# Exit with non-zero if anything failed.
exit !$OK;
