<package>
<job id="runtests">
    <runtime>
        <description>This script searches for runtests files and executes the tests described therein</description>
        <named
            name = "echo"
            helpstring = "Echo each mpiexec command"
            required = "false"
        />
        <named
			name = "config"
			helpstring = "test configuration, Debug or Release"
			required = "false"
		/>
		<named
			name = "out"
			helpstring = "name of output file"
			required = "false"
		/>
        <example>Example: runtests.wsf</example>
    </runtime>
<script language="VBScript">

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Set f = WScript.CreateObject("Scripting.FileSystemObject")
Set cur_folder = f.GetFolder(".")
Set files = CreateObject("Scripting.Dictionary")
num_files = 0
output = ""
num_passed = 0
num_failed = 0
num_skipped = 0

' check if we are running cscript or wscript
bEcho = false
If InStr(1, LCase(WScript.FullName), "cscript") > 0 Then
	bEcho = true
End If

' check config option
config = "Debug\"
if WScript.Arguments.Named.Exists("config") then
	config = WScript.Arguments.Named.Item("config") + "\"
end if

' check out option
outfile_name = "summary.xml"
if WScript.Arguments.Named.Exists("out") then
	outfile_name = WScript.Arguments.Named.Item("out")
end if

set ftmp = f.CreateTextFile(outfile_name)
ftmp.WriteLine("<?xml version='1.0' ?>")
ftmp.WriteLine("<?xml-stylesheet href=""../TestResults.xsl"" type=""text/xsl"" ?>")
ftmp.WriteLine("<MPITESTRESULTS>")
ftmp.WriteLine("<DATE>" & Date)
ftmp.WriteLine("</DATE>")
ftmp.WriteLine("<MPISOURCE></MPISOURCE>")
ftmp.Close()

Function GetTestLists(folder)
	For Each sub_folder in folder.SubFolders
		GetTestLists(sub_folder)
	Next
	For Each file in folder.Files
		If file.Name = "testlist" Then
			files.Add num_files, file.Path
			num_files = num_files + 1
		End If
	Next
End Function

Function RunTest(cmd_line)
	Set oExec = WshShell.Exec("cmd /c " & cmd_line & " 2>&1") 'WshShell.Exec(cmd_line)
	cmd_output = oExec.StdOut.ReadAll()
	cmd_output = cmd_output & oExec.StdErr.ReadAll()
	RunTest = cmd_output
End Function

Function RunTests(filename)
	Set fin = f.OpenTextFile(filename)
	while not fin.AtEndOfStream
		line = fin.ReadLine()
		line = Trim(line)
		twostrings = split(line, " ", -1, 1)
		if not IsNull(twostrings) then
			count = 0
			for each s in twostrings
				count = count + 1
			next
			if count = 2 then
			 if Left(twostrings(0), 1) <> "#" then
			  if Len(twostrings(1)) > 0 then
				exe = Replace( filename, "testlist", config + twostrings(0) + ".exe" )
				cmd = "mpiexec -timeout 180 -n " + twostrings(1) + " " + exe
				if bEcho = true then
			 		WScript.Echo cmd '+ vbCrLf
				end if
				set fout = f.OpenTextFile(outfile_name, 8, True)
				'fout.Write( cmd + vbCrLf )
				fout.WriteLine("<MPITEST>")
				fout.WriteLine("<NAME>" & twostrings(0) & "</NAME>")
				if f.FileExists(exe) then
					result = RunTest(cmd)
				else
					result = "no executable, test skipped"
					num_skipped = num_skipped + 1
				end if
				if bEcho = true then
					WScript.Echo result '+ vbCrLf
				end if
				'fout.Write( result + vbCrLf )
				if Instr(result, " No Errors") And len(result) < 14 Then
					fout.WriteLine("<STATUS>pass</STATUS>")
					num_passed = num_passed + 1
				else
					fout.WriteLine("<STATUS>fail</STATUS>")
					fout.WriteLine("<TESTDIFF>")
					result = cmd & vbCrLf & result
					result = Replace( result, "&", "&amp;" )
					result = Replace( result, "<", "&lt;" )
					result = Replace( result, ">", "&gt;" )
					fout.WriteLine(result)
					fout.WriteLine("</TESTDIFF>")
					if f.FileExists(exe) then
						num_failed = num_failed + 1
					end if
				end if
				fout.WriteLine("</MPITEST>")
				fout.Close()
			  end if
			 end if
			end if
		end if
	wend
	fin.Close()
End Function

GetTestLists(cur_folder)
a = files.Items
If files.Count > 0 Then
	For i = 0 to files.Count - 1
		RunTests a(i)
	Next
End If

set fout = f.OpenTextFile(outfile_name, 8, True)
fout.WriteLine("</MPITESTRESULTS>")
fout.Close()

if bEcho = true then
	WScript.Echo "num_passed = " & num_passed
	WScript.Echo "num_failed = " & num_failed
	WScript.Echo "num_skipped = " & num_skipped
end if

</script>
</job>
</package>
