提交 d3c0ccc4 编写于 作者: J Jared Parsons

Better diagnostics for test runner

上级 5641bf70
......@@ -123,78 +123,85 @@ private void Print(List<TestResult> testResults)
private async Task<TestResult> RunTest(string assemblyPath, CancellationToken cancellationToken)
{
var assemblyName = Path.GetFileName(assemblyPath);
var extension = _useHtml ? ".TestResults.html" : ".TestResults.xml";
var resultsPath = Path.Combine(Path.GetDirectoryName(assemblyPath), Path.ChangeExtension(assemblyName, extension));
DeleteFile(resultsPath);
var builder = new StringBuilder();
builder.AppendFormat(@"""{0}""", assemblyPath);
builder.AppendFormat(@" -{0} ""{1}""", _useHtml ? "html" : "xml", resultsPath);
builder.Append(" -noshadow");
var errorOutput = new StringBuilder();
var start = DateTime.UtcNow;
var xunitPath = UpgradedTests.Contains(assemblyName) ? Path.Combine($"{Path.GetDirectoryName(_xunitConsolePath)}", @"..\..\..\xunit.runner.console\2.1.0-beta4-build3109\tools", $"{Path.GetFileName(_xunitConsolePath)}") : _xunitConsolePath;
var processOutput = await ProcessRunner.RunProcessAsync(
xunitPath,
builder.ToString(),
lowPriority: false,
displayWindow: false,
captureOutput: true,
cancellationToken: cancellationToken).ConfigureAwait(false);
var span = DateTime.UtcNow - start;
if (processOutput.ExitCode != 0)
{
// On occasion we get a non-0 output but no actual data in the result file. The could happen
// if xunit manages to crash when running a unit test (a stack overflow could cause this, for instance).
// To avoid losing information, write the process output to the console. In addition, delete the results
// file to avoid issues with any tool attempting to interpret the (potentially malformed) text.
var all = string.Empty;
try
{
all = File.ReadAllText(resultsPath).Trim();
}
catch
try
{
var assemblyName = Path.GetFileName(assemblyPath);
var extension = _useHtml ? ".TestResults.html" : ".TestResults.xml";
var resultsPath = Path.Combine(Path.GetDirectoryName(assemblyPath), Path.ChangeExtension(assemblyName, extension));
DeleteFile(resultsPath);
var builder = new StringBuilder();
builder.AppendFormat(@"""{0}""", assemblyPath);
builder.AppendFormat(@" -{0} ""{1}""", _useHtml ? "html" : "xml", resultsPath);
builder.Append(" -noshadow");
var errorOutput = new StringBuilder();
var start = DateTime.UtcNow;
var xunitPath = UpgradedTests.Contains(assemblyName) ? Path.Combine($"{Path.GetDirectoryName(_xunitConsolePath)}", @"..\..\..\xunit.runner.console\2.1.0-beta4-build3109\tools", $"{Path.GetFileName(_xunitConsolePath)}") : _xunitConsolePath;
var processOutput = await ProcessRunner.RunProcessAsync(
xunitPath,
builder.ToString(),
lowPriority: false,
displayWindow: false,
captureOutput: true,
cancellationToken: cancellationToken).ConfigureAwait(false);
var span = DateTime.UtcNow - start;
if (processOutput.ExitCode != 0)
{
// Happens if xunit didn't produce a log file
}
// On occasion we get a non-0 output but no actual data in the result file. The could happen
// if xunit manages to crash when running a unit test (a stack overflow could cause this, for instance).
// To avoid losing information, write the process output to the console. In addition, delete the results
// file to avoid issues with any tool attempting to interpret the (potentially malformed) text.
var all = string.Empty;
try
{
all = File.ReadAllText(resultsPath).Trim();
}
catch
{
// Happens if xunit didn't produce a log file
}
bool noResultsData = (all.Length == 0);
if (noResultsData)
{
var output = processOutput.OutputLines.Concat(processOutput.ErrorLines);
Console.Write(string.Join(Environment.NewLine, output));
bool noResultsData = (all.Length == 0);
if (noResultsData)
{
var output = processOutput.OutputLines.Concat(processOutput.ErrorLines);
Console.Write(string.Join(Environment.NewLine, output));
// Delete the output file.
File.Delete(resultsPath);
}
// Delete the output file.
File.Delete(resultsPath);
}
errorOutput.AppendLine($"Command: {_xunitConsolePath} {builder}");
errorOutput.AppendLine($"Command: {_xunitConsolePath} {builder}");
if (processOutput.ErrorLines.Any())
{
foreach (var line in processOutput.ErrorLines)
if (processOutput.ErrorLines.Any())
{
errorOutput.AppendLine(line);
foreach (var line in processOutput.ErrorLines)
{
errorOutput.AppendLine(line);
}
}
else
{
errorOutput.AppendLine($"xunit produced no error output but had exit code {processOutput.ExitCode}");
}
}
else
{
errorOutput.AppendLine($"xunit produced no error output but had exit code {processOutput.ExitCode}");
}
// If the results are html, use Process.Start to open in the browser.
// If the results are html, use Process.Start to open in the browser.
if (_useHtml && !noResultsData)
{
Process.Start(resultsPath);
if (_useHtml && !noResultsData)
{
Process.Start(resultsPath);
}
}
}
return new TestResult(processOutput.ExitCode == 0, assemblyName, span, errorOutput.ToString());
return new TestResult(processOutput.ExitCode == 0, assemblyName, span, errorOutput.ToString());
}
catch (Exception ex)
{
throw new Exception($"Unable to run {assemblyPath}", ex);
}
}
private static void DeleteFile(string filePath)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册