1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/test/launcher/test_launcher.h"
7 #include "base/at_exit.h"
8 #include "base/base_paths.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/format_macros.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/path_service.h"
17 #include "base/process/launch.h"
18 #include "base/strings/string_util.h"
19 #include "base/test/launcher/unit_test_launcher.h"
20 #include "base/test/test_switches.h"
21 #include "base/test/test_timeouts.h"
25 const char kHelpFlag
[] = "help";
29 "Runs tests using the gtest framework, each batch of tests being\n"
30 "run in their own process. Supported command-line flags:\n"
33 " --gtest_filter=...\n"
34 " Runs a subset of tests (see --gtest_help for more info).\n"
37 " Shows this message.\n"
40 " --test-launcher-retry-limit=N\n"
41 " Sets the limit of test retries on failures to N.\n"
43 " --test-launcher-summary-output=PATH\n"
44 " Saves a JSON machine-readable summary of the run.\n"
46 " --test-launcher-print-test-stdio=auto|always|never\n"
47 " Controls when full test output is printed.\n"
48 " auto means to print it when the test failed.\n"
50 " --test-launcher-total-shards=N\n"
51 " Sets the total number of shards to N.\n"
53 " --test-launcher-shard-index=N\n"
54 " Sets the shard index to run to N (from 0 to TOTAL - 1).\n");
58 class IOSUnitTestPlatformDelegate
: public base::UnitTestPlatformDelegate
{
60 IOSUnitTestPlatformDelegate() {
63 bool Init() WARN_UNUSED_RESULT
{
64 if (!PathService::Get(base::DIR_EXE
, &dir_exe_
)) {
65 LOG(ERROR
) << "Failed to get directory of current executable.";
69 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
70 std::vector
<std::string
> args(command_line
->GetArgs());
71 if (args
.size() < 1) {
72 LOG(ERROR
) << "Arguments expected.";
77 base::CommandLine
cmd_line(dir_exe_
.AppendASCII(test_name_
+ ".app"));
78 cmd_line
.AppendSwitch(switches::kTestLauncherPrintWritablePath
);
79 cmd_line
.PrependWrapper(dir_exe_
.AppendASCII("iossim").value());
81 std::string raw_output
;
82 if (!base::GetAppOutput(cmd_line
, &raw_output
)) {
83 LOG(ERROR
) << "GetAppOutput failed.";
86 writable_path_
= base::FilePath(raw_output
);
91 bool GetTests(std::vector
<base::TestIdentifier
>* output
) override
{
92 base::ScopedTempDir temp_dir
;
93 if (!temp_dir
.CreateUniqueTempDirUnderPath(writable_path_
))
95 base::FilePath
test_list_path(
96 temp_dir
.path().AppendASCII("test_list.json"));
98 base::CommandLine
cmd_line(dir_exe_
.AppendASCII(test_name_
+ ".app"));
99 cmd_line
.AppendSwitchPath(switches::kTestLauncherListTests
, test_list_path
);
100 cmd_line
.PrependWrapper(dir_exe_
.AppendASCII("iossim").value());
102 base::LaunchOptions launch_options
;
103 launch_options
.wait
= true;
105 if (!base::LaunchProcess(cmd_line
, launch_options
).IsValid())
108 return base::ReadTestNamesFromFile(test_list_path
, output
);
111 bool CreateTemporaryFile(base::FilePath
* path
) override
{
112 if (!CreateTemporaryDirInDir(writable_path_
, std::string(), path
))
114 *path
= path
->AppendASCII("test_results.xml");
118 base::CommandLine
GetCommandLineForChildGTestProcess(
119 const std::vector
<std::string
>& test_names
,
120 const base::FilePath
& output_file
) override
{
121 base::CommandLine
cmd_line(dir_exe_
.AppendASCII(test_name_
+ ".app"));
122 cmd_line
.AppendSwitchPath(switches::kTestLauncherOutput
, output_file
);
123 cmd_line
.AppendSwitchASCII(base::kGTestFilterFlag
,
124 base::JoinString(test_names
, ":"));
128 std::string
GetWrapperForChildGTestProcess() override
{
129 return dir_exe_
.AppendASCII("iossim").value();
132 void RelaunchTests(base::TestLauncher
* test_launcher
,
133 const std::vector
<std::string
>& test_names
,
134 int launch_flags
) override
{
135 // Relaunch all tests in one big batch, since overhead of smaller batches
136 // is too big for serialized runs inside ios simulator.
137 RunUnitTestsBatch(test_launcher
, this, test_names
, launch_flags
);
141 // Directory containing test launcher's executable.
142 base::FilePath dir_exe_
;
144 // Name of the test executable to run.
145 std::string test_name_
;
147 // Path that launched test binary can write to.
148 base::FilePath writable_path_
;
150 DISALLOW_COPY_AND_ASSIGN(IOSUnitTestPlatformDelegate
);
155 int main(int argc
, char** argv
) {
156 base::AtExitManager at_exit
;
158 base::CommandLine::Init(argc
, argv
);
160 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag
)) {
165 base::TimeTicks
start_time(base::TimeTicks::Now());
167 TestTimeouts::Initialize();
169 base::MessageLoopForIO message_loop
;
171 IOSUnitTestPlatformDelegate platform_delegate
;
172 if (!platform_delegate
.Init()) {
173 fprintf(stderr
, "Failed to intialize test launcher platform delegate.\n");
177 base::UnitTestLauncherDelegate
delegate(&platform_delegate
, 0, false);
178 // Force one job since we can't run multiple simulators in parallel.
179 base::TestLauncher
launcher(&delegate
, 1);
180 bool success
= launcher
.Run();
182 fprintf(stdout
, "Tests took %" PRId64
" seconds.\n",
183 (base::TimeTicks::Now() - start_time
).InSeconds());
186 return (success
? 0 : 1);