From 5358af451cf073e538aad35bf752e2845010be20 Mon Sep 17 00:00:00 2001 From: Avital Oliver Date: Thu, 8 May 2014 16:21:05 -0700 Subject: [PATCH] selftest: return null when reading a file that doesn't exist --- tools/selftest.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/selftest.js b/tools/selftest.js index 494fe53ea2..0d84ac73e8 100644 --- a/tools/selftest.js +++ b/tools/selftest.js @@ -464,13 +464,16 @@ _.extend(Sandbox.prototype, { fs.writeFileSync(path.join(self.cwd, filename), contents, 'utf8'); }, - // Reads a file in the sandbox as a utf8 string. 'filename' is a path - // intepreted relative to the Sandbox's cwd. throws if the file does not - // exist. - // XXX maybe it should return null if the file does not exist? + // Reads a file in the sandbox as a utf8 string. 'filename' is a + // path intepreted relative to the Sandbox's cwd. Returns null if + // file does not exist. read: function (filename) { var self = this; - return fs.readFileSync(path.join(self.cwd, filename), 'utf8'); + var file = path.join(self.cwd, filename); + if (!fs.existsSync(file)) + return null; + else + return fs.readFileSync(path.join(self.cwd, filename), 'utf8'); }, // Delete a file in the sandbox. 'filename' is as in write().