#!/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;

# Override the users default .darcs settings
$ENV{HOME} = cwd();
mkdir '.darcs';
system 'echo ALL --ignore-times >> .darcs/defaults';
# Used for finding darcs, but may not be defined by the shell
$ENV{PWD} = cwd();

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

# Some environment variables can act as defaults that we don't want
delete $ENV{EMAIL};
delete $ENV{DARCS_EMAIL};

# 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;

for 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 "No bugs!\n";
}
else {
  print "Still have bugs in\n";
  print "\t$_\n" for @Failures;
}

exit 0;
