From 77f88c661dc7ca4a076efab015e9454c7bba3857 Mon Sep 17 00:00:00 2001 From: Sarabjot Singh Date: Fri, 26 Apr 2019 19:59:32 +0530 Subject: [PATCH] Revert "Fix - dotnet test on a multi-target projects logs only the last target (#1877)" (#1996) This reverts commit 37696768786ef1018f0b4499eab9ca27af141057. --- .../ObjectModel/TestRun.cs | 2 +- .../TrxLogger.cs | 16 ++++++---- .../CodeCoverageTests.cs | 32 ++++++------------- .../LoggerTests.cs | 7 ++-- .../ResultsDirectoryTests.cs | 17 ++++------ .../TrxLoggerTests.cs | 7 +--- 6 files changed, 29 insertions(+), 52 deletions(-) diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/TestRun.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/TestRun.cs index 00aad9b8e5..94e9449af8 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/TestRun.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/TestRun.cs @@ -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:fff", DateTimeFormatInfo.InvariantInfo); + return timeStamp.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo); } private void Initialize() diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs index 2f7c6d8841..fe26288e64 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs @@ -441,13 +441,17 @@ private void HandleSkippedTest(ObjectModel.TestResult rsTestResult) private void DeriveTrxFilePath() { - if (this.parametersDictionary != null && - this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue) && - !string.IsNullOrWhiteSpace(logFileNameValue)) + if (this.parametersDictionary != null) { - string logFileNameWithoutExt = Path.GetFileNameWithoutExtension(logFileNameValue); - logFileNameValue = logFileNameValue.Replace(logFileNameWithoutExt, logFileNameWithoutExt + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss-fff", DateTimeFormatInfo.InvariantInfo)); - this.trxFilePath = Path.Combine(this.testResultsDirPath, logFileNameValue); + 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 { diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/CodeCoverageTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/CodeCoverageTests.cs index 4da1842c88..d57fa665a6 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/CodeCoverageTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/CodeCoverageTests.cs @@ -59,27 +59,14 @@ private void CollectCodeCoverage(RunnerInfo runnerInfo, string targetPlatform, b { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo); - var trxFileName = Guid.NewGuid().ToString(); - var trxFileNamePattern = trxFileName + "*.trx"; - var arguments = CreateArguments(runnerInfo, targetPlatform, withRunsettings, trxFileName); + var arguments = CreateArguments(runnerInfo, targetPlatform, withRunsettings, out var trxFilePath); - // Delete existing trx files - var dir = new DirectoryInfo(this.resultsDirectory); - if (dir.Exists) - { - foreach (var file in dir.EnumerateFiles(trxFileNamePattern)) - { - file.Delete(); - } - } - - // Invoke tests this.InvokeVsTest(arguments); - // Validate this.ValidateSummaryStatus(1, 1, 1); - var actualCoverageFile = CodeCoverageTests.GetCoverageFileNameFromTrx(trxFileNamePattern, this.resultsDirectory); - Console.WriteLine($@"Coverage file: {actualCoverageFile} Results directory: {resultsDirectory} trxfile pattern: {trxFileNamePattern}"); + + var actualCoverageFile = CodeCoverageTests.GetCoverageFileNameFromTrx(trxFilePath, resultsDirectory); + Console.WriteLine($@"Coverage file: {actualCoverageFile} Results directory: {resultsDirectory} trxfile: {trxFilePath}"); Assert.IsTrue(File.Exists(actualCoverageFile), "Coverage file not found: {0}", actualCoverageFile); // Microsoft.VisualStudio.Coverage.Analysis assembly not avaialble for .NET Core. @@ -89,7 +76,8 @@ private void CollectCodeCoverage(RunnerInfo runnerInfo, string targetPlatform, b Directory.Delete(this.resultsDirectory, true); } - private string CreateArguments(RunnerInfo runnerInfo, string targetPlatform, bool withRunsettings, string trxFileName) + private string CreateArguments(RunnerInfo runnerInfo, string targetPlatform, bool withRunsettings, + out string trxFilePath) { var assemblyPaths = this.GetAssetFullPath(assemblyName); string runSettings = Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory, @@ -105,7 +93,7 @@ private string CreateArguments(RunnerInfo runnerInfo, string targetPlatform, boo $" /TestAdapterPath:{traceDataCollectorDir}"); arguments = string.Concat(arguments, $" /Platform:{targetPlatform}"); - var trxFilePath = Path.Combine(this.resultsDirectory, trxFileName + ".trx"); + trxFilePath = Path.Combine(this.resultsDirectory, Guid.NewGuid() + ".trx"); arguments = string.Concat(arguments, " /logger:trx;logfilename=" + trxFilePath); if (withRunsettings) @@ -160,11 +148,9 @@ private void AssertModuleCoverageCollected(CoverageDS coverageDS) } #endif - private static string GetCoverageFileNameFromTrx(string trxFileNamePattern, string resultsDirectory) + private static string GetCoverageFileNameFromTrx(string trxFilePath, string resultsDirectory) { - var trxFiles = Directory.EnumerateFiles(resultsDirectory, trxFileNamePattern); - Assert.IsTrue(trxFiles.Any(), "Trx file with pattern: {0} not found", trxFileNamePattern); - var trxFilePath = trxFiles.First(); + Assert.IsTrue(File.Exists(trxFilePath), "Trx file not found: {0}", trxFilePath); XmlDocument doc = new XmlDocument(); using (var trxStream = new FileStream(trxFilePath, FileMode.Open, FileAccess.Read)) { diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs index 88b36b44b6..7eb8b5e08b 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs @@ -8,7 +8,6 @@ namespace Microsoft.TestPlatform.AcceptanceTests { using Microsoft.VisualStudio.TestTools.UnitTesting; - using System.Linq; [TestClass] public class LoggerTests : AcceptanceTestBase @@ -21,7 +20,6 @@ public void TrxLoggerWithFriendlyNameShouldProperlyOverwriteFile(RunnerInfo runn var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue); var trxFileName = "TestResults.trx"; - var trxFileNamePattern = "TestResults*.trx"; arguments = string.Concat(arguments, $" /logger:\"trx;LogFileName={trxFileName}\""); this.InvokeVsTest(arguments); @@ -30,7 +28,7 @@ public void TrxLoggerWithFriendlyNameShouldProperlyOverwriteFile(RunnerInfo runn arguments = string.Concat(arguments, " /testcasefilter:Name~Pass"); this.InvokeVsTest(arguments); - var trxLogFilePath = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "TestResults"), trxFileNamePattern).First(); + var trxLogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "TestResults", trxFileName); Assert.IsTrue(IsValidXml(trxLogFilePath), "Invalid content in Trx log file"); } @@ -42,7 +40,6 @@ public void TrxLoggerWithExecutorUriShouldProperlyOverwriteFile(RunnerInfo runne var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue); var trxFileName = "TestResults.trx"; - var trxFileNamePattern = "TestResults*.trx"; arguments = string.Concat(arguments, $" /logger:\"logger://Microsoft/TestPlatform/TrxLogger/v1;LogFileName{trxFileName}\""); this.InvokeVsTest(arguments); @@ -51,7 +48,7 @@ public void TrxLoggerWithExecutorUriShouldProperlyOverwriteFile(RunnerInfo runne arguments = string.Concat(arguments, " /testcasefilter:Name~Pass"); this.InvokeVsTest(arguments); - var trxLogFilePath = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "TestResults"), trxFileNamePattern).First(); + var trxLogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "TestResults", trxFileName); Assert.IsTrue(IsValidXml(trxLogFilePath), "Invalid content in Trx log file"); } diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs index 262794a8c6..0f73ba33af 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/ResultsDirectoryTests.cs @@ -4,7 +4,6 @@ namespace Microsoft.TestPlatform.AcceptanceTests { using System.IO; - using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -17,22 +16,18 @@ public void TrxFileShouldBeCreatedInResultsDirectory(RunnerInfo runnerInfo) { AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo); var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue); - var trxFileName = "TestResultsbla.trx"; - var trxFileNamePattern = "TestResultsbla*.trx"; + var trxFileName = "TestResults.trx"; var resultsDir = Path.GetTempPath(); + var trxFilePath = Path.Combine(resultsDir, trxFileName); arguments = string.Concat(arguments, $" /logger:\"trx;LogFileName={trxFileName}\""); arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}"); // Delete if already exists - var dir = new DirectoryInfo(resultsDir); - foreach (var file in dir.EnumerateFiles(trxFileNamePattern)) - { - file.Delete(); - } + File.Delete(trxFilePath); this.InvokeVsTest(arguments); - Assert.IsTrue(Directory.EnumerateFiles(resultsDir, trxFileNamePattern).Any(), $"Expected Trx file with pattern: {trxFileNamePattern} not created in results directory"); + Assert.IsTrue(File.Exists(trxFilePath), $"Expected Trx file: {trxFilePath} not created in results directory"); } [TestMethod] @@ -44,10 +39,10 @@ public void ResultsDirectoryRelativePathShouldWork(RunnerInfo runnerInfo) var arguments = PrepareArguments(this.GetSampleTestAssembly(), this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue); var trxFileName = "TestResults.trx"; - var trxFileNamePattern = "TestResults*.trx"; var relativeDirectory = @"relative\directory"; var resultsDirectory = Path.Combine(Directory.GetCurrentDirectory(), relativeDirectory); + var trxFilePath = Path.Combine(resultsDirectory , trxFileName); arguments = string.Concat(arguments, $" /logger:\"trx;LogFileName={trxFileName}\""); arguments = string.Concat(arguments, $" /ResultsDirectory:{relativeDirectory}"); @@ -58,7 +53,7 @@ public void ResultsDirectoryRelativePathShouldWork(RunnerInfo runnerInfo) this.InvokeVsTest(arguments); - Assert.IsTrue(Directory.EnumerateFiles(resultsDirectory, trxFileNamePattern).Any(), $"Expected Trx file with pattern: { trxFileNamePattern} not created in results directory"); + Assert.IsTrue(File.Exists(trxFilePath), $"Expected Trx file: {trxFilePath} not created in results directory"); } } } diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs index 0f66b40a56..51e16e0e5b 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs @@ -637,12 +637,7 @@ public void CustomTrxFileNameShouldConstructFromLogFileParameter() { this.MakeTestRunComplete(); - string expectedFileNameWithoutTimestamp = Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, TrxLoggerTests.DefaultLogFileNameParameterValue); - string fileName = Path.GetFileNameWithoutExtension(this.testableTrxLogger.trxFile); - string actualFileNameWithoutTimestamp = this.testableTrxLogger.trxFile.Replace(fileName, fileName.Split('_')[0]); - - Assert.AreNotEqual(expectedFileNameWithoutTimestamp, this.testableTrxLogger.trxFile, "Expected time stamp to appear in file name"); - Assert.AreEqual(expectedFileNameWithoutTimestamp, actualFileNameWithoutTimestamp, "Trx file name should construct from log file parameter"); + Assert.AreEqual(Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, TrxLoggerTests.DefaultLogFileNameParameterValue), this.testableTrxLogger.trxFile, "Wrong Trx file name"); } ///