Skip to content
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
21 changes: 20 additions & 1 deletion test/mocking_utils/stdio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,26 @@ using _vsnprintf_s_type =
/// Patch _vscprintf with the given `replacement` in the given `scope`.
#define patch__vscprintf(scope, replacement) patch(scope, _vscprintf, replacement)

#endif // defined(_WIN32)
#else // defined(_WIN32)

// Deal with binary API nuances
#ifndef NDEBUG

/// Patch vsnprintf with the given `replacement` in the given `scope`.
#define patch_vsnprintf(scope, replacement) patch(scope, vsnprintf, replacement)

#else

/// Patch vsnprintf with the given `replacement` in the given `scope`.
#define patch_vsnprintf(scope, replacement) patch( \
scope, __vsnprintf_chk, [&](char * __str, size_t, int, size_t __size, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this kind of mocking is really fragile.

I prefer having less coverage and not doing this, but I guess it's not my call.

Copy link
Contributor Author

@hidmic hidmic Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this kind of mocking is really fragile.

Indeed mocking system libraries this way is fragile, but as of today the alternative is not having coverage at all. Perhaps, eventually we can migrate towards fault injection (which has its own share of ifs and buts, like Release binary and Debug binaries not being the same).

I prefer having less coverage

Generally speaking, I don't agree with reducing test coverage simply because it's inconvenient. I'd much rather look for a replacement if this becomes a pain point.

const char * __fmt, va_list __ap) { \
auto f = replacement; \
return f(__str, __size, __fmt, __ap); \
})

#endif
#endif

} // namespace mocking_utils

Expand Down
4 changes: 2 additions & 2 deletions test/test_char_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ TEST_F(ArrayCharTest, vsprintf_fail) {
return -1;
});
#else
auto mock = mocking_utils::patch(
"lib:rcutils", vsnprintf, [&](char * buffer, size_t buffer_size, auto...) {
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [&](char * buffer, size_t buffer_size, auto...) {
if (nullptr == buffer || buffer_size < buffer_threshold) {
return static_cast<int>(buffer_threshold);
}
Expand Down
4 changes: 2 additions & 2 deletions test/test_format_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ TEST(test_format_string_limit, on_internal_error) {
return -1;
});
#else
auto mock = mocking_utils::patch(
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
Expand Down
4 changes: 2 additions & 2 deletions test/test_shared_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ TEST_F(TestSharedLibrary, basic_load) {
return -1;
});
#else
auto mock = mocking_utils::patch(
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
Expand Down
8 changes: 4 additions & 4 deletions test/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ TEST_F(TestTimeFixture, test_rcutils_time_point_value_as_nanoseconds_string) {
return -1;
});
#else
auto mock = mocking_utils::patch(
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
Expand Down Expand Up @@ -391,8 +391,8 @@ TEST_F(TestTimeFixture, test_rcutils_time_point_value_as_seconds_string) {
return -1;
});
#else
auto mock = mocking_utils::patch(
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
auto mock = mocking_utils::patch_vsnprintf(
"lib:rcutils", [](char * buffer, auto && ...) {
if (nullptr == buffer) {
return 1; // provide a dummy value if buffer required size is queried
}
Expand Down