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 #ifndef BASE_TEST_LAUNCHER_UNIT_TEST_LAUNCHER_H_ 6 #define BASE_TEST_LAUNCHER_UNIT_TEST_LAUNCHER_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 #include <string_view> 12 #include <vector> 13 14 #include "base/files/file_path.h" 15 #include "base/files/scoped_temp_dir.h" 16 #include "base/functional/callback.h" 17 #include "base/memory/raw_ptr.h" 18 #include "build/blink_buildflags.h" 19 #include "build/build_config.h" 20 21 #if BUILDFLAG(USE_BLINK) 22 #include "base/test/launcher/test_launcher.h" 23 #endif 24 25 namespace base { 26 27 // Callback that runs a test suite and returns exit code. 28 using RunTestSuiteCallback = OnceCallback<int(void)>; 29 30 // Launches unit tests in given test suite. Returns exit code. 31 int LaunchUnitTests(int argc, 32 char** argv, 33 RunTestSuiteCallback run_test_suite, 34 size_t retry_limit = 1U); 35 36 // Same as above, but always runs tests serially. 37 int LaunchUnitTestsSerially(int argc, 38 char** argv, 39 RunTestSuiteCallback run_test_suite); 40 41 // The following is not supported in unit_test_launcher_ios.cc, which is used on 42 // iOS unless Blink is enabled. 43 #if BUILDFLAG(USE_BLINK) 44 45 // Launches unit tests in given test suite. Returns exit code. 46 // |parallel_jobs| is the number of parallel test jobs. 47 // |default_batch_limit| is the default size of test batch 48 // (use 0 to disable batching). 49 // |use_job_objects| determines whether to use job objects. 50 // |timeout_callback| is called each time a test batch times out. It can be used 51 // as a cue to print additional debugging information about the test system, 52 // such as log files or the names of running processes. 53 int LaunchUnitTestsWithOptions(int argc, 54 char** argv, 55 size_t parallel_jobs, 56 int default_batch_limit, 57 bool use_job_objects, 58 RepeatingClosure timeout_callback, 59 RunTestSuiteCallback run_test_suite); 60 61 #if BUILDFLAG(IS_WIN) 62 // Launches unit tests in given test suite. Returns exit code. 63 // |use_job_objects| determines whether to use job objects. 64 int LaunchUnitTests(int argc, 65 wchar_t** argv, 66 bool use_job_objects, 67 RunTestSuiteCallback run_test_suite); 68 #endif // BUILDFLAG(IS_WIN) 69 70 // Delegate to abstract away platform differences for unit tests. 71 class UnitTestPlatformDelegate { 72 public: 73 // Called to get names of tests available for running. The delegate 74 // must put the result in |output| and return true on success. 75 virtual bool GetTests(std::vector<TestIdentifier>* output) = 0; 76 77 // Called to create a temporary for storing test results. The delegate 78 // must put the resulting path in |path| and return true on success. 79 virtual bool CreateResultsFile(const base::FilePath& temp_dir, 80 base::FilePath* path) = 0; 81 82 // Called to create a new temporary file. The delegate must put the resulting 83 // path in |path| and return true on success. 84 virtual bool CreateTemporaryFile(const base::FilePath& temp_dir, 85 base::FilePath* path) = 0; 86 87 // Returns command line for child GTest process based on the command line 88 // of current process. |test_names| is a vector of test full names 89 // (e.g. "A.B"), |output_file| is path to the GTest XML output file. 90 virtual CommandLine GetCommandLineForChildGTestProcess( 91 const std::vector<std::string>& test_names, 92 const base::FilePath& output_file, 93 const base::FilePath& flag_file) = 0; 94 95 // Returns wrapper to use for child GTest process. Empty string means 96 // no wrapper. 97 virtual std::string GetWrapperForChildGTestProcess() = 0; 98 99 protected: 100 ~UnitTestPlatformDelegate() = default; 101 }; 102 103 // This default implementation uses gtest_util to get all 104 // compiled gtests into the binary. 105 // The delegate will relaunch test in parallel, 106 // but only use single test per launch. 107 class DefaultUnitTestPlatformDelegate : public UnitTestPlatformDelegate { 108 public: 109 DefaultUnitTestPlatformDelegate(); 110 111 DefaultUnitTestPlatformDelegate(const DefaultUnitTestPlatformDelegate&) = 112 delete; 113 DefaultUnitTestPlatformDelegate& operator=( 114 const DefaultUnitTestPlatformDelegate&) = delete; 115 116 private: 117 // UnitTestPlatformDelegate: 118 119 bool GetTests(std::vector<TestIdentifier>* output) override; 120 121 bool CreateResultsFile(const base::FilePath& temp_dir, 122 base::FilePath* path) override; 123 124 bool CreateTemporaryFile(const base::FilePath& temp_dir, 125 base::FilePath* path) override; 126 127 CommandLine GetCommandLineForChildGTestProcess( 128 const std::vector<std::string>& test_names, 129 const base::FilePath& output_file, 130 const base::FilePath& flag_file) override; 131 132 std::string GetWrapperForChildGTestProcess() override; 133 134 ScopedTempDir temp_dir_; 135 }; 136 137 // Test launcher delegate for unit tests (mostly to support batching). 138 class UnitTestLauncherDelegate : public TestLauncherDelegate { 139 public: 140 UnitTestLauncherDelegate(UnitTestPlatformDelegate* delegate, 141 size_t batch_limit, 142 bool use_job_objects, 143 RepeatingClosure timeout_callback); 144 145 UnitTestLauncherDelegate(const UnitTestLauncherDelegate&) = delete; 146 UnitTestLauncherDelegate& operator=(const UnitTestLauncherDelegate&) = delete; 147 148 ~UnitTestLauncherDelegate() override; 149 150 private: 151 // TestLauncherDelegate: 152 bool GetTests(std::vector<TestIdentifier>* output) override; 153 154 CommandLine GetCommandLine(const std::vector<std::string>& test_names, 155 const FilePath& temp_dir, 156 FilePath* output_file) override; 157 158 std::string GetWrapper() override; 159 160 int GetLaunchOptions() override; 161 162 TimeDelta GetTimeout() override; 163 164 size_t GetBatchSize() override; 165 166 void OnTestTimedOut(const CommandLine& cmd_line) override; 167 168 ThreadChecker thread_checker_; 169 170 raw_ptr<UnitTestPlatformDelegate> platform_delegate_; 171 172 // Maximum number of tests to run in a single batch. 173 size_t batch_limit_; 174 175 // Determines whether we use job objects on Windows. 176 bool use_job_objects_; 177 178 // Callback to invoke when a test process times out. 179 RepeatingClosure timeout_callback_; 180 }; 181 182 // We want to stop throwing away duplicate test filter file flags, but we're 183 // afraid of changing too much in fear of breaking other use cases. 184 // If you feel like another flag should be merged instead of overridden, 185 // feel free to make this into a set of flags in this function, 186 // or add its own merging code. 187 // 188 // out_value contains the existing value and is modified to resolve the 189 // duplicate 190 class MergeTestFilterSwitchHandler : public DuplicateSwitchHandler { 191 public: 192 ~MergeTestFilterSwitchHandler() override; 193 194 void ResolveDuplicate(std::string_view key, 195 CommandLine::StringPieceType new_value, 196 CommandLine::StringType& out_value) override; 197 }; 198 199 #endif // BUILDFLAG(USE_BLINK) 200 201 } // namespace base 202 203 #endif // BASE_TEST_LAUNCHER_UNIT_TEST_LAUNCHER_H_ 204