diff options
author | Derek Mauro <dmauro@google.com> | 2025-02-26 19:56:21 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2025-02-26 19:57:12 (GMT) |
commit | e5669fdffc94e901bcad20a69b38547012f81882 (patch) | |
tree | 356008986240a52b32ab03de0161bdde981bc4c6 /googletest/src | |
parent | 54501746a6cfd366b6e7bab3c902e0f8d5826521 (diff) | |
download | googletest-e5669fdffc94e901bcad20a69b38547012f81882.zip googletest-e5669fdffc94e901bcad20a69b38547012f81882.tar.gz googletest-e5669fdffc94e901bcad20a69b38547012f81882.tar.bz2 |
Output to the test warning file if no tests are linked.
Bazel sets the environment variable TEST_WARNINGS_OUTPUT_FILE
https://bazel.build/reference/test-encyclopedia#initial-conditions
If no tests are linked and the new flag --gtest_fail_if_no_test_linked
is not true (which is the current default), we can still warn the user
as this may be a programming error without failing the test (which would
break existing users).
PiperOrigin-RevId: 731402363
Change-Id: Ia481689efd4bd18889feaaa38bc56049a3f651cd
Diffstat (limited to 'googletest/src')
-rw-r--r-- | googletest/src/gtest.cc | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 81096ae..26959df 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -192,12 +192,17 @@ static const char kDefaultOutputFormat[] = "xml"; // The default output file. static const char kDefaultOutputFile[] = "test_detail"; +// These environment variables are set by Bazel. +// https://bazel.build/reference/test-encyclopedia#initial-conditions +// // The environment variable name for the test shard index. static const char kTestShardIndex[] = "GTEST_SHARD_INDEX"; // The environment variable name for the total number of test shards. static const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS"; // The environment variable name for the test shard status file. static const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE"; +// The environment variable name for the test output warnings file. +static const char kTestWarningsOutputFile[] = "TEST_WARNINGS_OUTPUT_FILE"; namespace internal { @@ -5875,6 +5880,23 @@ TestSuite* UnitTestImpl::GetTestSuite( static void SetUpEnvironment(Environment* env) { env->SetUp(); } static void TearDownEnvironment(Environment* env) { env->TearDown(); } +// If the environment variable TEST_WARNINGS_OUTPUT_FILE was provided, appends +// `str` to the file, creating the file if necessary. +#if GTEST_HAS_FILE_SYSTEM +static void AppendToTestWarningsOutputFile(const std::string& str) { + const char* const filename = posix::GetEnv(kTestWarningsOutputFile); + if (filename == nullptr) { + return; + } + auto* const file = posix::FOpen(filename, "a"); + if (file == nullptr) { + return; + } + GTEST_CHECK_(fwrite(str.data(), 1, str.size(), file) == str.size()); + GTEST_CHECK_(posix::FClose(file) == 0); +} +#endif // GTEST_HAS_FILE_SYSTEM + // Runs all tests in this UnitTest object, prints the result, and // returns true if all tests are successful. If any exception is // thrown during a test, the test is considered to be failed, but the @@ -5896,12 +5918,26 @@ bool UnitTestImpl::RunAllTests() { // user didn't call InitGoogleTest. PostFlagParsingInit(); - if (GTEST_FLAG_GET(fail_if_no_test_linked) && total_test_count() == 0) { + // Handle the case where the program has no tests linked. + // Sometimes this is a programmer mistake, but sometimes it is intended. + if (total_test_count() == 0) { + constexpr char kNoTestLinkedMessage[] = + "This test program does NOT link in any test case."; + constexpr char kNoTestLinkedFatal[] = + "This is INVALID. Please make sure to link in at least one test case."; + constexpr char kNoTestLinkedWarning[] = + "Please make sure this is intended."; + const bool fail_if_no_test_linked = GTEST_FLAG_GET(fail_if_no_test_linked); ColoredPrintf( - GTestColor::kRed, - "This test program does NOT link in any test case. This is INVALID. " - "Please make sure to link in at least one test case.\n"); - return false; + GTestColor::kRed, "%s %s\n", kNoTestLinkedMessage, + fail_if_no_test_linked ? kNoTestLinkedFatal : kNoTestLinkedWarning); + if (fail_if_no_test_linked) { + return false; + } +#if GTEST_HAS_FILE_SYSTEM + AppendToTestWarningsOutputFile(std::string(kNoTestLinkedMessage) + ' ' + + kNoTestLinkedWarning + '\n'); +#endif // GTEST_HAS_FILE_SYSTEM } #if GTEST_HAS_FILE_SYSTEM |