Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve formatting of Error Logs related to CHIP_ERROR in GoogleTest #36265

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build/chip/tests.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions build/toolchain/pw_fuzzer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions scripts/build/builders/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down Expand Up @@ -116,6 +117,7 @@ source_set("string-builder-adapters") {
public_deps = [
":error",
"$dir_pw_string",
"$dir_pw_unit_test",
]
}

Expand Down
11 changes: 11 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
* @}
*/
15 changes: 15 additions & 0 deletions src/lib/core/StringBuilderAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> 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
20 changes: 20 additions & 0 deletions src/lib/core/StringBuilderAdapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
/// Actual: CHIP_ERROR:<src/lib/core/TLVReader.cpp:889: Error 0x00000022> == CHIP_NO_ERROR

#include <pw_string/string_builder.h>
#include <pw_unit_test/framework.h>

#include <lib/core/CHIPError.h>

Expand All @@ -51,3 +52,22 @@ template <>
StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> 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:<src/lib/core/TLVWriter.cpp:674: Error 0x00000024>
/// 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
Loading