Skip to content
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
20 changes: 20 additions & 0 deletions src/vstest.console/Internal/MSBuildLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ protected static IOutput? Output
private set;
}

/// <summary>
/// Gets the verbosity passed by VSTestTask2. Defaults to <see cref="ConsoleLogger.Verbosity.Normal"/>,
/// so informational messages are forwarded when no verbosity is given.
/// </summary>
internal ConsoleLogger.Verbosity VerbosityLevel { get; private set; } = ConsoleLogger.Verbosity.Normal;

// Default constructor is needed for a logger to be able to activate it.
public MSBuildLogger() { }

Expand All @@ -67,6 +73,14 @@ public void Initialize(TestLoggerEvents events, string testRunDirectory)

public void Initialize(TestLoggerEvents events, Dictionary<string, string?> parameters)
{
ValidateArg.NotNull(parameters, nameof(parameters));

if (parameters.TryGetValue(ConsoleLogger.VerbosityParam, out var verbosity)
&& Enum.TryParse(verbosity, ignoreCase: true, out ConsoleLogger.Verbosity verbosityLevel))
{
VerbosityLevel = verbosityLevel;
}

Initialize(events, string.Empty);
}

Expand All @@ -75,6 +89,12 @@ private void TestMessageHandler(object? sender, TestRunMessageEventArgs e)
switch (e.Level)
{
case TestMessageLevel.Informational:
// Same policy as ConsoleLogger: hide informational messages at quiet and minimal verbosity.
if (VerbosityLevel is ConsoleLogger.Verbosity.Quiet or ConsoleLogger.Verbosity.Minimal)
{
break;
}

SendMessage($"output-info", e.Message);
break;
case TestMessageLevel.Warning:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ public void MSBuildLoggerCanBeEnabledByBuildPropertyAndDoesNotEatSpecialChars(Ru
ExitCodeEquals(1);
}

[TestMethod]
[TestMatrix(console: Net, testHost: Net)]
public void MSBuildLoggerHidesInformationalMessagesBelowNormalVerbosity(RunnerInfo runnerInfo)
{
SetTestEnvironment(_testEnvironment, runnerInfo);

var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj", runnerInfo.TargetFramework);
// MSTest sends this informational message because the test asset sets [assembly: Parallelize].
const string informationalMessage = "Test Parallelization enabled for";

// dotnet test defaults to minimal verbosity, which hides informational messages.
InvokeDotnetTest($@"{projectPath} -tl:on -nodereuse:false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath));
StdOutputContains("TESTERROR");
StdOutputDoesNotContains(informationalMessage);
ExitCodeEquals(1);

InvokeDotnetTest($@"{projectPath} -tl:on -v:n -nodereuse:false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath));
StdOutputContains(informationalMessage);
ExitCodeEquals(1);
}

[TestMethod]
[TestMatrix(console: Net, testHost: Net)]
public void MSBuildLoggerCanBeDisabledByBuildProperty(RunnerInfo runnerInfo)
Expand Down
4 changes: 4 additions & 0 deletions test/TestAssets/TerminalLoggerTestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#endif
using Microsoft.VisualStudio.TestTools.UnitTesting;

// MSTest reports this setting as an informational test run message, which
// DotnetTestMSBuildOutputTests uses to check verbosity filtering. One worker keeps execution sequential.
[assembly: Parallelize(Workers = 1, Scope = ExecutionScope.ClassLevel)]

namespace TerminalLoggerUnitTests
{
/// <summary>
Expand Down
95 changes: 95 additions & 0 deletions test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal;
using Microsoft.VisualStudio.TestPlatform.Common.Logging;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;

namespace vstest.console.UnitTests.Internal;

[TestClass]
public class MSBuildLoggerTests
{
private readonly List<string> _output = [];
private readonly MSBuildLogger _logger;

public MSBuildLoggerTests()
{
var mockOutput = new Mock<IOutput>();
mockOutput.Setup(o => o.WriteLine(It.IsAny<string>(), It.IsAny<OutputLevel>()))
.Callback<string, OutputLevel>((message, _) =>
{
lock (_output)
{
_output.Add(message);
}
});
_logger = new MSBuildLogger(mockOutput.Object);
}

[TestMethod]
[DataRow("quiet", false)]
[DataRow("minimal", false)]
[DataRow("Minimal", false)]
[DataRow("normal", true)]
[DataRow("detailed", true)]
[DataRow(null, true)]
[DataRow("diagnostic", true)]
public void InformationalMessageIsForwardedOnlyAtNormalVerbosityOrHigher(string? verbosity, bool expectedForwarded)
{
RaiseMessages(verbosity, new TestRunMessageEventArgs(TestMessageLevel.Informational, "Informational123"));

Assert.AreSequenceEqual(
expectedForwarded ? [MSBuildLogger.FormatMessage("output-info", "Informational123")] : Array.Empty<string>(),
_output);
}

[TestMethod]
[DataRow("quiet")]
[DataRow("minimal")]
public void WarningsAndErrorsAreForwardedAtLowVerbosity(string verbosity)
{
RaiseMessages(
verbosity,
new TestRunMessageEventArgs(TestMessageLevel.Warning, "Warning123"),
new TestRunMessageEventArgs(TestMessageLevel.Warning, "Skipped123 [SKIP]"),
new TestRunMessageEventArgs(TestMessageLevel.Error, "Error123"));

Assert.AreSequenceEqual(
new[]
{
MSBuildLogger.FormatMessage("output-warning", "Warning123"),
MSBuildLogger.FormatMessage("output-info", "Skipped123 [SKIP]"),
MSBuildLogger.FormatMessage("output-error", "Error123"),
},
_output);
}

private void RaiseMessages(string? verbosity, params TestRunMessageEventArgs[] messages)
{
using var loggerEvents = new InternalTestLoggerEvents(TestSessionMessageLogger.Instance);
loggerEvents.EnableEvents();

var parameters = new Dictionary<string, string?>();
if (verbosity is not null)
{
parameters[ConsoleLogger.VerbosityParam] = verbosity;
}

_logger.Initialize(loggerEvents, parameters);

foreach (var message in messages)
{
loggerEvents.RaiseTestRunMessage(message);
}

loggerEvents.WaitForEventCompletion();
}
}