Skip to content

Commit 454c48b

Browse files
committed
Yep, ASCII has its own problems indeed.
- added more tests to check characters converted to bytes with different encoding
1 parent 9968f73 commit 454c48b

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

exampleTest/BinaryTest.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Text;
45
using Xunit;
56

67
namespace CheckTestOutput.Example
@@ -25,9 +26,10 @@ public void CheckModifiedBinaryData()
2526
byte[] previousData = File.ReadAllBytes(imgPath);
2627
byte[] modifiedData = previousData.Append((byte)64).ToArray();
2728

28-
//File contents do not match
29+
// File contents do not match
2930
Assert.Throws<Exception>(() => check.CheckBinary(modifiedData));
3031

32+
// Return back the original content
3133
File.WriteAllBytes(Path.Combine(check.CheckDirectory, $"{nameof(BinaryTest)}.{nameof(CheckModifiedBinaryData)}.bin"), previousData);
3234
}
3335

@@ -38,5 +40,23 @@ public void CheckNonexistentBinaryData()
3840
// File does not exist
3941
Assert.Throws<ArgumentNullException>(() => check.CheckBinary(imageBytes));
4042
}
43+
44+
[Fact]
45+
public void CheckUTF8TextConvertedToBinaryData()
46+
{
47+
const string TEXT = "( ͡° ͜ʖ ͡°)";
48+
49+
check.CheckBinary(Encoding.UTF8.GetBytes(TEXT));
50+
Assert.True(true);
51+
}
52+
53+
54+
[Fact]
55+
public void CheckUTF8TextConvertedWithASCIIBinaryDataThrows()
56+
{
57+
const string TEXT = "( ͡° ͜ʖ ͡°)";
58+
59+
Assert.Throws<Exception>(() => check.CheckBinary(Encoding.ASCII.GetBytes(TEXT)));
60+
}
4161
}
4262
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
( ͡° ͜ʖ ͡°)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
( ͡° ͜ʖ ͡°)

src/OutputChecker.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public OutputChecker(
8383

8484
private readonly Lazy<bool> DoesGitWork;
8585

86-
private Process StartGitProcess(System.Text.Encoding encoding, params string[] args)
86+
private Process StartGitProcess(params string[] args)
8787
{
8888
#if DEBUG
8989
Console.WriteLine("Running git command: " + string.Join(" ", args));
@@ -97,8 +97,8 @@ private Process StartGitProcess(System.Text.Encoding encoding, params string[] a
9797
RedirectStandardError = true,
9898
RedirectStandardInput = true,
9999
CreateNoWindow = true,
100-
StandardOutputEncoding = encoding,
101-
StandardErrorEncoding = encoding,
100+
StandardOutputEncoding = System.Text.Encoding.UTF8,
101+
StandardErrorEncoding = System.Text.Encoding.UTF8,
102102
};
103103
foreach (var a in args)
104104
procInfo.ArgumentList.Add(a);
@@ -125,7 +125,7 @@ private void HandleProcessExit(Process proc, Task outputReaderTask, params strin
125125

126126
private string[] RunGitCommand(params string[] args)
127127
{
128-
var proc = StartGitProcess(System.Text.Encoding.UTF8, args);
128+
var proc = StartGitProcess(args);
129129

130130
var outputLines = new List<string>();
131131
var outputReaderTask = Task.Run(() =>
@@ -147,24 +147,24 @@ private byte[] RunGitBinaryCommand(params string[] args)
147147
{
148148
const int BUFFER_SIZE = 1024;
149149

150-
var proc = StartGitProcess(System.Text.Encoding.ASCII, args);
150+
var proc = StartGitProcess(args);
151151

152-
List<char> ret = new();
152+
List<byte> ret = new();
153153

154154
var outputReaderTask = Task.Run(() =>
155155
{
156-
char[] buffer = new char[BUFFER_SIZE];
156+
byte[] buffer = new byte[BUFFER_SIZE];
157157

158158
int charsRead = 0;
159-
while ((charsRead = proc.StandardOutput.ReadBlock(buffer, 0, BUFFER_SIZE)) != 0)
159+
while ((charsRead = proc.StandardOutput.BaseStream.Read(buffer, 0, BUFFER_SIZE)) != 0)
160160
{
161161
ret.AddRange(buffer.AsSpan(0, charsRead).ToArray());
162162
}
163163
});
164164

165165
HandleProcessExit(proc, outputReaderTask, args);
166166

167-
return ret.ConvertAll(c => (byte)c).ToArray();
167+
return ret.ToArray();
168168
}
169169

170170
static string[] ReadAllLines(StreamReader reader)

0 commit comments

Comments
 (0)