2011-11-23 20:46:02 +00:00
|
|
|
/*
|
|
|
|
* hierarchy-test.js: Basic tests for hierarchical file stores.
|
|
|
|
*
|
2011-11-24 05:33:08 +00:00
|
|
|
* (C) 2011, Nodejitsu Inc.
|
2011-11-23 20:46:02 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2011-11-24 02:48:33 +00:00
|
|
|
fs = require('fs'),
|
2011-11-23 20:46:02 +00:00
|
|
|
path = require('path'),
|
2011-11-24 02:48:33 +00:00
|
|
|
spawn = require('child_process').spawn,
|
2011-11-23 20:46:02 +00:00
|
|
|
vows = require('vows'),
|
|
|
|
nconf = require('../lib/nconf');
|
|
|
|
|
|
|
|
var configDir = path.join(__dirname, 'fixtures', 'hierarchy'),
|
|
|
|
globalConfig = path.join(configDir, 'global.json'),
|
|
|
|
userConfig = path.join(configDir, 'user.json');
|
|
|
|
|
|
|
|
vows.describe('nconf/hierarchy').addBatch({
|
|
|
|
"When using nconf": {
|
|
|
|
"configured with two file stores": {
|
|
|
|
topic: function () {
|
|
|
|
nconf.add('user', { type: 'file', file: userConfig })
|
|
|
|
nconf.add('global', { type: 'file', file: globalConfig })
|
|
|
|
nconf.load();
|
|
|
|
return nconf;
|
|
|
|
},
|
|
|
|
"should have the appropriate keys present": function () {
|
|
|
|
assert.equal(nconf.get('title'), 'My specific title');
|
|
|
|
assert.equal(nconf.get('color'), 'green');
|
|
|
|
assert.equal(nconf.get('movie'), 'Kill Bill');
|
|
|
|
}
|
2011-11-24 02:48:33 +00:00
|
|
|
},
|
|
|
|
"configured with .argv(), .env() and .file()": {
|
|
|
|
topic: function () {
|
|
|
|
var configFile = path.join(__dirname, 'fixtures', 'load-save.json'),
|
|
|
|
script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-save.js'),
|
|
|
|
argv = ['--foo', 'foo', '--bar', 'bar'],
|
|
|
|
that = this,
|
|
|
|
data = '',
|
|
|
|
child;
|
|
|
|
|
|
|
|
try { fs.unlinkSync(configFile) }
|
|
|
|
catch (ex) { }
|
|
|
|
|
|
|
|
child = spawn('node', [script].concat(argv));
|
|
|
|
|
|
|
|
child.stdout.on('data', function (d) {
|
|
|
|
data += d;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on('exit', function () {
|
|
|
|
fs.readFile(configFile, 'utf8', that.callback.bind(null, null, data));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"should not persist information passed in to process.env and process.argv to disk ": function (_, data, _, ondisk){
|
|
|
|
assert.equal(data, 'foo');
|
|
|
|
assert.deepEqual(JSON.parse(ondisk), {
|
|
|
|
database: {
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: 5984
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-11-23 20:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).export(module);
|