-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOutputChecker.cs
More file actions
388 lines (333 loc) · 16 KB
/
OutputChecker.cs
File metadata and controls
388 lines (333 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CheckTestOutput
{
public class OutputChecker
{
/// <summary> Checks that the provided test output matched a file from the <paramref name="directory"/>. Filename is a {callingClass}.{callingMethod}.fileExtension </summary>
/// <param name="directory">Directory with the reference outputs, relative to the <see cref="calledFrom"/> parameter.</param>
/// <param name="sanitizeGuids">Replace all strings that look like Guid by a sequential id. The sanitization preserves equality.</param>
/// <param name="sanitizeQuotedGuids">Replace all strings that look like Guid and are in quotes by a sequential id. The sanitization preserves equality.</param>
/// <param name="nonDeterminismSanitizers">List of regular expressions that are replaced by a sequential id for the purpose of the check.</param>
public OutputChecker(
string directory,
bool sanitizeGuids = false,
bool sanitizeQuotedGuids = false,
IEnumerable<string> nonDeterminismSanitizers = null,
[System.Runtime.CompilerServices.CallerFilePath] string calledFrom = null)
{
const string guidRegex = "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})";
var s = nonDeterminismSanitizers?.ToList() ?? new List<string>();
if (sanitizeGuids)
s.Add(guidRegex);
else if (sanitizeQuotedGuids)
s.Add('"' + guidRegex + '"');
_nonDeterminismSanitizers = s.ToArray();
if (Path.IsPathRooted(directory))
{
this.CheckDirectory = directory;
}
else
{
if (calledFrom == null)
throw new ArgumentException($"Either the directory must be absolute path or the calledFrom parameter must be specified.");
this.CheckDirectory = Path.Combine(Path.GetDirectoryName(calledFrom), directory);
}
DoesGitWork = doesGitWorkCache.GetOrAdd(directory, new Lazy<bool>(() => {
try
{
var path = RunGitCommand("rev-parse", "--show-toplevel");
return true;
}
catch (Win32Exception)
{
Console.WriteLine("CheckTestOutput warning: git command not found. Falling back to simple file-based checking. Make sure that git is installed and in the PATH.");
return false;
}
catch (Exception e) when (e.Message.StartsWith("Git command failed: fatal: not a git repository"))
{
Console.WriteLine("CheckTestOutput warning: project is not in git. Falling back to simple file-based checking");
return false;
}
catch (Exception e)
{
Console.WriteLine("CheckTestOutput warning: an error occurred while calling git. Falling back to simple file-based checking.");
Console.WriteLine("Error: " + e);
return false;
}
}));
}
private static ConcurrentDictionary<string, Lazy<bool>> doesGitWorkCache = new();
public string CheckDirectory { get; }
private string[] _nonDeterminismSanitizers;
/// <summary> List of regular expressions that are replaced by a sequential id for the purpose of the check. The sanitization preserves equality over the checked string (equal strings are replaced by equal id, different strings by different ids) </summary>
/// <remarks>
/// As a main point, this is useful for replacing Guids in the checked string by a sequential id.
/// </remarks>
public IEnumerable<string> NonDeterminismSanitizers => _nonDeterminismSanitizers;
private readonly Lazy<bool> DoesGitWork;
private Process StartGitProcess(params string[] args)
{
#if DEBUG
Console.WriteLine("Running git command: " + string.Join(" ", args));
#endif
// run `git ...args` in CheckDirectory working directory with 3 second timeout
var procInfo = new ProcessStartInfo("git")
{
WorkingDirectory = CheckDirectory,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true,
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);
}
#if !(NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER)
private static string WindowsEscapeArguments(params string[] args)
{
// based on the logic from http://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp.
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new InvalidOperationException("The nestandard2.0 build is only supported on Windows old .NET Framework");
return string.Join(" ", args.Select(a => {
a = Regex.Replace(a, @"(\\*)" + "\"", @"$1$1\" + "\"");
return "\"" + Regex.Replace(a, @"(\\+)$", @"$1$1") + "\"";
}));
}
#endif
private void HandleProcessExit(Process proc, Task outputReaderTask, params string[] args)
{
try
{
// 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");
}
if (proc.ExitCode != 0)
throw new Exception($"`git {string.Join(" ", args)}` command failed: " + proc.StandardError.ReadToEnd());
outputReaderTask.Wait();
}
finally
{
proc.Dispose();
}
}
private string[] RunGitCommand(params string[] args)
{
var output = RunGitBinaryCommand(args);
using var reader = new StreamReader(output);
return ReadAllLines(reader);
}
private MemoryStream RunGitBinaryCommand(params string[] args)
{
var proc = StartGitProcess(args);
MemoryStream ret = new();
var outputReaderTask = Task.Run(() =>
{
proc.StandardOutput.BaseStream.CopyTo(ret);
});
HandleProcessExit(proc, outputReaderTask, args);
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);
return lines.ToArray();
}
private string GetOldContent(string file)
{
if (DoesGitWork.Value)
{
var lsFiles = RunGitCommand("ls-files", "-s", file);
if (lsFiles.Length == 0) return null;
var hash = lsFiles[0].Split(new [] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries).ElementAtOrDefault(1);
if (String.IsNullOrEmpty(hash)) return null;
var contents = RunGitCommand("cat-file", "blob", hash);
return string.Join("\n", contents);
}
else
{
return string.Join("\n", File.ReadLines(file));
}
}
private byte[] GetOldBinaryContent(string file)
{
if (DoesGitWork.Value)
{
var lsFiles = RunGitCommand("ls-files", "-s", file);
if (lsFiles.Length == 0)
return null;
var hash = lsFiles[0].Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries).ElementAtOrDefault(1);
if (String.IsNullOrEmpty(hash))
return null;
var data = RunGitBinaryCommand("cat-file", "blob", hash);
return data.ToArray();
}
else
{
return File.ReadAllBytes(file);
}
}
private bool IsModified(string file)
{
// command `git ls-files --other --modified $file` returns the file name back iff it is modified or other (untracked)
var gitOut = RunGitCommand("ls-files", "--other", "--modified", "--deleted", file);
// if it outputs back the filename, it is changed
return !gitOut.All(string.IsNullOrEmpty);
}
private bool IsNewFile(string file)
{
var gitOut = RunGitCommand("ls-files", "--other", file);
// if it outputs back the filename, it is other (untracked)
return !gitOut.All(string.IsNullOrEmpty);
}
/// <summary> Applies the <see cref="NonDeterminismSanitizers" /> to the string. </summary>
public string SanitizeString(string outputString)
{
var x = new Dictionary<string, string>();
foreach (var p in this.NonDeterminismSanitizers)
{
outputString = Regex.Replace(outputString, p, match => {
if (!x.ContainsKey(match.Value))
{
x[match.Value] = $"aaaaaaaa-bbbb-cccc-dddd-{(x.Count + 1):D12}";
}
return x[match.Value];
});
}
return outputString;
}
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 alternativeIndex = 0;
string filename;
while (true)
{
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)
{
// fine! Just check that the file is not changed - if it is changed or deleted, we rewrite
if (IsModified(filename))
{
using (var t = File.CreateText(filename))
{
t.Write(outputString);
t.Write("\n");
}
}
return;
}
if (allowAlternatives && File.Exists(filename))
{
alternativeIndex++;
continue;
}
break;
}
if (DoesGitWork.Value)
{
using (var t = File.CreateText(filename))
{
t.Write(outputString);
t.Write("\n");
}
if (IsModified(filename))
{
if (IsNewFile(filename))
{
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");
}
var diff = RunGitCommand("diff", filename);
if (diff.All(string.IsNullOrEmpty))
{
// I guess fine from our perspective, but it's weird...
Console.WriteLine($"CheckTestOutput warning: {Path.GetFileName(filename)} is modified, but the diff is empty.");
return;
}
throw new Exception(
$"{Path.GetFileName(filename)} has changed, the actual output differs from the previous accepted output:\n\n" +
string.Join("\n", diff) + "\n\n" +
"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"
);
}
}
else
{
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.");
}
}
internal void CheckOutputBinaryCore(byte[] outputBytes, string checkName, string method, string fileExtension = "bin")
{
Directory.CreateDirectory(CheckDirectory);
var filename = Path.Combine(CheckDirectory, (checkName == null ? method : $"{method}-{checkName}") + "." + fileExtension);
if (GetOldBinaryContent(filename)?.SequenceEqual(outputBytes) == true)
{
// fine! Just check that the file is not changed - if it is changed or deleted, we rewrite
if (IsModified(filename))
{
using (var t = File.Create(filename))
{
t.Write(outputBytes, 0, outputBytes.Length);
}
}
return;
}
if (DoesGitWork.Value)
{
using (var t = File.Create(filename))
{
t.Write(outputBytes, 0, outputBytes.Length);
}
if (IsModified(filename))
{
if (IsNewFile(filename))
{
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");
}
var diff = RunGitCommand("diff", filename);
if (diff.All(string.IsNullOrEmpty))
{
// I guess fine from our perspective, but it's weird...
Console.WriteLine($"CheckTestOutput warning: {Path.GetFileName(filename)} is modified, but the diff is empty.");
return;
}
throw new Exception(
$"{Path.GetFileName(filename)} has changed, the actual output differs from the previous accepted output!"
+ "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"
);
}
}
else
{
throw new Exception($"{Path.GetFileName(filename)} has changed, the previous accepted output differs from the actual output.");
}
}
}
}