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

Logger support in run settings #1382

Merged
merged 15 commits into from
Jan 30, 2018
Prev Previous commit
Next Next commit
Initial commit 4
  • Loading branch information
abhishkk committed Jan 22, 2018
commit 4278003a4b8cfb093606d3c469d84272c040dc6f
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public void UpdateLoggerList(string argument, string loggerIdentifier, Dictionar
/// </summary>
public void Initialize(string runSettings)
{
// Enable logger events
this.loggerEvents.EnableEvents();

var loggers = XmlRunSettingsUtilities.GetLoggerRunSettings(runSettings);

foreach (var logger in loggers?.LoggerSettingsList ?? Enumerable.Empty<LoggerSettings>())
Expand Down
41 changes: 41 additions & 0 deletions src/vstest.console/Internal/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,5 +475,46 @@ private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
}
}
#endregion

/// <summary>
/// Raises test run errors occured before console logger starts listening error events.
/// </summary>
/// <param name="testRunResultAggregator"></param>
/// <param name="exception"></param>
public static void RaiseTestRunError(TestRunResultAggregator testRunResultAggregator, Exception exception)
{
if (Output == null)
{
Output = ConsoleOutput.Instance;
}

// testRunResultAggregator can be null, if error is being raised in discovery context.
testRunResultAggregator?.MarkTestRunFailed();

Output.Error(ConsoleLogger.AppendPrefix, exception.Message);

// Send inner exception only when its message is different to avoid duplicate.
if (exception is TestPlatformException &&
exception.InnerException != null &&
string.Compare(exception.Message, exception.InnerException.Message, StringComparison.CurrentCultureIgnoreCase) != 0)
{
Output.Error(ConsoleLogger.AppendPrefix, exception.InnerException.Message);
}
}

/// <summary>
/// Raises test run warning occured before console logger starts listening warning events.
/// </summary>
/// <param name="testRunResultAggregator"></param>
/// <param name="warningMessage"></param>
public static void RaiseTestRunWarning(TestRunResultAggregator testRunResultAggregator, string warningMessage)
{
if (ConsoleLogger.Output == null)
{
ConsoleLogger.Output = ConsoleOutput.Instance;
}

Output.Warning(ConsoleLogger.AppendPrefix, warningMessage);
}
}
}
26 changes: 0 additions & 26 deletions src/vstest.console/Processors/Utilities/LoggerUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,6 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities

internal class LoggerUtilities
{
internal static void RaiseTestRunError(TestRunResultAggregator testRunResultAggregator, Exception exception)
{
// TODO: add IOutput here. Check what happens in case of IDE.

// testRunResultAggregator can be null, if error is being raised in discovery context.
testRunResultAggregator?.MarkTestRunFailed();

TestRunMessageEventArgs errorMessage = new TestRunMessageEventArgs(TestMessageLevel.Error, exception.Message);
//loggerManager.SendTestRunMessage(errorMessage);

// Send inner exception only when its message is different to avoid duplicate.
if (exception is TestPlatformException && exception.InnerException != null && string.Compare(exception.Message, exception.InnerException.Message, StringComparison.CurrentCultureIgnoreCase) != 0)
{
errorMessage = new TestRunMessageEventArgs(TestMessageLevel.Error, exception.InnerException.Message);
//loggerManager.SendTestRunMessage(errorMessage);
}

//loggerManager.LoggerEvents.WaitForEventCompletion();
}

internal static void RaiseTestRunWarning(TestRunResultAggregator testRunResultAggregator, string warningMessage)
{
TestRunMessageEventArgs testRunMessage = new TestRunMessageEventArgs(TestMessageLevel.Warning, warningMessage);
//loggerManager.SendTestRunMessage(testRunMessage);
}

/// <summary>
/// Parses the parameters passed as name values pairs along with the logger argument.
/// </summary>
Expand Down
7 changes: 3 additions & 4 deletions src/vstest.console/TestPlatformHelpers/TestRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers
{
// TODO: add test in test platform that if console logger is present in run settings, then it should get initialized.
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -196,7 +195,7 @@ public bool DiscoverTests(DiscoveryRequestPayload discoveryPayload, ITestDiscove
ex is SettingsException ||
ex is InvalidOperationException)
{
LoggerUtilities.RaiseTestRunError(null, ex);
ConsoleLogger.RaiseTestRunError(null, ex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConsoleLogger.RaiseTestRunError(null, ex); [](start = 20, length = 42)

nitpick: can we move this closer to the logger initialization code alone..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that logger initialization is happening within this block only.

success = false;
}
else
Expand Down Expand Up @@ -393,7 +392,7 @@ private bool UpdateRunSettingsIfRequired(string runsettingsXml, List<string> sou
if (!string.IsNullOrEmpty(incompatibleSettingWarning))
{
EqtTrace.Info(incompatibleSettingWarning);
LoggerUtilities.RaiseTestRunWarning(this.testRunResultAggregator, incompatibleSettingWarning);
ConsoleLogger.RaiseTestRunWarning(this.testRunResultAggregator, incompatibleSettingWarning);
}

if (EqtTrace.IsInfoEnabled)
Expand Down Expand Up @@ -501,7 +500,7 @@ private bool RunTests(IRequestData requestData, TestRunCriteria testRunCriteria,
ex is SettingsException ||
ex is InvalidOperationException)
{
LoggerUtilities.RaiseTestRunError(this.testRunResultAggregator, ex);
ConsoleLogger.RaiseTestRunError(this.testRunResultAggregator, ex);
success = false;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace vstest.console.UnitTests.TestPlatformHelpers
using Moq;

using vstest.console.UnitTests.TestDoubles;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal;

[TestClass]
public class TestRequestManagerTests
Expand All @@ -46,6 +48,7 @@ public class TestRequestManagerTests
private TestLoggerManager mockLoggerManager;
private CommandLineOptions commandLineOptions;
private Mock<ITestPlatform> mockTestPlatform;
private Mock<IOutput> mockOutput;
private Mock<IDiscoveryRequest> mockDiscoveryRequest;
private Mock<ITestRunRequest> mockRunRequest;
private Mock<IAssemblyMetadataProvider> mockAssemblyMetadataProvider;
Expand All @@ -69,6 +72,7 @@ public TestRequestManagerTests()
this.mockLoggerEvents = new DummyLoggerEvents(TestSessionMessageLogger.Instance);
this.mockLoggerManager = new DummyTestLoggerManager(this.mockLoggerEvents);
this.commandLineOptions = new DummyCommandLineOptions();
this.mockOutput = new Mock<IOutput>();
this.mockTestPlatform = new Mock<ITestPlatform>();
this.mockDiscoveryRequest = new Mock<IDiscoveryRequest>();
this.mockRunRequest = new Mock<ITestRunRequest>();
Expand Down Expand Up @@ -106,20 +110,6 @@ public void Cleanup()
CommandLineOptions.Instance.Reset();
}

[TestMethod]
public void TestRequestManagerShouldInitializeConsoleLogger()
{
CommandLineOptions.Instance.IsDesignMode = false;
var requestManager = new TestRequestManager(CommandLineOptions.Instance,
new Mock<ITestPlatform>().Object,
TestRunResultAggregator.Instance,
new Mock<ITestPlatformEventSource>().Object,
this.inferHelper,
this.mockMetricsPublisherTask);

// Assert.IsTrue(this.mockLoggerEvents.EventsSubscribed());
}

[TestMethod]
public void TestRequestManagerShouldNotInitializeConsoleLoggerIfDesignModeIsSet()
{
Expand Down Expand Up @@ -1309,6 +1299,64 @@ public void RunTestsIfThrowsSettingsExceptionShouldNotThrowOut()
Assert.IsFalse(success, "RunTests call must fail due to exception");
}

[TestMethod]
public void RunTestsShouldOutputErrorForInvalidOperationException()
{
ConsoleLogger consoleLogger = new ConsoleLogger(mockOutput.Object);
var payload = new TestRunRequestPayload()
{
Sources = new List<string>() { "a.dll", "b.dll" },
RunSettings = DefaultRunsettings
};

TestRunCriteria observedCriteria = null;
var mockRunRequest = new Mock<ITestRunRequest>();
this.mockTestPlatform.Setup(mt => mt.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Callback(
(IRequestData requestData, TestRunCriteria runCriteria) =>
{
observedCriteria = runCriteria;
}).Returns(mockRunRequest.Object);

mockRunRequest.Setup(mr => mr.ExecuteAsync()).Throws(new InvalidOperationException("HelloWorld"));

var mockRunEventsRegistrar = new Mock<ITestRunEventsRegistrar>();
var mockCustomlauncher = new Mock<ITestHostLauncher>();

var success = this.testRequestManager.RunTests(payload, mockCustomlauncher.Object, mockRunEventsRegistrar.Object, this.protocolConfig);

Assert.IsFalse(success, "RunTests call must fail due to exception");
mockOutput.Verify(ot => ot.WriteLine("HelloWorld", OutputLevel.Error));
}

[TestMethod]
public void DiscoverTestsShouldOutputErrorForInvalidOperationException()
{
ConsoleLogger consoleLogger = new ConsoleLogger(mockOutput.Object);
var payload = new DiscoveryRequestPayload()
{
Sources = new List<string>() { "a.dll", "b.dll" },
RunSettings = DefaultRunsettings
};

DiscoveryCriteria observedCriteria = null;
var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();
this.mockTestPlatform.Setup(mt => mt.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Callback(
(IRequestData requestData, DiscoveryCriteria discoveryCriteria) =>
{
observedCriteria = discoveryCriteria;
}).Returns(mockDiscoveryRequest.Object);

mockDiscoveryRequest.Setup(mr => mr.DiscoverAsync()).Throws(new InvalidOperationException("HelloWorld"));

var mockDiscoveryEventsRegistrar = new Mock<ITestDiscoveryEventsRegistrar>();
var mockCustomlauncher = new Mock<ITestHostLauncher>();

var success = this.testRequestManager.DiscoverTests(payload, mockDiscoveryEventsRegistrar.Object, this.protocolConfig);

Assert.IsFalse(success, "DiscoverTests call must fail due to exception");
mockOutput.Verify(ot => ot.WriteLine("HelloWorld", OutputLevel.Error));
}

[TestMethod]
public void RunTestsIfThrowsInvalidOperationExceptionShouldNotThrowOut()
{
Expand Down Expand Up @@ -1914,7 +1962,6 @@ public void RunTestsShouldNotAddConsoleLoggerInRunSettingsInDesignMode()
{
actualTestRunCriteria = runCriteria;
}).Returns(mockTestRunRequest.Object);
System.Diagnostics.Debugger.Launch();
this.testRequestManager.RunTests(payload, new Mock<ITestHostLauncher>().Object, new Mock<ITestRunEventsRegistrar>().Object, this.protocolConfig);

Assert.IsFalse(actualTestRunCriteria.TestRunSettings.Contains("LoggerRunSettings"));
Expand Down