Skip to content

Commit

Permalink
feat(config): allow empty config file when called programmatically
Browse files Browse the repository at this point in the history
So that tools like grunt (that invokes Testacular directly), can pass all the config options without actually having a config file.

Closes #358
  • Loading branch information
vojtajina committed Feb 21, 2013
1 parent f39e564 commit f3d7742
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
83 changes: 45 additions & 38 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,7 @@ var normalizeConfig = function(config) {
};


var parseConfig = function(configFilePath, cliOptions) {

// default config
var config = {
port: constant.DEFAULT_PORT,
runnerPort: constant.DEFAULT_RUNNER_PORT,
hostname: constant.DEFAULT_HOSTNAME,
basePath: '',
files: [],
exclude: [],
logLevel: constant.LOG_INFO,
colors: true,
autoWatch: false,
reporters: ['progress'],
singleRun: false,
browsers: [],
captureTimeout: 60000,
proxies: {},
preprocessors: {'**/*.coffee': 'coffee'},
urlRoot: '/',
reportSlowerThan: 0,
junitReporter: {
outputFile: 'test-results.xml',
suite: ''
},
coverageReporter: {
type: 'html',
dir: 'coverage/'
},
loggers: [ constant.CONSOLE_APPENDER ]
};

var readConfigFile = function(filepath) {
var ADAPTER_DIR = __dirname + '/../adapter';
var configEnv = {
// constants
Expand All @@ -161,15 +130,15 @@ var parseConfig = function(configFilePath, cliOptions) {
console: console,
require: require,
process: process,
__filename: configFilePath,
__dirname: path.dirname(configFilePath)
__filename: filepath,
__dirname: path.dirname(filepath)
};

try {
var configSrc = fs.readFileSync(configFilePath);
var configSrc = fs.readFileSync(filepath);

// if the configuration file is coffeescript compile it
if (path.extname(configFilePath) === '.coffee') {
if (path.extname(filepath) === '.coffee') {
configSrc = coffee.compile(configSrc.toString(), {bare: true});
}

Expand All @@ -186,12 +155,50 @@ var parseConfig = function(configFilePath, cliOptions) {
process.exit(1);
}

return configEnv;
};


var parseConfig = function(configFilePath, cliOptions) {

var configFromFile = configFilePath ? readConfigFile(configFilePath) : {};

// default config
var config = {
port: constant.DEFAULT_PORT,
runnerPort: constant.DEFAULT_RUNNER_PORT,
hostname: constant.DEFAULT_HOSTNAME,
basePath: '',
files: [],
exclude: [],
logLevel: constant.LOG_INFO,
colors: true,
autoWatch: false,
reporters: ['progress'],
singleRun: false,
browsers: [],
captureTimeout: 60000,
proxies: {},
preprocessors: {'**/*.coffee': 'coffee'},
urlRoot: '/',
reportSlowerThan: 0,
junitReporter: {
outputFile: 'test-results.xml',
suite: ''
},
coverageReporter: {
type: 'html',
dir: 'coverage/'
},
loggers: [ constant.CONSOLE_APPENDER ]
};

// merge the config from config file and cliOptions (precendense)
Object.getOwnPropertyNames(config).forEach(function(key) {
if (cliOptions.hasOwnProperty(key)) {
config[key] = cliOptions[key];
} else if (configEnv.hasOwnProperty(key)) {
config[key] = configEnv[key];
} else if (configFromFile.hasOwnProperty(key)) {
config[key] = configFromFile[key];
}
});

Expand Down
14 changes: 12 additions & 2 deletions test/unit/config.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ describe 'config', ->
# Should parse configuration file and do some basic processing as well
#============================================================================
describe 'parseConfig', ->
logSpy = sinon.spy()
logSpy = null

beforeEach ->
logSpy = sinon.spy()

logger = require '../../lib/logger.js'
logger.setup 'ERROR', false

Expand Down Expand Up @@ -221,6 +223,14 @@ describe 'config', ->
expect(config.autoWatch).to.equal false


it 'should not read config file, when null', ->
config = e.parseConfig null, {basePath: '/some'}

expect(logSpy).not.to.have.been.called
expect(config.basePath).to.equal '/some' # overriden by CLI
expect(config.urlRoot).to.equal '/' # default value


describe 'normalizeConfig', ->

it 'should resolve junitReporter.outputFile to basePath and CWD', ->
Expand Down

0 comments on commit f3d7742

Please sign in to comment.