Add user configuration for Cylon internals
This commit is contained in:
parent
11082b44a9
commit
3b7c96e81e
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* cylon configuration loader
|
||||
* Cylon - Internal Configuration
|
||||
* cylonjs.com
|
||||
*
|
||||
* Copyright (c) 2013-2014 The Hybrid Group
|
||||
|
@ -8,8 +8,4 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var Utils = require('./utils');
|
||||
|
||||
module.exports = {
|
||||
testing_mode: Utils.fetch(process.env, 'CYLON_TEST', false)
|
||||
};
|
||||
module.exports = {};
|
||||
|
|
23
lib/cylon.js
23
lib/cylon.js
|
@ -12,6 +12,7 @@ var Async = require('async');
|
|||
|
||||
var Logger = require('./logger'),
|
||||
Robot = require('./robot'),
|
||||
Config = require('./config'),
|
||||
Utils = require('./utils');
|
||||
|
||||
Logger.setup();
|
||||
|
@ -76,6 +77,28 @@ Cylon.start = function start() {
|
|||
}
|
||||
};
|
||||
|
||||
// Public: Sets the internal configuration, based on passed options
|
||||
//
|
||||
// opts - object containing configuration key/value pairs
|
||||
//
|
||||
// Returns the updated config
|
||||
Cylon.setConfig = function(opts) {
|
||||
for (var o in opts) {
|
||||
Config[o] = opts[o];
|
||||
}
|
||||
|
||||
return Config;
|
||||
};
|
||||
|
||||
// Public: Fetches a value from the internal configuration
|
||||
//
|
||||
// key - string key to be fetched from config
|
||||
//
|
||||
// Returns the config value
|
||||
Cylon.config = function(key) {
|
||||
return Config[key];
|
||||
};
|
||||
|
||||
// Public: Halts the API and the robots
|
||||
//
|
||||
// callback - callback to be triggered when Cylon is ready to shutdown
|
||||
|
|
|
@ -12,7 +12,7 @@ var Connection = require("./connection"),
|
|||
Device = require("./device"),
|
||||
Logger = require('./logger'),
|
||||
Utils = require('./utils'),
|
||||
config = require('./config');
|
||||
Config = require('./config');
|
||||
|
||||
var Async = require("async"),
|
||||
EventEmitter = require('events').EventEmitter;
|
||||
|
@ -317,7 +317,7 @@ Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
|
|||
extraParams: opts
|
||||
});
|
||||
|
||||
if (config.testing_mode) {
|
||||
if (Config.test_mode) {
|
||||
var testAdaptor = this.requireAdaptor('test').adaptor({
|
||||
name: adaptorName,
|
||||
connection: connection,
|
||||
|
@ -389,7 +389,7 @@ Robot.prototype.initDriver = function(driverName, device, opts) {
|
|||
extraParams: opts
|
||||
});
|
||||
|
||||
if (config.testing_mode) {
|
||||
if (Config.test_mode) {
|
||||
var testDriver = this.requireDriver('test').driver({
|
||||
name: driverName,
|
||||
device: device,
|
||||
|
|
|
@ -6,7 +6,8 @@ var Cylon = source("cylon"),
|
|||
var API = source('api'),
|
||||
Logger = source('logger'),
|
||||
Adaptor = source('adaptor'),
|
||||
Driver = source('driver');
|
||||
Driver = source('driver'),
|
||||
Config = source('config');
|
||||
|
||||
describe("Cylon", function() {
|
||||
describe("exports", function() {
|
||||
|
@ -86,6 +87,40 @@ describe("Cylon", function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("#setConfig", function() {
|
||||
beforeEach(function() {
|
||||
for (var c in Config) {
|
||||
delete Config[c];
|
||||
}
|
||||
});
|
||||
|
||||
it("sets config variables", function() {
|
||||
Cylon.setConfig({ a: 1, b: 2 });
|
||||
expect(Config.a).to.be.eql(1);
|
||||
expect(Config.b).to.be.eql(2);
|
||||
});
|
||||
|
||||
it("updates existing config", function() {
|
||||
Cylon.setConfig({ a: 1, b: 2 });
|
||||
Cylon.setConfig({ a: 3 });
|
||||
expect(Config.a).to.be.eql(3);
|
||||
expect(Config.b).to.be.eql(2);
|
||||
});
|
||||
|
||||
it("returns updated config", function() {
|
||||
var config = Cylon.setConfig({ a: 1, b: 2 });
|
||||
expect(Config).to.be.eql(config);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#config", function() {
|
||||
it("returns a value from the Config object", function() {
|
||||
Config.a = "hello world";
|
||||
expect(Cylon.config("a")).to.be.eql("hello world");
|
||||
delete Config.a;
|
||||
})
|
||||
});
|
||||
|
||||
describe("#halt", function() {
|
||||
it("calls #halt() on all robots", function() {
|
||||
var bot1 = { halt: spy() },
|
||||
|
|
Loading…
Reference in New Issue