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