Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: base/test/test_suite.cc

Issue 2638763004: Report CHECK/DCHECK to test launcher summary output. (Closed)
Patch Set: Add comment. Fix missed usage of SetLogAssertHandler. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/test_suite.h" 5 #include "base/test/test_suite.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/at_exit.h" 9 #include "base/at_exit.h"
10 #include "base/base_paths.h" 10 #include "base/base_paths.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 #endif 58 #endif
59 59
60 #if defined(OS_IOS) 60 #if defined(OS_IOS)
61 #include "base/test/test_support_ios.h" 61 #include "base/test/test_support_ios.h"
62 #endif 62 #endif
63 63
64 namespace base { 64 namespace base {
65 65
66 namespace { 66 namespace {
67 67
68 TestSuite* g_test_suite = nullptr;
Paweł Hajdan Jr. 2017/01/27 17:19:44 This shouldn't be needed now, right?
alex-ac 2017/02/11 20:12:20 Done.
69
68 class MaybeTestDisabler : public testing::EmptyTestEventListener { 70 class MaybeTestDisabler : public testing::EmptyTestEventListener {
69 public: 71 public:
70 void OnTestStart(const testing::TestInfo& test_info) override { 72 void OnTestStart(const testing::TestInfo& test_info) override {
71 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info)) 73 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
72 << "Probably the OS #ifdefs don't include all of the necessary " 74 << "Probably the OS #ifdefs don't include all of the necessary "
73 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix " 75 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
74 "after the code is preprocessed."; 76 "after the code is preprocessed.";
75 } 77 }
76 }; 78 };
77 79
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 InitializeFromCommandLine(argc, argv); 153 InitializeFromCommandLine(argc, argv);
152 // Logging must be initialized before any thread has a chance to call logging 154 // Logging must be initialized before any thread has a chance to call logging
153 // functions. 155 // functions.
154 InitializeLogging(); 156 InitializeLogging();
155 } 157 }
156 #endif // defined(OS_WIN) 158 #endif // defined(OS_WIN)
157 159
158 TestSuite::~TestSuite() { 160 TestSuite::~TestSuite() {
159 if (initialized_command_line_) 161 if (initialized_command_line_)
160 CommandLine::Reset(); 162 CommandLine::Reset();
163
164 g_test_suite = nullptr;
161 } 165 }
162 166
163 void TestSuite::InitializeFromCommandLine(int argc, char** argv) { 167 void TestSuite::InitializeFromCommandLine(int argc, char** argv) {
164 initialized_command_line_ = CommandLine::Init(argc, argv); 168 initialized_command_line_ = CommandLine::Init(argc, argv);
165 testing::InitGoogleTest(&argc, argv); 169 testing::InitGoogleTest(&argc, argv);
166 testing::InitGoogleMock(&argc, argv); 170 testing::InitGoogleMock(&argc, argv);
167 171
168 #if defined(OS_IOS) 172 #if defined(OS_IOS)
169 InitIOSRunHook(this, argc, argv); 173 InitIOSRunHook(this, argc, argv);
170 #endif 174 #endif
171 } 175 }
172 176
173 #if defined(OS_WIN) 177 #if defined(OS_WIN)
174 void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) { 178 void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) {
175 // Windows CommandLine::Init ignores argv anyway. 179 // Windows CommandLine::Init ignores argv anyway.
176 initialized_command_line_ = CommandLine::Init(argc, NULL); 180 initialized_command_line_ = CommandLine::Init(argc, NULL);
177 testing::InitGoogleTest(&argc, argv); 181 testing::InitGoogleTest(&argc, argv);
178 testing::InitGoogleMock(&argc, argv); 182 testing::InitGoogleMock(&argc, argv);
179 } 183 }
180 #endif // defined(OS_WIN) 184 #endif // defined(OS_WIN)
181 185
182 void TestSuite::PreInitialize() { 186 void TestSuite::PreInitialize() {
187 g_test_suite = this;
183 #if defined(OS_WIN) 188 #if defined(OS_WIN)
184 testing::GTEST_FLAG(catch_exceptions) = false; 189 testing::GTEST_FLAG(catch_exceptions) = false;
185 #endif 190 #endif
186 EnableTerminationOnHeapCorruption(); 191 EnableTerminationOnHeapCorruption();
187 #if defined(OS_LINUX) && defined(USE_AURA) 192 #if defined(OS_LINUX) && defined(USE_AURA)
188 // When calling native char conversion functions (e.g wrctomb) we need to 193 // When calling native char conversion functions (e.g wrctomb) we need to
189 // have the locale set. In the absence of such a call the "C" locale is the 194 // have the locale set. In the absence of such a call the "C" locale is the
190 // default. In the gtk code (below) gtk_init() implicitly sets a locale. 195 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
191 setlocale(LC_ALL, ""); 196 setlocale(LC_ALL, "");
192 #endif // defined(OS_LINUX) && defined(USE_AURA) 197 #endif // defined(OS_LINUX) && defined(USE_AURA)
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 236
232 // Do not add the result printer if output path already exists. It's an 237 // Do not add the result printer if output path already exists. It's an
233 // indicator there is a process printing to that file, and we're likely 238 // indicator there is a process printing to that file, and we're likely
234 // its child. Do not clobber the results in that case. 239 // its child. Do not clobber the results in that case.
235 if (PathExists(output_path)) { 240 if (PathExists(output_path)) {
236 LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe() 241 LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe()
237 << " exists. Not adding test launcher result printer."; 242 << " exists. Not adding test launcher result printer.";
238 return; 243 return;
239 } 244 }
240 245
241 XmlUnitTestResultPrinter* printer = new XmlUnitTestResultPrinter; 246 printer_ = new XmlUnitTestResultPrinter;
242 CHECK(printer->Initialize(output_path)); 247 CHECK(printer_->Initialize(output_path));
243 testing::TestEventListeners& listeners = 248 testing::TestEventListeners& listeners =
244 testing::UnitTest::GetInstance()->listeners(); 249 testing::UnitTest::GetInstance()->listeners();
245 listeners.Append(printer); 250 listeners.Append(printer_);
246 } 251 }
247 252
248 // Don't add additional code to this method. Instead add it to 253 // Don't add additional code to this method. Instead add it to
249 // Initialize(). See bug 6436. 254 // Initialize(). See bug 6436.
250 int TestSuite::Run() { 255 int TestSuite::Run() {
251 #if defined(OS_IOS) 256 #if defined(OS_IOS)
252 RunTestsFromIOSApp(); 257 RunTestsFromIOSApp();
253 #endif 258 #endif
254 259
255 #if defined(OS_MACOSX) 260 #if defined(OS_MACOSX)
(...skipping 20 matching lines...) Expand all
276 // objects use to remove themselves as observers. 281 // objects use to remove themselves as observers.
277 scoped_pool.Recycle(); 282 scoped_pool.Recycle();
278 #endif 283 #endif
279 284
280 Shutdown(); 285 Shutdown();
281 286
282 return result; 287 return result;
283 } 288 }
284 289
285 // static 290 // static
286 void TestSuite::UnitTestAssertHandler(const std::string& str) { 291 void TestSuite::UnitTestAssertHandler(const char* file,
292 int line,
293 size_t message_start,
294 size_t stack_start,
295 const std::string& str) {
287 #if defined(OS_ANDROID) 296 #if defined(OS_ANDROID)
288 // Correlating test stdio with logcat can be difficult, so we emit this 297 // Correlating test stdio with logcat can be difficult, so we emit this
289 // helpful little hint about what was running. Only do this for Android 298 // helpful little hint about what was running. Only do this for Android
290 // because other platforms don't separate out the relevant logs in the same 299 // because other platforms don't separate out the relevant logs in the same
291 // way. 300 // way.
292 const ::testing::TestInfo* const test_info = 301 const ::testing::TestInfo* const test_info =
293 ::testing::UnitTest::GetInstance()->current_test_info(); 302 ::testing::UnitTest::GetInstance()->current_test_info();
294 if (test_info) { 303 if (test_info) {
295 LOG(ERROR) << "Currently running: " << test_info->test_case_name() << "." 304 LOG(ERROR) << "Currently running: " << test_info->test_case_name() << "."
296 << test_info->name(); 305 << test_info->name();
297 fflush(stderr); 306 fflush(stderr);
298 } 307 }
299 #endif // defined(OS_ANDROID) 308 #endif // defined(OS_ANDROID)
300 309
310 std::string message = str.substr(message_start);
311 std::string summary;
312
313 summary = message.substr(0, stack_start - message_start);
314 printer_->OnAssert(file, line, summary, message);
315
301 // The logging system actually prints the message before calling the assert 316 // The logging system actually prints the message before calling the assert
302 // handler. Just exit now to avoid printing too many stack traces. 317 // handler. Just exit now to avoid printing too many stack traces.
303 _exit(1); 318 _exit(1);
304 } 319 }
305 320
306 void TestSuite::SuppressErrorDialogs() { 321 void TestSuite::SuppressErrorDialogs() {
307 #if defined(OS_WIN) 322 #if defined(OS_WIN)
308 UINT new_flags = SEM_FAILCRITICALERRORS | 323 UINT new_flags = SEM_FAILCRITICALERRORS |
309 SEM_NOGPFAULTERRORBOX | 324 SEM_NOGPFAULTERRORBOX |
310 SEM_NOOPENFILEERRORBOX; 325 SEM_NOOPENFILEERRORBOX;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 // Make sure we run with high resolution timer to minimize differences 367 // Make sure we run with high resolution timer to minimize differences
353 // between production code and test code. 368 // between production code and test code.
354 Time::EnableHighResolutionTimer(true); 369 Time::EnableHighResolutionTimer(true);
355 #endif // defined(OS_WIN) 370 #endif // defined(OS_WIN)
356 371
357 // In some cases, we do not want to see standard error dialogs. 372 // In some cases, we do not want to see standard error dialogs.
358 if (!debug::BeingDebugged() && 373 if (!debug::BeingDebugged() &&
359 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) { 374 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) {
360 SuppressErrorDialogs(); 375 SuppressErrorDialogs();
361 debug::SetSuppressDebugUI(true); 376 debug::SetSuppressDebugUI(true);
362 logging::SetLogAssertHandler(UnitTestAssertHandler); 377 assert_handler_ = logging::ScopedLogAssertHandler(
378 base::Bind(&TestSuite::UnitTestAssertHandler, base::Unretained(this)));
363 } 379 }
364 380
365 base::test::InitializeICUForTesting(); 381 base::test::InitializeICUForTesting();
366 382
367 // On the Mac OS X command line, the default locale is *_POSIX. In Chromium, 383 // On the Mac OS X command line, the default locale is *_POSIX. In Chromium,
368 // the locale is set via an OS X locale API and is never *_POSIX. 384 // the locale is set via an OS X locale API and is never *_POSIX.
369 // Some tests (such as those involving word break iterator) will behave 385 // Some tests (such as those involving word break iterator) will behave
370 // differently and fail if we use *POSIX locale. Setting it to en_US here 386 // differently and fail if we use *POSIX locale. Setting it to en_US here
371 // does not affect tests that explicitly overrides the locale for testing. 387 // does not affect tests that explicitly overrides the locale for testing.
372 // This can be an issue on all platforms other than Windows. 388 // This can be an issue on all platforms other than Windows.
(...skipping 26 matching lines...) Expand all
399 415
400 void TestSuite::Shutdown() { 416 void TestSuite::Shutdown() {
401 base::debug::StopProfiling(); 417 base::debug::StopProfiling();
402 418
403 // Clear the FeatureList that was created by Initialize(). 419 // Clear the FeatureList that was created by Initialize().
404 if (created_feature_list_) 420 if (created_feature_list_)
405 FeatureList::ClearInstanceForTesting(); 421 FeatureList::ClearInstanceForTesting();
406 } 422 }
407 423
408 } // namespace base 424 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698