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

Multi Target Trx Issue : LogFilePrefix Parameter Added #2140

Merged
merged 6 commits into from Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,6 +47,23 @@ public static string GetTestResultsDirectory(RunConfiguration runConfiguration)
return resultsDirectory;
}

/// <summary>
/// Gets the taregt framework from the run configuration
vagisha-nidhi marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="runConfiguration">Test run configuration</param>
/// <returns>Target Framework</returns>
public static Framework GetTargetFramework(RunConfiguration runConfiguration)
{
Framework targetFramework = null;
if (runConfiguration != null)
{
// It will get target framework from runsettings
targetFramework = runConfiguration.TargetFramework;
}

return targetFramework;
}

/// <summary>
/// Gets the solution directory from run configuration
/// </summary>
Expand Down
Expand Up @@ -47,6 +47,11 @@ internal class TestLoggerManager : ITestLoggerManager
/// </summary>
private string testRunDirectory;

/// <summary>
/// Target framework.
/// </summary>
private string targetFramework;

/// <summary>
/// Test Logger Events instance which will be passed to loggers when they are initialized.
/// </summary>
Expand Down Expand Up @@ -139,6 +144,7 @@ public void Initialize(string runSettings)

// Store test run directory. This runsettings is the final runsettings merging CLI args and runsettings.
this.testRunDirectory = GetResultsDirectory(runSettings);
this.targetFramework = GetTargetFramework(runSettings)?.Name;

var loggers = XmlRunSettingsUtilities.GetLoggerRunSettings(runSettings);

Expand Down Expand Up @@ -454,6 +460,33 @@ internal string GetResultsDirectory(string runSettings)
return resultsDirectory;
}

/// <summary>
/// Gets the target framework of the test run.
/// </summary>
/// <param name="runSettings">Test run settings.</param>
/// <returns>Target framework</returns>
internal Framework GetTargetFramework(string runSettings)
{
Framework targetFramework = null;
if (runSettings != null)
{
try
{
RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
targetFramework = RunSettingsUtilities.GetTargetFramework(runConfiguration);
}
catch (SettingsException se)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("TestLoggerManager.GetResultsDirectory: Unable to get the target framework: Error {0}", se);
}
}
}

return targetFramework;
}

/// <summary>
/// Enables sending of events to the loggers which are registered.
/// </summary>
Expand Down Expand Up @@ -613,6 +646,7 @@ private bool InitializeLogger(object logger, string extensionUri, Dictionary<str

// Add default logger parameters...
loggerParams[DefaultLoggerParameterNames.TestRunDirectory] = testRunDirectory;
loggerParams[DefaultLoggerParameterNames.TargetFramework] = targetFramework;
return loggerParams;
}

Expand Down
Expand Up @@ -191,7 +191,7 @@ private static string FormatDateTimeForRunName(DateTime timeStamp)
{
// We use custom format string to make sure that runs are sorted in the same way on all intl machines.
// This is both for directory names and for Data Warehouse.
return timeStamp.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
return timeStamp.ToString("yyyy-MM-dd HH:mm:ss:fff", DateTimeFormatInfo.InvariantInfo);
vagisha-nidhi marked this conversation as resolved.
Show resolved Hide resolved
}

private void Initialize()
Expand Down
22 changes: 18 additions & 4 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.TrxLogger
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using NuGet.Frameworks;
using ObjectModel.Logging;
using TrxLoggerConstants = Microsoft.TestPlatform.Extensions.TrxLogger.Utility.Constants;
using TrxLoggerObjectModel = Microsoft.TestPlatform.Extensions.TrxLogger.ObjectModel;
Expand Down Expand Up @@ -443,14 +444,27 @@ private void DeriveTrxFilePath()
{
if (this.parametersDictionary != null)
{
vagisha-nidhi marked this conversation as resolved.
Show resolved Hide resolved
var isLogFileNameParameterExists = this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue);
if (isLogFileNameParameterExists && !string.IsNullOrWhiteSpace(logFileNameValue))
var isLogFilePrefixParameterExists = this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can someone give both logFilePrefix and LogFileName ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will throw in logger initialize in such case


if (isLogFilePrefixParameterExists && !string.IsNullOrWhiteSpace(logFilePrefixValue))
{
this.trxFilePath = Path.Combine(this.testResultsDirPath, logFileNameValue);
var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework] ?? string.Empty;
framework = NuGetFramework.Parse(framework).GetShortFolderName();
Copy link
Contributor

Choose a reason for hiding this comment

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

can this cause invalid chars in file path?


logFilePrefixValue = logFilePrefixValue.Replace(".trx", string.Empty) + "_" + framework + DateTime.Now.ToString("_yyyyMMddHHmmssfff", DateTimeFormatInfo.InvariantInfo) + ".trx";
vagisha-nidhi marked this conversation as resolved.
Show resolved Hide resolved
this.trxFilePath = Path.Combine(this.testResultsDirPath, logFilePrefixValue);
}
else
{
this.SetDefaultTrxFilePath();
var isLogFileNameParameterExists = this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue);
if (isLogFileNameParameterExists && !string.IsNullOrWhiteSpace(logFileNameValue))
{
this.trxFilePath = Path.Combine(this.testResultsDirPath, logFileNameValue);
}
else
{
this.SetDefaultTrxFilePath();
}
}
}
else
Expand Down
Expand Up @@ -29,6 +29,11 @@ internal static class Constants
/// </summary>
public const string LogFileNameKey = "LogFileName";

/// <summary>
/// Log file prefix key
/// </summary>
public const string LogFilePrefixKey = "LogFilePrefix";

/// <summary>
/// Ordered test element name
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.TestPlatform.ObjectModel/Constants.cs
Expand Up @@ -216,6 +216,9 @@ public static class DefaultLoggerParameterNames
// Denotes target location for test run resutls
// For ex. TrxLogger saves test run results at this target
public const string TestRunDirectory = "TestRunDirectory";

// Denotes target framework for the tests.
public const string TargetFramework = "TargetFramework";
}

}
22 changes: 21 additions & 1 deletion test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs
@@ -1,13 +1,13 @@
// 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.IO;
using System.Xml;

namespace Microsoft.TestPlatform.AcceptanceTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

[TestClass]
public class LoggerTests : AcceptanceTestBase
Expand Down Expand Up @@ -52,6 +52,26 @@ public void TrxLoggerWithExecutorUriShouldProperlyOverwriteFile(RunnerInfo runne
Assert.IsTrue(IsValidXml(trxLogFilePath), "Invalid content in Trx log file");
}

[TestMethod]
[NetFullTargetFrameworkDataSource(inIsolation: true, inProcess: true)]
public void TrxLoggerWithLogFilePrefixShouldGenerateMultipleTrx(RunnerInfo runnerInfo)
{
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
var trxFileNamePattern = "TestResults";

var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /logger:\"logger://Microsoft/TestPlatform/TrxLogger/v1;LogFilePrefix={trxFileNamePattern}\"");
this.InvokeVsTest(arguments);

arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /logger:\"logger://Microsoft/TestPlatform/TrxLogger/v1;LogFilePrefix={trxFileNamePattern}\"");
arguments = string.Concat(arguments, " /testcasefilter:Name~Pass");
this.InvokeVsTest(arguments);

var trxFilePaths = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "TestResults"), trxFileNamePattern + "_net*.trx");
Assert.IsTrue(trxFilePaths.Count() > 1);
}

private bool IsValidXml(string xmlFilePath)
{
var reader = System.Xml.XmlReader.Create(File.OpenRead(xmlFilePath));
Expand Down
Expand Up @@ -109,6 +109,24 @@ public void GetResultsDirectoryShouldReturnDefaultPathIfResultsDirectoryIsNotPro
Assert.AreEqual(string.Compare(Constants.DefaultResultsDirectory, result), 0);
}

[TestMethod]
public void GetTargetFrameworkShouldReturnFrameworkProvidedInRunSettings()
{
string runSettingsXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
<TargetPlatform> x64 </TargetPlatform>
<TargetFrameworkVersion> Framework45 </TargetFrameworkVersion>
</RunConfiguration>
</RunSettings> ";

var testLoggerManager = new DummyTestLoggerManager();
var framework = testLoggerManager.GetTargetFramework(runSettingsXml);

Assert.AreEqual(framework.Name, ".NETFramework,Version=v4.5");
}

[TestMethod]
public void HandleTestRunMessageShouldInvokeTestRunMessageHandlerOfLoggers()
{
Expand Down Expand Up @@ -1065,7 +1083,7 @@ public void InitializeShouldPassConfigurationElementAsParameters()
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);

Expand Down Expand Up @@ -1105,7 +1123,7 @@ public void InitializeShouldSkipEmptyConfigurationValueInParameters()
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(2, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.IsFalse(ValidLoggerWithParameters.parameters.TryGetValue("Key1", out var key1Value));
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);

Expand Down Expand Up @@ -1146,7 +1164,7 @@ public void InitializeShouldUseLastValueInParametersForDuplicateConfigurationVal
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value3", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);

Expand Down Expand Up @@ -1217,7 +1235,7 @@ public void InitializeShouldInitializeFromAssemblyNameIfAllAttributesPresent()
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
mockMetricsCollection.Verify(
Expand Down Expand Up @@ -1255,7 +1273,7 @@ public void InitializeShouldInitializeFromUriIfUriAndNamePresent()
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
mockMetricsCollection.Verify(
Expand Down Expand Up @@ -1293,7 +1311,7 @@ public void InitializeShouldInitializeFromUriIfUnableToFromAssemblyName()
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
mockMetricsCollection.Verify(
Expand Down Expand Up @@ -1331,7 +1349,7 @@ public void InitializeShouldInitializeFromNameIfUnableToFromUri()
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
mockMetricsCollection.Verify(
Expand Down Expand Up @@ -1375,7 +1393,7 @@ public void InitializeShouldInitializeLoggersWithTestRunDirectoryIfPresentInRunS
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual("DummyTestResultsFolder", ValidLoggerWithParameters.parameters["testRunDirectory"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
Expand Down Expand Up @@ -1419,7 +1437,7 @@ public void InitializeShouldInitializeLoggersWithDefaultTestRunDirectoryIfNotPre
testLoggerManager.Initialize(settingsXml);

Assert.AreEqual(1, ValidLoggerWithParameters.counter);
Assert.AreEqual(3, ValidLoggerWithParameters.parameters.Count); // One additional because of testRunDirectory
Assert.AreEqual(4, ValidLoggerWithParameters.parameters.Count); // Two additional because of testRunDirectory and targetFramework
Assert.AreEqual("Value1", ValidLoggerWithParameters.parameters["Key1"]);
Assert.AreEqual(Constants.DefaultResultsDirectory, ValidLoggerWithParameters.parameters["testRunDirectory"]);
Assert.AreEqual("Value2", ValidLoggerWithParameters.parameters["Key2"]);
Expand Down
Expand Up @@ -712,6 +712,41 @@ private void ValidateDateTimeInTrx(string trxFileName)
}
}

[TestMethod]
[DataRow("results")]
[DataRow("results.trx")]
public void CustomTrxFileNameShouldBeConstructedFromRelativeLogFilePrefixParameter(string prefixName)
{
this.parameters[TrxLoggerConstants.LogFilePrefixKey] = prefixName;
this.parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1";
this.testableTrxLogger.Initialize(events.Object, this.parameters);

this.MakeTestRunComplete();

string actualFileNameWithoutTimestamp = this.testableTrxLogger.trxFile.Substring(0, this.testableTrxLogger.trxFile.LastIndexOf('_'));

Assert.AreNotEqual(Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, "results.trx"), this.testableTrxLogger.trxFile, "Expected framework name to appear in file name");
Assert.AreNotEqual(Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, "results_net451.trx"), this.testableTrxLogger.trxFile, "Expected time stamp to appear in file name");
Assert.AreEqual(Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, "results_net451"), actualFileNameWithoutTimestamp);
}

[TestMethod]
public void CustomTrxFileNameShouldBeConstructedFromAbsoluteLogFilePrefixParameter()
{
var trxPrefix = Path.Combine(Path.GetTempPath(), "results");
this.parameters[TrxLoggerConstants.LogFilePrefixKey] = trxPrefix;
this.parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1";
this.testableTrxLogger.Initialize(events.Object, this.parameters);

this.MakeTestRunComplete();

string actualFileNameWithoutTimestamp = this.testableTrxLogger.trxFile.Substring(0, this.testableTrxLogger.trxFile.LastIndexOf('_'));

Assert.AreEqual(trxPrefix + "_net451", actualFileNameWithoutTimestamp);

File.Delete(this.testableTrxLogger.trxFile);
}

private void ValidateTestIdAndNameInTrx(bool isMstestAdapter)
{
ObjectModel.TestCase testCase = CreateTestCase("TestCase");
Expand Down