forked from csoltenborn/GoogleTestAdapter
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathStandardOutputTestResultParser.cs
More file actions
246 lines (207 loc) · 8.53 KB
/
StandardOutputTestResultParser.cs
File metadata and controls
246 lines (207 loc) · 8.53 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
// This file has been modified by Microsoft on 9/2017.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using GoogleTestAdapter.Common;
using GoogleTestAdapter.Model;
namespace GoogleTestAdapter.TestResults
{
public class StandardOutputTestResultParser
{
private const string Run = "[ RUN ]";
public const string Failed = "[ FAILED ]";
public const string Passed = "[ OK ]";
public const string Skipped = "[ SKIPPED ]";
public const string FailedFixture = "SetUpTestSuite or TearDownTestSuite";
public static readonly string CrashText = Resources.CrashText;
/// <summary>
/// 1000 ticks = 0.1ms to make sure VS shows "<1ms"
/// </summary>
public static readonly TimeSpan ShortTestDuration = TimeSpan.FromTicks(1000);
public TestCase CrashedTestCase { get; private set; }
private readonly List<string> _consoleOutput;
private readonly List<TestCase> _testCasesRun;
private readonly ILogger _logger;
public StandardOutputTestResultParser(IEnumerable<TestCase> testCasesRun, IEnumerable<string> consoleOutput, ILogger logger)
{
_consoleOutput = consoleOutput.ToList();
_testCasesRun = testCasesRun.ToList();
_logger = logger;
}
public List<TestResult> GetTestResults()
{
var testResults = new List<TestResult>();
int indexOfNextTestcase = FindIndexOfNextTestcase(0);
while (indexOfNextTestcase >= 0)
{
var testResult = CreateTestResult(indexOfNextTestcase);
if (testResult != null)
testResults.Add(testResult);
indexOfNextTestcase = FindIndexOfNextTestcase(indexOfNextTestcase + 1);
}
return testResults;
}
private TestResult CreateTestResult(int indexOfTestcase)
{
int currentLineIndex = indexOfTestcase;
string line = _consoleOutput[currentLineIndex++];
string qualifiedTestname = RemovePrefix(line).Trim();
TestCase testCase = FindTestcase(qualifiedTestname);
if (testCase == null)
{
_logger.DebugWarning(String.Format(Resources.NoKnownTestCaseMessage, line));
return null;
}
if (currentLineIndex >= _consoleOutput.Count)
{
CrashedTestCase = testCase;
return CreateFailedTestResult(testCase, TimeSpan.FromMilliseconds(0), CrashText, "");
}
line = _consoleOutput[currentLineIndex];
SplitLineIfNecessary(ref line, currentLineIndex);
currentLineIndex++;
string errorMsg = "";
while (!(IsFailedLine(line) || IsPassedLine(line) || IsSkippedLine(line)) && currentLineIndex <= _consoleOutput.Count)
{
errorMsg += line + "\n";
line = currentLineIndex < _consoleOutput.Count ? _consoleOutput[currentLineIndex] : "";
SplitLineIfNecessary(ref line, currentLineIndex);
currentLineIndex++;
}
if (IsFailedLine(line))
{
ErrorMessageParser parser = new ErrorMessageParser(errorMsg);
parser.Parse();
return CreateFailedTestResult(testCase, ParseDuration(line), parser.ErrorMessage, parser.ErrorStackTrace);
}
if (IsPassedLine(line))
{
return CreatePassedTestResult(testCase, ParseDuration(line));
}
if (IsSkippedLine(line))
{
return CreateSkippedTestResult(testCase, ParseDuration(line));
}
CrashedTestCase = testCase;
string message = CrashText;
message += errorMsg == "" ? "" : "\nTest output:\n\n" + errorMsg;
return CreateFailedTestResult(testCase, TimeSpan.FromMilliseconds(0), message, "");
}
private void SplitLineIfNecessary(ref string line, int currentLineIndex)
{
Match testEndMatch = StreamingStandardOutputTestResultParser.PrefixedLineRegex.Match(line);
if (testEndMatch.Success)
{
string restOfErrorMessage = testEndMatch.Groups[1].Value;
string testEndPart = testEndMatch.Groups[2].Value;
_consoleOutput.RemoveAt(currentLineIndex);
_consoleOutput.Insert(currentLineIndex, testEndPart);
_consoleOutput.Insert(currentLineIndex, restOfErrorMessage);
line = restOfErrorMessage;
}
}
private TimeSpan ParseDuration(string line)
{
return ParseDuration(line, _logger);
}
public static TimeSpan ParseDuration(string line, ILogger logger)
{
int durationInMs = 1;
try
{
// duration is a 64-bit number (no decimals) in the user's locale
int indexOfOpeningBracket = line.LastIndexOf('(');
int lengthOfDurationPart = line.Length - indexOfOpeningBracket - 2;
string durationPart = line.Substring(indexOfOpeningBracket + 1, lengthOfDurationPart);
durationPart = durationPart.Replace("ms", "").Trim();
durationInMs = int.Parse(durationPart, NumberStyles.Number);
}
catch (Exception)
{
logger.LogWarning(String.Format(Resources.ParseDurationMessage, line));
}
return NormalizeDuration(TimeSpan.FromMilliseconds(durationInMs));
}
public static TimeSpan NormalizeDuration(TimeSpan duration)
{
return duration.TotalMilliseconds < 1
? ShortTestDuration
: duration;
}
public static TestResult CreatePassedTestResult(TestCase testCase, TimeSpan duration)
{
return new TestResult(testCase)
{
ComputerName = Environment.MachineName,
DisplayName = testCase.DisplayName,
Outcome = TestOutcome.Passed,
Duration = duration
};
}
public static TestResult CreateSkippedTestResult(TestCase testCase, TimeSpan duration)
{
return new TestResult(testCase)
{
ComputerName = Environment.MachineName,
DisplayName = testCase.DisplayName,
Outcome = TestOutcome.Skipped,
Duration = duration
};
}
public static TestResult CreateFailedTestResult(TestCase testCase, TimeSpan duration, string errorMessage, string errorStackTrace)
{
return new TestResult(testCase)
{
ComputerName = Environment.MachineName,
DisplayName = testCase.DisplayName,
Outcome = TestOutcome.Failed,
ErrorMessage = errorMessage,
ErrorStackTrace = errorStackTrace,
Duration = duration
};
}
private int FindIndexOfNextTestcase(int currentIndex)
{
while (currentIndex < _consoleOutput.Count)
{
string line = _consoleOutput[currentIndex];
if (IsRunLine(line))
{
return currentIndex;
}
currentIndex++;
}
return -1;
}
private TestCase FindTestcase(string qualifiedTestname)
{
return FindTestcase(qualifiedTestname, _testCasesRun);
}
public static TestCase FindTestcase(string qualifiedTestname, IList<TestCase> testCasesRun)
{
return testCasesRun.SingleOrDefault(tc => tc.FullyQualifiedName == qualifiedTestname);
}
public static bool IsRunLine(string line)
{
return line.StartsWith(Run, StringComparison.Ordinal);
}
public static bool IsPassedLine(string line)
{
return line.StartsWith(Passed, StringComparison.Ordinal);
}
public static bool IsFailedLine(string line)
{
return line.StartsWith(Failed, StringComparison.Ordinal);
}
public static bool IsSkippedLine(string line)
{
return line.StartsWith(Skipped);
}
public static string RemovePrefix(string line)
{
return line.Substring(Run.Length);
}
}
}