diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index ee0630c5..56701c06 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -350,6 +350,21 @@ TEST(BufferedFileTest, CloseErrorInDtor) { }, FormatSystemErrorMessage(EBADF, "cannot close file") + "\n"); } +TEST(BufferedFileTest, Close) { + BufferedFile f = OpenFile(".travis.yml"); + int fd = fileno(f.get()); + f.close(); + EXPECT_TRUE(f.get() == 0); + EXPECT_TRUE(IsClosed(fd)); +} + +TEST(BufferedFileTest, CloseError) { + BufferedFile f = OpenFile(".travis.yml"); + close(fileno(f.get())); + EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file"); + EXPECT_TRUE(f.get() == 0); +} + TEST(FileTest, DefaultCtor) { File f; EXPECT_EQ(-1, f.descriptor()); @@ -656,7 +671,8 @@ TEST(OutputRedirectTest, ErrorInDtor) { write_dup.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail } -// TODO: compile both in C++11 & C++98 mode -#endif +// TODO: test retry on EINTR + +#endif // FMT_USE_FILE_DESCRIPTORS } // namespace diff --git a/test/gtest-extra.h b/test/gtest-extra.h index 052a3cee..ee9512f6 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -114,8 +114,6 @@ class BufferedFile { explicit BufferedFile(std::FILE *f) : file_(f) {} - void close(); - public: // Constructs a BufferedFile object which doesn't represent any file. BufferedFile() FMT_NOEXCEPT(true) : file_(0) {} @@ -182,10 +180,14 @@ class BufferedFile { } #endif + // Closes the file. + void close(); + + // Returns the pointer to a FILE object representing this file. std::FILE *get() const { return file_; } }; -// A file. +// A file. Closed file is represented by a File object with descriptor -1. // Methods that are not declared with FMT_NOEXCEPT(true) may throw // fmt::SystemError in case of failure. Note that some errors such as // closing the file multiple times will cause a crash on Windows rather @@ -277,7 +279,7 @@ class File { // Returns the file descriptor. int descriptor() const FMT_NOEXCEPT(true) { return fd_; } - // Closes the file if its descriptor is not -1. + // Closes the file. void close(); // Attempts to read count bytes from the file into the specified buffer.