2011-04-02 08:31:20 +00:00
|
|
|
/*
|
|
|
|
* file-store-test.js: Tests for the nconf File store.
|
|
|
|
*
|
2014-11-26 06:31:48 +00:00
|
|
|
* (C) 2011, Charlie Robbins and the Contributors.
|
2011-04-02 08:31:20 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
vows = require('vows'),
|
|
|
|
assert = require('assert'),
|
2017-10-21 20:01:01 +00:00
|
|
|
nconf = require('../lib/nconf')
|
2011-04-02 08:31:20 +00:00
|
|
|
|
|
|
|
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);
|
2017-10-21 20:42:24 +00:00
|
|
|
assert.isFunction(nconf.any);
|
2011-04-02 08:31:20 +00:00
|
|
|
assert.isFunction(nconf.get);
|
|
|
|
assert.isFunction(nconf.set);
|
|
|
|
assert.isFunction(nconf.clear);
|
|
|
|
assert.isFunction(nconf.load);
|
|
|
|
assert.isFunction(nconf.save);
|
|
|
|
assert.isFunction(nconf.reset);
|
2015-10-15 08:16:33 +00:00
|
|
|
assert.isFunction(nconf.required);
|
2011-04-02 08:31:20 +00:00
|
|
|
},
|
|
|
|
"the use() method": {
|
|
|
|
"should instaniate the correct store": function () {
|
2011-04-02 08:41:50 +00:00
|
|
|
nconf.use('memory');
|
2012-01-02 22:20:06 +00:00
|
|
|
nconf.load();
|
2011-11-23 02:22:09 +00:00
|
|
|
assert.instanceOf(nconf.stores['memory'], nconf.Memory);
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
2011-06-05 05:39:39 +00:00
|
|
|
},
|
|
|
|
"it should": {
|
|
|
|
topic: function () {
|
|
|
|
fs.readFile(path.join(__dirname, '..', 'package.json'), this.callback);
|
|
|
|
},
|
|
|
|
"have the correct version set": function (err, data) {
|
|
|
|
assert.isNull(err);
|
|
|
|
data = JSON.parse(data.toString());
|
|
|
|
assert.equal(nconf.version, data.version);
|
|
|
|
}
|
2015-10-15 08:16:33 +00:00
|
|
|
},
|
|
|
|
"the required() method": {
|
|
|
|
"should throw error with missing keys": function() {
|
|
|
|
nconf.set('foo:bar:bazz', 'buzz');
|
2015-10-15 08:44:26 +00:00
|
|
|
assert.throws(nconf.required.bind(nconf, ['missing', 'foo:bar:bazz']), Error);
|
2015-10-15 08:16:33 +00:00
|
|
|
},
|
2017-11-04 04:34:27 +00:00
|
|
|
"should return the provider if all required keys exist": function() {
|
|
|
|
var Provider = nconf.Provider;
|
2015-10-15 08:16:33 +00:00
|
|
|
nconf.set('foo:bar:bazz', 'buzz');
|
2017-11-04 04:34:27 +00:00
|
|
|
assert.isTrue(nconf.required(['foo:bar:bazz']) instanceof Provider);
|
2015-10-15 08:16:33 +00:00
|
|
|
}
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-02 08:41:50 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using the nconf": {
|
|
|
|
"with the memory store": {
|
|
|
|
"the set() method": {
|
|
|
|
"should respond with true": function () {
|
|
|
|
assert.isTrue(nconf.set('foo:bar:bazz', 'buzz'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"the get() method": {
|
2011-09-19 01:37:01 +00:00
|
|
|
"without a callback": {
|
|
|
|
"should respond with the correct value": function () {
|
|
|
|
assert.equal(nconf.get('foo:bar:bazz'), 'buzz');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"with a callback": {
|
|
|
|
topic: function () {
|
|
|
|
nconf.get('foo:bar:bazz', this.callback);
|
|
|
|
},
|
|
|
|
"should respond with the correct value": function (err, value) {
|
|
|
|
assert.equal(value, 'buzz');
|
|
|
|
}
|
2011-04-02 08:41:50 +00:00
|
|
|
}
|
2011-09-19 01:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 14:29:30 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using the nconf": {
|
|
|
|
"with the memory store": {
|
|
|
|
"the get() method": {
|
|
|
|
"should respond allow access to the root": function () {
|
|
|
|
assert(nconf.get(null));
|
|
|
|
assert(nconf.get(undefined));
|
|
|
|
assert(nconf.get());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"the set() method": {
|
|
|
|
"should respond allow access to the root and complain about non-objects": function () {
|
|
|
|
assert(!nconf.set(null, null));
|
|
|
|
assert(!nconf.set(null, undefined));
|
|
|
|
assert(!nconf.set(null));
|
|
|
|
assert(!nconf.set(null, ''));
|
|
|
|
assert(!nconf.set(null, 1));
|
|
|
|
var original = nconf.get();
|
|
|
|
assert(nconf.set(null, nconf.get()));
|
|
|
|
assert.notEqual(nconf.get(), original);
|
|
|
|
assert.deepEqual(nconf.get(), original)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-19 01:37:01 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using nconf": {
|
|
|
|
"with the memory store": {
|
2011-04-02 08:41:50 +00:00
|
|
|
"the clear() method": {
|
|
|
|
"should respond with the true": function () {
|
|
|
|
assert.equal(nconf.get('foo:bar:bazz'), 'buzz');
|
|
|
|
assert.isTrue(nconf.clear('foo:bar:bazz'));
|
|
|
|
assert.isTrue(typeof nconf.get('foo:bar:bazz') === 'undefined');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"the load() method": {
|
|
|
|
"without a callback": {
|
2012-01-02 22:20:06 +00:00
|
|
|
"should respond with the merged store": function () {
|
2012-01-02 22:27:09 +00:00
|
|
|
assert.deepEqual(nconf.load(), {
|
2017-11-04 04:34:27 +00:00
|
|
|
title: 'My specific title',
|
2014-11-26 17:49:50 +00:00
|
|
|
color: 'green',
|
2017-11-04 04:34:27 +00:00
|
|
|
movie: 'Kill Bill'
|
2012-01-02 22:27:09 +00:00
|
|
|
});
|
2011-04-02 08:41:50 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"with a callback": {
|
|
|
|
topic: function () {
|
2017-11-04 04:34:27 +00:00
|
|
|
nconf.load(this.callback.bind(null, null));
|
2011-04-02 08:41:50 +00:00
|
|
|
},
|
2012-01-02 22:20:06 +00:00
|
|
|
"should respond with the merged store": function (ign, err, store) {
|
|
|
|
assert.isNull(err);
|
2012-01-02 22:27:09 +00:00
|
|
|
assert.deepEqual(store, {
|
2017-11-04 04:34:27 +00:00
|
|
|
title: 'My specific title',
|
2014-11-26 17:49:50 +00:00
|
|
|
color: 'green',
|
2017-11-04 04:34:27 +00:00
|
|
|
movie: 'Kill Bill'
|
2012-01-02 22:27:09 +00:00
|
|
|
});
|
2011-04-02 08:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-02 08:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 02:31:53 +00:00
|
|
|
}).export(module);
|