Skip to content

Commit 44b5eac

Browse files
committed
Ensure single dedicated test run directory per test run
1 parent 5c6439d commit 44b5eac

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/Dibix.Testing/Utilities/TestResultComposer.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.IO;
55
using System.IO.Compression;
66
using System.Linq;
7-
using System.Reflection;
87
using Microsoft.VisualStudio.TestTools.UnitTesting;
98
using Newtonsoft.Json;
109
using Newtonsoft.Json.Serialization;
@@ -31,8 +30,7 @@ public TestResultComposer(TestContext testContext, bool useDedicatedTestResultsD
3130
this._testContext = testContext;
3231
this._useDedicatedTestResultsDirectory = useDedicatedTestResultsDirectory;
3332
this._defaultRunDirectory = testContext.TestRunResultsDirectory;
34-
string dedicatedRunDirectory = BuildDedicatedRunDirectory(testContext);
35-
this.RunDirectory = this._useDedicatedTestResultsDirectory ? dedicatedRunDirectory : this._defaultRunDirectory;
33+
this.RunDirectory = this._useDedicatedTestResultsDirectory ? TestRun.GetTestRunDirectory(testContext) : this._defaultRunDirectory;
3634
this.TestDirectory = Path.Combine(this.RunDirectory, "TestResults", testContext.TestName);
3735
this._expectedDirectory = Path.Combine(this.RunDirectory, ExpectedDirectoryName);
3836
this._actualDirectory = Path.Combine(this.RunDirectory, ActualDirectoryName);
@@ -217,15 +215,6 @@ private static void EnsureDirectory(string path)
217215
Directory.CreateDirectory(directory);
218216
}
219217

220-
private static string BuildDedicatedRunDirectory(TestContext testContext)
221-
{
222-
Assembly assembly = TestImplementationResolver.ResolveTestAssembly(testContext);
223-
string assemblyName = assembly.GetName().Name;
224-
string directoryName = $"Run_{DateTime.Now:yyyy-MM-dd HH_mm_ss}";
225-
string path = Path.Combine(Path.GetTempPath(), "TestResults", directoryName, assemblyName);
226-
return path;
227-
}
228-
229218
// Exposing the original enum System.Diagnostics.EventLogEntryType in the AddLastEventLogEntries method, causes the coverlet.collector to hang.
230219
// Might be related to: https://github.com/coverlet-coverage/coverlet/issues/1044
231220
public enum EventLogEntryType
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.IO;
4+
using System.Reflection;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace Dibix.Testing
8+
{
9+
internal static class TestRun
10+
{
11+
private static readonly ConcurrentDictionary<string, Lazy<string>> TestRunDirectoryMap = new ConcurrentDictionary<string, Lazy<string>>();
12+
13+
public static string GetTestRunDirectory(TestContext testContext)
14+
{
15+
// Unfortunately, MSTest does not provide a unique id to identity a test run, therefore we use the generated test run directory from MSTest
16+
string testRunIdentifier = testContext.TestRunDirectory;
17+
return TestRunDirectoryMap.GetOrAdd(testRunIdentifier, _ => new Lazy<string>(() => CreateTestRunDirectory(testContext))).Value;
18+
}
19+
20+
private static string CreateTestRunDirectory(TestContext testContext)
21+
{
22+
Assembly assembly = TestImplementationResolver.ResolveTestAssembly(testContext);
23+
string assemblyName = assembly.GetName().Name!;
24+
string directoryName = $"Run_{DateTime.Now:yyyy-MM-dd HH_mm_ss}";
25+
string path = Path.Combine(Path.GetTempPath(), "TestResults", directoryName, assemblyName);
26+
return path;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)