Skip to content

Commit 853646d

Browse files
committed
Fix initial winmerge.bat being added to the wrong test context
1 parent 002f30d commit 853646d

5 files changed

Lines changed: 55 additions & 45 deletions

File tree

src/Dibix.Testing/Utilities/TestMethodTestResultFileComposer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ internal sealed class TestMethodTestResultFileComposer : TestResultFileComposer
1010
{
1111
private readonly string _resultTestsDirectory;
1212
private readonly string _normalizedTestName;
13+
private readonly TestContext _testContext;
1314

14-
private TestMethodTestResultFileComposer(string directory, string resultTestsDirectory, string normalizedTestName, TestContext testContext) : base(directory, testContext)
15+
private TestMethodTestResultFileComposer(string directory, string resultTestsDirectory, string normalizedTestName, TestContext testContext) : base(directory)
1516
{
1617
_resultTestsDirectory = resultTestsDirectory;
1718
_normalizedTestName = normalizedTestName;
19+
_testContext = testContext;
1820
}
1921

2022
public static TestMethodTestResultFileComposer Create(TestContext testContext, string resultTestsDirectory)
@@ -40,5 +42,13 @@ public void ZipTestOutput(string resultRunDirectory, IEnumerable<string> resultF
4042
}
4143
RegisterResultFile(path);
4244
}
45+
46+
public string AddResultFile(string fileName) => AddResultFile(fileName, _testContext);
47+
48+
public string AddResultFile(string fileName, string content) => AddResultFile(fileName, content, _testContext);
49+
50+
public string ImportResultFile(string filePath) => ImportResultFile(filePath, _testContext);
51+
52+
public void RegisterResultFile(string path) => RegisterResultFile(path, _testContext);
4353
}
4454
}

src/Dibix.Testing/Utilities/TestResultComposer.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,40 @@ namespace Dibix.Testing
77
{
88
public abstract class TestResultFileComposer
99
{
10-
private readonly TestContext _testContext;
11-
1210
public ICollection<string> ResultFiles { get; }
1311
public string ResultDirectory { get; }
1412

15-
protected TestResultFileComposer(string directory, TestContext testContext)
13+
protected TestResultFileComposer(string directory)
1614
{
1715
ResultDirectory = directory;
1816
ResultFiles = new List<string>();
19-
_testContext = testContext;
2017
}
2118

22-
public string AddResultFile(string fileName)
19+
public string AddResultFile(string fileName, TestContext testContext)
2320
{
2421
string path = Path.Combine(ResultDirectory, fileName);
2522
EnsureDirectory(path);
26-
RegisterResultFile(path);
23+
RegisterResultFile(path, testContext);
2724
OnFileNameRegistered(fileName);
2825
return path;
2926
}
3027

31-
public string AddResultFile(string fileName, string content)
28+
public string AddResultFile(string fileName, string content, TestContext testContext)
3229
{
33-
string path = AddResultFile(fileName);
30+
string path = AddResultFile(fileName, testContext);
3431
WriteContentToFile(path, content);
3532
return path;
3633
}
3734

38-
public string ImportResultFile(string filePath)
35+
public string ImportResultFile(string filePath, TestContext testContext)
3936
{
4037
string fileName = Path.GetFileName(filePath);
41-
string targetPath = AddResultFile(fileName);
38+
string targetPath = AddResultFile(fileName, testContext);
4239
File.Copy(filePath, targetPath);
4340
return targetPath;
4441
}
4542

46-
public void RegisterResultFile(string path)
43+
public void RegisterResultFile(string path, TestContext testContext)
4744
{
4845
if (path.Length > 255)
4946
{
@@ -57,7 +54,7 @@ public void RegisterResultFile(string path)
5754
throw new InvalidOperationException($"Test result file already registered: {path}");
5855

5956
ResultFiles.Add(path);
60-
_testContext.AddResultFile(path);
57+
testContext.AddResultFile(path);
6158
}
6259

6360
public static void WriteContentToFile(string path, string content)

src/Dibix.Testing/Utilities/TestResultFileManager.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ internal sealed class TestResultFileManager
1313
private readonly TestRunTestResultFileComposer _testRunFileComposer;
1414
private readonly TestMethodTestResultFileComposer _testMethodFileComposer;
1515
private readonly WinMergeComparisonComposer _winMergeComparisonComposer;
16+
private readonly TestContext _testContext;
1617
private readonly TestClassInstanceScope _scope;
1718
private bool _eventLogCollected;
1819

1920
public string RunDirectory => _testRunFileComposer.ResultDirectory;
2021
public string TestDirectory => _testMethodFileComposer.ResultDirectory;
2122

22-
private TestResultFileManager(TestRunTestResultFileComposer testRunFileComposer, TestMethodTestResultFileComposer testMethodFileComposer, WinMergeComparisonComposer winMergeComparisonComposer, TestClassInstanceScope scope)
23+
private TestResultFileManager(TestRunTestResultFileComposer testRunFileComposer, TestMethodTestResultFileComposer testMethodFileComposer, WinMergeComparisonComposer winMergeComparisonComposer, TestContext testContext, TestClassInstanceScope scope)
2324
{
2425
_testRunFileComposer = testRunFileComposer;
2526
_testMethodFileComposer = testMethodFileComposer;
2627
_winMergeComparisonComposer = winMergeComparisonComposer;
28+
_testContext = testContext;
2729
_scope = scope;
2830
}
2931

@@ -32,8 +34,8 @@ public static TestResultFileManager FromTestContext(TestContext testContext, boo
3234
TestRunTestResultFileComposer testRunFileComposer = TestRunTestResultFileComposer.Resolve(testContext, useDedicatedTestResultsDirectory);
3335
string resultTestsDirectory = Path.Combine(testRunFileComposer.ResultDirectory, "Tests");
3436
TestMethodTestResultFileComposer testMethodFileComposer = TestMethodTestResultFileComposer.Create(testContext, resultTestsDirectory);
35-
WinMergeComparisonComposer winMergeComparisonComposer = new WinMergeComparisonComposer(testMethodFileComposer, testRunFileComposer);
36-
TestResultFileManager resultFileComposer = new TestResultFileManager(testRunFileComposer, testMethodFileComposer, winMergeComparisonComposer, scope);
37+
WinMergeComparisonComposer winMergeComparisonComposer = new WinMergeComparisonComposer(testMethodFileComposer, testRunFileComposer, testContext);
38+
TestResultFileManager resultFileComposer = new TestResultFileManager(testRunFileComposer, testMethodFileComposer, winMergeComparisonComposer, testContext, scope);
3739
return resultFileComposer;
3840
}
3941

@@ -49,10 +51,10 @@ public string AddTestFile(string fileName)
4951

5052
public string ImportTestFile(string filePath) => _testMethodFileComposer.ImportResultFile(filePath);
5153

52-
public string AddTestRunFile(string fileName) => _testRunFileComposer.AddResultFile(fileName);
53-
public string AddTestRunFile(string fileName, string content) => _testRunFileComposer.AddResultFile(fileName, content);
54+
public string AddTestRunFile(string fileName) => _testRunFileComposer.AddResultFile(fileName, _testContext);
55+
public string AddTestRunFile(string fileName, string content) => _testRunFileComposer.AddResultFile(fileName, content, _testContext);
5456

55-
public string ImportTestRunFile(string filePath) => _testRunFileComposer.ImportResultFile(filePath);
57+
public string ImportTestRunFile(string filePath) => _testRunFileComposer.ImportResultFile(filePath, _testContext);
5658

5759
public void AddFileComparison(string expectedContent, string actualContent, string outputName, string extension) => _winMergeComparisonComposer.AddFileComparison(expectedContent, actualContent, outputName, extension);
5860

@@ -91,7 +93,7 @@ string FormatContent(EventLogEntry entry)
9193

9294
TestResultFileComposer composer = GetTargetTestResultFileComposer(_scope);
9395

94-
composer.AddResultFile("EventLogOptions.json", JsonConvert.SerializeObject(options, Formatting.Indented));
96+
composer.AddResultFile("EventLogOptions.json", JsonConvert.SerializeObject(options, Formatting.Indented), _testContext);
9597

9698
EventLog eventLog = new EventLog(options.LogName, options.MachineName, options.Source);
9799

@@ -100,7 +102,7 @@ string FormatContent(EventLogEntry entry)
100102
.Select(x => eventLog.Entries[x])
101103
.Where(x => MatchEventLogEntry(x, options.Type, options.Since))
102104
.Take(options.Count)
103-
.Each((x, i) => composer.AddResultFile($"EventLogEntry_{i + 1}_{x.EntryType}.txt", FormatContent(x)));
105+
.Each((x, i) => composer.AddResultFile($"EventLogEntry_{i + 1}_{x.EntryType}.txt", FormatContent(x), _testContext));
104106
}
105107

106108
private TestResultFileComposer GetTargetTestResultFileComposer(TestClassInstanceScope scope) => scope switch

src/Dibix.Testing/Utilities/TestRunTestResultFileComposer.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ namespace Dibix.Testing
1515
internal sealed class TestRunTestResultFileComposer : TestResultFileComposer
1616
{
1717
private static readonly ConcurrentDictionary<string, Lazy<TestRunTestResultFileComposer>> Cache = new ConcurrentDictionary<string, Lazy<TestRunTestResultFileComposer>>();
18-
private readonly TestContext _testContext;
1918
private readonly string _defaultRunDirectory;
2019
private readonly bool _useDedicatedTestResultsDirectory;
2120
private readonly ICollection<string> _deployedFiles;
2221
private readonly ICollection<string> _registeredFileNames;
2322

24-
private TestRunTestResultFileComposer(string directory, TestContext testContext, string defaultRunDirectory, bool useDedicatedTestResultsDirectory) : base(directory, testContext)
23+
private TestRunTestResultFileComposer(string directory, string defaultRunDirectory, bool useDedicatedTestResultsDirectory) : base(directory)
2524
{
26-
_testContext = testContext;
2725
_defaultRunDirectory = defaultRunDirectory;
2826
_deployedFiles = new HashSet<string>();
2927
_useDedicatedTestResultsDirectory = useDedicatedTestResultsDirectory;
@@ -36,10 +34,13 @@ public static TestRunTestResultFileComposer Resolve(TestContext testContext, boo
3634
string testRunIdentifier = testContext.TestRunDirectory;
3735

3836
// Use Lazy<T> to ensure the test run attachments are only written to disk once when running tests in parallel
39-
TestRunTestResultFileComposer instance = Cache.GetOrAdd(testRunIdentifier, _ => new Lazy<TestRunTestResultFileComposer>(() => Create(testContext, useDedicatedTestResultsDirectory))).Value;
37+
Lazy<TestRunTestResultFileComposer> value = Cache.GetOrAdd(testRunIdentifier, _ => new Lazy<TestRunTestResultFileComposer>(() => Create(testContext, useDedicatedTestResultsDirectory)));
38+
bool isFirstTest = !value.IsValueCreated;
39+
TestRunTestResultFileComposer instance = value.Value;
4040

4141
// Make test run attachments available for each test method
42-
instance.ImportResultFilesIfNecessary(testContext);
42+
if (!isFirstTest)
43+
instance.ImportResultFiles(testContext);
4344

4445
return instance;
4546
}
@@ -56,28 +57,25 @@ public void CopyTestOutput(IEnumerable<string> resultFiles)
5657

5758
protected override void OnFileNameRegistered(string fileName) => _registeredFileNames.Add(fileName);
5859

59-
private void Initialize()
60+
private void Initialize(TestContext testContext)
6061
{
61-
EnsureTestContextDump();
62-
EnsureEnvironmentDump();
62+
EnsureTestContextDump(testContext);
63+
EnsureEnvironmentDump(testContext);
6364
}
6465

65-
private void ImportResultFilesIfNecessary(TestContext currentTestContext)
66+
private void ImportResultFiles(TestContext testContext)
6667
{
67-
if (currentTestContext == _testContext)
68-
return;
69-
7068
foreach (string resultFile in ResultFiles)
71-
currentTestContext.AddResultFile(resultFile);
69+
testContext.AddResultFile(resultFile);
7270
}
7371

74-
private void EnsureTestContextDump() => AddResultFile("TestContext.json", JsonConvert.SerializeObject(_testContext, new JsonSerializerSettings
72+
private void EnsureTestContextDump(TestContext testContext) => AddResultFile("TestContext.json", JsonConvert.SerializeObject(testContext, new JsonSerializerSettings
7573
{
7674
Formatting = Formatting.Indented,
7775
ContractResolver = new TestContextContractResolver()
78-
}));
76+
}), testContext);
7977

80-
private void EnsureEnvironmentDump()
78+
private void EnsureEnvironmentDump(TestContext testContext)
8179
{
8280
IDictionary<string, object> environmentVariables = Environment.GetEnvironmentVariables()
8381
.Cast<DictionaryEntry>()
@@ -94,7 +92,7 @@ private void EnsureEnvironmentDump()
9492
}
9593

9694
string environment = sb.ToString();
97-
AddResultFile("Environment.txt", environment);
95+
AddResultFile("Environment.txt", environment, testContext);
9896
}
9997

10098
private void CopyFiles(IEnumerable<string> files, string targetDirectory)
@@ -120,8 +118,8 @@ private static TestRunTestResultFileComposer Create(TestContext testContext, boo
120118
{
121119
string defaultRunDirectory = testContext.TestRunResultsDirectory;
122120
string runDirectory = useDedicatedTestResultsDirectory ? CreateTestRunDirectory(testContext) : defaultRunDirectory;
123-
TestRunTestResultFileComposer instance = new TestRunTestResultFileComposer(runDirectory, testContext, defaultRunDirectory, useDedicatedTestResultsDirectory);
124-
instance.Initialize();
121+
TestRunTestResultFileComposer instance = new TestRunTestResultFileComposer(runDirectory, defaultRunDirectory, useDedicatedTestResultsDirectory);
122+
instance.Initialize(testContext);
125123
return instance;
126124
}
127125

src/Dibix.Testing/Utilities/WinMergeComparisonComposer.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
34

45
namespace Dibix.Testing
56
{
@@ -10,13 +11,15 @@ internal class WinMergeComparisonComposer
1011
private const string ActualDirectoryName = "actual";
1112
private readonly TestMethodTestResultFileComposer _testMethodFileComposer;
1213
private readonly TestRunTestResultFileComposer _testRunFileComposer;
14+
private readonly TestContext _testContext;
1315
private readonly string _expectedDirectory;
1416
private readonly string _actualDirectory;
1517

16-
public WinMergeComparisonComposer(TestMethodTestResultFileComposer testMethodFileComposer, TestRunTestResultFileComposer testRunFileComposer)
18+
public WinMergeComparisonComposer(TestMethodTestResultFileComposer testMethodFileComposer, TestRunTestResultFileComposer testRunFileComposer, TestContext testContext)
1719
{
1820
_testMethodFileComposer = testMethodFileComposer;
1921
_testRunFileComposer = testRunFileComposer;
22+
_testContext = testContext;
2023
_expectedDirectory = Path.Combine(testRunFileComposer.ResultDirectory, ExpectedDirectoryName);
2124
_actualDirectory = Path.Combine(testRunFileComposer.ResultDirectory, ActualDirectoryName);
2225
}
@@ -37,9 +40,9 @@ private void EnsureWinMergeStarter()
3740
return;
3841

3942
_testRunFileComposer.AddResultFile(fileName, $"""
40-
@echo off
41-
start winmergeU "{ExpectedDirectoryName}" "{ActualDirectoryName}"
42-
""");
43+
@echo off
44+
start winmergeU "{ExpectedDirectoryName}" "{ActualDirectoryName}"
45+
""", _testContext);
4346
}
4447
}
4548

@@ -61,7 +64,7 @@ private void EnsureFileComparisonContent(string path, string outputName, string
6164
private void EnsureFileComparisonContent(string path, string content)
6265
{
6366
TestResultFileComposer.WriteContentToFile(path, content);
64-
_testMethodFileComposer.RegisterResultFile(path);
67+
_testMethodFileComposer.RegisterResultFile(path, _testContext);
6568
}
6669
}
6770
}

0 commit comments

Comments
 (0)