summaryrefslogtreecommitdiff
path: root/tests/src/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs
blob: 39281a880905ccf787f81518f6eb8158c6302453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CoreclrTestLib
{
    public class CoreclrTestWrapperLib
    {
        public const int EXIT_SUCCESS_CODE = 0;

        public int RunTest(string executable, string outputFile, string errorFile)
        {
            Debug.Assert(outputFile != errorFile);

            int exitCode = -100;
            int timeout = 1000 * 60*10;

            var outputStream = new FileStream(outputFile, FileMode.Create);
            var errorStream = new FileStream(errorFile, FileMode.Create);

            using (var outputWriter = new StreamWriter(outputStream))
            using (var errorWriter = new StreamWriter(errorStream))
            using (Process process = new Process())
            {
                process.StartInfo.FileName = executable;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;

                process.Start();

                Task copyOutput = process.StandardOutput.BaseStream.CopyToAsync(outputStream);
                Task copyError = process.StandardError.BaseStream.CopyToAsync(errorStream);

                bool completed = process.WaitForExit(timeout) &&
                    copyOutput.Wait(timeout) &&
                    copyError.Wait(timeout);

                if (completed)
                {
                    // Process completed. Check process.ExitCode here.
                    exitCode = process.ExitCode;
                }
                else
                {
                    // Timed out.
                    outputWriter.WriteLine("cmdLine:" + executable + " Timed Out");
                    errorWriter.WriteLine("cmdLine:" + executable + " Timed Out");
                }

               outputWriter.WriteLine("Test Harness Exitcode is : " + exitCode.ToString());
               outputWriter.Flush();

               errorWriter.Flush();
            }

            return exitCode;
        }

        
    }
}