OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/gtest_xml_util.h" | 5 #include "base/test/gtest_xml_util.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/test/gtest_util.h" | 10 #include "base/test/gtest_util.h" |
11 #include "base/test/launcher/test_launcher.h" | 11 #include "base/test/launcher/test_launcher.h" |
12 #include "third_party/libxml/chromium/libxml_utils.h" | 12 #include "third_party/libxml/chromium/libxml_utils.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // This is used for the xml parser to report errors. This assumes the context | 18 // This is used for the xml parser to report errors. This assumes the context |
19 // is a pointer to a std::string where the error message should be appended. | 19 // is a pointer to a std::string where the error message should be appended. |
20 static void XmlErrorFunc(void *context, const char *message, ...) { | 20 static void XmlErrorFunc(void *context, const char *message, ...) { |
21 va_list args; | 21 va_list args; |
22 va_start(args, message); | 22 va_start(args, message); |
23 std::string* error = static_cast<std::string*>(context); | 23 std::string* error = static_cast<std::string*>(context); |
24 base::StringAppendV(error, message, args); | 24 StringAppendV(error, message, args); |
25 va_end(args); | 25 va_end(args); |
26 } | 26 } |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL) { | |
31 } | |
32 | |
33 XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() { | |
34 if (output_file_) { | |
35 fprintf(output_file_, "</testsuites>\n"); | |
36 fflush(output_file_); | |
37 base::CloseFile(output_file_); | |
38 } | |
39 } | |
40 | |
41 bool XmlUnitTestResultPrinter::Initialize(const FilePath& output_file_path) { | |
42 DCHECK(!output_file_); | |
43 output_file_ = OpenFile(output_file_path, "w"); | |
44 if (!output_file_) | |
45 return false; | |
46 | |
47 fprintf(output_file_, | |
48 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n"); | |
49 fflush(output_file_); | |
50 | |
51 return true; | |
52 } | |
53 | |
54 void XmlUnitTestResultPrinter::OnTestCaseStart( | |
55 const testing::TestCase& test_case) { | |
56 fprintf(output_file_, " <testsuite>\n"); | |
57 fflush(output_file_); | |
58 } | |
59 | |
60 void XmlUnitTestResultPrinter::OnTestStart(const testing::TestInfo& test_info) { | |
61 // This is our custom extension - it helps to recognize which test was running | |
62 // when the test binary crashed. Note that we cannot even open the <testcase> | |
63 // tag here - it requires e.g. run time of the test to be known. | |
64 fprintf(output_file_, | |
65 " <x-teststart name=\"%s\" classname=\"%s\" />\n", | |
66 test_info.name(), | |
67 test_info.test_case_name()); | |
68 fflush(output_file_); | |
69 } | |
70 | |
71 void XmlUnitTestResultPrinter::OnTestEnd(const testing::TestInfo& test_info) { | |
72 fprintf(output_file_, | |
73 " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" | |
74 " classname=\"%s\">\n", | |
75 test_info.name(), | |
76 static_cast<double>(test_info.result()->elapsed_time()) / | |
77 Time::kMillisecondsPerSecond, | |
78 test_info.test_case_name()); | |
79 if (test_info.result()->Failed()) | |
80 fprintf(output_file_, " <failure message=\"\" type=\"\"></failure>\n"); | |
81 fprintf(output_file_, " </testcase>\n"); | |
82 fflush(output_file_); | |
83 } | |
84 | |
85 void XmlUnitTestResultPrinter::OnTestCaseEnd( | |
86 const testing::TestCase& test_case) { | |
87 fprintf(output_file_, " </testsuite>\n"); | |
88 fflush(output_file_); | |
89 } | |
90 | |
91 bool ProcessGTestOutput(const base::FilePath& output_file, | 30 bool ProcessGTestOutput(const base::FilePath& output_file, |
92 std::vector<TestResult>* results, | 31 std::vector<TestResult>* results, |
93 bool* crashed) { | 32 bool* crashed) { |
94 DCHECK(results); | 33 DCHECK(results); |
95 | 34 |
96 std::string xml_contents; | 35 std::string xml_contents; |
97 if (!ReadFileToString(output_file, &xml_contents)) | 36 if (!ReadFileToString(output_file, &xml_contents)) |
98 return false; | 37 return false; |
99 | 38 |
100 // Silence XML errors - otherwise they go to stderr. | 39 // Silence XML errors - otherwise they go to stderr. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 // format. | 159 // format. |
221 return false; | 160 return false; |
222 } | 161 } |
223 } | 162 } |
224 | 163 |
225 *crashed = (state != STATE_END); | 164 *crashed = (state != STATE_END); |
226 return true; | 165 return true; |
227 } | 166 } |
228 | 167 |
229 } // namespace base | 168 } // namespace base |
OLD | NEW |