-
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
Changes from 9 commits
dca420a
8ffe23b
d0d8cef
b563c50
67c73d0
e581c13
525aca3
3991d01
a9dd6d8
a9f784a
1996975
c52cd73
10cb94f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<Project> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<Project> | ||
</Project> |
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; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// 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) | ||
{ | ||
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) => | ||
{ | ||
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) { } | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace RunTests | ||
{ | ||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
if (args.Length < 7) | ||
{ | ||
Console.WriteLine($"Expected at least 7 args, got {args.Length}."); | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Environment.Exit(1); | ||
return; | ||
} | ||
|
||
var target = args[0]; | ||
var sdkVersion = args[1]; | ||
var runtimeVersion = args[2]; | ||
var helixQueue = args[3]; | ||
var architecture = args[4]; | ||
var quarantined = args[5]; | ||
var efVersion = args[6]; | ||
var HELIX_WORKITEM_ROOT = Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"); | ||
|
||
var path = Environment.GetEnvironmentVariable("PATH"); | ||
var dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT"); | ||
|
||
// Rename default.NuGet.config to NuGet.config if there is not a custom one from the project | ||
if (!File.Exists("NuGet.config")) | ||
{ | ||
File.Copy("default.NuGet.config", "NuGet.config"); | ||
} | ||
|
||
var environmentVariables = new Dictionary<string, string>(); | ||
environmentVariables.Add("PATH", path); | ||
environmentVariables.Add("DOTNET_ROOT", dotnetRoot); | ||
environmentVariables.Add("helix", helixQueue); | ||
|
||
Console.WriteLine($"Current Directory: {HELIX_WORKITEM_ROOT}"); | ||
var helixDir = Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"); | ||
Console.WriteLine($"Setting HELIX_DIR: {helixDir}"); | ||
environmentVariables.Add("HELIX_DIR", helixDir); | ||
environmentVariables.Add("NUGET_FALLBACK_PACKAGES", helixDir); | ||
var nugetRestore = Path.Combine(helixDir, "nugetRestore"); | ||
Console.WriteLine($"Creating nuget restore directory: {nugetRestore}"); | ||
environmentVariables.Add("NUGET_RESTORE", nugetRestore); | ||
var dotnetEFFullPath = Path.Combine(nugetRestore, $"dotnet-ef/{efVersion}/tools/netcoreapp3.1/any/dotnet-ef.exe"); | ||
Console.WriteLine($"Set DotNetEfFullPath: {dotnetEFFullPath}"); | ||
environmentVariables.Add("DotNetEfFullPath", dotnetEFFullPath); | ||
|
||
Console.WriteLine("Checking for Microsoft.AspNetCore.App"); | ||
if (Directory.Exists("Microsoft.AspNetCore.App")) | ||
{ | ||
Console.WriteLine($"Found Microsoft.AspNetCore.App, copying to {dotnetRoot}/shared/Microsoft.AspNetCore.App/{runtimeVersion}"); | ||
foreach (var file in Directory.EnumerateFiles("Microsoft.AspNetCore.App", "*.*", SearchOption.AllDirectories)) | ||
{ | ||
File.Copy(file, $"{dotnetRoot}/shared/Microsoft.AspNetCore.App/{runtimeVersion}", overwrite: true); | ||
} | ||
|
||
Console.WriteLine($"Adding current directory to nuget sources: {HELIX_WORKITEM_ROOT}"); | ||
|
||
await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
$"nuget add source {HELIX_WORKITEM_ROOT} --configfile NuGet.config", | ||
environmentVariables: environmentVariables); | ||
|
||
await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
"nuget add source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json --configfile NuGet.config", | ||
environmentVariables: environmentVariables); | ||
|
||
await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
"nuget list source", | ||
environmentVariables: environmentVariables); | ||
|
||
await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
$"tool install dotnet-ef --global --version {efVersion}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Global? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
environmentVariables: environmentVariables); | ||
|
||
path += $";{Environment.GetEnvironmentVariable("DOTNET_CLI_HOME")}/.dotnet/tools"; | ||
environmentVariables["PATH"] = path; | ||
} | ||
|
||
Directory.CreateDirectory(nugetRestore); | ||
|
||
// Rename default.runner.json to xunit.runner.json if there is not a custom one from the project | ||
if (!File.Exists("xunit.runner.json")) | ||
{ | ||
File.Copy("default.runner.json", "xunit.runner.json"); | ||
} | ||
|
||
Console.WriteLine(); | ||
Console.WriteLine("Displaying directory contents"); | ||
foreach (var file in Directory.EnumerateFiles("./")) | ||
{ | ||
Console.WriteLine(Path.GetFileName(file)); | ||
} | ||
Console.WriteLine(); | ||
|
||
var discoveryResult = await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
$"vstest {target} -lt", | ||
environmentVariables: environmentVariables); | ||
|
||
if (discoveryResult.StandardOutput.Contains("Exception thrown")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would this happen? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it ever in stderr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with the specifics of this |
||
{ | ||
Console.WriteLine("Exception thrown during test discovery."); | ||
Console.WriteLine(discoveryResult.StandardOutput); | ||
Environment.Exit(1); | ||
return; | ||
} | ||
|
||
var exitCode = 0; | ||
var commonTestArgs = $"vstest {target} --logger:xunit --logger:\"console;verbosity=normal\" --blame"; | ||
if (string.Equals(quarantined, "true") || string.Equals(quarantined, "1")) | ||
{ | ||
Console.WriteLine("Running quarantined tests."); | ||
|
||
// Filter syntax: https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md | ||
|
||
var result = await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
commonTestArgs + " --TestCaseFilter:\"Quarantined=true\"", | ||
environmentVariables: environmentVariables, | ||
outputDataReceived: Console.WriteLine, | ||
errorDataReceived: Console.WriteLine, | ||
throwOnError: false); | ||
|
||
if (result.ExitCode != 0) | ||
{ | ||
Console.WriteLine($"Failure in quarantined tests. Exit code: {result.ExitCode}."); | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Running non-quarantined tests."); | ||
|
||
// Filter syntax: https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md | ||
var result = await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet", | ||
commonTestArgs + " --TestCaseFilter:\"Quarantined!=true\"", | ||
environmentVariables: environmentVariables, | ||
outputDataReceived: Console.WriteLine, | ||
errorDataReceived: Console.WriteLine, | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throwOnError: false); | ||
|
||
if (result.ExitCode != 0) | ||
{ | ||
Console.WriteLine($"Failure in non-quarantined tests. Exit code: {result.ExitCode}."); | ||
exitCode = result.ExitCode; | ||
} | ||
} | ||
|
||
Console.WriteLine(); | ||
if (File.Exists("TestResults/TestResults.xml")) | ||
{ | ||
Console.WriteLine("Copying TestResults/TestResults.xml to ./testResults.xml"); | ||
File.Copy("TestResults/TestResults.xml", "testResults.xml"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("No test results found."); | ||
} | ||
|
||
var HELIX_WORKITEM_UPLOAD_ROOT = Environment.GetEnvironmentVariable("HELIX_WORKITEM_UPLOAD_ROOT"); | ||
Console.WriteLine($"Copying artifacts/log/ to {HELIX_WORKITEM_UPLOAD_ROOT}/"); | ||
if (Directory.Exists("artifacts/log")) | ||
{ | ||
foreach (var file in Directory.EnumerateFiles("artifacts/log", "*.log", SearchOption.AllDirectories)) | ||
{ | ||
// Combine the directory name + log name for the copied log file name to avoid overwriting duplicate test names in different test projects | ||
var logName = $"{Path.GetFileName(Path.GetDirectoryName(file))}_{Path.GetFileName(file)}"; | ||
Console.WriteLine($"Copying: {file} to {Path.Combine(HELIX_WORKITEM_UPLOAD_ROOT, logName)}"); | ||
File.Copy(file, Path.Combine(HELIX_WORKITEM_UPLOAD_ROOT, logName)); | ||
File.Copy(file, Path.Combine(HELIX_WORKITEM_UPLOAD_ROOT, "..", logName)); | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine("No logs found in artifacts/log"); | ||
} | ||
|
||
Console.WriteLine("Completed Helix job."); | ||
Environment.Exit(exitCode); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
Uh oh!
There was an error while loading. Please reload this page.