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

Commit 5fa94db

Browse files
committed
feat(exitCodes): adding exit codes for configuration file errors (#3128)
1 parent bd78dfc commit 5fa94db

File tree

7 files changed

+66
-27
lines changed

7 files changed

+66
-27
lines changed

lib/configParser.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import {resolve, dirname} from 'path';
2-
import {sync} from 'glob';
1+
import * as path from 'path';
2+
import * as glob from 'glob';
3+
34
import * as Logger from './logger';
5+
import {ConfigError} from './exitCodes';
46

57
// Coffee is required here to enable config files written in coffee-script.
68
try {
@@ -41,7 +43,7 @@ export interface Config {
4143
maxSessions?: number;
4244
}
4345

44-
export default class ConfigParser {
46+
export class ConfigParser {
4547
private config_: Config;
4648
constructor() {
4749
// Default configuration.
@@ -82,12 +84,12 @@ export default class ConfigParser {
8284

8385
if (patterns) {
8486
for (let fileName of patterns) {
85-
let matches = sync(fileName, {cwd});
87+
let matches = glob.sync(fileName, {cwd});
8688
if (!matches.length && !opt_omitWarnings) {
8789
Logger.warn('pattern ' + fileName + ' did not match any files.');
8890
}
8991
for (let match of matches) {
90-
let resolvedPath = resolve(cwd, match);
92+
let resolvedPath = path.resolve(cwd, match);
9193
resolvedFiles.push(resolvedPath);
9294
}
9395
}
@@ -139,7 +141,7 @@ export default class ConfigParser {
139141
if (additionalConfig[name] &&
140142
typeof additionalConfig[name] === 'string') {
141143
additionalConfig[name] =
142-
resolve(relativeTo, additionalConfig[name]);
144+
path.resolve(relativeTo, additionalConfig[name]);
143145
}
144146
});
145147

@@ -153,23 +155,22 @@ export default class ConfigParser {
153155
* @param {String} filename
154156
*/
155157
public addFileConfig(filename: string): ConfigParser {
158+
if (!filename) {
159+
return this;
160+
}
161+
let filePath = path.resolve(process.cwd(), filename);
162+
let fileConfig: any;
156163
try {
157-
if (!filename) {
158-
return this;
159-
}
160-
let filePath = resolve(process.cwd(), filename);
161-
let fileConfig = require(filePath).config;
162-
if (!fileConfig) {
163-
Logger.error(
164-
'configuration file ' + filename + ' did not export a config ' +
165-
'object');
166-
}
167-
fileConfig.configDir = dirname(filePath);
168-
this.addConfig_(fileConfig, fileConfig.configDir);
164+
fileConfig = require(filePath).config;
169165
} catch (e) {
170-
Logger.error('failed loading configuration file ' + filename);
171-
throw e;
166+
throw new ConfigError('failed loading configuration file ' + filename)
167+
}
168+
if (!fileConfig) {
169+
throw new ConfigError(
170+
'configuration file ' + filename + ' did not export a config object');
172171
}
172+
fileConfig.configDir = path.dirname(filePath);
173+
this.addConfig_(fileConfig, fileConfig.configDir);
173174
return this;
174175
}
175176

@@ -208,7 +209,6 @@ let merge_ = function(into: any, from: any): any {
208209
!(into[key] instanceof Function)) {
209210
merge_(into[key], from[key]);
210211
} else {
211-
// console.log(from[key].toString());
212212
into[key] = from[key];
213213
}
214214
}

lib/exitCodes.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as Logger from './logger';
2+
3+
export class ProtractorError extends Error {
4+
msg: string;
5+
code: number;
6+
constructor(msg: string, code: number) {
7+
super(msg);
8+
this.msg = msg;
9+
this.code = code;
10+
Logger.error('error code: ' + this.code + ' - ' + this.msg);
11+
}
12+
}
13+
14+
const CONFIG_ERROR_CODE = 105;
15+
16+
/**
17+
* Configuration file error
18+
*/
19+
export class ConfigError extends ProtractorError {
20+
constructor(msg: string) { super(msg, CONFIG_ERROR_CODE); }
21+
}

lib/launcher.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
'use strict';
66

7-
var ConfigParser = require('./configParser').default,
7+
var ConfigParser = require('./configParser').ConfigParser,
88
TaskScheduler = require('./taskScheduler').default,
99
helper = require('./util'),
1010
log = require('./logger'),

lib/runnerCli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* requested by the launcher.
44
*/
55

6-
var ConfigParser = require('./configParser').default;
6+
var ConfigParser = require('./configParser').ConfigParser;
77
var Runner = require('./runner');
88
var log = require('./logger');
99

lib/taskRunner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {EventEmitter} from 'events';
33
import {defer, Promise} from 'q';
44
import {inherits} from 'util';
55

6-
import ConfigParser, {Config} from './configParser';
6+
import {ConfigParser, Config} from './configParser';
77
import * as Logger from './logger';
88
import TaskLogger from './taskLogger';
99

lib/taskScheduler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ConfigParser, {Config} from './configParser';
1+
import {ConfigParser, Config} from './configParser';
22

33

44
export interface Task {
@@ -45,7 +45,7 @@ export default class TaskScheduler {
4545
ConfigParser
4646
.resolveFilePatterns(
4747
ConfigParser.getSpecs(config), false, config.configDir)
48-
.filter((path) => { return excludes.indexOf(path) < 0; });
48+
.filter((path: string) => { return excludes.indexOf(path) < 0; });
4949

5050
let taskQueues: Array<TaskQueue> = [];
5151
config.multiCapabilities.forEach((capabilities) => {

spec/unit/config_test.js spec/unit/configParser_test.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
var ConfigParser = require('../../built/configParser').default;
1+
var ConfigParser = require('../../built/configParser').ConfigParser;
2+
var ConfigError = require('../../built/exitCodes').ConfigError;
23
var path = require('path');
34

45
describe('the config parser', function() {
6+
it('should throw an error if the file is not found', function() {
7+
var config = new ConfigParser();
8+
try {
9+
config.addFileConfig('foobar.js');
10+
} catch (err) {
11+
expect(err.code).toEqual(ConfigError.CODE);
12+
}
13+
});
14+
15+
it('should throw an error if the file does not have export config', function() {
16+
var config = new ConfigParser();
17+
try {
18+
config.addFileConfig(path.resolve('./spec/environment.js'));
19+
} catch (err) {
20+
expect(err.code).toEqual(ConfigError.CODE);
21+
}
22+
});
523

624
it('should have a default config', function() {
725
var config = new ConfigParser().getConfig();

0 commit comments

Comments
 (0)