Skip to content

Commit

Permalink
refactor: use mutex for file determination (#1113)
Browse files Browse the repository at this point in the history
* feat: enable TestOutputXmlFileNameWithoutExtension run setting to setup a custom result file name rather than the name of the assembly to be tested

* refactor: ensure the file determination process is aware of multiple test runs in different processes by using a mutex for the assembly path

* chore: adjust name to TestOutputXmlFileName

* chore: take care of correct mutex name in case assembly path is null or empty

* chore: use name of file instead of path

* chore: fix merge issue

---------

Co-authored-by: Marcus Lorentschk <[email protected]>
  • Loading branch information
Lorilatschki and Lorilatschki authored Aug 3, 2023
1 parent b3b5f4a commit f3b5c9c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/NUnitTestAdapter/NUnitEngine/NUnitEngineAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
// ***********************************************************************

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

using NUnit.Engine;
using NUnit.VisualStudio.TestAdapter.Internal;
Expand Down Expand Up @@ -163,14 +165,30 @@ public void GenerateTestOutput(NUnitResults testResults, string assemblyPath, st
if (!settings.UseTestOutputXml)
return;

string path = GetXmlFilePath(testOutputXmlFolder, GetTestOutputFileName(assemblyPath), "xml");
var resultService = GetService<IResultService>();

// Following null argument should work for nunit3 format. Empty array is OK as well.
// If you decide to handle other formats in the runsettings, it needs more work.
var resultWriter = resultService.GetResultWriter("nunit3", null);
resultWriter.WriteResultFile(testResults.FullTopNode, path);
logger.Info($" Test results written to {path}");
using (Mutex mutex = new Mutex(false, string.IsNullOrWhiteSpace(assemblyPath) ? nameof(GenerateTestOutput) : Path.GetFileNameWithoutExtension(assemblyPath)))
{
bool received = false;
try
{
received = mutex.WaitOne();
string path = GetXmlFilePath(testOutputXmlFolder, GetTestOutputFileName(assemblyPath), "xml");

// Following null argument should work for nunit3 format. Empty array is OK as well.
// If you decide to handle other formats in the runsettings, it needs more work.
var resultWriter = resultService.GetResultWriter("nunit3", null);
resultWriter.WriteResultFile(testResults.FullTopNode, path);
logger.Info($" Test results written to {path}");
}
finally
{
if (received)
{
mutex.ReleaseMutex();
}
}
}
}

public string GetTestOutputFileName(string assemblyPath)
Expand Down

0 comments on commit f3b5c9c

Please sign in to comment.