Added complete-test.js & fixture.

`complete-test.js` correctly tests the modified `save()` method.  It is
an attempt at a more complete functional test of nconf.
This commit is contained in:
Russell Frank 2012-05-01 22:33:00 -04:00
parent 36e061c4bd
commit 94bdb7dbd8
4 changed files with 164 additions and 1 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
.DS_Store
config.json
test/fixtures/*.json
!test/fixtures/complete.json
!test/fixtures/malformed.json
node_modules/
node_modules/*

127
test/complete-test.js Normal file
View file

@ -0,0 +1,127 @@
/*
* complete-test.js: Complete test with multiple providers
*/
var fs = require('fs'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
nconf = require('../lib/nconf'),
data = require('./fixtures/data').data,
helpers = require('./helpers');
var complete = helpers.fixture('complete.json');
var completeTest = helpers.fixture('complete-test.json');
vows.describe('nconf').addBatch({
"When using the nconf with multiple providers": {
topic: function () {
var that = this;
helpers.cp(complete, completeTest, function () {
nconf.env();
nconf.file({file: completeTest});
nconf.use('argv', {type: 'literal', store: data});
that.callback();
});
},
"env vars": {
"are present": function () {
Object.keys(process.env).forEach(function (key) {
assert.equal(nconf.get(key), process.env[key]);
});
}
},
"json vars": {
topic: function () {
fs.readFile(complete, 'utf8', this.callback);
},
"are present": function (err, data) {
assert.isNull(err);
data = JSON.parse(data);
Object.keys(data).forEach(function (key) {
assert.deepEqual(nconf.get(key), data[key]);
});
}
},
"literal vars": {
"are present": function () {
Object.keys(data).forEach(function (key) {
assert.equal(nconf.get(key), data[key]);
});
}
},
"and saving *synchronously*": {
topic: function () {
nconf.set('weebls', 'stuff');
return nconf.save();
},
"correct return value": function (topic) {
Object.keys(topic).forEach(function (key) {
assert.deepEqual(topic[key], nconf.get(key));
});
},
"the file": {
topic: function () {
fs.readFile(completeTest, 'utf8', this.callback);
},
"saved correctly": function (err, data) {
data = JSON.parse(data);
Object.keys(data).forEach(function (key) {
assert.deepEqual(data[key], nconf.get(key));
});
assert.equal(nconf.get('weebls'), 'stuff');
}
}
}
}
}).addBatch({
// Threw this in it's own batch to make sure it's run separately from the
// sync check
"When using the nconf with multiple providers": {
"and saving *asynchronously*": {
topic: function () {
nconf.set('weebls', 'crap');
nconf.save(this.callback);
},
"correct return value": function (err, data) {
assert.isNull(err);
Object.keys(data).forEach(function (key) {
assert.deepEqual(data[key], nconf.get(key));
});
},
"the file": {
topic: function () {
fs.readFile(completeTest, 'utf8', this.callback);
},
"saved correctly": function (err, data) {
assert.isNull(err);
data = JSON.parse(data);
Object.keys(data).forEach(function (key) {
assert.deepEqual(nconf.get(key), data[key]);
});
assert.equal(nconf.get('weebls'), 'crap');
}
}
},
teardown: function () {
nconf.remove('file');
nconf.remove('memory');
nconf.remove('argv');
nconf.remove('env');
}
}
}).export(module);
// vim: ts=2 shiftwidth=2 softtabstop=2

19
test/fixtures/complete.json vendored Normal file
View file

@ -0,0 +1,19 @@
{
"I've seen things": {
"like": [
"carrots",
"handbags",
"cheese",
"toilets",
"russians",
"planets",
"hampsters",
"weddings",
"poets",
"stalin",
"kuala lumpur"
]
},
"host": "weebls-stuff.com",
"port": 78304
}

View file

@ -7,6 +7,9 @@
var assert = require('assert'),
spawn = require('child_process').spawn,
util = require('util'),
fs = require('fs'),
path = require('path'),
nconf = require('../lib/nconf');
exports.assertMerged = function (err, merged) {
@ -52,3 +55,16 @@ exports.assertSystemConf = function (options) {
}
}
// copy a file
exports.cp = function (from, to, callback) {
fs.readFile(from, function (err, data) {
if (err) return callback(err);
fs.writeFile(to, data, callback);
});
};
exports.fixture = function (file) {
return require('path').join(__dirname, 'fixtures', file);
};
// vim: ts=2 shiftwidth=2 softtabstop=2