diff options
author | 2017-02-15 18:07:29 +0000 | |
---|---|---|
committer | 2017-02-15 19:19:54 +0000 | |
commit | b535e6df1eda2a799647c9db9a0b9e1baa26f0bc (patch) | |
tree | 0c8ed9825c753326015fcc1037ec44fca1949547 /src/test/cpp/util/file_test.cc | |
parent | 9d88394848fa6742513d85fa5f892ae1702d015e (diff) |
Bazel client: refactors, small bugfixes
In this change:
* Fix pdie and PrintError to acquire the last
error string as the first thing the method does,
otherwise we may display an error coming from
pdie's/PrintError's own logic
* Auto-close file handles in file_test in order to
avoid leaking them in case an assertion fails
(which means an early return from the function)
--
Change-Id: Ia4392b42cbc93b931dcee76993db0ad264d0c147
Reviewed-on: https://cr.bazel.build/8932
PiperOrigin-RevId: 147610527
MOS_MIGRATED_REVID=147610527
Diffstat (limited to 'src/test/cpp/util/file_test.cc')
-rw-r--r-- | src/test/cpp/util/file_test.cc | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/test/cpp/util/file_test.cc b/src/test/cpp/util/file_test.cc index 2b9063c820..ab9ce916d1 100644 --- a/src/test/cpp/util/file_test.cc +++ b/src/test/cpp/util/file_test.cc @@ -19,6 +19,7 @@ #include "src/main/cpp/util/file.h" #include "src/main/cpp/util/file_platform.h" +#include "src/test/cpp/util/test_util.h" #include "gtest/gtest.h" namespace blaze_util { @@ -59,10 +60,10 @@ TEST(FileTest, TestReadFile) { ASSERT_NE(0, tempdir[0]); std::string filename(JoinPath(tempdir, "test.readfile")); - FILE* fh = fopen(filename.c_str(), "wt"); - ASSERT_NE(nullptr, fh); + AutoFileStream fh(fopen(filename.c_str(), "wt")); + EXPECT_TRUE(fh.IsOpen()); ASSERT_EQ(11, fwrite("hello world", 1, 11, fh)); - fclose(fh); + fh.Close(); std::string actual; ASSERT_TRUE(ReadFile(filename, &actual)); @@ -85,19 +86,19 @@ TEST(FileTest, TestWriteFile) { ASSERT_TRUE(WriteFile("hello", 3, filename)); char buf[6] = {0}; - FILE* fh = fopen(filename.c_str(), "rt"); + AutoFileStream fh(fopen(filename.c_str(), "rt")); + EXPECT_TRUE(fh.IsOpen()); fflush(fh); - ASSERT_NE(nullptr, fh); ASSERT_EQ(3, fread(buf, 1, 5, fh)); - fclose(fh); + fh.Close(); ASSERT_EQ(std::string(buf), std::string("hel")); ASSERT_TRUE(WriteFile("hello", 5, filename)); fh = fopen(filename.c_str(), "rt"); - ASSERT_NE(nullptr, fh); + EXPECT_TRUE(fh.IsOpen()); memset(buf, 0, 6); ASSERT_EQ(5, fread(buf, 1, 5, fh)); - fclose(fh); + fh.Close(); ASSERT_EQ(std::string(buf), std::string("hello")); ASSERT_TRUE(WriteFile("hello", 5, "/dev/null")); |