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()); | |
71 if (args.size() < 1) { | |
72 LOG(ERROR) << "Arguments expected."; | |
73 return false; | |
74 } | |
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; | 75 return true; |
89 } | 76 } |
90 | 77 |
91 bool GetTests(std::vector<base::SplitTestName>* output) override { | 78 private: |
92 base::ScopedTempDir temp_dir; | |
93 if (!temp_dir.CreateUniqueTempDirUnderPath(writable_path_)) | |
94 return false; | |
95 base::FilePath test_list_path( | |
96 temp_dir.path().AppendASCII("test_list.json")); | |
97 | |
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()); | |
101 | |
102 base::LaunchOptions launch_options; | |
103 launch_options.wait = true; | |
104 | |
105 if (!base::LaunchProcess(cmd_line, launch_options).IsValid()) | |
106 return false; | |
107 | |
108 return base::ReadTestNamesFromFile(test_list_path, output); | |
109 } | |
110 | |
111 bool CreateTemporaryFile(base::FilePath* path) override { | 79 bool CreateTemporaryFile(base::FilePath* path) override { |
112 if (!CreateTemporaryDirInDir(writable_path_, std::string(), path)) | 80 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), path)) |
113 return false; | 81 return false; |
114 *path = path->AppendASCII("test_results.xml"); | 82 *path = path->AppendASCII("test_results.xml"); |
115 return true; | 83 return true; |
116 } | 84 } |
117 | 85 |
86 bool GetTests(std::vector<base::SplitTestName>* output) override { | |
87 base::FilePath output_file; | |
88 if (!base::CreateTemporaryFile(&output_file)) { | |
89 LOG(ERROR) << "Failed to create a temp file."; | |
90 return false; | |
91 } | |
92 | |
93 base::CommandLine cmd_line(test_path_); | |
94 cmd_line.AppendSwitchPath(switches::kTestLauncherListTests, output_file); | |
95 | |
96 std::string subprocess_output; | |
97 if (!base::GetAppOutput(cmd_line, &subprocess_output)) { | |
Paweł Hajdan Jr.
2015/06/02 10:37:36
Why not LaunchProcess? We're ignoring subprocess_o
hidehiko
2015/06/02 14:55:20
I thought GetAppOutput is slightly simpler as it h
| |
98 LOG(ERROR) << "Failed to run test with --" | |
99 << switches::kTestLauncherListTests; | |
100 return false; | |
101 } | |
102 | |
103 return base::ReadTestNamesFromFile(output_file, output); | |
104 } | |
105 | |
106 std::string GetWrapperForChildGTestProcess() override { | |
107 return std::string(); | |
108 } | |
109 | |
118 base::CommandLine GetCommandLineForChildGTestProcess( | 110 base::CommandLine GetCommandLineForChildGTestProcess( |
119 const std::vector<std::string>& test_names, | 111 const std::vector<std::string>& test_names, |
120 const base::FilePath& output_file) override { | 112 const base::FilePath& output_file) override { |
121 base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); | 113 base::CommandLine cmd_line(test_path_); |
122 cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); | 114 cmd_line.AppendSwitchPath( |
123 cmd_line.AppendSwitchASCII(base::kGTestFilterFlag, | 115 switches::kTestLauncherOutput, output_file); |
124 JoinString(test_names, ":")); | 116 cmd_line.AppendSwitchASCII( |
117 base::kGTestFilterFlag, JoinString(test_names, ":")); | |
125 return cmd_line; | 118 return cmd_line; |
126 } | 119 } |
127 | 120 |
128 std::string GetWrapperForChildGTestProcess() override { | |
129 return dir_exe_.AppendASCII("iossim").value(); | |
130 } | |
131 | |
132 void RelaunchTests(base::TestLauncher* test_launcher, | 121 void RelaunchTests(base::TestLauncher* test_launcher, |
133 const std::vector<std::string>& test_names, | 122 const std::vector<std::string>& test_names, |
134 int launch_flags) override { | 123 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); | 124 RunUnitTestsBatch(test_launcher, this, test_names, launch_flags); |
138 } | 125 } |
139 | 126 |
140 private: | 127 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 }; | 128 }; |
152 | 129 |
153 } // namespace | 130 } // namespace |
154 | 131 |
155 int main(int argc, char** argv) { | 132 int TestLauncherMain(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)) { | 133 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) { |
161 PrintUsage(); | 134 PrintUsage(); |
162 return 0; | 135 return 0; |
163 } | 136 } |
164 | 137 |
165 base::TimeTicks start_time(base::TimeTicks::Now()); | 138 base::TimeTicks start_time(base::TimeTicks::Now()); |
166 | 139 |
167 TestTimeouts::Initialize(); | 140 TestTimeouts::Initialize(); |
168 | 141 |
169 base::MessageLoopForIO message_loop; | 142 base::MessageLoopForIO message_loop; |
170 | 143 |
171 IOSUnitTestPlatformDelegate platform_delegate; | 144 NonSfiUnitTestPlatformDelegate platform_delegate; |
172 if (!platform_delegate.Init()) { | 145 if (!platform_delegate.Init(test_binary)) { |
173 fprintf(stderr, "Failed to intialize test launcher platform delegate.\n"); | 146 fprintf(stderr, "Failed to initialize test launcher.\n"); |
174 fflush(stderr); | 147 fflush(stderr); |
175 return 1; | 148 return 1; |
176 } | 149 } |
177 base::UnitTestLauncherDelegate delegate(&platform_delegate, 0, false); | 150 |
178 // Force one job since we can't run multiple simulators in parallel. | 151 base::UnitTestLauncherDelegate delegate(&platform_delegate, 10, true); |
179 base::TestLauncher launcher(&delegate, 1); | 152 base::TestLauncher launcher(&delegate, base::SysInfo::NumberOfProcessors()); |
180 bool success = launcher.Run(); | 153 bool success = launcher.Run(); |
181 | 154 |
182 fprintf(stdout, "Tests took %" PRId64 " seconds.\n", | 155 fprintf(stdout, "Tests took %" PRId64 " seconds.\n", |
183 (base::TimeTicks::Now() - start_time).InSeconds()); | 156 (base::TimeTicks::Now() - start_time).InSeconds()); |
184 fflush(stdout); | 157 fflush(stdout); |
158 return success ? 0 : 1; | |
159 } | |
185 | 160 |
186 return (success ? 0 : 1); | 161 } // namespace base |
187 } | |
OLD | NEW |