OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/launcher/test_launcher.h" | 5 #include <inttypes.h> |
| 6 #include <stdio.h> |
6 | 7 |
7 #include "base/at_exit.h" | 8 #include <string> |
8 #include "base/base_paths.h" | 9 |
9 #include "base/bind.h" | |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.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" | 13 #include "base/message_loop/message_loop.h" |
16 #include "base/path_service.h" | 14 #include "base/path_service.h" |
17 #include "base/process/launch.h" | 15 #include "base/process/launch.h" |
| 16 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/string_tokenizer.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/sys_info.h" |
| 20 #include "base/test/launcher/test_launcher.h" |
19 #include "base/test/launcher/unit_test_launcher.h" | 21 #include "base/test/launcher/unit_test_launcher.h" |
20 #include "base/test/test_switches.h" | 22 #include "base/test/test_switches.h" |
21 #include "base/test/test_timeouts.h" | 23 #include "base/test/test_timeouts.h" |
22 | 24 |
| 25 namespace base { |
| 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 const char kHelpFlag[] = "help"; | 29 const char kHelpFlag[] = "help"; |
26 | 30 |
27 void PrintUsage() { | 31 void PrintUsage() { |
28 fprintf(stdout, | 32 fprintf(stdout, |
29 "Runs tests using the gtest framework, each batch of tests being\n" | 33 "Runs tests using the gtest framework, each batch of tests being\n" |
30 "run in their own process. Supported command-line flags:\n" | 34 "run in their own process. Supported command-line flags:\n" |
31 "\n" | 35 "\n" |
32 " Common flags:\n" | 36 " Common flags:\n" |
(...skipping 15 matching lines...) Expand all Loading... |
48 " auto means to print it when the test failed.\n" | 52 " auto means to print it when the test failed.\n" |
49 "\n" | 53 "\n" |
50 " --test-launcher-total-shards=N\n" | 54 " --test-launcher-total-shards=N\n" |
51 " Sets the total number of shards to N.\n" | 55 " Sets the total number of shards to N.\n" |
52 "\n" | 56 "\n" |
53 " --test-launcher-shard-index=N\n" | 57 " --test-launcher-shard-index=N\n" |
54 " Sets the shard index to run to N (from 0 to TOTAL - 1).\n"); | 58 " Sets the shard index to run to N (from 0 to TOTAL - 1).\n"); |
55 fflush(stdout); | 59 fflush(stdout); |
56 } | 60 } |
57 | 61 |
58 class IOSUnitTestPlatformDelegate : public base::UnitTestPlatformDelegate { | 62 class NonSfiUnitTestPlatformDelegate : public base::UnitTestPlatformDelegate { |
59 public: | 63 public: |
60 IOSUnitTestPlatformDelegate() { | 64 NonSfiUnitTestPlatformDelegate() { |
61 } | 65 } |
62 | 66 |
63 bool Init() WARN_UNUSED_RESULT { | 67 bool Init(const std::string& test_binary) { |
64 if (!PathService::Get(base::DIR_EXE, &dir_exe_)) { | 68 base::FilePath dir_exe; |
65 LOG(ERROR) << "Failed to get directory of current executable."; | 69 if (!PathService::Get(base::DIR_EXE, &dir_exe)) { |
| 70 LOG(ERROR) << "Failed to get directory of the current executable."; |
66 return false; | 71 return false; |
67 } | 72 } |
68 | 73 |
69 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 74 test_path_ = dir_exe.AppendASCII(test_binary); |
70 std::vector<std::string> args(command_line->GetArgs()); | 75 return true; |
71 if (args.size() < 1) { | 76 } |
72 LOG(ERROR) << "Arguments expected."; | 77 |
| 78 private: |
| 79 bool CreateTemporaryFile(base::FilePath* path) override { |
| 80 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), path)) |
73 return false; | 81 return false; |
74 } | 82 *path = path->AppendASCII("test_results.xml"); |
75 test_name_ = args[0]; | |
76 | |
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()); | |
80 | |
81 std::string raw_output; | |
82 if (!base::GetAppOutput(cmd_line, &raw_output)) { | |
83 LOG(ERROR) << "GetAppOutput failed."; | |
84 return false; | |
85 } | |
86 writable_path_ = base::FilePath(raw_output); | |
87 | |
88 return true; | 83 return true; |
89 } | 84 } |
90 | 85 |
91 bool GetTests(std::vector<base::SplitTestName>* output) override { | 86 bool GetTests(std::vector<base::SplitTestName>* output) override { |
92 base::ScopedTempDir temp_dir; | 87 base::FilePath output_file; |
93 if (!temp_dir.CreateUniqueTempDirUnderPath(writable_path_)) | 88 if (!base::CreateTemporaryFile(&output_file)) { |
| 89 LOG(ERROR) << "Failed to create a temp file."; |
94 return false; | 90 return false; |
95 base::FilePath test_list_path( | 91 } |
96 temp_dir.path().AppendASCII("test_list.json")); | |
97 | 92 |
98 base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); | 93 base::CommandLine cmd_line(test_path_); |
99 cmd_line.AppendSwitchPath(switches::kTestLauncherListTests, test_list_path); | 94 cmd_line.AppendSwitchPath(switches::kTestLauncherListTests, output_file); |
100 cmd_line.PrependWrapper(dir_exe_.AppendASCII("iossim").value()); | |
101 | 95 |
102 base::LaunchOptions launch_options; | 96 base::LaunchOptions launch_options; |
103 launch_options.wait = true; | 97 launch_options.wait = true; |
104 | 98 |
105 if (!base::LaunchProcess(cmd_line, launch_options).IsValid()) | 99 if (!base::LaunchProcess(cmd_line, launch_options).IsValid()) |
106 return false; | 100 return false; |
107 | 101 |
108 return base::ReadTestNamesFromFile(test_list_path, output); | 102 return base::ReadTestNamesFromFile(output_file, output); |
109 } | 103 } |
110 | 104 |
111 bool CreateTemporaryFile(base::FilePath* path) override { | 105 std::string GetWrapperForChildGTestProcess() override { |
112 if (!CreateTemporaryDirInDir(writable_path_, std::string(), path)) | 106 return std::string(); |
113 return false; | |
114 *path = path->AppendASCII("test_results.xml"); | |
115 return true; | |
116 } | 107 } |
117 | 108 |
118 base::CommandLine GetCommandLineForChildGTestProcess( | 109 base::CommandLine GetCommandLineForChildGTestProcess( |
119 const std::vector<std::string>& test_names, | 110 const std::vector<std::string>& test_names, |
120 const base::FilePath& output_file) override { | 111 const base::FilePath& output_file) override { |
121 base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); | 112 base::CommandLine cmd_line(test_path_); |
122 cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); | 113 cmd_line.AppendSwitchPath( |
123 cmd_line.AppendSwitchASCII(base::kGTestFilterFlag, | 114 switches::kTestLauncherOutput, output_file); |
124 JoinString(test_names, ":")); | 115 cmd_line.AppendSwitchASCII( |
| 116 base::kGTestFilterFlag, JoinString(test_names, ":")); |
125 return cmd_line; | 117 return cmd_line; |
126 } | 118 } |
127 | 119 |
128 std::string GetWrapperForChildGTestProcess() override { | |
129 return dir_exe_.AppendASCII("iossim").value(); | |
130 } | |
131 | |
132 void RelaunchTests(base::TestLauncher* test_launcher, | 120 void RelaunchTests(base::TestLauncher* test_launcher, |
133 const std::vector<std::string>& test_names, | 121 const std::vector<std::string>& test_names, |
134 int launch_flags) override { | 122 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); | 123 RunUnitTestsBatch(test_launcher, this, test_names, launch_flags); |
138 } | 124 } |
139 | 125 |
140 private: | 126 base::FilePath test_path_; |
141 // Directory containing test launcher's executable. | |
142 base::FilePath dir_exe_; | |
143 | |
144 // Name of the test executable to run. | |
145 std::string test_name_; | |
146 | |
147 // Path that launched test binary can write to. | |
148 base::FilePath writable_path_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(IOSUnitTestPlatformDelegate); | |
151 }; | 127 }; |
152 | 128 |
153 } // namespace | 129 } // namespace |
154 | 130 |
155 int main(int argc, char** argv) { | 131 int TestLauncherNonSfiMain(const std::string& test_binary) { |
156 base::AtExitManager at_exit; | |
157 | |
158 base::CommandLine::Init(argc, argv); | |
159 | |
160 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) { | 132 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) { |
161 PrintUsage(); | 133 PrintUsage(); |
162 return 0; | 134 return 0; |
163 } | 135 } |
164 | 136 |
165 base::TimeTicks start_time(base::TimeTicks::Now()); | 137 base::TimeTicks start_time(base::TimeTicks::Now()); |
166 | 138 |
167 TestTimeouts::Initialize(); | 139 TestTimeouts::Initialize(); |
168 | 140 |
169 base::MessageLoopForIO message_loop; | 141 base::MessageLoopForIO message_loop; |
170 | 142 |
171 IOSUnitTestPlatformDelegate platform_delegate; | 143 NonSfiUnitTestPlatformDelegate platform_delegate; |
172 if (!platform_delegate.Init()) { | 144 if (!platform_delegate.Init(test_binary)) { |
173 fprintf(stderr, "Failed to intialize test launcher platform delegate.\n"); | 145 fprintf(stderr, "Failed to initialize test launcher.\n"); |
174 fflush(stderr); | 146 fflush(stderr); |
175 return 1; | 147 return 1; |
176 } | 148 } |
177 base::UnitTestLauncherDelegate delegate(&platform_delegate, 0, false); | 149 |
178 // Force one job since we can't run multiple simulators in parallel. | 150 base::UnitTestLauncherDelegate delegate(&platform_delegate, 10, true); |
179 base::TestLauncher launcher(&delegate, 1); | 151 base::TestLauncher launcher(&delegate, base::SysInfo::NumberOfProcessors()); |
180 bool success = launcher.Run(); | 152 bool success = launcher.Run(); |
181 | 153 |
182 fprintf(stdout, "Tests took %" PRId64 " seconds.\n", | 154 fprintf(stdout, "Tests took %" PRId64 " seconds.\n", |
183 (base::TimeTicks::Now() - start_time).InSeconds()); | 155 (base::TimeTicks::Now() - start_time).InSeconds()); |
184 fflush(stdout); | 156 fflush(stdout); |
| 157 return success ? 0 : 1; |
| 158 } |
185 | 159 |
186 return (success ? 0 : 1); | 160 } // namespace base |
187 } | |
OLD | NEW |