|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.ComponentModel; |
| 4 | +using System.Diagnostics; |
4 | 5 | using System.IO; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Text.RegularExpressions; |
7 | | -using Medallion.Shell; |
8 | 8 |
|
9 | 9 | namespace CheckTestOutput |
10 | 10 | { |
@@ -49,7 +49,7 @@ public OutputChecker( |
49 | 49 | } |
50 | 50 | catch (Win32Exception) |
51 | 51 | { |
52 | | - Console.WriteLine("CheckTestOutput warning: git command not found. Falling back to simple file-based checking"); |
| 52 | + 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."); |
53 | 53 | return false; |
54 | 54 | } |
55 | 55 | catch (Exception e) when (e.Message.StartsWith("Git command failed: fatal: not a git repository")) |
@@ -79,14 +79,39 @@ public OutputChecker( |
79 | 79 |
|
80 | 80 | private string[] RunGitCommand(params string[] args) |
81 | 81 | { |
82 | | - using(var cmd = Command.Run("git", args, o => { o.WorkingDirectory(CheckDirectory); o.Timeout(TimeSpan.FromSeconds(3)); })) |
| 82 | + // run `git ...args` in CheckDirectory working directory with 3 second timeout |
| 83 | + var procInfo = new ProcessStartInfo("git") |
83 | 84 | { |
84 | | - cmd.Wait(); |
85 | | - if (cmd.Task.Result.ExitCode != 0) |
86 | | - throw new Exception($"Git command failed: {cmd.Task.Result.StandardError}"); |
87 | | - |
88 | | - return cmd.StandardOutput.GetLines().ToArray(); |
| 85 | + WorkingDirectory = CheckDirectory, |
| 86 | + UseShellExecute = false, |
| 87 | + RedirectStandardOutput = true, |
| 88 | + RedirectStandardError = true, |
| 89 | + CreateNoWindow = true, |
| 90 | + StandardOutputEncoding = System.Text.Encoding.UTF8, |
| 91 | + StandardErrorEncoding = System.Text.Encoding.UTF8, |
| 92 | + }; |
| 93 | + foreach (var a in args) |
| 94 | + procInfo.ArgumentList.Add(a); |
| 95 | + |
| 96 | + var proc = Process.Start(procInfo); |
| 97 | + if (!proc.WaitForExit(3000)) |
| 98 | + { |
| 99 | + proc.Kill(); |
| 100 | + throw new Exception("Git command timed out"); |
89 | 101 | } |
| 102 | + |
| 103 | + if (proc.ExitCode != 0) |
| 104 | + throw new Exception("Git command failed: " + proc.StandardError.ReadToEnd()); |
| 105 | + |
| 106 | + return proc.StandardOutput.ReadToEnd().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); |
| 107 | + } |
| 108 | + |
| 109 | + static string[] ReadAllLines(StreamReader reader) |
| 110 | + { |
| 111 | + var lines = new List<string>(); |
| 112 | + while (!reader.EndOfStream && reader.ReadLine() is {} line) |
| 113 | + lines.Add(line); |
| 114 | + return lines.ToArray(); |
90 | 115 | } |
91 | 116 |
|
92 | 117 | private string GetOldContent(string file) |
@@ -167,13 +192,18 @@ internal void CheckOutputCore(string outputString, string checkName, string meth |
167 | 192 | { |
168 | 193 | var diff = RunGitCommand("diff", filename); |
169 | 194 | if (diff.All(string.IsNullOrEmpty)) |
170 | | - throw new Exception($"Check {Path.GetFileName(filename)} - the file is probably untracked in git"); |
171 | | - throw new Exception($"Check {Path.GetFileName(filename)} - the expected output is different:\n{string.Join("\n", diff)}"); |
| 195 | + throw new Exception($"{Path.GetFileName(filename)} is not explicitly accepted - the file is untracked in git. View the file and stage to let this test pass. Confused? See https://github.com/exyi/CheckTestOutput/blob/master/trouble.md#untracked-file\n"); |
| 196 | + throw new Exception( |
| 197 | + $"{Path.GetFileName(filename)} has changed, the actual output differs from the previous accepted output:\n\n" + |
| 198 | + string.Join("\n", diff) + "\n\n" + |
| 199 | + "If this change OK? Stage the file in git to let the test pass. Confused? See https://github.com/exyi/CheckTestOutput/blob/master/trouble.md#changed-file\n" |
| 200 | + |
| 201 | + ); |
172 | 202 | } |
173 | 203 | } |
174 | 204 | else |
175 | 205 | { |
176 | | - throw new Exception($"Check {Path.GetFileName(filename)} - the expected output is different:\n{outputString}"); |
| 206 | + 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."); |
177 | 207 | } |
178 | 208 | } |
179 | 209 | } |
|
0 commit comments