Skip to content

Commit

Permalink
Revert "Fix - dotnet test on a multi-target projects logs only the la…
Browse files Browse the repository at this point in the history
…st target (#1877)" (#1996)

This reverts commit 3769676.
  • Loading branch information
singhsarab authored Apr 26, 2019
1 parent 7dd4ef7 commit 77f88c6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 52 deletions.
Original file line number Diff line number Diff line change
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:fff", DateTimeFormatInfo.InvariantInfo);
return timeStamp.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
}

private void Initialize()
Expand Down
16 changes: 10 additions & 6 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
32 changes: 9 additions & 23 deletions test/Microsoft.TestPlatform.AcceptanceTests/CodeCoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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))
{
Expand Down
7 changes: 2 additions & 5 deletions test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Microsoft.TestPlatform.AcceptanceTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

[TestClass]
public class LoggerTests : AcceptanceTestBase
Expand All @@ -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);

Expand All @@ -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");
}

Expand All @@ -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);

Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Microsoft.TestPlatform.AcceptanceTests
{
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand All @@ -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]
Expand All @@ -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}");

Expand All @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/// <summary>
Expand Down

0 comments on commit 77f88c6

Please sign in to comment.