Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
feat(exitCodes): adding exit codes for configuration file errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina committed Apr 13, 2016
1 parent 6a73f78 commit ca4f5a5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
42 changes: 21 additions & 21 deletions lib/configParser.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -41,7 +43,7 @@ export interface Config {
maxSessions?: number;
}

export default class ConfigParser {
export class ConfigParser {
private config_: Config;
constructor() {
// Default configuration.
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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]);
}
});

Expand All @@ -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;
}

Expand Down Expand Up @@ -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];
}
}
Expand Down
21 changes: 21 additions & 0 deletions lib/exitCodes.ts
Original file line number Diff line number Diff line change
@@ -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 = 100;

/**
* Configuration file error
*/
export class ConfigError extends ProtractorError {
constructor(msg: string) { super(msg, CONFIG_ERROR_CODE); }
}
2 changes: 1 addition & 1 deletion lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion lib/taskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions lib/taskScheduler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ConfigParser, {Config} from './configParser';
import {ConfigParser, Config} from './configParser';


export interface Task {
Expand Down Expand Up @@ -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<TaskQueue> = [];
config.multiCapabilities.forEach((capabilities) => {
Expand Down
20 changes: 19 additions & 1 deletion spec/unit/config_test.js → spec/unit/configParser_test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit ca4f5a5

Please sign in to comment.