2011-04-02 08:31:20 +00:00
|
|
|
/*
|
|
|
|
* file-store-test.js: Tests for the nconf File store.
|
|
|
|
*
|
|
|
|
* (C) 2011, Charlie Robbins
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
vows = require('vows'),
|
|
|
|
assert = require('assert'),
|
2011-09-19 01:37:01 +00:00
|
|
|
nconf = require('../../lib/nconf'),
|
2011-11-24 02:37:50 +00:00
|
|
|
data = require('../fixtures/data').data,
|
2011-04-02 08:31:20 +00:00
|
|
|
store;
|
|
|
|
|
|
|
|
vows.describe('nconf/stores/file').addBatch({
|
|
|
|
"When using the nconf file store": {
|
2011-11-24 02:37:50 +00:00
|
|
|
"with a valid JSON file": {
|
2011-04-02 08:31:20 +00:00
|
|
|
topic: function () {
|
2011-11-24 02:37:50 +00:00
|
|
|
var filePath = path.join(__dirname, '..', 'fixtures', 'store.json');
|
|
|
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
|
|
this.store = store = new nconf.File({ file: filePath });
|
|
|
|
return null;
|
2011-04-02 08:31:20 +00:00
|
|
|
},
|
2011-11-24 02:37:50 +00:00
|
|
|
"the load() method": {
|
|
|
|
topic: function () {
|
|
|
|
this.store.load(this.callback);
|
|
|
|
},
|
|
|
|
"should load the data correctly": function (err, data) {
|
|
|
|
assert.isNull(err);
|
|
|
|
assert.deepEqual(data, this.store.store);
|
|
|
|
}
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
2011-06-08 04:06:58 +00:00
|
|
|
},
|
2011-11-24 02:37:50 +00:00
|
|
|
"with a malformed JSON file": {
|
2011-06-08 04:06:58 +00:00
|
|
|
topic: function () {
|
2011-11-24 02:37:50 +00:00
|
|
|
var filePath = path.join(__dirname, '..', 'fixtures', 'malformed.json');
|
|
|
|
this.store = new nconf.File({ file: filePath });
|
|
|
|
return null;
|
2011-06-08 04:06:58 +00:00
|
|
|
},
|
2011-11-24 02:37:50 +00:00
|
|
|
"the load() method with a malformed JSON config file": {
|
|
|
|
topic: function () {
|
|
|
|
this.store.load(this.callback.bind(null, null));
|
|
|
|
},
|
|
|
|
"should respond with an error": function (_, err) {
|
|
|
|
assert.isTrue(!!err);
|
|
|
|
}
|
2011-06-08 04:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
2011-04-19 21:32:13 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using the nconf file store": {
|
|
|
|
topic: function () {
|
2011-09-19 01:37:01 +00:00
|
|
|
var tmpPath = path.join(__dirname, '..', 'fixtures', 'tmp.json'),
|
2011-11-23 02:22:09 +00:00
|
|
|
tmpStore = new nconf.File({ file: tmpPath });
|
2011-04-20 05:57:56 +00:00
|
|
|
return tmpStore;
|
2011-04-19 21:32:13 +00:00
|
|
|
},
|
|
|
|
"the save() method": {
|
|
|
|
topic: function (tmpStore) {
|
|
|
|
var that = this;
|
|
|
|
|
2011-11-24 02:37:50 +00:00
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
tmpStore.set(key, data[key]);
|
|
|
|
});
|
2011-04-19 21:32:13 +00:00
|
|
|
|
|
|
|
tmpStore.save(function () {
|
2011-11-24 02:37:50 +00:00
|
|
|
fs.readFile(tmpStore.file, function (err, d) {
|
|
|
|
return err ? that.callback(err) : that.callback(err, JSON.parse(d.toString()));
|
2011-04-19 21:32:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2011-11-24 02:37:50 +00:00
|
|
|
"should save the data correctly": function (err, read) {
|
2011-04-19 21:32:13 +00:00
|
|
|
assert.isNull(err);
|
2011-11-24 02:37:50 +00:00
|
|
|
assert.deepEqual(read, data);
|
2011-04-19 21:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 08:31:20 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using the nconf file store": {
|
|
|
|
"the set() method": {
|
|
|
|
"should respond with true": function () {
|
|
|
|
assert.isTrue(store.set('foo:bar:bazz', 'buzz'));
|
2011-05-22 19:30:19 +00:00
|
|
|
assert.isTrue(store.set('falsy:number', 0));
|
|
|
|
assert.isTrue(store.set('falsy:string', ''));
|
|
|
|
assert.isTrue(store.set('falsy:boolean', false));
|
|
|
|
assert.isTrue(store.set('falsy:object', null));
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"the get() method": {
|
|
|
|
"should respond with the correct value": function () {
|
|
|
|
assert.equal(store.get('foo:bar:bazz'), 'buzz');
|
2011-05-22 19:30:19 +00:00
|
|
|
assert.equal(store.get('falsy:number'), 0);
|
|
|
|
assert.equal(store.get('falsy:string'), '');
|
|
|
|
assert.equal(store.get('falsy:boolean'), false);
|
|
|
|
assert.equal(store.get('falsy:object'), null);
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"the clear() method": {
|
|
|
|
"should respond with the true": function () {
|
|
|
|
assert.equal(store.get('foo:bar:bazz'), 'buzz');
|
|
|
|
assert.isTrue(store.clear('foo:bar:bazz'));
|
|
|
|
assert.isTrue(typeof store.get('foo:bar:bazz') === 'undefined');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-23 10:38:51 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using the nconf file store": {
|
|
|
|
"the search() method": {
|
|
|
|
"when the target file exists higher in the directory tree": {
|
|
|
|
topic: function () {
|
|
|
|
var filePath = this.filePath = path.join(process.env.HOME, '.nconf');
|
|
|
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
2011-11-23 02:22:09 +00:00
|
|
|
return new (nconf.File)({
|
2011-08-23 10:38:51 +00:00
|
|
|
file: '.nconf'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
"should update the file appropriately": function (store) {
|
|
|
|
store.search();
|
|
|
|
assert.equal(store.file, this.filePath);
|
|
|
|
fs.unlinkSync(this.filePath);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"when the target file doesn't exist higher in the directory tree": {
|
|
|
|
topic: function () {
|
2011-09-19 01:37:01 +00:00
|
|
|
var filePath = this.filePath = path.join(__dirname, '..', 'fixtures', 'search-store.json');
|
2011-11-23 02:22:09 +00:00
|
|
|
return new (nconf.File)({
|
2011-08-23 10:38:51 +00:00
|
|
|
dir: path.dirname(filePath),
|
|
|
|
file: 'search-store.json'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
"should update the file appropriately": function (store) {
|
|
|
|
store.search();
|
|
|
|
assert.equal(store.file, this.filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 08:31:20 +00:00
|
|
|
}).export(module);
|