forked from csoltenborn/GoogleTestAdapter
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathStreamingStandardOutputTestResultParser.cs
More file actions
253 lines (224 loc) · 10.5 KB
/
StreamingStandardOutputTestResultParser.cs
File metadata and controls
253 lines (224 loc) · 10.5 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
// This file has been modified by Microsoft on 9/2017.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using GoogleTestAdapter.Common;
using GoogleTestAdapter.Framework;
using GoogleTestAdapter.Helpers;
using GoogleTestAdapter.Model;
namespace GoogleTestAdapter.TestResults
{
public class StreamingStandardOutputTestResultParser
{
public static readonly Regex PrefixedLineRegex;
public static readonly Regex FixtureMethodResultRegex;
public TestCase CrashedTestCase { get; private set; }
public IList<TestResult> TestResults { get; } = new List<TestResult>();
public List<TestResult> XmlTestResults { get; set; } = new List<TestResult>();
// Used so that we only get the XML results once per test run.
private bool doXMLResultsExist = false;
private readonly List<TestCase> _testCasesRun;
private readonly ILogger _logger;
private readonly ITestFrameworkReporter _reporter;
private readonly List<string> _consoleOutput = new List<string>();
private readonly string _executable;
static StreamingStandardOutputTestResultParser()
{
string passedMarker = Regex.Escape(StandardOutputTestResultParser.Passed);
string failedMarker = Regex.Escape(StandardOutputTestResultParser.Failed);
PrefixedLineRegex = new Regex($"(.+)((?:{passedMarker}|{failedMarker}).*)", RegexOptions.Compiled);
FixtureMethodResultRegex = new Regex($@"(?:{failedMarker}\s*)(\w+):(?:\s+{StandardOutputTestResultParser.FailedFixture})", RegexOptions.Compiled);
}
public StreamingStandardOutputTestResultParser(IEnumerable<TestCase> testCasesRun,
ILogger logger, ITestFrameworkReporter reporter, string executable)
{
_testCasesRun = testCasesRun.ToList();
_logger = logger;
_reporter = reporter;
_executable = executable;
}
public List<TestResult> GetXMLResults(IEnumerable<TestCase> testCasesRun)
{
// Parse in XML so we can get the error message and stack trace in UTF8 since console output does not support this by default.
string xmlResultFilePath = Path.Combine(Path.GetDirectoryName(_executable), "XMLGoogleTestResults.xml");
var xmlParser = new XmlTestResultParser(testCasesRun, xmlResultFilePath, _logger);
var xmlTestResults = xmlParser.GetTestResults();
return xmlTestResults;
}
public void ReportLine(string line)
{
Match testEndMatch = PrefixedLineRegex.Match(line);
if (testEndMatch.Success)
{
string restOfErrorMessage = testEndMatch.Groups[1].Value;
if (!string.IsNullOrEmpty(restOfErrorMessage))
DoReportLine(restOfErrorMessage);
string testEndPart = testEndMatch.Groups[2].Value;
DoReportLine(testEndPart);
}
else
{
DoReportLine(line);
}
}
private void DoReportLine(string line)
{
if (StandardOutputTestResultParser.IsRunLine(line))
{
if (_consoleOutput.Count > 0)
{
ReportTestResult();
_consoleOutput.Clear();
}
ReportTestStart(line);
}
else if (StandardOutputTestResultParser.IsFailedLine(line) && line.Contains(StandardOutputTestResultParser.FailedFixture))
{
ReportFixtureMethodFailure(line);
}
_consoleOutput.Add(line);
}
public void Flush()
{
if (_consoleOutput.Count > 0)
{
ReportTestResult();
_consoleOutput.Clear();
}
}
private void ReportTestStart(string line)
{
string qualifiedTestname = StandardOutputTestResultParser.RemovePrefix(line).Trim();
TestCase testCase = StandardOutputTestResultParser.FindTestcase(qualifiedTestname, _testCasesRun);
if (testCase != null)
_reporter.ReportTestsStarted(testCase.Yield());
}
private void ReportTestResult()
{
TestResult result = CreateTestResult();
if (result != null)
{
_reporter.ReportTestResults(result.Yield());
TestResults.Add(result);
}
}
private void ReportFixtureMethodFailure(string line)
{
// Google test reports fixture method failures ambiguously with the output:
// [ FAILED ] TestMe: SetUpTestSuite or TearDownTestSuite
// For V1, we fail both SetUp and TearDown nodes if a failure is reported.
string suite = FixtureMethodResultRegex.Match(line).Groups[1].Value;
string[] supportedFixtureMethods = { GoogleTestConstants.SetUpFixtureMethod, GoogleTestConstants.TearDownFixtureMethod };
foreach (string fixtureMethodName in supportedFixtureMethods)
{
string qualifiedTestName = $"{suite}.{fixtureMethodName}";
TestCase testCase = StandardOutputTestResultParser.FindTestcase(qualifiedTestName, _testCasesRun);
if(testCase != null)
{
TestResult result = StandardOutputTestResultParser.CreateFailedTestResult(testCase, TimeSpan.FromMilliseconds(0),"","");
if (result != null)
{
_reporter.ReportTestResults(result.Yield());
TestResults.Add(result);
}
}
}
}
private TestResult CreateTestResult()
{
int currentLineIndex = 0;
while (currentLineIndex < _consoleOutput.Count &&
!StandardOutputTestResultParser.IsRunLine(_consoleOutput[currentLineIndex]))
currentLineIndex++;
if (currentLineIndex == _consoleOutput.Count)
return null;
string line = _consoleOutput[currentLineIndex++];
string qualifiedTestname = StandardOutputTestResultParser.RemovePrefix(line).Trim();
TestCase testCase = StandardOutputTestResultParser.FindTestcase(qualifiedTestname, _testCasesRun);
if (testCase == null)
{
_logger.DebugWarning(String.Format(Resources.NoKnownTestCaseMessage, line));
return null;
}
if (currentLineIndex == _consoleOutput.Count)
{
CrashedTestCase = testCase;
return StandardOutputTestResultParser.CreateFailedTestResult(
testCase,
TimeSpan.FromMilliseconds(0),
StandardOutputTestResultParser.CrashText,
"");
}
line = _consoleOutput[currentLineIndex++];
string errorMsg = "";
while (
!(StandardOutputTestResultParser.IsFailedLine(line)
|| StandardOutputTestResultParser.IsPassedLine(line)
|| StandardOutputTestResultParser.IsSkippedLine(line))
&& currentLineIndex <= _consoleOutput.Count)
{
errorMsg += line + "\n";
line = currentLineIndex < _consoleOutput.Count ? _consoleOutput[currentLineIndex] : "";
currentLineIndex++;
}
if (StandardOutputTestResultParser.IsFailedLine(line))
{
string testResultErrorMessage = String.Empty;
string testResultErrorStackTrace = String.Empty;
string xmlErrorMessage = String.Empty;
string xmlErrorStackTrace = String.Empty;
// Map and extract error messages from xml parser to inject into this test failed result. This allows support for UTF8 error messages. Bug 1951549.
// Console output does not always support UTF8 output by default, but XML results do.
if (!doXMLResultsExist)
{
XmlTestResults = GetXMLResults(_testCasesRun.AsEnumerable());
doXMLResultsExist = true;
}
foreach (var xmlTestResult in XmlTestResults)
{
if (xmlTestResult.TestCase.FullyQualifiedNameWithNamespace == testCase.FullyQualifiedNameWithNamespace)
{
testResultErrorMessage = xmlTestResult.ErrorMessage;
testResultErrorStackTrace = xmlTestResult.ErrorStackTrace;
}
}
// If we did not find the error message or stack trace in the XML parser, then parse the error message from the console output.
if (testResultErrorMessage == String.Empty || testResultErrorStackTrace == String.Empty)
{
ErrorMessageParser parser = new ErrorMessageParser(errorMsg);
parser.Parse();
testResultErrorMessage = parser.ErrorMessage;
testResultErrorStackTrace = parser.ErrorStackTrace;
}
return StandardOutputTestResultParser.CreateFailedTestResult(
testCase,
StandardOutputTestResultParser.ParseDuration(line, _logger),
testResultErrorMessage,
testResultErrorStackTrace);
}
if (StandardOutputTestResultParser.IsPassedLine(line))
{
return StandardOutputTestResultParser.CreatePassedTestResult(
testCase,
StandardOutputTestResultParser.ParseDuration(line, _logger));
}
if (StandardOutputTestResultParser.IsSkippedLine(line))
{
return StandardOutputTestResultParser.CreateSkippedTestResult(
testCase,
StandardOutputTestResultParser.ParseDuration(line, _logger));
}
CrashedTestCase = testCase;
string message = StandardOutputTestResultParser.CrashText;
message += errorMsg == "" ? "" : ("\n" + Resources.TestOutput + $"\n\n{errorMsg}");
TestResult result = StandardOutputTestResultParser.CreateFailedTestResult(
testCase,
TimeSpan.FromMilliseconds(0),
message,
"");
return result;
}
}
}