Index: components/nacl/loader/nonsfi/test_launcher_nacl_nonsfi.cc |
diff --git a/base/test/launcher/test_launcher_ios.cc b/components/nacl/loader/nonsfi/test_launcher_nacl_nonsfi.cc |
similarity index 54% |
copy from base/test/launcher/test_launcher_ios.cc |
copy to components/nacl/loader/nonsfi/test_launcher_nacl_nonsfi.cc |
index ecd31aedf5d00cb3754b0c1d4220b65e1dce3e51..5417fce4348620a244887c4b597b0ffc8c6f6355 100644 |
--- a/base/test/launcher/test_launcher_ios.cc |
+++ b/components/nacl/loader/nonsfi/test_launcher_nacl_nonsfi.cc |
@@ -2,22 +2,24 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "base/test/launcher/test_launcher.h" |
+#include <inttypes.h> |
+#include <stdio.h> |
+ |
+#include <string> |
#include "base/at_exit.h" |
-#include "base/base_paths.h" |
-#include "base/bind.h" |
#include "base/command_line.h" |
#include "base/files/file_path.h" |
#include "base/files/file_util.h" |
-#include "base/files/scoped_temp_dir.h" |
-#include "base/format_macros.h" |
#include "base/message_loop/message_loop.h" |
#include "base/path_service.h" |
#include "base/process/launch.h" |
+#include "base/strings/string_piece.h" |
+#include "base/strings/string_tokenizer.h" |
#include "base/strings/string_util.h" |
+#include "base/sys_info.h" |
+#include "base/test/launcher/test_launcher.h" |
#include "base/test/launcher/unit_test_launcher.h" |
-#include "base/test/test_switches.h" |
#include "base/test/test_timeouts.h" |
namespace { |
@@ -55,99 +57,111 @@ void PrintUsage() { |
fflush(stdout); |
} |
-class IOSUnitTestPlatformDelegate : public base::UnitTestPlatformDelegate { |
- public: |
- IOSUnitTestPlatformDelegate() { |
+// gtest's --gtest_list_tests option outputs a list of test cases and test |
+// methods in a following format. |
+// |
+// test_case1. |
+// test_method1 |
+// test_method2 |
+// test_case2. |
+// test_method3 |
+// test_method4 |
+// |
+// This function parses it. |
+bool ParseGTestList(const std::string& text, |
Paweł Hajdan Jr.
2015/05/29 13:48:20
Why not output structured list of tests like on io
hidehiko
2015/05/29 14:00:47
Because vanilla gtest does not support it.
Paweł Hajdan Jr.
2015/06/01 10:56:59
That's right. However, we have small utility funct
hidehiko
2015/06/02 08:12:17
Done.
|
+ std::vector<base::SplitTestName>* output) { |
+ std::string test_case_name; |
+ base::StringTokenizer tokenizer(text, "\n"); |
+ // As nacl_helper_nonsfi_unittests_main uses vanilla gtest_main, so it |
+ // outputs a line at first. Check and skip it. |
+ tokenizer.GetNext(); |
+ if (tokenizer.token_piece() != "Running main() from gtest_main.cc") { |
+ return false; |
} |
- bool Init() WARN_UNUSED_RESULT { |
- if (!PathService::Get(base::DIR_EXE, &dir_exe_)) { |
- LOG(ERROR) << "Failed to get directory of current executable."; |
- return false; |
+ while (tokenizer.GetNext()) { |
+ base::StringPiece token = tokenizer.token_piece(); |
+ if (token.ends_with(".")) { |
+ // This is test case name. |
+ token.remove_suffix(1); // Drop the trailing '.' |
+ token.CopyToString(&test_case_name); |
+ continue; |
} |
- base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
- std::vector<std::string> args(command_line->GetArgs()); |
- if (args.size() < 1) { |
- LOG(ERROR) << "Arguments expected."; |
- return false; |
+ if (token.starts_with(" ")) { |
+ // This is test method name. |
+ token.remove_prefix(2); // Drop the leading " ". |
+ output->push_back(base::SplitTestName(test_case_name, token.as_string())); |
+ continue; |
} |
- test_name_ = args[0]; |
- base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); |
- cmd_line.AppendSwitch(switches::kTestLauncherPrintWritablePath); |
- cmd_line.PrependWrapper(dir_exe_.AppendASCII("iossim").value()); |
+ // Unknown token. |
+ output->clear(); |
+ return false; |
+ } |
+ return true; |
+} |
- std::string raw_output; |
- if (!base::GetAppOutput(cmd_line, &raw_output)) { |
- LOG(ERROR) << "GetAppOutput failed."; |
+class NonSfiUnitTestPlatformDelegate : public base::UnitTestPlatformDelegate { |
+ public: |
+ NonSfiUnitTestPlatformDelegate() { |
+ } |
+ |
+ bool Init() { |
+ base::FilePath dir_exe; |
+ if (!PathService::Get(base::DIR_EXE, &dir_exe)) { |
+ LOG(ERROR) << "Failed to get directory of the current executable."; |
return false; |
} |
- writable_path_ = base::FilePath(raw_output); |
+ test_path_ = dir_exe.AppendASCII("nacl_helper_nonsfi_unittests_main"); |
return true; |
} |
- bool GetTests(std::vector<base::SplitTestName>* output) override { |
- base::ScopedTempDir temp_dir; |
- if (!temp_dir.CreateUniqueTempDirUnderPath(writable_path_)) |
+ private: |
+ bool CreateTemporaryFile(base::FilePath* path) override { |
+ if (!base::CreateNewTempDirectory(base::FilePath::StringType(), path)) |
return false; |
- base::FilePath test_list_path( |
- temp_dir.path().AppendASCII("test_list.json")); |
- |
- base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); |
- cmd_line.AppendSwitchPath(switches::kTestLauncherListTests, test_list_path); |
- cmd_line.PrependWrapper(dir_exe_.AppendASCII("iossim").value()); |
+ *path = path->AppendASCII("test_results.xml"); |
+ return true; |
+ } |
- base::LaunchOptions launch_options; |
- launch_options.wait = true; |
+ bool GetTests(std::vector<base::SplitTestName>* output) override { |
+ base::CommandLine cmd_line(test_path_); |
+ cmd_line.AppendSwitch(base::kGTestListTestsFlag); |
- if (!base::LaunchProcess(cmd_line, launch_options).IsValid()) |
+ std::string raw_output; |
+ if (!base::GetAppOutput(cmd_line, &raw_output)) { |
+ LOG(ERROR) << "Failed to run test with --gtest_list"; |
return false; |
+ } |
- return base::ReadTestNamesFromFile(test_list_path, output); |
+ return ParseGTestList(raw_output, output); |
} |
- bool CreateTemporaryFile(base::FilePath* path) override { |
- if (!CreateTemporaryDirInDir(writable_path_, std::string(), path)) |
- return false; |
- *path = path->AppendASCII("test_results.xml"); |
- return true; |
+ std::string GetWrapperForChildGTestProcess() override { |
+ return std::string(); |
} |
base::CommandLine GetCommandLineForChildGTestProcess( |
const std::vector<std::string>& test_names, |
const base::FilePath& output_file) override { |
- base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); |
- cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); |
- cmd_line.AppendSwitchASCII(base::kGTestFilterFlag, |
- JoinString(test_names, ":")); |
- return cmd_line; |
- } |
+ base::CommandLine cmd_line(test_path_); |
+ cmd_line.AppendSwitchASCII( |
+ base::kGTestOutputFlag, "xml:" + output_file.value()); |
+ cmd_line.AppendSwitchASCII( |
+ base::kGTestFilterFlag, JoinString(test_names, ":")); |
- std::string GetWrapperForChildGTestProcess() override { |
- return dir_exe_.AppendASCII("iossim").value(); |
+ return cmd_line; |
} |
void RelaunchTests(base::TestLauncher* test_launcher, |
const std::vector<std::string>& test_names, |
int launch_flags) override { |
- // Relaunch all tests in one big batch, since overhead of smaller batches |
- // is too big for serialized runs inside ios simulator. |
RunUnitTestsBatch(test_launcher, this, test_names, launch_flags); |
} |
- private: |
- // Directory containing test launcher's executable. |
- base::FilePath dir_exe_; |
- |
- // Name of the test executable to run. |
- std::string test_name_; |
- |
- // Path that launched test binary can write to. |
- base::FilePath writable_path_; |
- |
- DISALLOW_COPY_AND_ASSIGN(IOSUnitTestPlatformDelegate); |
+ base::FilePath test_path_; |
}; |
} // namespace |
@@ -168,20 +182,21 @@ int main(int argc, char** argv) { |
base::MessageLoopForIO message_loop; |
- IOSUnitTestPlatformDelegate platform_delegate; |
+ NonSfiUnitTestPlatformDelegate platform_delegate; |
if (!platform_delegate.Init()) { |
- fprintf(stderr, "Failed to intialize test launcher platform delegate.\n"); |
+ fprintf(stderr, "Failed to initialize test launcher.\n"); |
fflush(stderr); |
return 1; |
} |
- base::UnitTestLauncherDelegate delegate(&platform_delegate, 0, false); |
- // Force one job since we can't run multiple simulators in parallel. |
- base::TestLauncher launcher(&delegate, 1); |
+ |
+ base::UnitTestLauncherDelegate delegate(&platform_delegate, 10, true); |
+ base::TestLauncher launcher(&delegate, base::SysInfo::NumberOfProcessors()); |
+ // Allow --gtest_output option of the vanilla gtest library. |
+ launcher.set_use_child_gtest_output(true); |
bool success = launcher.Run(); |
fprintf(stdout, "Tests took %" PRId64 " seconds.\n", |
(base::TimeTicks::Now() - start_time).InSeconds()); |
fflush(stdout); |
- |
- return (success ? 0 : 1); |
+ return success ? 0 : 1; |
} |