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