Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class HtmlLogger : ITestLoggerWithParameters
private readonly IFileHelper _fileHelper;
private readonly XmlObjectSerializer _xmlSerializer;
private readonly IHtmlTransformer _htmlTransformer;
private static readonly object FileCreateLockObject = new();
private Dictionary<string, string?>? _parametersDictionary;

public HtmlLogger()
Expand Down Expand Up @@ -293,7 +292,7 @@ public void TestRunCompleteHandler(object? sender, TestRunCompleteEventArgs e)
logFilePrefixValue = logFilePrefixValue + "_" + framework;
}

logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + $".{HtmlLoggerConstants.HtmlFileExtension}";
logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMdd_HHmmss.fffffff", DateTimeFormatInfo.InvariantInfo) + $".{HtmlLoggerConstants.HtmlFileExtension}";
Comment thread
nohwnd marked this conversation as resolved.
Resolved
HtmlFilePath = Path.Combine(TestResultsDirPath!, logFilePrefixValue);
}
else
Expand Down Expand Up @@ -356,13 +355,14 @@ private string GenerateUniqueFilePath(string fileName, string fileExtension)
{
var fileNameWithIter = i == 0 ? fileName : Path.GetFileNameWithoutExtension(fileName) + $"[{i}]";
Comment thread
nohwnd marked this conversation as resolved.
Resolved
fullFilePath = Path.Combine(TestResultsDirPath!, $"TestResult_{fileNameWithIter}.{fileExtension}");
lock (FileCreateLockObject)
try
{
if (!File.Exists(fullFilePath))
{
using var _ = File.Create(fullFilePath);
return fullFilePath;
}
using var _ = new FileStream(fullFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
return fullFilePath;
}
catch (IOException) when (File.Exists(fullFilePath))
{
// File already exists (another process created it), try next iteration.
Comment thread
nohwnd marked this conversation as resolved.
Resolved
}
}

Expand All @@ -371,7 +371,7 @@ private string GenerateUniqueFilePath(string fileName, string fileExtension)

private static string FormatDateTimeForRunName(DateTime timeStamp)
{
return timeStamp.ToString("yyyyMMdd_HHmmss", DateTimeFormatInfo.InvariantInfo);
return timeStamp.ToString("yyyyMMdd_HHmmss.fffffff", DateTimeFormatInfo.InvariantInfo);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,21 @@ public void TestCompleteHandlerShouldNotDivideByZeroWhenThereAre0TestResults()
Assert.AreEqual(0, _htmlLogger.TestRunDetails.Summary.PassPercentage);
}

[TestMethod]
public void XmlFilePathShouldContainFractionalSecondsForCrossProcessUniqueness()
{
_htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, null, TimeSpan.Zero));

Assert.IsNotNull(_htmlLogger.XmlFilePath);
var fileName = Path.GetFileNameWithoutExtension(_htmlLogger.XmlFilePath);

// The filename should contain a fractional-seconds timestamp: yyyyMMdd_HHmmss.fffffff
// e.g., TestResult_user_MACHINE_20260324_065512.1234567
StringAssert.Matches(fileName, new System.Text.RegularExpressions.Regex(
@"\d{8}_\d{6}\.\d{7}$"),
$"Filename should end with yyyyMMdd_HHmmss.fffffff pattern: {fileName}");
Comment thread
nohwnd marked this conversation as resolved.
Resolved
}

private static TestCase CreateTestCase(string testCaseName)
{
return new TestCase(testCaseName, new Uri("some://uri"), "DummySourceFileName");
Expand Down