diff --git a/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsUtilities.cs b/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsUtilities.cs index aba69fca5b..e9408978fb 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsUtilities.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/RunSettingsUtilities.cs @@ -47,6 +47,23 @@ public static string GetTestResultsDirectory(RunConfiguration runConfiguration) return resultsDirectory; } + /// + /// Gets the target framework from the run configuration + /// + /// Test run configuration + /// Target Framework + public static Framework GetTargetFramework(RunConfiguration runConfiguration) + { + Framework targetFramework = null; + if (runConfiguration != null) + { + // It will get target framework from runsettings + targetFramework = runConfiguration.TargetFramework; + } + + return targetFramework; + } + /// /// Gets the solution directory from run configuration /// diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/TestLoggerManager.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/TestLoggerManager.cs index 9dfad5fb16..c17b898d8f 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/TestLoggerManager.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/TestLoggerManager.cs @@ -47,6 +47,11 @@ internal class TestLoggerManager : ITestLoggerManager /// private string testRunDirectory; + /// + /// Target framework. + /// + private string targetFramework; + /// /// Test Logger Events instance which will be passed to loggers when they are initialized. /// @@ -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); @@ -454,6 +460,33 @@ internal string GetResultsDirectory(string runSettings) return resultsDirectory; } + /// + /// Gets the target framework of the test run. + /// + /// Test run settings. + /// Target framework + 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; + } + /// /// Enables sending of events to the loggers which are registered. /// @@ -582,9 +615,9 @@ private bool InitializeLogger(object logger, string extensionUri, Dictionary @@ -20,7 +19,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.TrxLogger.Resources { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class TrxResource { @@ -40,7 +39,7 @@ internal class TrxResource { internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.VisualStudio.TestPlatform.Extensions.TrxLogger.Resources.TrxResource", typeof(TrxResource).GetTypeInfo().Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.VisualStudio.TestPlatform.Extensions.TrxLogger.Resources.TrxResource", typeof(TrxResource).Assembly); resourceMan = temp; } return resourceMan; @@ -179,6 +178,15 @@ internal class TrxResource { } } + /// + /// Looks up a localized string similar to The parameters LogFileName and LogFilePrefix cannot be used together. . + /// + internal static string PrefixAndNameProvidedError { + get { + return ResourceManager.GetString("PrefixAndNameProvidedError", resourceCulture); + } + } + /// /// Looks up a localized string similar to Aborted. /// diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/TrxResource.resx b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/TrxResource.resx index 9bfad88ae8..b50850a64f 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/TrxResource.resx +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/TrxResource.resx @@ -211,4 +211,7 @@ Error Details: {1}:{2} WARNING: Overwriting results file: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.cs.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.cs.xlf index 74c81207aa..17f5cd7028 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.cs.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.cs.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} UPOZORNĚNÍ: Přepíše se soubor výsledků: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.de.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.de.xlf index a75ac3284c..0ae3be6ad8 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.de.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.de.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} WARNUNG: Ergebnisdatei wird überschrieben: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.es.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.es.xlf index 6deb47fefd..a5aac41667 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.es.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.es.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} ADVERTENCIA: Se sobrescribirá el archivo de resultados: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.fr.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.fr.xlf index 0c39d0f879..e1b4567ccf 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.fr.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.fr.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} AVERTISSEMENT : Fichier de résultats en cours de remplacement : {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.it.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.it.xlf index 5f1aef5ca0..8a6b8b2ba5 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.it.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.it.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} AVVISO: il file dei risultati {0} verrà sovrascritto + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ja.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ja.xlf index 402b3a2318..0b76bc19ca 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ja.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ja.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} 警告: 結果ファイルを上書きしています: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ko.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ko.xlf index d89047993e..1d85fec1cb 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ko.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ko.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} 경고: 결과 파일을 덮어쓰는 중: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pl.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pl.xlf index d138f7eb51..8f95f1d7f6 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pl.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pl.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} OSTRZEŻENIE: Plik wynikowy jest zastępowany: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pt-BR.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pt-BR.xlf index 6f4e215643..f81b844f3e 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pt-BR.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.pt-BR.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} AVISO: sobrescrevendo o arquivo de resultados: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ru.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ru.xlf index 532add5b88..be1a14080f 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ru.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.ru.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} Внимание! Перезапись файла с результатами: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.tr.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.tr.xlf index 995404129f..dd74d4d9fb 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.tr.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.tr.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} UYARI: Sonuç dosyasının üzerine yazılıyor: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.xlf index 04819dfdbb..31f1cbdf24 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.xlf @@ -128,6 +128,11 @@ Error Details: {1}:{2} WARNING: Overwriting results file: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hans.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hans.xlf index 7c6b42c507..658c3fe0fe 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hans.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hans.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} 警告: 正在覆盖结果文件: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hant.xlf b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hant.xlf index 7143e2df97..99185293a2 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hant.xlf +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Resources/xlf/TrxResource.zh-Hant.xlf @@ -298,6 +298,11 @@ Fehlerdetails: {1}: {2} 警告: 將要覆寫結果檔案: {0} + + The parameters LogFileName and LogFilePrefix cannot be used together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs index 0928d93089..f553556a24 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs @@ -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 Microsoft.VisualStudio.TestPlatform.Utilities.Helpers; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; using ObjectModel.Logging; @@ -87,6 +88,8 @@ protected TrxLogger(IFileHelper fileHelper) private DateTime testRunStartTime; + private string trxFileExtension = ".trx"; + /// /// Parameters dictionary for logger. Ex: {"LogFileName":"TestResults.trx"}. /// @@ -120,7 +123,6 @@ public void Initialize(TestLoggerEvents events, string testResultsDirPath) events.TestRunComplete += this.TestRunCompleteHandler; this.testResultsDirPath = testResultsDirPath; - this.InitializeInternal(); } @@ -137,6 +139,18 @@ public void Initialize(TestLoggerEvents events, Dictionary param throw new ArgumentException("No default parameters added", nameof(parameters)); } + var isLogFilePrefixParameterExists = parameters.TryGetValue(TrxLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue); + var isLogFileNameParameterExists = parameters.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue); + + if (isLogFilePrefixParameterExists && isLogFileNameParameterExists) + { + var trxParameterErrorMsg = string.Format(CultureInfo.CurrentCulture, + TrxLoggerResources.PrefixAndNameProvidedError); + + EqtTrace.Error(trxParameterErrorMsg); + throw new ArgumentException(trxParameterErrorMsg); + } + this.parametersDictionary = parameters; this.Initialize(events, this.parametersDictionary[DefaultLoggerParameterNames.TestRunDirectory]); } @@ -468,22 +482,33 @@ private void HandleSkippedTest(ObjectModel.TestResult rsTestResult) private void DeriveTrxFilePath() { - if (this.parametersDictionary != null) + var isLogFilePrefixParameterExists = this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue); + var isLogFileNameParameterExists = this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue); + + if (isLogFilePrefixParameterExists) { - var isLogFileNameParameterExists = this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue); - if (isLogFileNameParameterExists && !string.IsNullOrWhiteSpace(logFileNameValue)) - { - this.trxFilePath = Path.Combine(this.testResultsDirPath, logFileNameValue); - } - else + if (!string.IsNullOrWhiteSpace(logFilePrefixValue)) { - this.SetDefaultTrxFilePath(); + var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework] ?? string.Empty; + framework = NuGetFramework.Parse(framework).GetShortFolderName(); + + logFilePrefixValue = logFilePrefixValue.Replace(".trx", string.Empty) + "_" + framework + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + this.trxFileExtension; + this.trxFilePath = Path.Combine(this.testResultsDirPath, logFilePrefixValue); + + return; } } - else + + else if (isLogFileNameParameterExists) { - this.SetDefaultTrxFilePath(); + if (!string.IsNullOrWhiteSpace(logFileNameValue)) + { + this.trxFilePath = Path.Combine(this.testResultsDirPath, logFileNameValue); + return; + } } + + this.SetDefaultTrxFilePath(); } /// diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs index 4f54b7a0d8..2d776e6399 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs @@ -29,6 +29,11 @@ internal static class Constants /// public const string LogFileNameKey = "LogFileName"; + /// + /// Log file prefix key + /// + public const string LogFilePrefixKey = "LogFilePrefix"; + /// /// Ordered test element name /// diff --git a/src/Microsoft.TestPlatform.ObjectModel/Constants.cs b/src/Microsoft.TestPlatform.ObjectModel/Constants.cs index 8bd3ee6a0d..1a06464645 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/Constants.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/Constants.cs @@ -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"; } } diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs index 9c86723df4..b8bc16f19b 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs @@ -1,13 +1,13 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// 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.IO; -using System.Xml; - namespace Microsoft.TestPlatform.AcceptanceTests { using Microsoft.VisualStudio.TestTools.UnitTesting; + using System.Linq; using System.Text; + using System.IO; + using System.Xml; [TestClass] public class LoggerTests : AcceptanceTestBase @@ -72,6 +72,27 @@ 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); + + } + [TestMethod] [NetCoreTargetFrameworkDataSource] public void HtmlLoggerWithExecutorUriShouldProperlyOverwriteFile(RunnerInfo runnerInfo) diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/TestLoggerManagerTests.cs b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/TestLoggerManagerTests.cs index 664ebd249f..2e2865af26 100644 --- a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/TestLoggerManagerTests.cs +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/TestLoggerManagerTests.cs @@ -109,6 +109,24 @@ public void GetResultsDirectoryShouldReturnDefaultPathIfResultsDirectoryIsNotPro Assert.AreEqual(string.Compare(Constants.DefaultResultsDirectory, result), 0); } + [TestMethod] + public void GetTargetFrameworkShouldReturnFrameworkProvidedInRunSettings() + { + string runSettingsXml = @" + + + 0 + x64 + Framework45 + + "; + + var testLoggerManager = new DummyTestLoggerManager(); + var framework = testLoggerManager.GetTargetFramework(runSettingsXml); + + Assert.AreEqual(framework.Name, ".NETFramework,Version=v4.5"); + } + [TestMethod] public void HandleTestRunMessageShouldInvokeTestRunMessageHandlerOfLoggers() { @@ -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"]); @@ -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"]); @@ -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"]); @@ -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( @@ -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( @@ -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( @@ -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( @@ -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"]); @@ -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"]); diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs index 7144b49a6c..fa8fd8907e 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs @@ -710,6 +710,54 @@ private void ValidateDateTimeInTrx(string trxFileName) } } + [TestMethod] + [DataRow("results")] + [DataRow("results.trx")] + public void CustomTrxFileNameShouldBeConstructedFromRelativeLogFilePrefixParameter(string prefixName) + { + this.parameters.Remove(TrxLoggerConstants.LogFileNameKey); + 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() + { + this.parameters.Remove(TrxLoggerConstants.LogFileNameKey); + 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); + } + + [TestMethod] + public void IntializeShouldThrowExceptionIfBothPrefixAndNameProvided() + { + this.parameters[TrxLoggerConstants.LogFileNameKey] = "results.trx"; + var trxPrefix = Path.Combine(Path.GetTempPath(), "results"); + this.parameters[TrxLoggerConstants.LogFilePrefixKey] = trxPrefix; + this.parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1"; + + Assert.ThrowsException(() => this.testableTrxLogger.Initialize(events.Object, this.parameters)); + } + private void ValidateTestIdAndNameInTrx(bool isMstestAdapter) { ObjectModel.TestCase testCase = CreateTestCase("TestCase");