Skip to content

Commit

Permalink
Persist test output on successful test
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nquistadore committed May 31, 2022
1 parent 305e848 commit 0cb800f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Dibix.Testing/Utilities/TestResultComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public void Complete()
}
}
this.RegisterFile(path);

this.PersistTestOutputOnSuccessfulTest();
}

#if NETCOREAPP
Expand Down Expand Up @@ -133,12 +135,47 @@ private void AddLastEventLogEntries(EventLogEntryType eventLogEntryType, int cou
{x.Message}"));
}

// The test run directory is cleaned up, if all tests in the current run have passed.
// Therefore we copy the test output to a dedicated directory, if specified.
// Ultimately, this is done once after the whole test run is completed, to determine, if all tests have actually passed.
// Unfortunately, there is no way for us at this level to execute code with a test context, after the whole test run has completed.
// So this is executed after each test, that has passed, without knowing, if there is another failed test in the run,
// preventing the original test results folder from getting cleaned up.
private void PersistTestOutputOnSuccessfulTest()
{
if (this._testContext.CurrentTestOutcome != UnitTestOutcome.Passed)
return;

string privateTestResultsDirectory = (string)this._testContext.Properties["PrivateTestResultsDirectory"];
if (privateTestResultsDirectory == null)
return;

CopyDirectoryContents(this._runDirectory, privateTestResultsDirectory);
}

private static void WriteContentToFile(string path, string content)
{
if (File.Exists(path))
throw new InvalidOperationException($"Path exists already: {path}");

File.WriteAllText(path, content);
}

private static void CopyDirectoryContents(string sourceDirectory, string targetDirectory)
{
foreach (string directory in Directory.EnumerateDirectories(sourceDirectory, "*", SearchOption.AllDirectories))
{
string relativeDirectoryPath = directory.Substring(sourceDirectory.Length + 1);
string targetDirectoryPath = Path.Combine(targetDirectory, relativeDirectoryPath);
Directory.CreateDirectory(targetDirectoryPath);
}

foreach (string file in Directory.EnumerateFiles(sourceDirectory, "*", SearchOption.AllDirectories))
{
string relativeFilePath = file.Substring(sourceDirectory.Length + 1);
string targetFilePath = Path.Combine(targetDirectory, relativeFilePath);
File.Copy(file, targetFilePath);
}
}
}
}

0 comments on commit 0cb800f

Please sign in to comment.