Skip to content
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

Add Logger and MSBuild --verbosity option to dotnet test #46909

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/Cli/dotnet/commands/dotnet-test/CliConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal static class CliConstants
public const string DLLExtension = ".dll";

public const string MTPTarget = "_MTPBuild";

public const string TestTraceLoggingEnvVar = "DOTNET_CLI_TEST_TRACEFILE";
}

internal static class TestStates
Expand Down
43 changes: 43 additions & 0 deletions src/Cli/dotnet/commands/dotnet-test/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli;

namespace Microsoft.DotNet.Tools.Test
{
internal static class Logger
{
public static bool TraceEnabled { get; private set; }
private static readonly string _traceFilePath;
private static readonly object _lock = new();

static Logger()
{
_traceFilePath = Environment.GetEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar);
TraceEnabled = !string.IsNullOrEmpty(_traceFilePath);
}

public static void LogTrace(Func<string> messageLog)
{
if (!TraceEnabled)
{
return;
}

try
{
string message = $"[dotnet test - {DateTimeOffset.UtcNow:MM/dd/yyyy HH:mm:ss.fff}]{messageLog()}";

lock (_lock)
{
using StreamWriter logFile = File.AppendText(_traceFilePath);
logFile.WriteLine(message);
}
}
catch (Exception ex)
{
Console.WriteLine($"[dotnet test - {DateTimeOffset.UtcNow:MM/dd/yyyy HH:mm:ss.fff}]{ex}");
}
}
}
}
18 changes: 7 additions & 11 deletions src/Cli/dotnet/commands/dotnet-test/MSBuildHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,19 @@ public bool EnqueueTestApplications()

private void LogProjectProperties(IEnumerable<Module> modules)
{
if (!VSTestTrace.TraceEnabled)
if (!Logger.TraceEnabled)
{
return;
}

foreach (var module in modules)
{
Console.WriteLine();

VSTestTrace.SafeWriteTrace(() => $"{ProjectProperties.ProjectFullPath}: {module.ProjectFullPath}");
VSTestTrace.SafeWriteTrace(() => $"{ProjectProperties.IsTestProject}: {module.IsTestProject}");
VSTestTrace.SafeWriteTrace(() => $"{ProjectProperties.IsTestingPlatformApplication}: {module.IsTestingPlatformApplication}");
VSTestTrace.SafeWriteTrace(() => $"{ProjectProperties.TargetFramework}: {module.TargetFramework}");
VSTestTrace.SafeWriteTrace(() => $"{ProjectProperties.TargetPath}: {module.TargetPath}");
VSTestTrace.SafeWriteTrace(() => $"{ProjectProperties.RunSettingsFilePath}: {module.RunSettingsFilePath}");

Console.WriteLine();
Logger.LogTrace(() => $"{ProjectProperties.ProjectFullPath}: {module.ProjectFullPath}");
Logger.LogTrace(() => $"{ProjectProperties.IsTestProject}: {module.IsTestProject}");
Logger.LogTrace(() => $"{ProjectProperties.IsTestingPlatformApplication}: {module.IsTestingPlatformApplication}");
Logger.LogTrace(() => $"{ProjectProperties.TargetFramework}: {module.TargetFramework}");
Logger.LogTrace(() => $"{ProjectProperties.TargetPath}: {module.TargetPath}");
Logger.LogTrace(() => $"{ProjectProperties.RunSettingsFilePath}: {module.RunSettingsFilePath}");
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Cli/dotnet/commands/dotnet-test/MSBuildUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static BuildOptions GetBuildOptions(ParseResult parseResult, int degreeOf
buildProperties,
parseResult.GetValue(CommonOptions.NoRestoreOption),
parseResult.GetValue(TestingPlatformOptions.NoBuildOption),
parseResult.HasOption(CommonOptions.VerbosityOption) ? parseResult.GetValue(CommonOptions.VerbosityOption) : null,
degreeOfParallelism,
unmatchedTokens,
msbuildArgs);
Expand Down Expand Up @@ -110,6 +111,11 @@ private static bool BuildOrRestoreProjectOrSolution(string filePath, BuildOption
{
List<string> msbuildArgs = [.. buildOptions.MSBuildArgs];

if (buildOptions.Verbosity is null)
{
msbuildArgs.Add($"-verbosity:quiet");
}

msbuildArgs.Add(filePath);
msbuildArgs.Add($"-target:{CliConstants.MTPTarget}");

Expand Down
2 changes: 1 addition & 1 deletion src/Cli/dotnet/commands/dotnet-test/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ internal record PathOptions(string ProjectPath, string SolutionPath, string Dire

internal record BuildProperties(string Configuration, string RuntimeIdentifier, string TargetFramework);

internal record BuildOptions(PathOptions PathOptions, BuildProperties BuildProperties, bool HasNoRestore, bool HasNoBuild, int DegreeOfParallelism, List<string> UnmatchedTokens, IEnumerable<string> MSBuildArgs);
internal record BuildOptions(PathOptions PathOptions, BuildProperties BuildProperties, bool HasNoRestore, bool HasNoBuild, VerbosityOptions? Verbosity, int DegreeOfParallelism, List<string> UnmatchedTokens, IEnumerable<string> MSBuildArgs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public void OnHandshakeReceived(object sender, HandshakeArgs args)
_executions[testApplication] = appInfo;
_output.AssemblyRunStarted(appInfo.ModulePath, appInfo.TargetFramework, appInfo.Architecture, appInfo.ExecutionId);

if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

foreach (var property in args.Handshake.Properties)
{
VSTestTrace.SafeWriteTrace(() => $"{GetHandshakePropertyName(property.Key)}: {property.Value}");
Logger.LogTrace(() => $"{GetHandshakePropertyName(property.Key)}: {property.Value}");
}
}

Expand Down Expand Up @@ -65,12 +65,12 @@ public void OnDiscoveredTestsReceived(object sender, DiscoveredTestEventArgs arg
test.Uid);
}

if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

VSTestTrace.SafeWriteTrace(() => $"DiscoveredTests Execution Id: {args.ExecutionId}");
Logger.LogTrace(() => $"DiscoveredTests Execution Id: {args.ExecutionId}");
foreach (var discoveredTestMessage in args.DiscoveredTests)
{
VSTestTrace.SafeWriteTrace(() => $"DiscoveredTest: {discoveredTestMessage.Uid}, {discoveredTestMessage.DisplayName}");
Logger.LogTrace(() => $"DiscoveredTest: {discoveredTestMessage.Uid}, {discoveredTestMessage.DisplayName}");
}
}

Expand Down Expand Up @@ -108,20 +108,20 @@ public void OnTestResultsReceived(object sender, TestResultEventArgs args)
errorOutput: null);
}

if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

VSTestTrace.SafeWriteTrace(() => $"TestResults Execution Id: {args.ExecutionId}");
Logger.LogTrace(() => $"TestResults Execution Id: {args.ExecutionId}");

foreach (SuccessfulTestResult successfulTestResult in args.SuccessfulTestResults)
{
VSTestTrace.SafeWriteTrace(() => $"SuccessfulTestResult: {successfulTestResult.Uid}, {successfulTestResult.DisplayName}, " +
Logger.LogTrace(() => $"SuccessfulTestResult: {successfulTestResult.Uid}, {successfulTestResult.DisplayName}, " +
$"{successfulTestResult.State}, {successfulTestResult.Duration}, {successfulTestResult.Reason}, {successfulTestResult.StandardOutput}," +
$"{successfulTestResult.ErrorOutput}, {successfulTestResult.SessionUid}");
}

foreach (FailedTestResult failedTestResult in args.FailedTestResults)
{
VSTestTrace.SafeWriteTrace(() => $"FailedTestResult: {failedTestResult.Uid}, {failedTestResult.DisplayName}, " +
Logger.LogTrace(() => $"FailedTestResult: {failedTestResult.Uid}, {failedTestResult.DisplayName}, " +
$"{failedTestResult.State}, {failedTestResult.Duration}, {failedTestResult.Reason}, {string.Join(", ", failedTestResult.Exceptions?.Select(e => $"{e.ErrorMessage}, {e.ErrorType}, {e.StackTrace}"))}" +
$"{failedTestResult.StandardOutput}, {failedTestResult.ErrorOutput}, {failedTestResult.SessionUid}");
}
Expand All @@ -140,31 +140,31 @@ public void OnFileArtifactsReceived(object sender, FileArtifactEventArgs args)
artifact.TestDisplayName, artifact.FullPath);
}

if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

VSTestTrace.SafeWriteTrace(() => $"FileArtifactMessages Execution Id: {args.ExecutionId}");
Logger.LogTrace(() => $"FileArtifactMessages Execution Id: {args.ExecutionId}");

foreach (FileArtifact fileArtifactMessage in args.FileArtifacts)
{
VSTestTrace.SafeWriteTrace(() => $"FileArtifact: {fileArtifactMessage.FullPath}, {fileArtifactMessage.DisplayName}, " +
Logger.LogTrace(() => $"FileArtifact: {fileArtifactMessage.FullPath}, {fileArtifactMessage.DisplayName}, " +
$"{fileArtifactMessage.Description}, {fileArtifactMessage.TestUid}, {fileArtifactMessage.TestDisplayName}, " +
$"{fileArtifactMessage.SessionUid}");
}
}

public void OnSessionEventReceived(object sender, SessionEventArgs args)
{
if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

var sessionEvent = args.SessionEvent;
VSTestTrace.SafeWriteTrace(() => $"TestSessionEvent: {sessionEvent.SessionType}, {sessionEvent.SessionUid}, {sessionEvent.ExecutionId}");
Logger.LogTrace(() => $"TestSessionEvent: {sessionEvent.SessionType}, {sessionEvent.SessionUid}, {sessionEvent.ExecutionId}");
}

public void OnErrorReceived(object sender, ErrorEventArgs args)
{
if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

VSTestTrace.SafeWriteTrace(() => args.ErrorMessage);
Logger.LogTrace(() => args.ErrorMessage);
}

public void OnTestProcessExited(object sender, TestProcessExitEventArgs args)
Expand All @@ -180,21 +180,21 @@ public void OnTestProcessExited(object sender, TestProcessExitEventArgs args)
_output.AssemblyRunCompleted(testApplication.Module.TargetPath ?? testApplication.Module.ProjectFullPath, testApplication.Module.TargetFramework, architecture: null, null, args.ExitCode, string.Join(Environment.NewLine, args.OutputData), string.Join(Environment.NewLine, args.ErrorData));
}

if (!VSTestTrace.TraceEnabled) return;
if (!Logger.TraceEnabled) return;

if (args.ExitCode != ExitCode.Success)
{
VSTestTrace.SafeWriteTrace(() => $"Test Process exited with non-zero exit code: {args.ExitCode}");
Logger.LogTrace(() => $"Test Process exited with non-zero exit code: {args.ExitCode}");
}

if (args.OutputData.Count > 0)
{
VSTestTrace.SafeWriteTrace(() => $"Output Data: {string.Join("\n", args.OutputData)}");
Logger.LogTrace(() => $"Output Data: {string.Join("\n", args.OutputData)}");
}

if (args.ErrorData.Count > 0)
{
VSTestTrace.SafeWriteTrace(() => $"Error Data: {string.Join("\n", args.ErrorData)}");
Logger.LogTrace(() => $"Error Data: {string.Join("\n", args.ErrorData)}");
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private static CliCommand GetTestingPlatformCliCommand()
command.Options.Add(TestingPlatformOptions.FrameworkOption);
command.Options.Add(CommonOptions.OperatingSystemOption);
command.Options.Add(CommonOptions.RuntimeOption);
command.Options.Add(CommonOptions.VerbosityOption);
command.Options.Add(CommonOptions.NoRestoreOption);
command.Options.Add(TestingPlatformOptions.NoBuildOption);
command.Options.Add(TestingPlatformOptions.NoAnsiOption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Microsoft.DotNet.Cli
internal sealed class TestModulesFilterHandler
{
private readonly List<string> _args;

private readonly TestApplicationActionQueue _actionQueue;

public TestModulesFilterHandler(List<string> args, TestApplicationActionQueue actionQueue)
Expand Down Expand Up @@ -44,7 +43,7 @@ public bool RunWithTestModulesFilter(ParseResult parseResult)
// If no matches were found, we simply return
if (!testModulePaths.Any())
{
VSTestTrace.SafeWriteTrace(() => $"No test modules found for the given test module pattern: {testModules} with root directory: {rootDirectory}");
Logger.LogTrace(() => $"No test modules found for the given test module pattern: {testModules} with root directory: {rootDirectory}");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,24 @@ public void RunMultiTFMsProjectSolutionWithFrameworkOption_ShouldReturnExitCodeG
result.ExitCode.Should().Be(ExitCode.GenericFailure);
}

[InlineData(TestingConstants.Debug)]
[InlineData(TestingConstants.Release)]
[Theory]
public void RunWithTraceFileLogging_ShouldReturnExitCodeGenericFailure(string configuration)
{
TestAsset testInstance = _testAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()).WithSource();

string traceFile = "logs.txt";
CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.WithEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar, traceFile)
.WithEnableTestingPlatform()
.Execute(TestingPlatformOptions.ConfigurationOption.Name, configuration);

Assert.True(File.Exists(Path.Combine(testInstance.Path, traceFile)), "Trace file should exist after test execution.");

result.ExitCode.Should().Be(ExitCode.GenericFailure);
}

}
}
Loading