[minor test] Add tests for File store `save()`. Improve default file format to pretty print the JSON output

master
indexzero 2011-04-19 17:32:13 -04:00
parent 96859f913c
commit 067d58a99d
3 changed files with 35 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
.DS_Store
config.json
test/fixtures/store.json
test/fixtures/*.json

View File

@ -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

View File

@ -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": {