
load_lib "dejagnu.exp"


# If tracing has been enabled at the top level, then turn it on here
# too.
if $tracelevel {
    strace $tracelevel
}

# After these many seconds of execution the test script is aborted
# with a failure.
# This is to handle deadlocks. We don't reset the timeout when a match is
# found to avoid hanging in case of a testcase sending matches in an
# infinite loops.
# (not unlikely as it seems, think about flash movies...)
#
# Expressed in seconds.
#
set timeout 300
set file all
set params ""

global env

# testcases is set by the Makefile in the site.exp data file.
foreach file $testcases {

    verbose "Running test $file"

    # spawn the executable and look for the DejaGnu output messages from the
    # test case.

    # this version of the call allows use of 'wait' to check return code
    # -open [open "|cmd" "r"] doesn't work for that
  
    # Ignore SIGHUP or we'd get a lot of them on Debian stable
    set PID [spawn -noecho -ignore SIGHUP ./$file]

    expect {
	-re "^\[^\n]*NOTE:\[^\n]*\n" {
	    regsub ".*NOTE: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    verbose "${file} $output" 
	    # notes tipically come from the test runner, so we'll trust it to mean 
	    # things are someone not too bad...
	    # -continue_timer
	    exp_continue
	}
	-re "^\[^\n]* ERROR:\[^\n]*\n" {
	    regsub ".* ERROR: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    verbose "ERROR: ${file}: $output" 
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*XPASSED:\[^\n]*\n" {
	    regsub ".*XPASSED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    xpass "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*PASSED:\[^\n]*\n" {
	    regsub ".*PASSED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    pass "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*XFAILED:\[^\n]*\n" {
	    regsub ".*XFAILED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2] 
	    xfail "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*FAILED:\[^\n]*\n" {
	    regsub ".*FAILED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2] 
	    fail "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*UNTESTED:\[^\n]*\n" {
	    regsub ".*UNTESTED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    untested "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*UNRESOLVED:\[^\n]*\n" {
	    regsub ".*UNRESOLVED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    unresolved "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*\n" {
            # just remove non-matching lines!
            exp_continue -continue_timer
	}
	eof {
	    #	    unresolved "${file} died prematurely"
	    #	    catch close
	    #	    return "${file} died prematurely"
	}
	timeout {
		fail "Test case ${file} still running after ${timeout} seconds, killing it (deadlock?)"
		exec kill -9 $PID
		catch close
		continue;
	}
   }

	# wait for the process to coplete to
	# check return code
	set retcode [wait]

	# debugging
	#set i 0; foreach j $retcode { print "${file} wait($i) $j"; incr i }

	# This snippet catches segfaults and aborts.
	# Would also catch SIGHUP, but we're ignoring them
	# as on Debian Stable we unexpectedly get them for no apparent reason
	#
	if { [ llength $retcode ] > 5 } {
		fail "${file} died prematurely ([lindex $retcode 6])"
	}

	# This snippet catches non-zero returns
	if { [ lindex $retcode 3 ] != 0 } {
		fail "${file} exited with non-zero code ([lindex $retcode 3])"
	}

    # force a close of the executable to be safe.
    catch close
}


