Skip to content

Commit af7ad68

Browse files
committed
Dedicated test output directory improvements
1 parent 072a299 commit af7ad68

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<DebugType>embedded</DebugType>
1414
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1515
<ContinuousIntegrationBuild Condition="'$(TF_BUILD)' == 'true'">true</ContinuousIntegrationBuild>
16+
<DefineConstants Condition="'$(ContinuousIntegrationBuild)' == 'true'">$(DefineConstants);CI_BUILD</DefineConstants>
1617

1718
<GitVersionBaseDirectory>$(MSBuildThisFileDirectory)</GitVersionBaseDirectory>
1819

src/Dibix.Testing/TestBase.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,26 @@ internal TestOutputWriter TestOutputHelper
6161
/// It might be desired to keep the test output even if all tests have passed, i.E. for investigation purposes.
6262
/// Setting this property to true, will store the output in the temp directory, so they are still available after each run.
6363
/// </summary>
64+
#if CI_BUILD
6465
protected virtual bool UseDedicatedTestResultsDirectory => true;
66+
#else
67+
protected virtual bool UseDedicatedTestResultsDirectory => false;
68+
#endif
6569
private TestResultComposer TestResultComposer
6670
{
6771
get => SafeGetProperty(ref this._testResultComposer);
6872
set => this._testResultComposer = value;
6973
}
70-
#endregion
74+
#endregion
7175

72-
#region Constructor
76+
#region Constructor
7377
protected TestBase()
7478
{
7579
this._assembly = this.GetType().Assembly;
7680
}
77-
#endregion
81+
#endregion
7882

79-
#region Public Methods
83+
#region Public Methods
8084
[TestInitialize]
8185
public async Task OnTestInitialize()
8286
{
@@ -86,9 +90,9 @@ public async Task OnTestInitialize()
8690

8791
await this.OnTestInitialized().ConfigureAwait(false);
8892
}
89-
#endregion
93+
#endregion
9094

91-
#region Protected Methods
95+
#region Protected Methods
9296
protected virtual Task OnTestInitialized() => Task.CompletedTask;
9397

9498
protected void WriteLine(string message) => this.TestOutputHelper.WriteLine(message);
@@ -152,9 +156,9 @@ protected static TException AssertThrows<TException>(Action action) where TExcep
152156
protected static Task Retry(Func<Task<bool>> retryMethod, TimeSpan timeout) => Retry(retryMethod, x => x, timeout);
153157
protected static Task<TResult> Retry<TResult>(Func<Task<TResult>> retryMethod, Func<TResult, bool> condition) => Retry(retryMethod, condition, TimeSpan.FromMinutes(30));
154158
protected static Task<TResult> Retry<TResult>(Func<Task<TResult>> retryMethod, Func<TResult, bool> condition, TimeSpan timeout) => retryMethod.Retry(condition, (int)TimeSpan.FromSeconds(1).TotalMilliseconds, (int)timeout.TotalMilliseconds);
155-
#endregion
159+
#endregion
156160

157-
#region Private Methods
161+
#region Private Methods
158162
private void OnFirstChanceException(object sender, FirstChanceExceptionEventArgs e)
159163
{
160164
if (e.Exception is UnitTestAssertException)
@@ -198,9 +202,9 @@ private static T SafeGetProperty<T>(ref T field, [CallerMemberName] string prope
198202

199203
return field;
200204
}
201-
#endregion
205+
#endregion
202206

203-
#region IDisposable Members
207+
#region IDisposable Members
204208
public void Dispose()
205209
{
206210
this.Dispose(true);
@@ -215,6 +219,6 @@ protected virtual void Dispose(bool disposing)
215219
TestResultComposer.Complete();
216220
}
217221
}
218-
#endregion
222+
#endregion
219223
}
220224
}

src/Dibix.Testing/Utilities/TestResultComposer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public TestResultComposer(TestContext testContext, bool useDedicatedTestResultsD
4040
public string AddFile(string fileName)
4141
{
4242
string path = Path.Combine(this._testDirectory, fileName);
43+
EnsureDirectory(path);
4344
this.RegisterFile(path, scopeIsTestRun: false);
4445
return path;
4546
}
@@ -100,7 +101,6 @@ private void RegisterFile(string path, bool scopeIsTestRun)
100101
if (files.Contains(path))
101102
throw new InvalidOperationException($"Test result file already registered: {path}");
102103

103-
EnsureDirectory(path);
104104
this._testContext.AddResultFile(path);
105105
files.Add(path);
106106
}
@@ -140,6 +140,7 @@ private static void WriteContentToFile(string path, string content)
140140
if (File.Exists(path))
141141
throw new InvalidOperationException($"Path exists already: {path}");
142142

143+
EnsureDirectory(path);
143144
File.WriteAllText(path, content);
144145
}
145146

@@ -195,8 +196,9 @@ private static string BuildDedicatedRunDirectory(TestContext testContext)
195196
{
196197
Assembly assembly = TestImplementationResolver.ResolveTestAssembly(testContext);
197198
string assemblyName = assembly.GetName().Name;
198-
string directoryName = new DirectoryInfo(testContext.TestRunDirectory).Name;
199-
string path = Path.Combine(Path.GetTempPath(), assemblyName, directoryName);
199+
string testRunDirectoryName = new DirectoryInfo(testContext.TestRunDirectory).Name;
200+
string directoryName = $"Run_{testRunDirectoryName.Split(' ').Last()}";
201+
string path = Path.Combine(Path.GetTempPath(), "TestResults", directoryName, assemblyName);
200202
return path;
201203
}
202204
}

0 commit comments

Comments
 (0)