diff --git a/lib/nconf.js b/lib/nconf.js index da3e02c..33d1f83 100644 --- a/lib/nconf.js +++ b/lib/nconf.js @@ -19,7 +19,7 @@ nconf.stores = require('nconf/stores'); // specified `type`. // nconf.use = function (type, options) { - nconf.store = new nconf.stores[type](options); + nconf.store = new nconf.stores.create(type, options); }; // diff --git a/lib/nconf/stores.js b/lib/nconf/stores.js index 6fd277c..2859780 100644 --- a/lib/nconf/stores.js +++ b/lib/nconf/stores.js @@ -7,6 +7,21 @@ var stores = exports; +function capitalize (str) { + return str && str[0].toUpperCase() + str.slice(1); +}; + stores.Memory = require('nconf/stores/memory').Memory; stores.File = require('nconf/stores/file').File; -stores.Redis = require('nconf/stores/redis').Redis; \ No newline at end of file +stores.Redis = require('nconf/stores/redis').Redis; + +// +// ### function create (type, options) +// #### @type {string} Type of the nconf store to use. +// #### @options {Object} Options for the store instance. +// Creates a store of the specified `type` using the +// specified `options`. +// +stores.create = function (type, options) { + return new stores[capitalize(type.toLowerCase())](options); +}; \ No newline at end of file diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 4ab09dc..9d6a629 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -20,7 +20,7 @@ var File = exports.File = function (options) { throw new Error ('Missing required option `files`'); } - nconf.stores.Memory.call(this, options); + Memory.call(this, options); this.file = options.file; this.format = options.format || JSON; @@ -42,7 +42,7 @@ File.prototype.save = function (value, callback) { value = null; } - fs.save(this.file, this.format.stringify(this.store), function (err) { + fs.writeFile(this.file, this.format.stringify(this.store), function (err) { return err ? callback(err) : callback(); }); }; @@ -54,12 +54,13 @@ File.prototype.save = function (value, callback) { // File.prototype.load = function (callback) { var self = this; - fs.load(this.file, function (err, data) { + fs.readFile(this.file, function (err, data) { if (err) { return callback(err); } - self.store = self.format.parse(data.toString()); + data = self.format.parse(data.toString()); + self.store = data; callback(null, self.store); }); }; \ No newline at end of file diff --git a/test/file-store-test.js b/test/file-store-test.js new file mode 100644 index 0000000..8881cd9 --- /dev/null +++ b/test/file-store-test.js @@ -0,0 +1,56 @@ +/* + * file-store-test.js: Tests for the nconf File store. + * + * (C) 2011, Charlie Robbins + * + */ + +require.paths.unshift(require('path').join(__dirname, '..', 'lib')); + +var fs = require('fs'), + path = require('path'), + vows = require('vows'), + assert = require('assert'), + nconf = require('nconf'), + data = require('./fixtures/data').data, + store; + +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)); + store = new nconf.stores.File({ file: filePath }); + return null; + }, + "the load() method": { + topic: function () { + store.load(this.callback); + }, + "should load the data correctly": function (err, data) { + assert.isNull(err); + assert.deepEqual(data, store.store); + } + } + } +}).addBatch({ + "When using the nconf file store": { + "the set() method": { + "should respond with true": function () { + assert.isTrue(store.set('foo:bar:bazz', 'buzz')); + } + }, + "the get() method": { + "should respond with the correct value": function () { + assert.equal(store.get('foo:bar:bazz'), 'buzz'); + } + }, + "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'); + } + } + } +}).export(module); \ No newline at end of file diff --git a/test/fixtures/data.js b/test/fixtures/data.js new file mode 100644 index 0000000..29a1c24 --- /dev/null +++ b/test/fixtures/data.js @@ -0,0 +1,20 @@ +/* + * data.js: Simple data fixture for configuration test. + * + * (C) 2011, Charlie Robbins + * + */ + +exports.data = { + literal: 'bazz', + arr: ['one', 2, true, { value: 'foo' }], + obj: { + host: 'localhost', + port: 5984, + array: ['one', 2, true, { foo: 'bar' }], + auth: { + username: 'admin', + password: 'password' + } + } +} \ No newline at end of file diff --git a/test/fixtures/store.json b/test/fixtures/store.json new file mode 100644 index 0000000..1bea533 --- /dev/null +++ b/test/fixtures/store.json @@ -0,0 +1 @@ +{"literal":"bazz","arr":["one",2,true,{"value":"foo"}],"obj":{"host":"localhost","port":5984,"array":["one",2,true,{"foo":"bar"}],"auth":{"username":"admin","password":"password"}}} \ No newline at end of file diff --git a/test/memory-store-test.js b/test/memory-store-test.js index 11b0bf0..199ca3b 100644 --- a/test/memory-store-test.js +++ b/test/memory-store-test.js @@ -1,5 +1,5 @@ /* - * memory.js: Simple memory storage engine for nconf configuration(s) + * memory-store-test.js: Tests for the nconf Memory store. * * (C) 2011, Charlie Robbins * diff --git a/test/nconf-test.js b/test/nconf-test.js new file mode 100644 index 0000000..fe68590 --- /dev/null +++ b/test/nconf-test.js @@ -0,0 +1,57 @@ +/* + * file-store-test.js: Tests for the nconf File store. + * + * (C) 2011, Charlie Robbins + * + */ + +require.paths.unshift(require('path').join(__dirname, '..', 'lib')); + +var fs = require('fs'), + path = require('path'), + vows = require('vows'), + assert = require('assert'), + nconf = require('nconf'), + data = require('./fixtures/data').data; + +vows.describe('nconf').addBatch({ + "When using the nconf": { + "should have the correct methods set": function () { + assert.isFunction(nconf.key); + assert.isFunction(nconf.path); + assert.isFunction(nconf.use); + assert.isFunction(nconf.get); + assert.isFunction(nconf.set); + assert.isFunction(nconf.clear); + assert.isFunction(nconf.load); + assert.isFunction(nconf.save); + assert.isFunction(nconf.reset); + }, + "the use() method": { + "should instaniate the correct store": function () { + nconf.use('redis'); + assert.instanceOf(nconf.store, nconf.stores.Redis); + } + } + } +})/*.addBatch({ + "When using the nconf file store": { + "the set() method": { + "should respond with true": function () { + assert.isTrue(store.set('foo:bar:bazz', 'buzz')); + } + }, + "the get() method": { + "should respond with the correct value": function () { + assert.equal(store.get('foo:bar:bazz'), 'buzz'); + } + }, + "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'); + } + } + } +})*/.export(module); \ No newline at end of file diff --git a/test/redis-store-test.js b/test/redis-store-test.js index 60a0b4b..1b059d8 100644 --- a/test/redis-store-test.js +++ b/test/redis-store-test.js @@ -1,5 +1,5 @@ /* - * redis-test.js: Tests for the redis nconf storage engine. + * redis-store-test.js: Tests for the redis nconf storage engine. * * (C) 2011, Charlie Robbins * @@ -9,21 +9,8 @@ require.paths.unshift(require('path').join(__dirname, '..', 'lib')); var vows = require('vows'), assert = require('assert'), - nconf = require('nconf'); - -var data = { - literal: 'bazz', - arr: ['one', 2, true, { value: 'foo' }], - obj: { - host: 'localhost', - port: 5984, - array: ['one', 2, true, { foo: 'bar' }], - auth: { - username: 'admin', - password: 'password' - } - } -} + nconf = require('nconf'), + data = require('./fixtures/data').data; vows.describe('nconf/stores/redis').addBatch({ "When using the nconf redis store": {