|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Collections.Concurrent; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.ComponentModel; |
@@ -160,6 +160,11 @@ private string GetOldContent(string file) |
160 | 160 | } |
161 | 161 | } |
162 | 162 |
|
| 163 | + private byte[] GetOldBinaryContent(string file) |
| 164 | + { |
| 165 | + return File.ReadAllBytes(file); |
| 166 | + } |
| 167 | + |
163 | 168 | private bool IsModified(string file) |
164 | 169 | { |
165 | 170 | // 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 |
242 | 247 | throw new Exception( |
243 | 248 | $"{Path.GetFileName(filename)} has changed, the actual output differs from the previous accepted output:\n\n" + |
244 | 249 | 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); |
246 | 266 |
|
| 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" |
247 | 305 | ); |
248 | 306 | } |
249 | 307 | } |
250 | 308 | else |
251 | 309 | { |
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."); |
253 | 311 | } |
254 | 312 | } |
255 | 313 | } |
|
0 commit comments