Skip to content

Commit

Permalink
[fix test] Update nconf.stores.File to respond with an error when loa…
Browse files Browse the repository at this point in the history
…ding malformed JSON async
  • Loading branch information
indexzero committed Jun 8, 2011
1 parent d7495f8 commit 76db254
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
.DS_Store
config.json
test/fixtures/*.json
!test/fixtures/malformed.json
node_modules/
node_modules/*
npm-debug.log
11 changes: 8 additions & 3 deletions lib/nconf/stores/file.js
Expand Up @@ -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);
}
});
};

Expand Down
15 changes: 15 additions & 0 deletions test/file-store-test.js
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/malformed.json
@@ -0,0 +1,3 @@
{
"literal": "bazz",
}

0 comments on commit 76db254

Please sign in to comment.