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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/gtest_xml_util.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/test/gtest_util.h"
#include "base/test/launcher/test_launcher.h"
#include "third_party/libxml/chromium/libxml_utils.h"
namespace base {
namespace {
// This is used for the xml parser to report errors. This assumes the context
// is a pointer to a std::string where the error message should be appended.
static void XmlErrorFunc(void *context, const char *message, ...) {
va_list args;
va_start(args, message);
std::string* error = static_cast<std::string*>(context);
base::StringAppendV(error, message, args);
va_end(args);
}
} // namespace
XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL) {
}
XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() {
if (output_file_) {
fprintf(output_file_, "</testsuites>\n");
fflush(output_file_);
base::CloseFile(output_file_);
}
}
bool XmlUnitTestResultPrinter::Initialize(const FilePath& output_file_path) {
DCHECK(!output_file_);
output_file_ = OpenFile(output_file_path, "w");
if (!output_file_)
return false;
fprintf(output_file_,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n");
fflush(output_file_);
return true;
}
void XmlUnitTestResultPrinter::OnTestCaseStart(
const testing::TestCase& test_case) {
fprintf(output_file_, " <testsuite>\n");
fflush(output_file_);
}
void XmlUnitTestResultPrinter::OnTestStart(const testing::TestInfo& test_info) {
// This is our custom extension - it helps to recognize which test was running
// when the test binary crashed. Note that we cannot even open the <testcase>
// tag here - it requires e.g. run time of the test to be known.
fprintf(output_file_,
" <x-teststart name=\"%s\" classname=\"%s\" />\n",
test_info.name(),
test_info.test_case_name());
fflush(output_file_);
}
void XmlUnitTestResultPrinter::OnTestEnd(const testing::TestInfo& test_info) {
fprintf(output_file_,
" <testcase name=\"%s\" status=\"run\" time=\"%.3f\""
" classname=\"%s\">\n",
test_info.name(),
static_cast<double>(test_info.result()->elapsed_time()) /
Time::kMillisecondsPerSecond,
test_info.test_case_name());
if (test_info.result()->Failed())
fprintf(output_file_, " <failure message=\"\" type=\"\"></failure>\n");
fprintf(output_file_, " </testcase>\n");
fflush(output_file_);
}
void XmlUnitTestResultPrinter::OnTestCaseEnd(
const testing::TestCase& test_case) {
fprintf(output_file_, " </testsuite>\n");
fflush(output_file_);
}
bool ProcessGTestOutput(const base::FilePath& output_file,
std::vector<TestResult>* results,
bool* crashed) {
DCHECK(results);
std::string xml_contents;
if (!ReadFileToString(output_file, &xml_contents))
return false;
// Silence XML errors - otherwise they go to stderr.
std::string xml_errors;
ScopedXmlErrorFunc error_func(&xml_errors, &XmlErrorFunc);
XmlReader xml_reader;
if (!xml_reader.Load(xml_contents))
return false;
enum {
STATE_INIT,
STATE_TESTSUITE,
STATE_TESTCASE,
STATE_FAILURE,
STATE_END,
} state = STATE_INIT;
while (xml_reader.Read()) {
xml_reader.SkipToElement();
std::string node_name(xml_reader.NodeName());
switch (state) {
case STATE_INIT:
if (node_name == "testsuites" && !xml_reader.IsClosingElement())
state = STATE_TESTSUITE;
else
return false;
break;
case STATE_TESTSUITE:
if (node_name == "testsuites" && xml_reader.IsClosingElement())
state = STATE_END;
else if (node_name == "testsuite" && !xml_reader.IsClosingElement())
state = STATE_TESTCASE;
else
return false;
break;
case STATE_TESTCASE:
if (node_name == "testsuite" && xml_reader.IsClosingElement()) {
state = STATE_TESTSUITE;
} else if (node_name == "x-teststart" &&
!xml_reader.IsClosingElement()) {
// This is our custom extension that helps recognize which test was
// running when the test binary crashed.
TestResult result;
std::string test_case_name;
if (!xml_reader.NodeAttribute("classname", &test_case_name))
return false;
std::string test_name;
if (!xml_reader.NodeAttribute("name", &test_name))
return false;
result.full_name = FormatFullTestName(test_case_name, test_name);
result.elapsed_time = TimeDelta();
// Assume the test crashed - we can correct that later.
result.status = TestResult::TEST_CRASH;
results->push_back(result);
} else if (node_name == "testcase" && !xml_reader.IsClosingElement()) {
std::string test_status;
if (!xml_reader.NodeAttribute("status", &test_status))
return false;
if (test_status != "run" && test_status != "notrun")
return false;
if (test_status != "run")
break;
TestResult result;
std::string test_case_name;
if (!xml_reader.NodeAttribute("classname", &test_case_name))
return false;
std::string test_name;
if (!xml_reader.NodeAttribute("name", &test_name))
return false;
result.full_name = test_case_name + "." + test_name;
std::string test_time_str;
if (!xml_reader.NodeAttribute("time", &test_time_str))
return false;
result.elapsed_time = TimeDelta::FromMicroseconds(
static_cast<int64>(strtod(test_time_str.c_str(), NULL) *
Time::kMicrosecondsPerSecond));
result.status = TestResult::TEST_SUCCESS;
if (!results->empty() &&
results->at(results->size() - 1).full_name == result.full_name &&
results->at(results->size() - 1).status ==
TestResult::TEST_CRASH) {
// Erase the fail-safe "crashed" result - now we know the test did
// not crash.
results->pop_back();
}
results->push_back(result);
} else if (node_name == "failure" && !xml_reader.IsClosingElement()) {
std::string failure_message;
if (!xml_reader.NodeAttribute("message", &failure_message))
return false;
DCHECK(!results->empty());
results->at(results->size() - 1).status = TestResult::TEST_FAILURE;
state = STATE_FAILURE;
} else if (node_name == "testcase" && xml_reader.IsClosingElement()) {
// Deliberately empty.
} else {
return false;
}
break;
case STATE_FAILURE:
if (node_name == "failure" && xml_reader.IsClosingElement())
state = STATE_TESTCASE;
else
return false;
break;
case STATE_END:
// If we are here and there are still XML elements, the file has wrong
// format.
return false;
}
}
*crashed = (state != STATE_END);
return true;
}
} // namespace base
|