From ffa5b14fe3c05fb111c6e1c4f9b98a7f5e0cc8ee Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 24 Nov 2023 10:09:21 -0800 Subject: [PATCH] Make gtest-extra-test more portable --- test/gtest-extra-test.cc | 2 +- test/gtest-extra.cc | 4 ++-- test/gtest-extra.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 42340a2d..34054b68 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -354,7 +354,7 @@ TEST(output_redirect_test, dup_error_in_ctor) { FMT_POSIX(close(fd)); std::unique_ptr redir{nullptr}; EXPECT_SYSTEM_ERROR_NOASSERT( - redir.reset(new output_redirect(f.get())), EBADF, + redir.reset(new output_redirect(f.get(), false)), EBADF, fmt::format("cannot duplicate file descriptor {}", fd)); copy.dup2(fd); // "undo" close or dtor will fail } diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 542e4b5e..3d27cf96 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -11,8 +11,8 @@ using fmt::file; -output_redirect::output_redirect(FILE* f) : file_(f) { - flush(); +output_redirect::output_redirect(FILE* f, bool flush) : file_(f) { + if (flush) this->flush(); int fd = FMT_POSIX(fileno(f)); // Create a file object referring to the original file. original_ = file::dup(fd); diff --git a/test/gtest-extra.h b/test/gtest-extra.h index 03a07a2a..e08c94c0 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -77,7 +77,7 @@ class output_redirect { void restore(); public: - explicit output_redirect(FILE* file); + explicit output_redirect(FILE* file, bool flush = true); ~output_redirect() noexcept; output_redirect(const output_redirect&) = delete;