-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Replace RunTests scripts with .NET app #20337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dca420a
wip
BrennanConroy 8ffe23b
stash
BrennanConroy d0d8cef
cleanup
BrennanConroy b563c50
cleanup
BrennanConroy 67c73d0
fix things
BrennanConroy e581c13
Don't throw on test run
BrennanConroy 525aca3
cleanup
BrennanConroy 3991d01
why is this casing different >.>
BrennanConroy a9dd6d8
why is there a random folder
BrennanConroy a9f784a
fb
BrennanConroy 1996975
fixes
BrennanConroy c52cd73
command options
BrennanConroy 10cb94f
fix
BrennanConroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<Project> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<Project> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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. | ||
|
||
namespace RunTests | ||
{ | ||
public class ProcessResult | ||
{ | ||
public ProcessResult(string standardOutput, string standardError, int exitCode) | ||
{ | ||
StandardOutput = standardOutput; | ||
StandardError = standardError; | ||
ExitCode = exitCode; | ||
} | ||
|
||
public string StandardOutput { get; } | ||
public string StandardError { get; } | ||
public int ExitCode { get; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace RunTests | ||
{ | ||
public static class ProcessUtil | ||
{ | ||
[DllImport("libc", SetLastError = true, EntryPoint = "kill")] | ||
private static extern int sys_kill(int pid, int sig); | ||
|
||
public static async Task<ProcessResult> RunAsync( | ||
string filename, | ||
string arguments, | ||
string? workingDirectory = null, | ||
bool throwOnError = true, | ||
IDictionary<string, string?>? environmentVariables = null, | ||
Action<string>? outputDataReceived = null, | ||
Action<string>? errorDataReceived = null, | ||
Action<int>? onStart = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
Console.WriteLine($"Running '{filename} {arguments}'"); | ||
using var process = new Process() | ||
{ | ||
StartInfo = | ||
{ | ||
FileName = filename, | ||
Arguments = arguments, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
}, | ||
EnableRaisingEvents = true | ||
}; | ||
|
||
|
||
if (workingDirectory != null) | ||
{ | ||
process.StartInfo.WorkingDirectory = workingDirectory; | ||
} | ||
|
||
if (environmentVariables != null) | ||
{ | ||
foreach (var kvp in environmentVariables) | ||
{ | ||
process.StartInfo.Environment.Add(kvp); | ||
} | ||
} | ||
|
||
var outputBuilder = new StringBuilder(); | ||
process.OutputDataReceived += (_, e) => | ||
{ | ||
if (e.Data != null) | ||
{ | ||
if (outputDataReceived != null) | ||
{ | ||
outputDataReceived.Invoke(e.Data); | ||
} | ||
else | ||
{ | ||
outputBuilder.AppendLine(e.Data); | ||
} | ||
} | ||
}; | ||
|
||
var errorBuilder = new StringBuilder(); | ||
process.ErrorDataReceived += (_, e) => | ||
{ | ||
if (e.Data != null) | ||
{ | ||
if (errorDataReceived != null) | ||
{ | ||
errorDataReceived.Invoke(e.Data); | ||
} | ||
else | ||
{ | ||
errorBuilder.AppendLine(e.Data); | ||
} | ||
} | ||
}; | ||
|
||
var processLifetimeTask = new TaskCompletionSource<ProcessResult>(); | ||
|
||
process.Exited += (_, e) => | ||
{ | ||
Console.WriteLine($"'{process.StartInfo.FileName} {process.StartInfo.Arguments}' completed with exit code '{process.ExitCode}'"); | ||
if (throwOnError && process.ExitCode != 0) | ||
{ | ||
processLifetimeTask.TrySetException(new InvalidOperationException($"Command {filename} {arguments} returned exit code {process.ExitCode}")); | ||
} | ||
else | ||
{ | ||
processLifetimeTask.TrySetResult(new ProcessResult(outputBuilder.ToString(), errorBuilder.ToString(), process.ExitCode)); | ||
} | ||
}; | ||
|
||
process.Start(); | ||
onStart?.Invoke(process.Id); | ||
|
||
process.BeginOutputReadLine(); | ||
process.BeginErrorReadLine(); | ||
|
||
var cancelledTcs = new TaskCompletionSource<object?>(); | ||
await using var _ = cancellationToken.Register(() => cancelledTcs.TrySetResult(null)); | ||
|
||
var result = await Task.WhenAny(processLifetimeTask.Task, cancelledTcs.Task); | ||
|
||
if (result == cancelledTcs.Task) | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
sys_kill(process.Id, sig: 2); // SIGINT | ||
|
||
var cancel = new CancellationTokenSource(); | ||
|
||
await Task.WhenAny(processLifetimeTask.Task, Task.Delay(TimeSpan.FromSeconds(5), cancel.Token)); | ||
|
||
cancel.Cancel(); | ||
} | ||
|
||
if (!process.HasExited) | ||
{ | ||
process.CloseMainWindow(); | ||
|
||
if (!process.HasExited) | ||
{ | ||
process.Kill(); | ||
} | ||
} | ||
} | ||
|
||
return await processLifetimeTask.Task; | ||
} | ||
|
||
public static void KillProcess(int pid) | ||
{ | ||
try | ||
{ | ||
using var process = Process.GetProcessById(pid); | ||
process?.Kill(); | ||
} | ||
catch (ArgumentException) { } | ||
catch (InvalidOperationException) { } | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.