diff --git a/lib/configParser.ts b/lib/configParser.ts index b818be0e5..0b9d5b4f3 100644 --- a/lib/configParser.ts +++ b/lib/configParser.ts @@ -1,6 +1,8 @@ -import {resolve, dirname} from 'path'; -import {sync} from 'glob'; +import * as path from 'path'; +import * as glob from 'glob'; + import * as Logger from './logger'; +import {ConfigError} from './exitCodes'; // Coffee is required here to enable config files written in coffee-script. try { @@ -41,7 +43,7 @@ export interface Config { maxSessions?: number; } -export default class ConfigParser { +export class ConfigParser { private config_: Config; constructor() { // Default configuration. @@ -82,12 +84,12 @@ export default class ConfigParser { if (patterns) { for (let fileName of patterns) { - let matches = sync(fileName, {cwd}); + let matches = glob.sync(fileName, {cwd}); if (!matches.length && !opt_omitWarnings) { Logger.warn('pattern ' + fileName + ' did not match any files.'); } for (let match of matches) { - let resolvedPath = resolve(cwd, match); + let resolvedPath = path.resolve(cwd, match); resolvedFiles.push(resolvedPath); } } @@ -139,7 +141,7 @@ export default class ConfigParser { if (additionalConfig[name] && typeof additionalConfig[name] === 'string') { additionalConfig[name] = - resolve(relativeTo, additionalConfig[name]); + path.resolve(relativeTo, additionalConfig[name]); } }); @@ -153,23 +155,22 @@ export default class ConfigParser { * @param {String} filename */ public addFileConfig(filename: string): ConfigParser { + if (!filename) { + return this; + } + let filePath = path.resolve(process.cwd(), filename); + let fileConfig: any; try { - if (!filename) { - return this; - } - let filePath = resolve(process.cwd(), filename); - let fileConfig = require(filePath).config; - if (!fileConfig) { - Logger.error( - 'configuration file ' + filename + ' did not export a config ' + - 'object'); - } - fileConfig.configDir = dirname(filePath); - this.addConfig_(fileConfig, fileConfig.configDir); + fileConfig = require(filePath).config; } catch (e) { - Logger.error('failed loading configuration file ' + filename); - throw e; + throw new ConfigError('failed loading configuration file ' + filename) + } + if (!fileConfig) { + throw new ConfigError( + 'configuration file ' + filename + ' did not export a config object'); } + fileConfig.configDir = path.dirname(filePath); + this.addConfig_(fileConfig, fileConfig.configDir); return this; } @@ -208,7 +209,6 @@ let merge_ = function(into: any, from: any): any { !(into[key] instanceof Function)) { merge_(into[key], from[key]); } else { - // console.log(from[key].toString()); into[key] = from[key]; } } diff --git a/lib/exitCodes.ts b/lib/exitCodes.ts new file mode 100644 index 000000000..433c9ef17 --- /dev/null +++ b/lib/exitCodes.ts @@ -0,0 +1,21 @@ +import * as Logger from './logger'; + +export class ProtractorError extends Error { + msg: string; + code: number; + constructor(msg: string, code: number) { + super(msg); + this.msg = msg; + this.code = code; + Logger.error('error code: ' + this.code + ' - ' + this.msg); + } +} + +const CONFIG_ERROR_CODE = 105; + +/** + * Configuration file error + */ +export class ConfigError extends ProtractorError { + constructor(msg: string) { super(msg, CONFIG_ERROR_CODE); } +} diff --git a/lib/launcher.js b/lib/launcher.js index 33ee0998b..5832361b7 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -4,7 +4,7 @@ */ 'use strict'; -var ConfigParser = require('./configParser').default, +var ConfigParser = require('./configParser').ConfigParser, TaskScheduler = require('./taskScheduler').default, helper = require('./util'), log = require('./logger'), diff --git a/lib/runnerCli.js b/lib/runnerCli.js index 708f74c60..fd300d68f 100644 --- a/lib/runnerCli.js +++ b/lib/runnerCli.js @@ -3,7 +3,7 @@ * requested by the launcher. */ -var ConfigParser = require('./configParser').default; +var ConfigParser = require('./configParser').ConfigParser; var Runner = require('./runner'); var log = require('./logger'); diff --git a/lib/taskRunner.ts b/lib/taskRunner.ts index cea472fec..1db925c7d 100644 --- a/lib/taskRunner.ts +++ b/lib/taskRunner.ts @@ -3,7 +3,7 @@ import {EventEmitter} from 'events'; import {defer, Promise} from 'q'; import {inherits} from 'util'; -import ConfigParser, {Config} from './configParser'; +import {ConfigParser, Config} from './configParser'; import * as Logger from './logger'; import TaskLogger from './taskLogger'; diff --git a/lib/taskScheduler.ts b/lib/taskScheduler.ts index 95965a15f..d25dec255 100644 --- a/lib/taskScheduler.ts +++ b/lib/taskScheduler.ts @@ -1,4 +1,4 @@ -import ConfigParser, {Config} from './configParser'; +import {ConfigParser, Config} from './configParser'; export interface Task { @@ -45,7 +45,7 @@ export default class TaskScheduler { ConfigParser .resolveFilePatterns( ConfigParser.getSpecs(config), false, config.configDir) - .filter((path) => { return excludes.indexOf(path) < 0; }); + .filter((path: string) => { return excludes.indexOf(path) < 0; }); let taskQueues: Array = []; config.multiCapabilities.forEach((capabilities) => { diff --git a/spec/unit/config_test.js b/spec/unit/configParser_test.js similarity index 81% rename from spec/unit/config_test.js rename to spec/unit/configParser_test.js index 23d488563..883c6cc7d 100644 --- a/spec/unit/config_test.js +++ b/spec/unit/configParser_test.js @@ -1,7 +1,25 @@ -var ConfigParser = require('../../built/configParser').default; +var ConfigParser = require('../../built/configParser').ConfigParser; +var ConfigError = require('../../built/exitCodes').ConfigError; var path = require('path'); describe('the config parser', function() { + it('should throw an error if the file is not found', function() { + var config = new ConfigParser(); + try { + config.addFileConfig('foobar.js'); + } catch (err) { + expect(err.code).toEqual(ConfigError.CODE); + } + }); + + it('should throw an error if the file does not have export config', function() { + var config = new ConfigParser(); + try { + config.addFileConfig(path.resolve('./spec/environment.js')); + } catch (err) { + expect(err.code).toEqual(ConfigError.CODE); + } + }); it('should have a default config', function() { var config = new ConfigParser().getConfig();