From 312d904b5becbba56ced66ce2e82e57d77fa4c5f Mon Sep 17 00:00:00 2001 From: cliftonc Date: Sat, 21 Apr 2012 11:32:34 +0100 Subject: [PATCH] Fix to config instance vs library on calipso.config --- app.js | 2 +- lib/calipso.js | 1 + lib/core/Config.js | 160 ------------------------------ test/helpers/calipsoHelper.js | 3 +- test/lib.core.config.js | 182 ---------------------------------- test/lib.core.storage.js | 2 - 6 files changed, 3 insertions(+), 347 deletions(-) delete mode 100644 lib/core/Config.js delete mode 100644 test/lib.core.config.js diff --git a/app.js b/app.js index d1a66e2d5..00fce0e87 100644 --- a/app.js +++ b/app.js @@ -47,7 +47,7 @@ function bootApplication(next) { app.isCluster = false; // Load configuration - var Config = calipso.config; //require(path + "/lib/core/Config").Config; + var Config = calipso.configuration; //require(path + "/lib/core/Config").Config; app.config = new Config(); app.config.init(function(err) { diff --git a/lib/calipso.js b/lib/calipso.js index 01729dbaa..7b2a7150f 100644 --- a/lib/calipso.js +++ b/lib/calipso.js @@ -76,6 +76,7 @@ function init(app, initCallback) { // Load the calipso package.json into app.about calipso.module.loadAbout(app, rootpath, 'package.json'); + // config is the actual instance of loaded config, configuration is the library. calipso.config = app.config; // Store the callback function for later diff --git a/lib/core/Config.js b/lib/core/Config.js deleted file mode 100644 index b55aa9269..000000000 --- a/lib/core/Config.js +++ /dev/null @@ -1,160 +0,0 @@ -/** - * Configuration library - */ - -var rootpath = process.cwd() + '/', - path = require('path'), - step = require('step'), - _ = require('underscore'), - fs = require('fs'); - -/** - * Config object, wrapper for nconf - * @type - * @options - */ - -function Config(options) { - - // Defaults - this.type = options && options.type ? options.type : 'file'; - this.env = options && options.env ? options.env : (process.env.NODE_ENV || 'development'); - this.path = options && options.path ? options.path : path.join(rootpath, 'conf'); - this.defaultConfig = options && options.defaultConfig ? options.defaultConfig : path.join(rootpath, 'lib', 'conf', 'default.json'); - this.file = path.join(this.path, this.env + '.json'); - - // Track if changes have been made to config - this.dirty = false; - -} - -Config.prototype.init = function(next) { - if (typeof next !== 'function') next = function(err) { - if (err) console.error(err.message) - }; - this.nconf = new require('nconf'); - this.load(next); -} - -/** - * Check to see if configuration for this environment - * doesn't exist, if so, it loads the default from default.json. - */ -Config.prototype.check = function() { - - if (!path.existsSync(this.file)) { - try { - var defaultFile = fs.readFileSync(this.defaultConfig); - // Parse it to make sure there are no errors - defaultFile = JSON.stringify(JSON.parse(defaultFile), true); - fs.writeFileSync(this.file, defaultFile); - } catch (ex) { - return ex.message; - } - return; - } else { - return; - } - -} - -/** - * Load the configuration - */ -Config.prototype.load = function(next) { - - // Check if config exists for this environment or default it - var checkConfig = this.check(); - if (!checkConfig) { - - // Initialise nconf - try { - this.nconf.use(this.type, this); - } catch (ex) { - return next(ex); - } - - this.nconf.load(next); - - } else { - - next(new Error("Unable to load configuration defined in " + this.env + ".json, there may be a problem with the default configuration in " + this.defaultConfig + ", reason: " + checkConfig)); - - } - -} - -/** - * Get config - wrapper - */ -Config.prototype.get = function(key) { - return this.nconf.get(key); -} - -/** - * Get config for module - wrapper - */ -Config.prototype.getModuleConfig = function(moduleName, key) { - var moduleKey = 'modules:' + moduleName + ':config' + (key ? ':' + key : ''); - return this.nconf.get(moduleKey); -} - - -/** - * Set config - */ -Config.prototype.set = function(key, value) { - this.dirty = true; - this.nconf.set(key, value); -} - -/** - * Set config for module - wrapper - */ -Config.prototype.setModuleConfig = function(moduleName, key, value) { - var moduleKey = 'modules:' + moduleName + ':config' + (key ? ':' + key : ''); - this.dirty = true; - this.nconf.set(moduleKey, value); -} - -/** - * Set default config for module - wrapper - */ -Config.prototype.setDefaultModuleConfig = function(moduleName, config) { - - var moduleKey = 'modules:' + moduleName + ':config'; - this.dirty = true; - - // Extract the defaults from the config - var defaultConfig = _.reduce(_.keys(config), function(memo, key) { - memo[key] = config[key]. - default; - return memo; - }, {}) - - this.nconf.set(moduleKey, defaultConfig); - -} - -/** - * Save config - */ -Config.prototype.save = function(next) { - this.dirty = false; - this.nconf.save(next); -} - -/** - * Set & save config - */ - -Config.prototype.setSave = function(key, value, next) { - this.set(key, value); - this.dirty = false; - this.save(next); -} - -/** - * Export the config object - */ -module.exports = Config; diff --git a/test/helpers/calipsoHelper.js b/test/helpers/calipsoHelper.js index 64dd7ebac..9c07811f8 100644 --- a/test/helpers/calipsoHelper.js +++ b/test/helpers/calipsoHelper.js @@ -6,7 +6,7 @@ var calipso = require('./require')('calipso'), fs = require('fs'), colors = require('colors'), rootpath = process.cwd() + '/', - Config = require('./require')('core/Config'), + Config = require('./require')('core/Configuration'), http = require('http'), mochaConfig = path.join(rootpath,'tmp','mocha.json'); @@ -23,7 +23,6 @@ function MockApp(next) { // Configuration - always start with default var defaultConfig = path.join(rootpath, 'test', 'helpers', 'defaultConfig.json'); - var statusMsg = '\r\nBase path: '.grey + rootpath.cyan + '\r\nUsing config: '.grey + defaultConfig.cyan + '\r\nIn environment: '.grey + (process.env.NODE_ENV || 'development').cyan; if(!process.env.CALIPSO_COV) console.log(statusMsg); diff --git a/test/lib.core.config.js b/test/lib.core.config.js deleted file mode 100644 index eeae0f580..000000000 --- a/test/lib.core.config.js +++ /dev/null @@ -1,182 +0,0 @@ -/** - * Mocha test case for core configuration library - */ -var should = require('should'), - fs = require('fs'), - rootpath = process.cwd() + '/', - path = require('path'), - exec = require('child_process').exec, - Config = require('./helpers/require')('core/Config'), - defaultConfig = { - "test":"test", - "modules": { - "module":{ - "enabled":true - } - } - }; - -describe('Configuration', function(){ - - before(function(){ - // - }); - - describe('Negative', function(){ - - /** - * For some reason this fails in Travis?!?! - it('Invalid configuration type results in an exception', function(done){ - var conf = new Config({type:'invalid', env:'invalid1', 'path': path.join(rootpath,'tmp')}); - conf.init(function(err) { - err.message.should.equal('Cannot add store with unknown type: invalid'); - // Delete the development.json - fs.unlinkSync(path.join(rootpath,'tmp','invalid1.json')); - done(); - }); - });**/ - - it('Invalid default configuration results in an exception', function(done){ - var conf = new Config({env:'invalid2', 'path': path.join(rootpath,'tmp'), 'defaultConfig':'invalidDefault.json'}); - conf.init(function(err) { - err.message.should.equal('Unable to load configuration defined in invalid2.json, there may be a problem with the default configuration in invalidDefault.json, reason: ENOENT, no such file or directory \'invalidDefault.json\'') - done() - }) - }); - - - }); - - describe('Positive', function(){ - - it('I can create a new configuration file based on a default one.', function(done){ - - mkdir(path.join(rootpath,'tmp'), function() { - fs.writeFileSync(path.join(rootpath,'tmp','default.json'),JSON.stringify(defaultConfig)); - var conf = new Config({env:'development', 'path':path.join(rootpath,'tmp')}); - conf.type.should.equal('file'); - conf.file.should.equal(path.join(rootpath,'tmp','development.json')); - done(); - }); - - }); - - it('I can store and retrieve configuration values', function(done){ - - var conf = new Config({env:'development',path:path.join(rootpath,'tmp')}); - conf.init(function(err) { - - conf.set('test:v1','v1'); - conf.get('test:v1').should.equal('v1'); - conf.set('test:v2','v2'); - conf.get('test:v2').should.equal('v2'); - - var test = conf.get('test'); - test.v1.should.equal('v1'); - test.v2.should.equal('v2'); - - done(); - - }); - - }); - - it('I can use different environments', function(done){ - - var confTest = new Config({env:'test',path:path.join(rootpath,'tmp')}); - confTest.init(function(err) { - - confTest.set('test:v1','v1'); - confTest.get('test:v1').should.equal('v1'); - confTest.save(function(err) { - path.existsSync(confTest.file); - done(); - }); - }); - - - }); - - it('I can use the setSave shortcut', function(done){ - - var conf = new Config({path:path.join(rootpath,'tmp')}); - conf.init(function(err) { - - conf.setSave('test:setsave','Yah!',function(err) { - var confTest = new Config({path:path.join(rootpath,'tmp')}); - confTest.init(function(err) { - confTest.get('test:setsave').should.equal('Yah!'); - done(); - }); - }); - - }); - - }); - - }); - - describe('Modules', function(){ - - it('I can set and retrieve module configuration', function(done){ - - var conf = new Config({path:path.join(rootpath,'tmp')}); - conf.init(function(err) { - conf.setModuleConfig('module','hello','world'); - var value = conf.getModuleConfig('module','hello'); - value.should.equal('world'); - done(); - }); - - }); - - it('I can set default module configuration', function(done){ - - var conf = new Config({path:path.join(rootpath,'tmp')}); - - conf.init(function(err) { - - var defaultConfig = { - "goodbye": { - "default": "cruel world", - "label": "Example", - "description": "Example" - } - }; - - conf.setDefaultModuleConfig('module',defaultConfig); - var value = conf.getModuleConfig('module','goodbye'); - value.should.equal('cruel world'); - done(); - - }); - - }); - - }); - - after(function() { - try { fs.unlinkSync(path.join(rootpath,'tmp','default.json')); } catch(ex) {}; - try { fs.unlinkSync(path.join(rootpath,'tmp','development.json')); } catch(ex) {}; - try { fs.unlinkSync(path.join(rootpath,'tmp','test.json')); } catch(ex) {}; - try { fs.unlinkSync(path.join(rootpath,'tmp','invalid.json')); } catch(ex) {}; - try { fs.unlinkSync(path.join(rootpath,'tmp',process.env.NODE_ENV + '.json')); } catch(ex) {}; - }) - -}); - -/** - * Mkdir -p. - * - * TODO - these are unix only ... - * - * @param {String} path - * @param {Function} fn - */ - -function mkdir(path, fn) { - exec('mkdir -p ' + path, function(err){ - if (err) throw err; - fn && fn(); - }); -} diff --git a/test/lib.core.storage.js b/test/lib.core.storage.js index 248d89bd3..10c531935 100644 --- a/test/lib.core.storage.js +++ b/test/lib.core.storage.js @@ -13,8 +13,6 @@ var should = require('should'), describe('Storage', function(){ before(function(done) { - // Initialise calipso, set installed to false - calipsoHelper.calipso.loaded.should.equal(true); done(); });