Skip to content

Commit 8bd7a3b

Browse files
committed
Add binary comparison option
- added net7.0 target - added CheckBinary function to well... check binary content - added GetOldBinaryContent which avoids the terminal and string.Join("\n") - added CheckOutputBinaryCore which avoids string.Replace and other undesirable operations on binary files
1 parent 3435d2e commit 8bd7a3b

3 files changed

Lines changed: 81 additions & 5 deletions

File tree

src/BasicChecks.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -25,6 +25,24 @@ public static void CheckString(
2525
);
2626
}
2727

28+
/// <summary> Verifies that the provided <paramref name="output" /> equals to the `outputDirectory/TestClass.TestMethod.bin` file. </summary>
29+
/// <param name="checkName"> If not null, checkName will be appended to the calling <paramref name="memberName" />. Intended to be used when having multiple checks in one method. </param>
30+
public static void CheckBinary(
31+
this OutputChecker t,
32+
byte[] output,
33+
string checkName = null,
34+
string fileExtension = "bin",
35+
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
36+
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null)
37+
{
38+
t.CheckOutputBinaryCore(
39+
output,
40+
checkName,
41+
$"{Path.GetFileNameWithoutExtension(sourceFilePath)}.{memberName}",
42+
fileExtension
43+
);
44+
}
45+
2846
/// <summary> Verifies that the provided <paramref name="output" /> equals to the `outputDirectory/TestClass.TestMethod.txt` file. File is compared line-by-line. </summary>
2947
/// <param name="checkName"> If not null, checkName will be appended to the calling <paramref name="memberName" />. Intended to be used when having multiple checks in one method. </param>
3048
public static void CheckLines(

src/CheckTestOutput.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.1;net6.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.1;net6.0;net7.0</TargetFrameworks>
55
<LangVersion>9</LangVersion>
66

77
<Description>Simple helper which checks that output of a test matches a file. If not matching, just git staging the new file will accept the new version.</Description>

src/OutputChecker.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.ComponentModel;
@@ -160,6 +160,11 @@ private string GetOldContent(string file)
160160
}
161161
}
162162

163+
private byte[] GetOldBinaryContent(string file)
164+
{
165+
return File.ReadAllBytes(file);
166+
}
167+
163168
private bool IsModified(string file)
164169
{
165170
// command `git ls-files --other --modified $file` returns the file name back iff it is modified or other (untracked)
@@ -242,14 +247,67 @@ internal void CheckOutputCore(string outputString, string checkName, string meth
242247
throw new Exception(
243248
$"{Path.GetFileName(filename)} has changed, the actual output differs from the previous accepted output:\n\n" +
244249
string.Join("\n", diff) + "\n\n" +
245-
"If this change OK? To let the test pass, stage the file in git. Confused? See https://github.com/exyi/CheckTestOutput/blob/master/trouble.md#changed-file\n"
250+
"Is this change OK? To let the test pass, stage the file in git. Confused? See https://github.com/exyi/CheckTestOutput/blob/master/trouble.md#changed-file\n"
251+
252+
);
253+
}
254+
}
255+
else
256+
{
257+
throw new Exception($"{Path.GetFileName(filename)} has changed, the previous accepted output differs from the actual output:\n\n{outputString}\n\nNote that CheckTestOutput could not use git on your system, so the \"UX\" is limited.");
258+
}
259+
}
260+
261+
internal void CheckOutputBinaryCore(byte[] outputBytes, string checkName, string method, string fileExtension = "bin")
262+
{
263+
Directory.CreateDirectory(CheckDirectory);
264+
265+
var filename = Path.Combine(CheckDirectory, (checkName == null ? method : $"{method}-{checkName}") + "." + fileExtension);
246266

267+
if (GetOldBinaryContent(filename).SequenceEqual(outputBytes))
268+
{
269+
// fine! Just check that the file is not changed - if it is changed or deleted, we rewrite
270+
if (IsModified(filename))
271+
{
272+
using (var t = File.Create(filename))
273+
{
274+
t.Write(outputBytes);
275+
}
276+
}
277+
return;
278+
}
279+
280+
if (DoesGitWork.Value)
281+
{
282+
using (var t = File.Create(filename))
283+
{
284+
t.Write(outputBytes);
285+
}
286+
287+
if (IsModified(filename))
288+
{
289+
if (IsNewFile(filename))
290+
{
291+
throw new Exception($"{Path.GetFileName(filename)} is not explicitly accepted - the file is untracked in git. To let this test pass, view the file and stage it. Confused? See https://github.com/exyi/CheckTestOutput/blob/master/trouble.md#untracked-file\n");
292+
}
293+
294+
295+
var diff = RunGitCommand("diff", filename);
296+
if (diff.All(string.IsNullOrEmpty))
297+
{
298+
// I guess fine from our perspective, but it's weird...
299+
Console.WriteLine($"CheckTestOutput warning: {Path.GetFileName(filename)} is modified, but the diff is empty.");
300+
return;
301+
}
302+
throw new Exception(
303+
$"{Path.GetFileName(filename)} has changed, the actual output differs from the previous accepted output!"
304+
+ "Is the change OK? To let the test pass, stage the file in git. Confused? See https://github.com/exyi/CheckTestOutput/blob/master/trouble.md#changed-file\n"
247305
);
248306
}
249307
}
250308
else
251309
{
252-
throw new Exception($"{Path.GetFileName(filename)}has changed, the previous accepted output differs from the actual output:\n\n{outputString}\n\nNote that CheckTestOutput could not use git on your system, so the \"UX\" is limited.");
310+
throw new Exception($"{Path.GetFileName(filename)} has changed, the previous accepted output differs from the actual output.");
253311
}
254312
}
255313
}

0 commit comments

Comments
 (0)