From f84a92989fe406b9f1e9e3d417eefb287885c13c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herceg?= Date: Thu, 16 Jan 2025 14:31:13 +0100 Subject: [PATCH] Added an option to have multiple valid outputs --- src/BasicChecks.cs | 6 ++++-- src/OutputChecker.cs | 35 +++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/BasicChecks.cs b/src/BasicChecks.cs index 821b5a3..4a48768 100644 --- a/src/BasicChecks.cs +++ b/src/BasicChecks.cs @@ -13,13 +13,15 @@ public static void CheckString( string checkName = null, string fileExtension = "txt", [System.Runtime.CompilerServices.CallerMemberName] string memberName = null, - [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null) + [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null, + bool allowAlternatives = false) { t.CheckOutputCore( output, checkName, $"{Path.GetFileNameWithoutExtension(sourceFilePath)}.{memberName}", - fileExtension + fileExtension, + allowAlternatives ); } diff --git a/src/OutputChecker.cs b/src/OutputChecker.cs index b357bea..2e717b3 100644 --- a/src/OutputChecker.cs +++ b/src/OutputChecker.cs @@ -257,27 +257,42 @@ public string SanitizeString(string outputString) return outputString; } - internal void CheckOutputCore(string outputString, string checkName, string method, string fileExtension = "txt") + internal void CheckOutputCore(string outputString, string checkName, string method, string fileExtension = "txt", bool allowAlternatives = false) { outputString = outputString.Replace("\r\n", "\n").TrimEnd('\n'); outputString = SanitizeString(outputString); Directory.CreateDirectory(CheckDirectory); - var filename = Path.Combine(CheckDirectory, (checkName == null ? method : $"{method}-{checkName}") + "." + fileExtension); - - if (GetOldContent(filename)?.TrimEnd('\n') == outputString) + var alternativeIndex = 0; + string filename; + while (true) { - // fine! Just check that the file is not changed - if it is changed or deleted, we rewrite - if (IsModified(filename)) + filename = !allowAlternatives + ? Path.Combine(CheckDirectory, (checkName == null ? method : $"{method}-{checkName}") + "." + fileExtension) + : Path.Combine(CheckDirectory, (checkName == null ? method : $"{method}-{checkName}") + $"-alt{alternativeIndex:000}." + fileExtension); + + if (GetOldContent(filename)?.TrimEnd('\n') == outputString) { - using (var t = File.CreateText(filename)) + // fine! Just check that the file is not changed - if it is changed or deleted, we rewrite + if (IsModified(filename)) { - t.Write(outputString); - t.Write("\n"); + using (var t = File.CreateText(filename)) + { + t.Write(outputString); + t.Write("\n"); + } } + + return; } - return; + + if (allowAlternatives && File.Exists(filename)) + { + alternativeIndex++; + continue; + } + break; } if (DoesGitWork.Value)