Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/BasicChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
35 changes: 25 additions & 10 deletions src/OutputChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down