Skip to content

Commit

Permalink
Fix - dotnet test on a multi-target projects logs only the last target (
Browse files Browse the repository at this point in the history
#1877)

* Fix - dotnet test on a multi-target projects logs only the last target

* Fixing acceptance tests
  • Loading branch information
abhishkk committed Dec 28, 2018
1 parent 5f00329 commit 3769676
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 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", DateTimeFormatInfo.InvariantInfo);
return timeStamp.ToString("yyyy-MM-dd HH:mm:ss:fff", DateTimeFormatInfo.InvariantInfo);
}

private void Initialize()
Expand Down
16 changes: 6 additions & 10 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,17 +441,13 @@ private void HandleSkippedTest(ObjectModel.TestResult rsTestResult)

private void DeriveTrxFilePath()
{
if (this.parametersDictionary != null)
if (this.parametersDictionary != null &&
this.parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string logFileNameValue) &&
!string.IsNullOrWhiteSpace(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();
}
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);
}
else
{
Expand Down
32 changes: 23 additions & 9 deletions test/Microsoft.TestPlatform.AcceptanceTests/CodeCoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,27 @@ private void CollectCodeCoverage(RunnerInfo runnerInfo, string targetPlatform, b
{
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);

var arguments = CreateArguments(runnerInfo, targetPlatform, withRunsettings, out var trxFilePath);
var trxFileName = Guid.NewGuid().ToString();
var trxFileNamePattern = trxFileName + "*.trx";
var arguments = CreateArguments(runnerInfo, targetPlatform, withRunsettings, trxFileName);

// 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(trxFilePath, resultsDirectory);
Console.WriteLine($@"Coverage file: {actualCoverageFile} Results directory: {resultsDirectory} trxfile: {trxFilePath}");
var actualCoverageFile = CodeCoverageTests.GetCoverageFileNameFromTrx(trxFileNamePattern, this.resultsDirectory);
Console.WriteLine($@"Coverage file: {actualCoverageFile} Results directory: {resultsDirectory} trxfile pattern: {trxFileNamePattern}");
Assert.IsTrue(File.Exists(actualCoverageFile), "Coverage file not found: {0}", actualCoverageFile);

// Microsoft.VisualStudio.Coverage.Analysis assembly not avaialble for .NET Core.
Expand All @@ -76,8 +89,7 @@ private void CollectCodeCoverage(RunnerInfo runnerInfo, string targetPlatform, b
Directory.Delete(this.resultsDirectory, true);
}

private string CreateArguments(RunnerInfo runnerInfo, string targetPlatform, bool withRunsettings,
out string trxFilePath)
private string CreateArguments(RunnerInfo runnerInfo, string targetPlatform, bool withRunsettings, string trxFileName)
{
var assemblyPaths = this.GetAssetFullPath(assemblyName);
string runSettings = Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory,
Expand All @@ -93,7 +105,7 @@ private string CreateArguments(RunnerInfo runnerInfo, string targetPlatform, boo
$" /TestAdapterPath:{traceDataCollectorDir}");
arguments = string.Concat(arguments, $" /Platform:{targetPlatform}");

trxFilePath = Path.Combine(this.resultsDirectory, Guid.NewGuid() + ".trx");
var trxFilePath = Path.Combine(this.resultsDirectory, trxFileName + ".trx");
arguments = string.Concat(arguments, " /logger:trx;logfilename=" + trxFilePath);

if (withRunsettings)
Expand Down Expand Up @@ -148,9 +160,11 @@ private void AssertModuleCoverageCollected(CoverageDS coverageDS)
}
#endif

private static string GetCoverageFileNameFromTrx(string trxFilePath, string resultsDirectory)
private static string GetCoverageFileNameFromTrx(string trxFileNamePattern, string resultsDirectory)
{
Assert.IsTrue(File.Exists(trxFilePath), "Trx file not found: {0}", trxFilePath);
var trxFiles = Directory.EnumerateFiles(resultsDirectory, trxFileNamePattern);
Assert.IsTrue(trxFiles.Any(), "Trx file with pattern: {0} not found", trxFileNamePattern);
var trxFilePath = trxFiles.First();
XmlDocument doc = new XmlDocument();
using (var trxStream = new FileStream(trxFilePath, FileMode.Open, FileAccess.Read))
{
Expand Down
7 changes: 5 additions & 2 deletions test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Microsoft.TestPlatform.AcceptanceTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

[TestClass]
public class LoggerTests : AcceptanceTestBase
Expand All @@ -20,6 +21,7 @@ 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 @@ -28,7 +30,7 @@ public void TrxLoggerWithFriendlyNameShouldProperlyOverwriteFile(RunnerInfo runn
arguments = string.Concat(arguments, " /testcasefilter:Name~Pass");
this.InvokeVsTest(arguments);

var trxLogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "TestResults", trxFileName);
var trxLogFilePath = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "TestResults"), trxFileNamePattern).First();
Assert.IsTrue(IsValidXml(trxLogFilePath), "Invalid content in Trx log file");
}

Expand All @@ -40,6 +42,7 @@ 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 @@ -48,7 +51,7 @@ public void TrxLoggerWithExecutorUriShouldProperlyOverwriteFile(RunnerInfo runne
arguments = string.Concat(arguments, " /testcasefilter:Name~Pass");
this.InvokeVsTest(arguments);

var trxLogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "TestResults", trxFileName);
var trxLogFilePath = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "TestResults"), trxFileNamePattern).First();
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,6 +4,7 @@
namespace Microsoft.TestPlatform.AcceptanceTests
{
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand All @@ -16,18 +17,22 @@ 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 = "TestResults.trx";
var trxFileName = "TestResultsbla.trx";
var trxFileNamePattern = "TestResultsbla*.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
File.Delete(trxFilePath);
var dir = new DirectoryInfo(resultsDir);
foreach (var file in dir.EnumerateFiles(trxFileNamePattern))
{
file.Delete();
}

this.InvokeVsTest(arguments);

Assert.IsTrue(File.Exists(trxFilePath), $"Expected Trx file: {trxFilePath} not created in results directory");
Assert.IsTrue(Directory.EnumerateFiles(resultsDir, trxFileNamePattern).Any(), $"Expected Trx file with pattern: {trxFileNamePattern} not created in results directory");
}

[TestMethod]
Expand All @@ -39,10 +44,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 @@ -53,7 +58,7 @@ public void ResultsDirectoryRelativePathShouldWork(RunnerInfo runnerInfo)

this.InvokeVsTest(arguments);

Assert.IsTrue(File.Exists(trxFilePath), $"Expected Trx file: {trxFilePath} not created in results directory");
Assert.IsTrue(Directory.EnumerateFiles(resultsDirectory, trxFileNamePattern).Any(), $"Expected Trx file with pattern: { trxFileNamePattern} not created in results directory");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,12 @@ public void CustomTrxFileNameShouldConstructFromLogFileParameter()
{
this.MakeTestRunComplete();

Assert.AreEqual(Path.Combine(TrxLoggerTests.DefaultTestRunDirectory, TrxLoggerTests.DefaultLogFileNameParameterValue), this.testableTrxLogger.trxFile, "Wrong Trx file name");
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");
}

/// <summary>
Expand Down

0 comments on commit 3769676

Please sign in to comment.