From 94bdb7dbd88372b5a70b9762e90a2209949cf2a0 Mon Sep 17 00:00:00 2001 From: Russell Frank Date: Tue, 1 May 2012 22:33:00 -0400 Subject: [PATCH] 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. --- .gitignore | 3 +- test/complete-test.js | 127 ++++++++++++++++++++++++++++++++++++ test/fixtures/complete.json | 19 ++++++ test/helpers.js | 16 +++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 test/complete-test.js create mode 100644 test/fixtures/complete.json diff --git a/.gitignore b/.gitignore index 1a52174..ee49fdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ .DS_Store config.json test/fixtures/*.json +!test/fixtures/complete.json !test/fixtures/malformed.json node_modules/ node_modules/* -npm-debug.log \ No newline at end of file +npm-debug.log diff --git a/test/complete-test.js b/test/complete-test.js new file mode 100644 index 0000000..7ff1e08 --- /dev/null +++ b/test/complete-test.js @@ -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 diff --git a/test/fixtures/complete.json b/test/fixtures/complete.json new file mode 100644 index 0000000..6576c98 --- /dev/null +++ b/test/fixtures/complete.json @@ -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 +} diff --git a/test/helpers.js b/test/helpers.js index c1ac598..d62f87e 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -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