[test] Add additional test coverage for hierarchical configuration
This commit is contained in:
parent
a9c354032b
commit
b658f68a89
2 changed files with 66 additions and 0 deletions
32
test/fixtures/scripts/nconf-hierarchical-load-save.js
vendored
Normal file
32
test/fixtures/scripts/nconf-hierarchical-load-save.js
vendored
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* nconf-hierarchical-load-save.js: Test fixture for using optimist, envvars and a file store with nconf.
|
||||||
|
*
|
||||||
|
* (C) 2011, Charlie Robbins
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
var fs = require('fs'),
|
||||||
|
path = require('path'),
|
||||||
|
nconf = require('../../../lib/nconf');
|
||||||
|
|
||||||
|
//
|
||||||
|
// Setup nconf to use (in-order):
|
||||||
|
// 1. Command-line arguments
|
||||||
|
// 2. Environment variables
|
||||||
|
// 3. A file located at 'path/to/config.json'
|
||||||
|
//
|
||||||
|
nconf.argv()
|
||||||
|
.env()
|
||||||
|
.file({ file: path.join(__dirname, '..', 'load-save.json') });
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set a few variables on `nconf`.
|
||||||
|
//
|
||||||
|
nconf.set('database:host', '127.0.0.1');
|
||||||
|
nconf.set('database:port', 5984);
|
||||||
|
|
||||||
|
process.stdout.write(nconf.get('foo'));
|
||||||
|
//
|
||||||
|
// Save the configuration object to disk
|
||||||
|
//
|
||||||
|
nconf.save();
|
|
@ -6,7 +6,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var assert = require('assert'),
|
var assert = require('assert'),
|
||||||
|
fs = require('fs'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
|
spawn = require('child_process').spawn,
|
||||||
vows = require('vows'),
|
vows = require('vows'),
|
||||||
nconf = require('../lib/nconf');
|
nconf = require('../lib/nconf');
|
||||||
|
|
||||||
|
@ -28,6 +30,38 @@ vows.describe('nconf/hierarchy').addBatch({
|
||||||
assert.equal(nconf.get('color'), 'green');
|
assert.equal(nconf.get('color'), 'green');
|
||||||
assert.equal(nconf.get('movie'), 'Kill Bill');
|
assert.equal(nconf.get('movie'), 'Kill Bill');
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).export(module);
|
}).export(module);
|
Loading…
Reference in a new issue