-
Notifications
You must be signed in to change notification settings - Fork 9
/
ExecuteHelper.cs
115 lines (99 loc) · 3.76 KB
/
ExecuteHelper.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Text;
using Xunit.Abstractions;
namespace Microsoft.DotNet.ScenarioTests.Common;
public static class ExecuteHelper
{
public static (Process Process, string StdOut, string StdErr) ExecuteProcess(
string fileName,
string args,
ITestOutputHelper outputHelper,
bool logOutput = false,
Action<Process>? configure = null,
int millisecondTimeout = -1)
{
outputHelper.WriteLine($"Executing: {fileName} {args}");
Process process = new()
{
EnableRaisingEvents = true,
StartInfo =
{
FileName = fileName,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
}
};
configure?.Invoke(process);
StringBuilder stdOutput = new();
process.OutputDataReceived += new DataReceivedEventHandler(
(sender, e) =>
{
lock (stdOutput)
{
stdOutput.AppendLine(e.Data);
}
});
StringBuilder stdError = new();
process.ErrorDataReceived += new DataReceivedEventHandler(
(sender, e) =>
{
lock (stdError)
{
stdError.AppendLine(e.Data);
}
});
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(millisecondTimeout);
if (!process.HasExited)
{
outputHelper.WriteLine($"Killing: {fileName} {args}");
process.Kill(true);
process.WaitForExit();
ProcessStartInfo startInfo = process.StartInfo;
string msg = $" {startInfo.FileName} {startInfo.Arguments} timed out after " +
$"{millisecondTimeout} milliseconds" +
$"{Environment.NewLine}Exit code: {process.ExitCode}";
throw new InvalidOperationException(msg);
}
string output = stdOutput.ToString().Trim();
if (logOutput && !string.IsNullOrWhiteSpace(output))
{
outputHelper.WriteLine(output);
}
string error = stdError.ToString().Trim();
if (logOutput && !string.IsNullOrWhiteSpace(error))
{
outputHelper.WriteLine(error);
}
return (process, output, error);
}
public static string ExecuteProcessValidateExitCode(string fileName, string args, ITestOutputHelper outputHelper)
{
(Process Process, string StdOut, string StdErr) result = ExecuteHelper.ExecuteProcess(fileName, args, outputHelper);
ValidateExitCode(result);
return result.StdOut;
}
public static void ValidateExitCode((Process Process, string StdOut, string StdErr) result, int expectedExitCode = 0)
{
if (result.Process.ExitCode != expectedExitCode)
{
ProcessStartInfo startInfo = result.Process.StartInfo;
string msg = $"Failed to execute {startInfo.FileName} {startInfo.Arguments}" +
$"{Environment.NewLine}Exit code: {result.Process.ExitCode}" +
$"{Environment.NewLine}{result.StdOut}" +
$"{Environment.NewLine}{result.StdErr}";
var ex = new InvalidOperationException(msg);
if (result.StdErr.Contains("Microsoft.AspNetCore.Connections.AddressInUseException"))
{
ex.Data["IsAddressInUseException"] = true;
}
throw ex;
}
}
}