1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/test/launcher/unit_test_launcher.h"
6
7 #include "base/apple/foundation_util.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/test/allow_check_is_test_for_testing.h"
13 #include "base/test/gtest_util.h"
14 #include "base/test/test_support_ios.h"
15 #include "base/test/test_switches.h"
16
17 namespace {
18
WriteCompiledInTestsToFileAndLog(const base::FilePath & list_path)19 int WriteCompiledInTestsToFileAndLog(const base::FilePath& list_path) {
20 if (WriteCompiledInTestsToFile(list_path)) {
21 LOG(INFO) << "Wrote compiled tests to file: " << list_path.value();
22 return 0;
23 }
24 LOG(ERROR) << "Failed to write compiled tests to file: " << list_path.value();
25 return 1;
26 }
27
28 } // namespace
29
30 namespace base {
31
LaunchUnitTests(int argc,char ** argv,RunTestSuiteCallback run_test_suite,size_t retry_limit)32 int LaunchUnitTests(int argc,
33 char** argv,
34 RunTestSuiteCallback run_test_suite,
35 size_t retry_limit) {
36 base::test::AllowCheckIsTestForTesting();
37
38 return LaunchUnitTestsSerially(argc, argv, std::move(run_test_suite));
39 }
40
LaunchUnitTestsSerially(int argc,char ** argv,RunTestSuiteCallback run_test_suite)41 int LaunchUnitTestsSerially(int argc,
42 char** argv,
43 RunTestSuiteCallback run_test_suite) {
44 CHECK(CommandLine::InitializedForCurrentProcess() ||
45 CommandLine::Init(argc, argv));
46 const CommandLine* command_line = CommandLine::ForCurrentProcess();
47 bool only_write_tests =
48 command_line->HasSwitch(switches::kTestLauncherListTests);
49 bool write_and_run_tests =
50 command_line->HasSwitch(switches::kWriteCompiledTestsJsonToWritablePath);
51 if (only_write_tests || write_and_run_tests) {
52 // File needs to be stored under Documents DIR because only files
53 // under that DIR can be pulled to the host using idevicefs
54 // in order to support test location ResultSink reporting on
55 // physical iOS device testing.
56 FilePath list_path =
57 only_write_tests
58 ? (command_line->GetSwitchValuePath(
59 switches::kTestLauncherListTests))
60 : apple::GetUserDocumentPath().Append("compiled_tests.json");
61 int write_result = WriteCompiledInTestsToFileAndLog(list_path);
62 if (only_write_tests) {
63 return write_result;
64 }
65 } else if (command_line->HasSwitch(
66 switches::kTestLauncherPrintWritablePath)) {
67 fprintf(stdout, "%s", apple::GetUserLibraryPath().value().c_str());
68 fflush(stdout);
69 return 0;
70 }
71
72 InitIOSRunHook(std::move(run_test_suite));
73 return RunTestsFromIOSApp();
74 }
75
76 } // namespace base
77