#!/usr/bin/perl

# This is a proof of concept in terms of rewriting sanity.sh in perl
# (as suggested in the TESTS file).

# Future directions/tasks:
#   * Extend this to include a slightly larger set of tests (e.g. the
#   whole basica test from sanity.sh) and also a larger set of test
#   needs (e.g. tests which supply input on stdin.
#   * Try it in another language (tcl and python spring to mind, but
#   pick your favorite language).
#   * Try running it on at least one non-unix platform.
#   * Someday, think about maybe replacing/supplementing sanity.sh
#   (depending on how this is done, might involve a lot of people and
#   the opinions thereof).


sub fail {
    print "FAIL: ", $_[0], "\n";
    # This way the tester can go and see what remnants were left
    die;
}

sub pass {
    print "PASS: ", $_[0], "\n";
}

sub dotest {
    local ($output) = "";
    local ($_);
    local ($status);

    # Does 2>&1 work on Windows?  Is there another way?
    open (CMD, $_[1] . " 2>&1 |") || die "cannot open: $!";
    while (<CMD>) {
	$output = $output . $_;
    }
    close CMD || die "cannot close: $!";
    $status = $? >> 8;

    if ($status) {
	print "exit status was ", $status, "\n";
	fail $_[0];
    } elsif ($output =~ /$_[2]/) {
	pass $_[0];
    } else {
	print "** expected:\n";
	print $_[2];
	print "** got:\n";
	print $output;
	fail $_[0];
    }
}

dotest 1, "echo foo", "foo\n";
dotest 2, "cvs --version", "
Concurrent Versions System \\(CVS\\) 1.9.26.1 \\(client/server\\)

Copyright \\(c\\) 1989-1998 Brian Berliner, david d `zoo' zuhn, 
                        Jeff Polk, and other authors

CVS may be copied only under the terms of the GNU General Public License,
a copy of which can be found with the CVS distribution kit.

Specify the --help option for further information about CVS
";
