Skip to content

Commit

Permalink
[test] Add additional test coverage for hierarchical configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Nov 24, 2011
1 parent a9c3540 commit b658f68
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/fixtures/scripts/nconf-hierarchical-load-save.js
Original file line number Diff line number Diff line change
@@ -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();
34 changes: 34 additions & 0 deletions test/hierarchy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

var assert = require('assert'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
vows = require('vows'),
nconf = require('../lib/nconf');

Expand All @@ -28,6 +30,38 @@ vows.describe('nconf/hierarchy').addBatch({
assert.equal(nconf.get('color'), 'green');
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);

0 comments on commit b658f68

Please sign in to comment.