summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-06-15 15:45:45 +0200
committerRyan Dahl <ry@tinyclouds.org>2018-06-15 22:19:00 +0200
commit97923e3d26d2fb5a6be6c00b8d8d6a085cf71b8e (patch)
treeb535881c51659cc9d79f3759e97e440f00adf3ef
parentb2694ecbd85a5a32803e24f1d6c9020173408646 (diff)
Fix error handling in deno::ReadFileToString
Starts a unit test for it, and adds to mock_runtime_test.
-rw-r--r--deno2/BUILD.gn1
-rw-r--r--deno2/file_util.cc5
-rw-r--r--deno2/file_util_test.cc13
3 files changed, 18 insertions, 1 deletions
diff --git a/deno2/BUILD.gn b/deno2/BUILD.gn
index 37b9f85c6..5cb49aaa7 100644
--- a/deno2/BUILD.gn
+++ b/deno2/BUILD.gn
@@ -17,6 +17,7 @@ executable("deno") {
executable("mock_runtime_test") {
testonly = true
sources = [
+ "file_util_test.cc",
"from_snapshot.cc",
"mock_runtime_test.cc",
]
diff --git a/deno2/file_util.cc b/deno2/file_util.cc
index e2c1b3049..ece14e9b1 100644
--- a/deno2/file_util.cc
+++ b/deno2/file_util.cc
@@ -11,8 +11,11 @@ namespace deno {
bool ReadFileToString(const char* fn, std::string* contents) {
std::ifstream file(fn, std::ios::binary);
+ if (file.fail()) {
+ return false;
+ }
contents->assign(std::istreambuf_iterator<char>{file}, {});
- return !file.bad();
+ return !file.fail();
}
class StartupDataCppWriter {
diff --git a/deno2/file_util_test.cc b/deno2/file_util_test.cc
new file mode 100644
index 000000000..3b1c3e239
--- /dev/null
+++ b/deno2/file_util_test.cc
@@ -0,0 +1,13 @@
+// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
+// All rights reserved. MIT License.
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include "file_util.h"
+
+TEST(FileUtilTest, ReadFileToStringFileNotExist) {
+ std::string output;
+ EXPECT_FALSE(deno::ReadFileToString("/should_error_out.txt", &output));
+}
+
+// TODO(ry) success unit test. Needs a tempfile or fixture.
+// TEST(FileUtilTest, ReadFileToStringSuccess) { }