From d93f3f6077f34e19665a25b2b93d7e08a0468986 Mon Sep 17 00:00:00 2001 From: Amine Alami <43780877+Alami-Amine@users.noreply.github.com> Date: Thu, 31 Oct 2024 21:53:20 +0100 Subject: [PATCH] Improve formatting of Error Logs related to CHIP_ERROR in GoogleTest (#36265) * Improve formatting of errors related to CHIP_ERROR in GoogleTest * Improve formatting of errors related to CHIP_ERROR in GoogleTest * Add a buildconfig header for GoogleTest * Activating CHIP_ERROR log formatter for pw_fuzzer fuzztests * Restyled by clang-format --------- Co-authored-by: Restyled.io --- build/chip/tests.gni | 4 ++++ build/toolchain/pw_fuzzer/BUILD.gn | 3 +++ scripts/build/builders/host.py | 1 + src/lib/core/BUILD.gn | 2 ++ src/lib/core/CHIPConfig.h | 11 +++++++++++ src/lib/core/StringBuilderAdapters.cpp | 15 +++++++++++++++ src/lib/core/StringBuilderAdapters.h | 20 ++++++++++++++++++++ 7 files changed, 56 insertions(+) diff --git a/build/chip/tests.gni b/build/chip/tests.gni index 0c2c742d646a15..57d0311694f9be 100755 --- a/build/chip/tests.gni +++ b/build/chip/tests.gni @@ -26,6 +26,10 @@ declare_args() { declare_args() { # Enable building tests. chip_build_tests = current_os != "freertos" + + # Enabling useful support functions when building using GoogleTest framework (used in unit tests and pw_fuzzer FuzzTests) + # For unit tests: this should only be enabled through build_examples.py, see PR #36268 + chip_build_tests_googletest = false } declare_args() { diff --git a/build/toolchain/pw_fuzzer/BUILD.gn b/build/toolchain/pw_fuzzer/BUILD.gn index 468a615448312a..b5a751bc98a903 100644 --- a/build/toolchain/pw_fuzzer/BUILD.gn +++ b/build/toolchain/pw_fuzzer/BUILD.gn @@ -64,6 +64,9 @@ gcc_toolchain("chip_pw_fuzztest") { dir_pw_third_party_fuzztest = "//third_party/fuzztest" dir_pw_third_party_googletest = "$dir_googletest" + # Since pw_fuzzer uses GoogleTest, activating this allows us to benefit from supporting functions + chip_build_tests_googletest = true + # TODO: Seems that re2 support within FuzzTest was deprecated, keeping it defined is triggering warning # Remove if re2 is indeed not needed # dir_pw_third_party_re2 = "//third_party/re2/src" diff --git a/scripts/build/builders/host.py b/scripts/build/builders/host.py index 114f47f971f1df..dbb85f99e87d16 100644 --- a/scripts/build/builders/host.py +++ b/scripts/build/builders/host.py @@ -502,6 +502,7 @@ def __init__(self, root, runner, app: HostApp, board=HostBoard.NATIVE, self.extra_gn_options.append('import("//build_overrides/googletest.gni")') self.extra_gn_options.append('pw_unit_test_BACKEND="$dir_pw_unit_test:googletest"') self.extra_gn_options.append('dir_pw_third_party_googletest="$dir_googletest"') + self.extra_gn_options.append('chip_build_tests_googletest=true') def GnBuildArgs(self): if self.board == HostBoard.NATIVE: diff --git a/src/lib/core/BUILD.gn b/src/lib/core/BUILD.gn index 70680640b025b2..5a6e6e114e4aae 100644 --- a/src/lib/core/BUILD.gn +++ b/src/lib/core/BUILD.gn @@ -71,6 +71,7 @@ buildconfig_header("chip_buildconfig") { "CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_WRITE=${chip_tlv_validate_char_string_on_write}", "CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_READ=${chip_tlv_validate_char_string_on_read}", "CHIP_CONFIG_COMMAND_SENDER_BUILTIN_SUPPORT_FOR_BATCHED_COMMANDS=${chip_enable_sending_batch_commands}", + "CHIP_CONFIG_TEST_GOOGLETEST=${chip_build_tests_googletest}", ] visibility = [ ":chip_config_header" ] @@ -116,6 +117,7 @@ source_set("string-builder-adapters") { public_deps = [ ":error", "$dir_pw_string", + "$dir_pw_unit_test", ] } diff --git a/src/lib/core/CHIPConfig.h b/src/lib/core/CHIPConfig.h index 382c63497da51d..fca3472826a310 100644 --- a/src/lib/core/CHIPConfig.h +++ b/src/lib/core/CHIPConfig.h @@ -1845,6 +1845,17 @@ extern const char CHIP_NON_PRODUCTION_MARKER[]; #define CHIP_CONFIG_MAX_BDX_LOG_TRANSFERS 5 #endif // CHIP_CONFIG_MAX_BDX_LOG_TRANSFERS +/** + * @def CHIP_CONFIG_TEST_GOOGLETEST + * + * @brief + * If asserted (1), enable APIs that support unit tests built with the GoogleTest framework + * + */ +#ifndef CHIP_CONFIG_TEST_GOOGLETEST +#define CHIP_CONFIG_TEST_GOOGLETEST 0 +#endif // CHIP_CONFIG_TEST_GOOGLETEST + /** * @} */ diff --git a/src/lib/core/StringBuilderAdapters.cpp b/src/lib/core/StringBuilderAdapters.cpp index d072c1ee905ab7..be07ae92cf7923 100644 --- a/src/lib/core/StringBuilderAdapters.cpp +++ b/src/lib/core/StringBuilderAdapters.cpp @@ -29,3 +29,18 @@ StatusWithSize ToString(const CHIP_ERROR & err, pw::span buffe } } // namespace pw + +#if CHIP_CONFIG_TEST_GOOGLETEST +namespace chip { + +void PrintTo(const CHIP_ERROR & err, std::ostream * os) +{ + if (CHIP_ERROR::IsSuccess(err)) + { + *os << "CHIP_NO_ERROR"; + return; + } + *os << "CHIP_ERROR:<" << err.Format() << ">"; +} +} // namespace chip +#endif // CHIP_CONFIG_TEST_GOOGLETEST diff --git a/src/lib/core/StringBuilderAdapters.h b/src/lib/core/StringBuilderAdapters.h index f173d56b46e6a1..ad3bfb71e2299a 100644 --- a/src/lib/core/StringBuilderAdapters.h +++ b/src/lib/core/StringBuilderAdapters.h @@ -42,6 +42,7 @@ /// Actual: CHIP_ERROR: == CHIP_NO_ERROR #include +#include #include @@ -51,3 +52,22 @@ template <> StatusWithSize ToString(const CHIP_ERROR & err, pw::span buffer); } // namespace pw +#if CHIP_CONFIG_TEST_GOOGLETEST + +namespace chip { + +/// The following function is for usage with GoogleTest. +/// This implementation of PrintTo allows GoogleTest to print CHIP_ERROR for better logs in the event of a failure. +/// Example output with PrintTo(): +/// +/// src/lib/core/tests/TestTLV.cpp:382: Failure +/// Expected equality of these values: +/// err +/// Which is: CHIP_ERROR: +/// CHIP_ERROR(0, "src/lib/core/tests/TestTLV.cpp", 382) +/// Which is: CHIP_NO_ERROR +/// +/// This enhances the readability and diagnostic information in GoogleTest test logs. +void PrintTo(const CHIP_ERROR & err, std::ostream * os); +} // namespace chip +#endif // CHIP_CONFIG_TEST_GOOGLETEST