From 76db2547406c01d65ec804de33d561859486661d Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 8 Jun 2011 00:06:58 -0400 Subject: [PATCH] [fix test] Update nconf.stores.File to respond with an error when loading malformed JSON async --- .gitignore | 1 + lib/nconf/stores/file.js | 11 ++++++++--- test/file-store-test.js | 15 +++++++++++++++ test/fixtures/malformed.json | 3 +++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/malformed.json diff --git a/.gitignore b/.gitignore index 844977e..1a52174 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store config.json test/fixtures/*.json +!test/fixtures/malformed.json node_modules/ node_modules/* npm-debug.log \ No newline at end of file diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 968d4e8..45e2bdd 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -95,9 +95,14 @@ File.prototype.load = function (callback) { return callback(err); } - data = self.format.parse(data.toString()); - self.store = data; - callback(null, self.store); + try { + self.store = self.format.parse(data.toString()); + callback(null, self.store); + } + catch (ex) { + self.store = {}; + callback(ex); + } }); }; diff --git a/test/file-store-test.js b/test/file-store-test.js index c66301f..918adbb 100644 --- a/test/file-store-test.js +++ b/test/file-store-test.js @@ -30,6 +30,21 @@ vows.describe('nconf/stores/file').addBatch({ assert.deepEqual(data, store.store); } } + }, + "When using the nconf file store": { + topic: function () { + var filePath = path.join(__dirname, 'fixtures', 'malformed.json'); + store = new nconf.stores.File({ file: filePath }); + return null; + }, + "the load() method with a malformed JSON config file": { + topic: function () { + store.load(this.callback.bind(null, null)); + }, + "should respond with an error": function (ign, err) { + console.dir(err); + } + } } }).addBatch({ "When using the nconf file store": { diff --git a/test/fixtures/malformed.json b/test/fixtures/malformed.json new file mode 100644 index 0000000..fffa155 --- /dev/null +++ b/test/fixtures/malformed.json @@ -0,0 +1,3 @@ +{ + "literal": "bazz", +} \ No newline at end of file