From 0cb800faa4646e46d35cbc1cbcbb84a3fa66b692 Mon Sep 17 00:00:00 2001 From: Tommy <info@flexforge.me> Date: Tue, 31 May 2022 12:17:34 +0200 Subject: [PATCH] Persist test output on successful test --- .../Utilities/TestResultComposer.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Dibix.Testing/Utilities/TestResultComposer.cs b/src/Dibix.Testing/Utilities/TestResultComposer.cs index 117d4a75..080ba666 100644 --- a/src/Dibix.Testing/Utilities/TestResultComposer.cs +++ b/src/Dibix.Testing/Utilities/TestResultComposer.cs @@ -60,6 +60,8 @@ public void Complete() } } this.RegisterFile(path); + + this.PersistTestOutputOnSuccessfulTest(); } #if NETCOREAPP @@ -133,6 +135,24 @@ 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)) @@ -140,5 +160,22 @@ private static void WriteContentToFile(string path, string content) 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); + } + } } } \ No newline at end of file