Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,18 @@ public static string PrepareArguments(string testAssembly, string? testAdapterPa
/// </summary>
/// <param name="arguments">Arguments provided to <c>vstest.console</c>.exe</param>
/// <param name="environmentVariables">Environment variables to set to the started process.</param>
public void InvokeVsTest(string? arguments, Dictionary<string, string?>? environmentVariables = null)
/// <param name="collectDiagnostics">When true, automatically adds --diag flag and attaches logs to test results on failure.</param>
public void InvokeVsTest(string? arguments, Dictionary<string, string?>? environmentVariables = null, bool collectDiagnostics = true)
{
if (collectDiagnostics && !IsDiagAlreadyEnabled(arguments ?? ""))
{
var diagLogsDir = Path.Combine(TempDirectory.Path, "logs");
Directory.CreateDirectory(diagLogsDir);
arguments = string.Concat(arguments, GetDiagArg(diagLogsDir));
_attachments.Add(diagLogsDir);
Console.WriteLine($"Diagnostic logs directory: {diagLogsDir}");
}

var debugEnvironmentVariables = AddDebugEnvironmentVariables(environmentVariables);
ExecuteVsTestConsole(arguments, out _standardTestOutput, out _standardTestError, out _runnerExitCode, debugEnvironmentVariables);
FormatStandardOutCome();
Expand All @@ -223,7 +233,8 @@ public void InvokeVsTest(string? arguments, Dictionary<string, string?>? environ
/// <param name="arguments">Arguments provided to <c>vstest.console</c>.exe</param>
/// <param name="environmentVariables">Environment variables to set to the started process.</param>
/// <param name="workingDirectory"></param>
public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? environmentVariables = null, string? workingDirectory = null)
/// <param name="collectDiagnostics">When true, automatically adds --diag flag and attaches logs to test results on failure.</param>
public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? environmentVariables = null, string? workingDirectory = null, bool collectDiagnostics = true)
{
string globalJsonPath = Path.Combine(workingDirectory!, "global.json");
if (workingDirectory is not null && !File.Exists(globalJsonPath))
Expand Down Expand Up @@ -272,6 +283,28 @@ public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? envi
// https://github.com/dotnet/sdk/blob/main/src/Cli/dotnet/commands/dotnet-test/VSTestForwardingApp.cs#L30-L39
debugEnvironmentVariables["VSTEST_CONSOLE_PATH"] = vstestConsolePath;

if (collectDiagnostics && !IsDiagAlreadyEnabled(arguments))
{
var diagLogsDir = Path.Combine(TempDirectory.Path, "logs");
Directory.CreateDirectory(diagLogsDir);
var diagPath = Path.Combine(diagLogsDir, "log.txt");
var diagArg = " --diag " + diagPath.AddDoubleQuote();

// Insert --diag before the -- separator so dotnet test forwards it to vstest.console.
var separatorPos = arguments.IndexOf(" -- ");
if (separatorPos == -1)
{
arguments += diagArg;
}
else
{
arguments = arguments.Insert(separatorPos, diagArg);
}
Comment thread
nohwnd marked this conversation as resolved.
Resolved

_attachments.Add(diagLogsDir);
Console.WriteLine($"Diagnostic logs directory: {diagLogsDir}");
Comment thread
nohwnd marked this conversation as resolved.
Resolved
}

IntegrationTestBase.ExecutePatchedDotnet("test", arguments, out _standardTestOutput, out _standardTestError, out _runnerExitCode, debugEnvironmentVariables, workingDirectory);
FormatStandardOutCome();
}
Expand All @@ -284,14 +317,16 @@ public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? envi
/// <param name="framework">Dotnet Framework of test assembly.</param>
/// <param name="runSettings">Run settings for execution.</param>
/// <param name="environmentVariables">Environment variables to set to the started process.</param>
/// <param name="collectDiagnostics">When true, automatically adds --diag flag and attaches logs to test results on failure.</param>
public void InvokeVsTestForExecution(string testAssembly,
string? testAdapterPath,
string framework,
string? runSettings = "",
Dictionary<string, string?>? environmentVariables = null)
Dictionary<string, string?>? environmentVariables = null,
bool collectDiagnostics = true)
{
var arguments = PrepareArguments(testAssembly, testAdapterPath, runSettings, framework, _testEnvironment.InIsolationValue, resultsDirectory: TempDirectory.Path);
InvokeVsTest(arguments, environmentVariables);
InvokeVsTest(arguments, environmentVariables, collectDiagnostics);
}

private Dictionary<string, string?> AddDebugEnvironmentVariables(Dictionary<string, string?>? environmentVariables)
Expand Down Expand Up @@ -1037,6 +1072,21 @@ protected string BuildMultipleAssemblyPath(params string[] assetNames)
return string.Join(" ", GetTestDlls(assetNames).Select(a => a.AddDoubleQuote()));
}

private static bool IsDiagAlreadyEnabled(string arguments)
{
// Check args for --diag, /diag, -diag (case-insensitive)
if (arguments.Contains("--diag", StringComparison.OrdinalIgnoreCase)
|| arguments.Contains("/diag", StringComparison.OrdinalIgnoreCase)
|| arguments.Contains("-diag", StringComparison.OrdinalIgnoreCase))
{
return true;
}

// Check environment variable
var envDiag = Environment.GetEnvironmentVariable("VSTEST_DIAG");
return !StringUtils.IsNullOrEmpty(envDiag);
}

protected static string GetDiagArg(string rootDir)
=> " --diag:" + Path.Combine(rootDir, "log.txt");

Expand Down
Loading