Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added asnychronous test completed events #1802

Merged
merged 8 commits into from
Aug 18, 2020
5 changes: 5 additions & 0 deletions src/OmniSharp.DotNetTest/TestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ private static int FindFreePort()
}
}

protected void EmitTestComletedEvent(DotNetTestResult result)
{
EventEmitter.Emit("TestCompleted", result);
}

protected void EmitTestMessage(TestMessageLevel messageLevel, string message)
{
EventEmitter.Emit(TestMessageEvent.Id,
Expand Down
51 changes: 33 additions & 18 deletions src/OmniSharp.DotNetTest/VSTestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ public override async Task<RunTestResponse> RunTestAsync(string[] methodNames, s

var testCases = await DiscoverTestsAsync(methodNames, runSettings, targetFrameworkVersion, cancellationToken);

var testResults = new List<TestResult>();
var passed = true;
var results = new List<DotNetTestResult>();

if (testCases.Length > 0)
{
Expand All @@ -400,43 +401,57 @@ public override async Task<RunTestResponse> RunTestAsync(string[] methodNames, s

case MessageType.TestRunStatsChange:
var testRunChange = message.DeserializePayload<TestRunChangedEventArgs>();
testResults.AddRange(testRunChange.NewTestResults);
var newResults = ConvertResults(testRunChange.NewTestResults);
passed = passed && !testRunChange.NewTestResults.Any(o => o.Outcome == TestOutcome.Failed);
results.AddRange(newResults);
foreach (var result in newResults)
{
EmitTestComletedEvent(result);
}
break;

case MessageType.ExecutionComplete:
var payload = message.DeserializePayload<TestRunCompletePayload>();
if (payload.LastRunTests != null && payload.LastRunTests.NewTestResults != null)
{
testResults.AddRange(payload.LastRunTests.NewTestResults);
var lastRunResults = ConvertResults(payload.LastRunTests.NewTestResults);
passed = passed && !payload.LastRunTests.NewTestResults.Any(o => o.Outcome == TestOutcome.Failed);
results.AddRange(lastRunResults);
foreach (var result in lastRunResults)
{
EmitTestComletedEvent(result);
}
}
done = true;
break;
}
}
}

var results = testResults.Select(testResult =>
new DotNetTestResult
{
MethodName = testResult.TestCase.FullyQualifiedName,
Outcome = testResult.Outcome.ToString().ToLowerInvariant(),
ErrorMessage = testResult.ErrorMessage,
ErrorStackTrace = testResult.ErrorStackTrace,
StandardOutput = testResult.Messages
.Where(message => message.Category == TestResultMessage.StandardOutCategory)
.Select(message => message.Text).ToArray(),
StandardError = testResult.Messages.Where(message => message.Category == TestResultMessage.StandardErrorCategory)
.Select(message => message.Text).ToArray()
});

return new RunTestResponse
{
Results = results.ToArray(),
Pass = !testResults.Any(r => r.Outcome == TestOutcome.Failed),
Pass = passed,
ContextHadNoTests = false
};
}

private static IEnumerable<DotNetTestResult> ConvertResults(IEnumerable<TestResult> results)
{
return results.Select(testResult => new DotNetTestResult
{
MethodName = testResult.TestCase.FullyQualifiedName,
Outcome = testResult.Outcome.ToString().ToLowerInvariant(),
ErrorMessage = testResult.ErrorMessage,
ErrorStackTrace = testResult.ErrorStackTrace,
StandardOutput = testResult.Messages
.Where(message => message.Category == TestResultMessage.StandardOutCategory)
.Select(message => message.Text).ToArray(),
StandardError = testResult.Messages.Where(message => message.Category == TestResultMessage.StandardErrorCategory)
.Select(message => message.Text).ToArray()
});
}

private async Task<TestCase[]> DiscoverTestsAsync(string[] methodNames, string runSettings, string targetFrameworkVersion, CancellationToken cancellationToken)
{
SendMessage(MessageType.StartDiscovery,
Expand Down