Skip to content

Commit

Permalink
Merge pull request #2 from exyi/maybe-netframework
Browse files Browse the repository at this point in the history
Add back old .NET framework support
  • Loading branch information
exyi authored Jan 28, 2023
2 parents d423d1f + be3b3d0 commit 9593a06
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
4 changes: 3 additions & 1 deletion exampleTest/CheckTestOutput.Example.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net6.0;net472</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0</TargetFrameworks>


<IsPackable>false</IsPackable>
<RollForward>major</RollForward>
Expand Down
1 change: 0 additions & 1 deletion exampleTest/testoutputs/SomeTest.BiggerTable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,3 @@ ID Age Title SomeText Something PolymorphicSomething
197 47 X197
198 18 X198
199 49 X199

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ Yes 1 10 hmm
"please no" 2 20 1
"I don't know" 3 20
... 4 20

7 changes: 5 additions & 2 deletions src/CheckTestOutput.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<LangVersion>9</LangVersion>

<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>
Expand All @@ -16,7 +16,7 @@

<AssemblyOriginatorKeyFile>dotvvmwizard.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign>true</PublicSign>
<!-- <PublicSign >true</PublicSign> -->

<PackageReadmeFile>README.md</PackageReadmeFile>

Expand All @@ -30,6 +30,9 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
Expand Down
88 changes: 48 additions & 40 deletions src/OutputChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,78 +100,86 @@ private Process StartGitProcess(params string[] args)
StandardOutputEncoding = System.Text.Encoding.UTF8,
StandardErrorEncoding = System.Text.Encoding.UTF8,
};
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
foreach (var a in args)
procInfo.ArgumentList.Add(a);


#else
procInfo.Arguments = WindowsEscapeArguments(args);
#endif
return Process.Start(procInfo);
}

private void HandleProcessExit(Process proc, Task outputReaderTask, params string[] args)

#if !(NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER)
private static string WindowsEscapeArguments(params string[] args)
{
// Literally, a Raspberry PI with a shitty SD card has faster IO than Azure Windows VM
var timeout = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 15_000 : 3_000;
if (!proc.WaitForExit(timeout))
{
proc.Kill();
throw new Exception($"`git {string.Join(" ", args)}` command timed out");
}
// based on the logic from http://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp.

if (proc.ExitCode != 0)
throw new Exception($"`git {string.Join(" ", args)}` command failed: " + proc.StandardError.ReadToEnd());
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new InvalidOperationException("The nestandard2.0 build is only supported on Windows old .NET Framework");

outputReaderTask.Wait();
return string.Join(" ", args.Select(a => {
a = Regex.Replace(a, @"(\\*)" + "\"", @"$1$1\" + "\"");
return "\"" + Regex.Replace(a, @"(\\+)$", @"$1$1") + "\"";
}));
}
#endif

private string[] RunGitCommand(params string[] args)
{
var proc = StartGitProcess(args);

var outputLines = new List<string>();
var outputReaderTask = Task.Run(() =>
private void HandleProcessExit(Process proc, Task outputReaderTask, params string[] args)
{
try
{
string line;
while ((line = proc.StandardOutput.ReadLine()) != null)
// Literally, a Raspberry PI with a shitty SD card has faster IO than Azure Windows VM
var timeout = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 15_000 : 3_000;
if (!proc.WaitForExit(timeout))
{
if (line.Length > 0)
outputLines.Add(line);
proc.Kill();
throw new Exception($"`git {string.Join(" ", args)}` command timed out");
}
});

HandleProcessExit(proc, outputReaderTask, args);
if (proc.ExitCode != 0)
throw new Exception($"`git {string.Join(" ", args)}` command failed: " + proc.StandardError.ReadToEnd());

return outputLines.ToArray();
outputReaderTask.Wait();
}
finally
{
proc.Dispose();
}
}

private byte[] RunGitBinaryCommand(params string[] args)
private string[] RunGitCommand(params string[] args)
{
const int BUFFER_SIZE = 1024;
var output = RunGitBinaryCommand(args);

using var reader = new StreamReader(output);
return ReadAllLines(reader);
}

private MemoryStream RunGitBinaryCommand(params string[] args)
{
var proc = StartGitProcess(args);

List<byte> ret = new();
MemoryStream ret = new();

var outputReaderTask = Task.Run(() =>
{
byte[] buffer = new byte[BUFFER_SIZE];

int charsRead = 0;
while ((charsRead = proc.StandardOutput.BaseStream.Read(buffer, 0, BUFFER_SIZE)) != 0)
{
ret.AddRange(buffer.AsSpan(0, charsRead).ToArray());
}
proc.StandardOutput.BaseStream.CopyTo(ret);
});

HandleProcessExit(proc, outputReaderTask, args);

return ret.ToArray();
ret.Position = 0;
return ret;
}

static string[] ReadAllLines(StreamReader reader)
{
var lines = new List<string>();
while (!reader.EndOfStream && reader.ReadLine() is {} line)
lines.Add(line);
if (line.Length > 0)
lines.Add(line);
return lines.ToArray();
}

Expand Down Expand Up @@ -209,7 +217,7 @@ private byte[] GetOldBinaryContent(string file)

var data = RunGitBinaryCommand("cat-file", "blob", hash);

return data;
return data.ToArray();
}
else
{
Expand Down Expand Up @@ -323,7 +331,7 @@ internal void CheckOutputBinaryCore(byte[] outputBytes, string checkName, string
{
using (var t = File.Create(filename))
{
t.Write(outputBytes);
t.Write(outputBytes, 0, outputBytes.Length);
}
}
return;
Expand All @@ -333,7 +341,7 @@ internal void CheckOutputBinaryCore(byte[] outputBytes, string checkName, string
{
using (var t = File.Create(filename))
{
t.Write(outputBytes);
t.Write(outputBytes, 0, outputBytes.Length);
}

if (IsModified(filename))
Expand Down

0 comments on commit 9593a06

Please sign in to comment.