@@ -407,6 +407,18 @@ GTEST_DEFINE_bool_(
407407 "if exceptions are enabled or exit the program with a non-zero code "
408408 "otherwise. For use with an external test framework.");
409409
410+ GTEST_DEFINE_int32_ (
411+ shard_index,
412+ testing::internal::Int32FromEnvOrDie (testing::kTestShardIndex , -1 ),
413+ "The zero-based index of the shard to run. A value of -1 "
414+ "(the default ) indicates that sharding is disabled.");
415+
416+ GTEST_DEFINE_int32_ (
417+ total_shards,
418+ testing::internal::Int32FromEnvOrDie (testing::kTestTotalShards , -1 ),
419+ "The total number of shards to use when running tests in parallel. "
420+ "A value of -1 (the default ) indicates that sharding is disabled.");
421+
410422#if GTEST_USE_OWN_FLAGFILE_FLAG_
411423GTEST_DEFINE_string_ (
412424 flagfile, testing::internal::StringFromGTestEnv(" flagfile" , " " ),
@@ -3475,11 +3487,11 @@ void PrettyUnitTestResultPrinter::OnTestIterationStart(
34753487 filter);
34763488 }
34773489
3478- if (internal::ShouldShard (kTestTotalShards , kTestShardIndex , false )) {
3479- const int32_t shard_index = Int32FromEnvOrDie ( kTestShardIndex , - 1 );
3480- ColoredPrintf (GTestColor::kYellow , " Note: This is test shard %d of %s .\n " ,
3490+ if (internal::ShouldShard (false )) {
3491+ const int32_t shard_index = GTEST_FLAG_GET (shard_index );
3492+ ColoredPrintf (GTestColor::kYellow , " Note: This is test shard %d of %d .\n " ,
34813493 static_cast <int >(shard_index) + 1 ,
3482- internal::posix::GetEnv ( kTestTotalShards ));
3494+ GTEST_FLAG_GET (total_shards ));
34833495 }
34843496
34853497 if (GTEST_FLAG_GET (shuffle)) {
@@ -5983,8 +5995,7 @@ bool UnitTestImpl::RunAllTests() {
59835995#endif // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)
59845996#endif // GTEST_HAS_DEATH_TEST
59855997
5986- const bool should_shard = ShouldShard (kTestTotalShards , kTestShardIndex ,
5987- in_subprocess_for_death_test);
5998+ const bool should_shard = ShouldShard (in_subprocess_for_death_test);
59885999
59896000 // Compares the full test names with the filter to decide which
59906001 // tests to run.
@@ -6196,45 +6207,44 @@ void WriteToShardStatusFileIfNeeded() {
61966207}
61976208#endif // GTEST_HAS_FILE_SYSTEM
61986209
6199- // Checks whether sharding is enabled by examining the relevant
6200- // environment variable values. If the variables are present,
6201- // but inconsistent (i.e., shard_index >= total_shards), prints
6202- // an error and exits. If in_subprocess_for_death_test, sharding is
6203- // disabled because it must only be applied to the original test
6204- // process. Otherwise, we could filter out death tests we intended to execute.
6205- bool ShouldShard (const char * total_shards_env, const char * shard_index_env,
6206- bool in_subprocess_for_death_test) {
6210+ // Checks whether sharding is enabled by examining the relevant command line
6211+ // arguments. If the arguments are present, but inconsistent
6212+ // (i.e., shard_index >= total_shards), prints an error and exits.
6213+ // If in_subprocess_for_death_test, sharding is disabled because it must only
6214+ // be applied to the original test process. Otherwise, we could filter out death
6215+ // tests we intended to execute.
6216+ bool ShouldShard (bool in_subprocess_for_death_test) {
62076217 if (in_subprocess_for_death_test) {
62086218 return false ;
62096219 }
62106220
6211- const int32_t total_shards = Int32FromEnvOrDie (total_shards_env, - 1 );
6212- const int32_t shard_index = Int32FromEnvOrDie (shard_index_env, - 1 );
6221+ const int32_t total_shards = GTEST_FLAG_GET (total_shards );
6222+ const int32_t shard_index = GTEST_FLAG_GET (shard_index );
62136223
62146224 if (total_shards == -1 && shard_index == -1 ) {
62156225 return false ;
62166226 } else if (total_shards == -1 && shard_index != -1 ) {
6217- const Message msg = Message () << " Invalid environment variables: you have "
6218- << kTestShardIndex << " = " << shard_index
6219- << " , but have left " << kTestTotalShards
6220- << " unset.\n " ;
6227+ const Message msg = Message ()
6228+ << " Invalid sharding: you have " << kTestShardIndex
6229+ << " = " << shard_index << " , but have left "
6230+ << kTestTotalShards << " unset.\n " ;
62216231 ColoredPrintf (GTestColor::kRed , " %s" , msg.GetString ().c_str ());
62226232 fflush (stdout);
62236233 exit (EXIT_FAILURE);
62246234 } else if (total_shards != -1 && shard_index == -1 ) {
62256235 const Message msg = Message ()
6226- << " Invalid environment variables : you have "
6227- << kTestTotalShards << " = " << total_shards
6228- << " , but have left " << kTestShardIndex << " unset.\n " ;
6236+ << " Invalid sharding : you have " << kTestTotalShards
6237+ << " = " << total_shards << " , but have left "
6238+ << kTestShardIndex << " unset.\n " ;
62296239 ColoredPrintf (GTestColor::kRed , " %s" , msg.GetString ().c_str ());
62306240 fflush (stdout);
62316241 exit (EXIT_FAILURE);
62326242 } else if (shard_index < 0 || shard_index >= total_shards) {
62336243 const Message msg =
6234- Message () << " Invalid environment variables : we require 0 <= "
6235- << kTestShardIndex << " < " << kTestTotalShards
6236- << " , but you have " << kTestShardIndex << " =" << shard_index
6237- << " , " << kTestTotalShards << " =" << total_shards << " .\n " ;
6244+ Message () << " Invalid sharding : we require 0 <= " << kTestShardIndex
6245+ << " < " << kTestTotalShards << " , but you have "
6246+ << kTestShardIndex << " =" << shard_index << " , "
6247+ << kTestTotalShards << " =" << total_shards << " .\n " ;
62386248 ColoredPrintf (GTestColor::kRed , " %s" , msg.GetString ().c_str ());
62396249 fflush (stdout);
62406250 exit (EXIT_FAILURE);
@@ -6277,11 +6287,10 @@ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) {
62776287// . Returns the number of tests that should run.
62786288int UnitTestImpl::FilterTests (ReactionToSharding shard_tests) {
62796289 const int32_t total_shards = shard_tests == HONOR_SHARDING_PROTOCOL
6280- ? Int32FromEnvOrDie ( kTestTotalShards , - 1 )
6290+ ? GTEST_FLAG_GET (total_shards )
62816291 : -1 ;
6282- const int32_t shard_index = shard_tests == HONOR_SHARDING_PROTOCOL
6283- ? Int32FromEnvOrDie (kTestShardIndex , -1 )
6284- : -1 ;
6292+ const int32_t shard_index =
6293+ shard_tests == HONOR_SHARDING_PROTOCOL ? GTEST_FLAG_GET (shard_index) : -1 ;
62856294
62866295 const PositiveAndNegativeUnitTestFilter gtest_flag_filter (
62876296 GTEST_FLAG_GET (filter));
@@ -6810,6 +6819,8 @@ static bool ParseGoogleTestFlag(const char* const arg) {
68106819 GTEST_INTERNAL_PARSE_FLAG (print_utf8);
68116820 GTEST_INTERNAL_PARSE_FLAG (random_seed);
68126821 GTEST_INTERNAL_PARSE_FLAG (repeat);
6822+ GTEST_INTERNAL_PARSE_FLAG (shard_index);
6823+ GTEST_INTERNAL_PARSE_FLAG (total_shards);
68136824 GTEST_INTERNAL_PARSE_FLAG (recreate_environments_when_repeating);
68146825 GTEST_INTERNAL_PARSE_FLAG (shuffle);
68156826 GTEST_INTERNAL_PARSE_FLAG (stack_trace_depth);
0 commit comments