Merge d4774d625961bf2fba8b5a72a60cfb081616f315 into 7140cd416cecd7462a8aae488024abeee55598e4

This commit is contained in:
Krish Satasiya 2026-06-03 09:04:01 +08:00 committed by GitHub
commit 7a7904ec3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 73 additions and 17 deletions

View File

@ -64,3 +64,4 @@ Vadim Berman <vadimb@google.com>
Vlad Losev <vladl@google.com>
Wolfgang Klier <wklier@google.com>
Zhanyong Wan <wan@google.com>
Krish Satasiya <krishsatasiya44@gmail.com>

View File

@ -167,14 +167,12 @@ function(cxx_library_with_type name type cxx_flags)
set_target_properties(${name}
PROPERTIES
COMPILE_FLAGS "${cxx_flags}")
# Set the output directory for build artifacts.
set_target_properties(${name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
# Do not hardcode output directories so that the user's
# CMAKE_RUNTIME_OUTPUT_DIRECTORY, CMAKE_ARCHIVE_OUTPUT_DIRECTORY,
# CMAKE_LIBRARY_OUTPUT_DIRECTORY, CMAKE_PDB_OUTPUT_DIRECTORY, and
# CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY variables are respected.
# CMake will apply those variables automatically when the corresponding
# target properties are left unset.
# Make PDBs match library name.
get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
set_target_properties(${name}
@ -317,12 +315,14 @@ function(install_project)
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# Install PDBs.
# Use $<TARGET_FILE_DIR:t> so the correct directory is resolved at
# install time regardless of what CMAKE_PDB_OUTPUT_DIRECTORY or
# CMAKE_RUNTIME_OUTPUT_DIRECTORY is set to by the user.
foreach(t ${ARGN})
get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
install(FILES
"${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
"$<TARGET_FILE_DIR:${t}>/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
COMPONENT "${PROJECT_NAME}"
DESTINATION ${CMAKE_INSTALL_LIBDIR}
OPTIONAL)

View File

@ -222,10 +222,20 @@ struct PointerPrinter {
if (p == nullptr) {
*os << "NULL";
} else {
// T is not a function type. We just call << to print p,
// relying on ADL to pick up user-defined << for their pointer
// types, if any.
*os << p;
// Print the pointer value as a hex address using const void*.
// We use an explicit static_cast rather than relying on the
// implicit T* -> const void* conversion + ADL lookup of
// operator<<(ostream&, const void*), because ADL rules for pointer
// types vary across compilers: AppleClang 17 on macOS arm64 restricts
// ADL to the namespace of the pointed-to type T and does not find
// std::operator<<(ostream&, const void*) via the implicit conversion
// path. The explicit cast makes this well-defined and portable on all
// platforms.
//
// Note: users who want PrintTo() to show the pointed-at value should
// define PrintTo(T*, ostream*) in T's own namespace — that is the
// portable, documented extension point.
*os << static_cast<const void*>(p);
}
}
};

View File

@ -6003,6 +6003,21 @@ bool UnitTestImpl::RunAllTests() {
FilterTests(should_shard ? HONOR_SHARDING_PROTOCOL
: IGNORE_SHARDING_PROTOCOL) > 0;
// Check if any tests matched the filter, including disabled ones.
// This is needed to decide whether to run environment SetUp/TearDown:
// environments should be set up and torn down even if all matching tests
// are disabled, since the environment may need to perform cleanup.
bool has_tests_matching_filter = false;
for (const auto* test_suite : test_suites_) {
for (const TestInfo* test_info : test_suite->test_info_list()) {
if (test_info->matches_filter_ && !test_info->is_in_another_shard_) {
has_tests_matching_filter = true;
break;
}
}
if (has_tests_matching_filter) break;
}
// Lists the tests and exits if the --gtest_list_tests flag was specified.
if (GTEST_FLAG_GET(list_tests)) {
// This must be called *after* FilterTests() has been called.
@ -6056,8 +6071,12 @@ bool UnitTestImpl::RunAllTests() {
// Tells the unit test event listeners that the tests are about to start.
repeater->OnTestIterationStart(*parent_, i);
// Runs each test suite if there is at least one test to run.
if (has_tests_to_run) {
// Sets up and tears down global test environments if any tests matched
// the filter for this shard, including disabled tests. This ensures that
// environment TearDown() runs for cleanup even when all matching tests
// are disabled. Test suites are only actually executed when there are
// non-disabled tests to run (each TestSuite::Run() checks should_run_).
if (has_tests_matching_filter) {
// Sets up all environments beforehand. If test environments aren't
// recreated for each iteration, only do so on the first iteration.
if (i == 0 || recreate_environments_when_repeating) {
@ -6113,7 +6132,9 @@ bool UnitTestImpl::RunAllTests() {
TearDownEnvironment);
repeater->OnEnvironmentsTearDownEnd(*parent_);
}
} else if (GTEST_FLAG_GET(fail_if_no_test_selected)) {
}
if (!has_tests_to_run && GTEST_FLAG_GET(fail_if_no_test_selected)) {
// If there were no tests to run, bail if we were requested to be
// strict.
constexpr char kNoTestsSelectedMessage[] =

View File

@ -86,6 +86,10 @@ class MyEnvironment : public testing::Environment {
// was run.
TEST(FooTest, Bar) { test_was_run = true; }
// A disabled test used to verify that environments are still set up and
// torn down when all matching tests are disabled.
TEST(FooTest, DISABLED_Baz) { FAIL() << "Disabled test should not run."; }
// Prints the message and aborts the program if condition is false.
void Check(bool condition, const char* msg) {
if (!condition) {
@ -158,6 +162,25 @@ void TestNoTestsRunSetUpFailure() {
"The global tear-down should run, as the global set-up was run.");
}
// Verifies that RUN_ALL_TESTS() runs the global set-up and tear-down
// even when all tests matching the filter are disabled.
void TestDisabledTestsRunEnvironments() {
MyEnvironment* const env = RegisterTestEnv();
GTEST_FLAG_SET(filter, "FooTest.DISABLED_Baz");
Check(RunAllTests(env, NO_FAILURE) != 0,
"RUN_ALL_TESTS() should return non-zero, as the global tear-down "
"should generate a failure.");
Check(!test_was_run,
"The disabled test should not have run.");
Check(set_up_was_run,
"The global set-up should run, even when all matching tests "
"are disabled.");
Check(tear_down_was_run,
"The global tear-down should run, even when all matching tests "
"are disabled.");
GTEST_FLAG_SET(filter, "*");
}
// Verifies that RUN_ALL_TESTS() doesn't do global set-up or
// tear-down when there is no test to run.
void TestNoTestsSkipsSetUp() {
@ -180,6 +203,7 @@ int main(int argc, char** argv) {
TestGlobalSetUp();
TestTestsRun();
TestNoTestsRunSetUpFailure();
TestDisabledTestsRunEnvironments();
TestNoTestsSkipsSetUp();
printf("PASS\n");