2011-06-25 04:34:07 +00:00
|
|
|
/*
|
2012-07-10 07:14:00 +00:00
|
|
|
* provider-test.js: Tests for the nconf Provider object.
|
2011-06-25 04:34:07 +00:00
|
|
|
*
|
2014-11-26 06:31:48 +00:00
|
|
|
* (C) 2011, Charlie Robbins and the Contributors.
|
2011-06-25 04:34:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-23 10:38:51 +00:00
|
|
|
var assert = require('assert'),
|
|
|
|
fs = require('fs'),
|
2011-06-25 04:34:07 +00:00
|
|
|
path = require('path'),
|
2011-08-23 10:38:51 +00:00
|
|
|
spawn = require('child_process').spawn,
|
2011-06-25 04:34:07 +00:00
|
|
|
vows = require('vows'),
|
2011-09-16 10:49:47 +00:00
|
|
|
helpers = require('./helpers'),
|
2012-01-02 22:14:17 +00:00
|
|
|
nconf = require('../lib/nconf');
|
2014-11-26 06:28:31 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
var fixturesDir = path.join(__dirname, 'fixtures'),
|
|
|
|
mergeFixtures = path.join(fixturesDir, 'merge'),
|
2011-08-28 12:50:26 +00:00
|
|
|
files = [path.join(mergeFixtures, 'file1.json'), path.join(mergeFixtures, 'file2.json')],
|
2011-09-19 01:37:01 +00:00
|
|
|
override = JSON.parse(fs.readFileSync(files[0]), 'utf8');
|
2011-06-25 04:34:07 +00:00
|
|
|
|
2012-07-10 05:50:18 +00:00
|
|
|
function assertProvider(test) {
|
|
|
|
return {
|
|
|
|
topic: new nconf.Provider(),
|
|
|
|
"should use the correct File store": test
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-06-25 04:34:07 +00:00
|
|
|
vows.describe('nconf/provider').addBatch({
|
2011-08-23 10:38:51 +00:00
|
|
|
"When using nconf": {
|
|
|
|
"an instance of 'nconf.Provider'": {
|
|
|
|
"calling the use() method with the same store type and different options": {
|
2011-09-19 01:37:01 +00:00
|
|
|
topic: new nconf.Provider().use('file', { file: files[0] }),
|
2011-08-23 10:38:51 +00:00
|
|
|
"should use a new instance of the store type": function (provider) {
|
2011-11-23 02:22:09 +00:00
|
|
|
var old = provider.stores['file'];
|
2011-06-25 04:34:07 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
assert.equal(provider.stores.file.file, files[0]);
|
2011-09-19 01:37:01 +00:00
|
|
|
provider.use('file', { file: files[1] });
|
2011-06-25 04:34:07 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
assert.notStrictEqual(old, provider.stores.file);
|
|
|
|
assert.equal(provider.stores.file.file, files[1]);
|
2011-08-23 10:38:51 +00:00
|
|
|
}
|
|
|
|
},
|
2011-09-19 01:37:01 +00:00
|
|
|
"when 'argv' is true": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'provider-argv.js'),
|
|
|
|
argv: ['--something', 'foobar']
|
|
|
|
}),
|
|
|
|
"when 'env' is true": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'provider-env.js'),
|
|
|
|
env: { SOMETHING: 'foobar' }
|
2011-11-20 21:21:09 +00:00
|
|
|
})
|
2011-08-23 10:38:51 +00:00
|
|
|
},
|
2011-09-19 01:37:01 +00:00
|
|
|
"the default nconf provider": {
|
|
|
|
"when 'argv' is set to true": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'nconf-argv.js'),
|
|
|
|
argv: ['--something', 'foobar'],
|
|
|
|
env: { SOMETHING: true }
|
|
|
|
}),
|
|
|
|
"when 'env' is set to true": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'nconf-env.js'),
|
|
|
|
env: { SOMETHING: 'foobar' }
|
2011-11-19 01:02:22 +00:00
|
|
|
}),
|
|
|
|
"when 'argv' is set to true and process.argv is modified": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'nconf-change-argv.js'),
|
|
|
|
argv: ['--something', 'badValue', 'evenWorse', 'OHNOEZ', 'foobar']
|
2011-10-23 08:30:51 +00:00
|
|
|
}),
|
|
|
|
"when hierarchical 'argv' get": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'nconf-hierarchical-file-argv.js'),
|
|
|
|
argv: ['--something', 'foobar'],
|
|
|
|
env: { SOMETHING: true }
|
2012-06-21 07:04:06 +00:00
|
|
|
}),
|
|
|
|
"when 'env' is set to true with a nested separator": helpers.assertSystemConf({
|
|
|
|
script: path.join(fixturesDir, 'scripts', 'nconf-nested-env.js'),
|
|
|
|
env: { SOME_THING: 'foobar' }
|
2011-09-19 01:37:01 +00:00
|
|
|
})
|
|
|
|
}
|
2011-06-25 04:34:07 +00:00
|
|
|
}
|
2011-09-19 01:37:01 +00:00
|
|
|
}).addBatch({
|
2011-08-28 12:50:26 +00:00
|
|
|
"When using nconf": {
|
|
|
|
"an instance of 'nconf.Provider'": {
|
|
|
|
"the merge() method": {
|
|
|
|
topic: new nconf.Provider().use('file', { file: files[1] }),
|
|
|
|
"should have the result merged in": function (provider) {
|
|
|
|
provider.load();
|
|
|
|
provider.merge(override);
|
2011-11-23 02:22:09 +00:00
|
|
|
helpers.assertMerged(null, provider.stores.file.store);
|
2011-12-20 01:35:20 +00:00
|
|
|
assert.equal(provider.stores.file.store.candy.something, 'file1');
|
2012-12-21 03:02:36 +00:00
|
|
|
},
|
|
|
|
"should merge Objects over null": function (provider) {
|
|
|
|
provider.load();
|
|
|
|
provider.merge(override);
|
|
|
|
assert.equal(provider.stores.file.store.unicorn.exists, true);
|
2011-09-25 04:01:44 +00:00
|
|
|
}
|
2011-12-20 01:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).addBatch({
|
|
|
|
"When using nconf": {
|
|
|
|
"an instance of 'nconf.Provider'": {
|
|
|
|
"the load() method": {
|
|
|
|
"when sources are passed in": {
|
|
|
|
topic: new nconf.Provider({
|
|
|
|
sources: {
|
|
|
|
user: {
|
|
|
|
type: 'file',
|
|
|
|
file: files[0]
|
|
|
|
},
|
|
|
|
global: {
|
|
|
|
type: 'file',
|
|
|
|
file: files[1]
|
|
|
|
}
|
2011-11-23 21:41:07 +00:00
|
|
|
}
|
2011-12-20 01:35:20 +00:00
|
|
|
}),
|
|
|
|
"should respect the hierarchy ": function (provider) {
|
|
|
|
var merged = provider.load();
|
|
|
|
|
|
|
|
helpers.assertMerged(null, merged);
|
|
|
|
assert.equal(merged.candy.something, 'file1');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"when multiple stores are used": {
|
|
|
|
topic: new nconf.Provider().overrides({foo: {bar: 'baz'}})
|
|
|
|
.add('file1', {type: 'file', file: files[0]})
|
|
|
|
.add('file2', {type: 'file', file: files[1]}),
|
|
|
|
"should respect the hierarchy": function(provider) {
|
|
|
|
var merged = provider.load();
|
2014-11-26 06:28:31 +00:00
|
|
|
|
2011-12-20 01:35:20 +00:00
|
|
|
helpers.assertMerged(null, merged);
|
|
|
|
assert.equal(merged.foo.bar, 'baz');
|
|
|
|
assert.equal(merged.candy.something, 'file1');
|
2011-11-23 21:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-28 12:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 05:50:18 +00:00
|
|
|
}).addBatch({
|
|
|
|
"When using nconf": {
|
|
|
|
"an instance of 'nconf.Provider'": {
|
|
|
|
"the .file() method": {
|
|
|
|
"with a single filepath": assertProvider(function (provider) {
|
|
|
|
provider.file(helpers.fixture('store.json'));
|
|
|
|
assert.isObject(provider.stores.file);
|
|
|
|
}),
|
|
|
|
"with a name and a filepath": assertProvider(function (provider) {
|
|
|
|
provider.file('custom', helpers.fixture('store.json'));
|
|
|
|
assert.isObject(provider.stores.custom);
|
|
|
|
}),
|
|
|
|
"with a single object": assertProvider(function (provider) {
|
|
|
|
provider.file({
|
2012-09-07 14:40:15 +00:00
|
|
|
dir: helpers.fixture(''),
|
2012-07-10 05:50:18 +00:00
|
|
|
file: 'store.json',
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isObject(provider.stores.file);
|
|
|
|
assert.equal(provider.stores.file.file, helpers.fixture('store.json'));
|
|
|
|
}),
|
|
|
|
"with a name and an object": assertProvider(function (provider) {
|
|
|
|
provider.file('custom', {
|
2012-09-07 14:40:15 +00:00
|
|
|
dir: helpers.fixture(''),
|
2012-07-10 05:50:18 +00:00
|
|
|
file: 'store.json',
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isObject(provider.stores.custom);
|
|
|
|
assert.equal(provider.stores.custom.file, helpers.fixture('store.json'));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 02:31:53 +00:00
|
|
|
}).export(module);
|