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

test run directory fix for loggers #1399

Merged
merged 2 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client
using System.Linq;
using System.Reflection;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.Common.Logging;
using Microsoft.VisualStudio.TestPlatform.Common.Telemetry;
Expand Down Expand Up @@ -66,6 +65,11 @@ internal class TestLoggerManager : ITestLoggerManager
/// </summary>
private IAssemblyLoadContext assemblyLoadContext;

/// <summary>
/// Test run directory.
/// </summary>
private string testRunDirectory;

#endregion

#region Constructor
Expand Down Expand Up @@ -149,6 +153,9 @@ public void Initialize(string runSettings)
// Enable logger events
EnableLogging();

// Store test run directory
testRunDirectory = GetResultsDirectory(runSettings);

var loggers = XmlRunSettingsUtilities.GetLoggerRunSettings(runSettings);

foreach (var logger in loggers?.LoggerSettingsList ?? Enumerable.Empty<LoggerSettings>())
Expand Down Expand Up @@ -326,8 +333,7 @@ private bool InitializeLogger(object logger, string extensionUri, Dictionary<str
break;

case ITestLogger _:
((ITestLogger)logger).Initialize(loggerEvents,
GetResultsDirectory(RunSettingsManager.Instance.ActiveRunSettings));
((ITestLogger)logger).Initialize(loggerEvents, testRunDirectory);
break;

default:
Expand Down Expand Up @@ -449,14 +455,14 @@ protected virtual void Dispose(bool disposing)
/// </summary>
/// <param name="runSettings">Test run settings.</param>
/// <returns>Test results directory</returns>
internal string GetResultsDirectory(RunSettings runSettings)
internal string GetResultsDirectory(string runSettings)
{
string resultsDirectory = null;
if (runSettings != null)
{
try
{
RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings.SettingsXml);
RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
resultsDirectory = RunSettingsUtilities.GetTestResultsDirectory(runConfiguration);
}
catch (SettingsException se)
Expand All @@ -483,7 +489,7 @@ private Dictionary<string, string> UpdateLoggerParameters(Dictionary<string, str
}

// Add default logger parameters...
loggerParams[DefaultLoggerParameterNames.TestRunDirectory] = this.GetResultsDirectory(RunSettingsManager.Instance.ActiveRunSettings);
loggerParams[DefaultLoggerParameterNames.TestRunDirectory] = testRunDirectory;
return loggerParams;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,15 @@ public void GetResultsDirectoryShouldReadResultsDirectoryFromSettingsIfSpecified
string runSettingsXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
<MaxCpuCount>0</MaxCpuCount>
<ResultsDirectory>DummyTestResultsFolder</ResultsDirectory>
<TargetPlatform> x64 </TargetPlatform>
<TargetFrameworkVersion> Framework45 </TargetFrameworkVersion>
</RunConfiguration>
</RunSettings> ";

RunSettings runsettings = new RunSettings();
runsettings.LoadSettingsXml(runSettingsXml);

var testLoggerManager = new DummyTestLoggerManager();
string result = testLoggerManager.GetResultsDirectory(runsettings);
string result = testLoggerManager.GetResultsDirectory(runSettingsXml);
Assert.AreEqual(string.Compare("DummyTestResultsFolder", result), 0);
}

Expand All @@ -106,11 +103,8 @@ public void GetResultsDirectoryShouldReturnDefaultPathIfResultsDirectoryIsNotPro
</RunConfiguration>
</RunSettings> ";

RunSettings runsettings = new RunSettings();
runsettings.LoadSettingsXml(runSettingsXml);

var testLoggerManager = new DummyTestLoggerManager();
string result = testLoggerManager.GetResultsDirectory(runsettings);
string result = testLoggerManager.GetResultsDirectory(runSettingsXml);

Assert.AreEqual(string.Compare(Constants.DefaultResultsDirectory, result), 0);
}
Expand Down Expand Up @@ -253,8 +247,31 @@ public void AddLoggerShouldNotThrowExceptionIfUriIsNonExistent()
public void AddLoggerShouldAddDefaultLoggerParameterForTestLoggerWithParameters()
{
ValidLoggerWithParameters.Reset();
var testLoggerManager = new DummyTestLoggerManager();
testLoggerManager.InitializeLoggerByUri(new Uri("test-logger-with-parameter://logger"), new Dictionary<string, string>());
var mockRequestData = new Mock<IRequestData>();
var mockMetricsCollection = new Mock<IMetricsCollection>();
mockRequestData.Setup(rd => rd.MetricsCollection).Returns(mockMetricsCollection.Object);

var codeBase = typeof(TestLoggerManagerTests).GetTypeInfo().Assembly.Location;

string settingsXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
<TargetPlatform> x64 </TargetPlatform>
<TargetFrameworkVersion> Framework45 </TargetFrameworkVersion>
</RunConfiguration>
<LoggerRunSettings>
<Loggers>
<Logger friendlyName=""TestLoggerExtension"" />
<Logger uri=""test-logger-with-parameter://logger"" enabled=""true""></Logger>
</Loggers>
</LoggerRunSettings>
</RunSettings>";

var testLoggerManager = new DummyTestLoggerManager(mockRequestData.Object);
testLoggerManager.Initialize(settingsXml);

Assert.IsNotNull(ValidLoggerWithParameters.parameters, "parameters not getting passed");
Assert.IsTrue(
ValidLoggerWithParameters.parameters.ContainsKey(DefaultLoggerParameterNames.TestRunDirectory),
Expand Down Expand Up @@ -1205,6 +1222,95 @@ public void InitializeShouldInitializeFromNameIfUnableToFromUri()
rd => rd.Add(TelemetryDataConstants.LoggerUsed, "TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLogger,TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLogger2,TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLoggerWithParameters"));
}

[TestMethod]
public void InitializeShouldInitializeLoggersWithTestRunDirectoryIfPresentInRunSettings()
{
ValidLoggerWithParameters.Reset();
var mockRequestData = new Mock<IRequestData>();
var mockMetricsCollection = new Mock<IMetricsCollection>();
mockRequestData.Setup(rd => rd.MetricsCollection).Returns(mockMetricsCollection.Object);

var codeBase = typeof(TestLoggerManagerTests).GetTypeInfo().Assembly.Location;

string settingsXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
<ResultsDirectory>DummyTestResultsFolder</ResultsDirectory>
<TargetPlatform> x64 </TargetPlatform>
<TargetFrameworkVersion> Framework45 </TargetFrameworkVersion>
</RunConfiguration>
<LoggerRunSettings>
<Loggers>
<Logger friendlyName=""TestLoggerExtension"" />
<Logger uri=""testlogger://logger2"" enabled=""true""></Logger>
<Logger friendlyName=""TestLoggerWithParameterExtension"" uri=""invalid://invalid"" assemblyQualifiedName=""invalidAssembly"" codeBase=""" + codeBase + @""">
<Configuration>
<Key1>Value1</Key1>
<Key2>Value2</Key2>
</Configuration>
</Logger>
</Loggers>
</LoggerRunSettings>
</RunSettings>";

var testLoggerManager = new DummyTestLoggerManager(mockRequestData.Object);
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("DummyTestResultsFolder", ValidLoggerWithParameters.parameters["testRunDirectory"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
mockMetricsCollection.Verify(
rd => rd.Add(TelemetryDataConstants.LoggerUsed, "TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLogger,TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLogger2,TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLoggerWithParameters"));
}

[TestMethod]
public void InitializeShouldInitializeLoggersWithDefaultTestRunDirectoryIfNotPresentInRunSettings()
{
ValidLoggerWithParameters.Reset();
var mockRequestData = new Mock<IRequestData>();
var mockMetricsCollection = new Mock<IMetricsCollection>();
mockRequestData.Setup(rd => rd.MetricsCollection).Returns(mockMetricsCollection.Object);

var codeBase = typeof(TestLoggerManagerTests).GetTypeInfo().Assembly.Location;

string settingsXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
<TargetPlatform> x64 </TargetPlatform>
<TargetFrameworkVersion> Framework45 </TargetFrameworkVersion>
</RunConfiguration>
<LoggerRunSettings>
<Loggers>
<Logger friendlyName=""TestLoggerExtension"" />
<Logger uri=""testlogger://logger2"" enabled=""true""></Logger>
<Logger friendlyName=""TestLoggerWithParameterExtension"" uri=""invalid://invalid"" assemblyQualifiedName=""invalidAssembly"" codeBase=""" + codeBase + @""">
<Configuration>
<Key1>Value1</Key1>
<Key2>Value2</Key2>
</Configuration>
</Logger>
</Loggers>
</LoggerRunSettings>
</RunSettings>";

var testLoggerManager = new DummyTestLoggerManager(mockRequestData.Object);
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual(Constants.DefaultResultsDirectory, ValidLoggerWithParameters.parameters["testRunDirectory"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
mockMetricsCollection.Verify(
rd => rd.Add(TelemetryDataConstants.LoggerUsed, "TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLogger,TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLogger2,TestPlatform.CrossPlatEngine.UnitTests.TestLoggerManagerTests+ValidLoggerWithParameters"));
}

[TestMethod]
public void InitializeShouldNotInitializeIfUnableToFromName()
{
Expand Down