diff --git a/.gitignore b/.gitignore index 008c66b..d6d4ffe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .DS_Store config.json -test/fixtures/store.json \ No newline at end of file +test/fixtures/*.json \ No newline at end of file diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 5e2df2f..28f5d9f 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -24,7 +24,12 @@ var File = exports.File = function (options) { this.type = 'file'; this.file = options.file; - this.format = options.format || JSON; + this.format = options.format || { + stringify: function (obj) { + return JSON.stringify(obj, null, 2) + }, + parse: JSON.parse + }; }; // Inherit from the Memory store diff --git a/test/file-store-test.js b/test/file-store-test.js index 8881cd9..2d2eacb 100644 --- a/test/file-store-test.js +++ b/test/file-store-test.js @@ -19,7 +19,7 @@ vows.describe('nconf/stores/file').addBatch({ "When using the nconf file store": { topic: function () { var filePath = path.join(__dirname, 'fixtures', 'store.json'); - fs.writeFileSync(filePath, JSON.stringify(data)); + fs.writeFileSync(filePath, JSON.stringify(data, null, 2)); store = new nconf.stores.File({ file: filePath }); return null; }, @@ -33,6 +33,33 @@ vows.describe('nconf/stores/file').addBatch({ } } } +}).addBatch({ + "When using the nconf file store": { + topic: function () { + var tmpPath = path.join(__dirname, 'fixtures', 'tmp.json'), + tmpStore = new nconf.stores.File({ file: tmpPath }); + return tmpStore + }, + "the save() method": { + topic: function (tmpStore) { + var that = this; + + Object.keys(store.store).forEach(function (key) { + tmpStore.set(key, store.store[key]); + }); + + tmpStore.save(function () { + fs.readFile(tmpStore.file, function (err, data) { + return err ? that.callback(err) : that.callback(err, JSON.parse(data.toString())); + }); + }); + }, + "should save the data correctly": function (err, data) { + assert.isNull(err); + assert.deepEqual(data, store.store); + } + } + } }).addBatch({ "When using the nconf file store": { "the set() method": {