Skip to content

Commit

Permalink
feat: Fail on launcher-, reporter-, or preprocessor-load errors.
Browse files Browse the repository at this point in the history
Fail out of karma if a launcher, reporter, or preprocessor fails to
load, after attempting to load each of them, and reporting errors for
each load error.

Closes karma-runner#855
  • Loading branch information
srawlins committed Jul 16, 2015
1 parent 09d64ed commit 73432eb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
9 changes: 5 additions & 4 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ var Launcher = function (emitter, injector) {
browser = injector.createChild([locals], ['launcher:' + name]).get('launcher:' + name)
} catch (e) {
if (e.message.indexOf('No provider for "launcher:' + name + '"') !== -1) {
log.warn('Can not load "%s", it is not registered!\n ' +
'Perhaps you are missing some plugin?', name)
log.error('Cannot load browser "%s": it is not registered! ' +
'Perhaps you are missing some plugin?', name)
} else {
log.warn('Can not load "%s"!\n ' + e.stack, name)
log.error('Cannot load browser "%s"!\n ' + e.stack, name)
}

return
emitter.emit('load_error', 'launcher', name);
return;
}

// TODO(vojta): remove in v1.0 (BC for old launchers)
Expand Down
35 changes: 19 additions & 16 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ extensions.forEach(function (extension) {
})

// TODO(vojta): instantiate preprocessors at the start to show warnings immediately
var createPreprocessor = function (config, basePath, injector) {
var alreadyDisplayedWarnings = Object.create(null)
var createPreprocessor = function(config, basePath, injector, emitter) {
var patterns = Object.keys(config);
var alreadyDisplayedErrors = Object.create(null);

return function (file, done) {
var patterns = Object.keys(config)
Expand All @@ -45,24 +46,25 @@ var createPreprocessor = function (config, basePath, injector) {
return done()
}

preprocessors.shift()(content, file, nextPreprocessor)
}
var instantiatePreprocessor = function (name) {
if (alreadyDisplayedWarnings[name]) {
return
preprocessors.shift()(content, file, nextPreprocessor);
};
var instantiatePreprocessor = function(name) {
if (alreadyDisplayedErrors[name]) {
return;
}

try {
preprocessors.push(injector.get('preprocessor:' + name))
} catch (e) {
if (e.message.indexOf('No provider for "preprocessor:' + name + '"') !== -1) {
log.warn('Can not load "%s", it is not registered!\n ' +
'Perhaps you are missing some plugin?', name)
log.error('Cannot load preprocessor "%s": it is not registered! ' +
'Perhaps you are missing some plugin?', name)
} else {
log.warn('Can not load "%s"!\n ' + e.stack, name)
log.error('Cannot load preprocessor "%s"!\n ' + e.stack, name)
}

alreadyDisplayedWarnings[name] = true
emitter.emit('load_error', 'preprocessor', name);
alreadyDisplayedErrors[name] = true;
}
}

Expand All @@ -83,10 +85,11 @@ var createPreprocessor = function (config, basePath, injector) {
if (err) {
throw err
}
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
})
}
}
createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector']
file.sha = sha1(buffer);
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString());
});
};
};
createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector', 'emitter'];

exports.createPreprocessor = createPreprocessor
8 changes: 5 additions & 3 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ var createReporters = function (names, config, emitter, injector) {
reporters.push(injector.createChild([locals], ['reporter:' + name]).get('reporter:' + name))
} catch (e) {
if (e.message.indexOf('No provider for "reporter:' + name + '"') !== -1) {
log.warn('Can not load "%s", it is not registered!\n ' +
'Perhaps you are missing some plugin?', name)
log.error('Cannot load reporter "%s": it is not registered! ' +
'Perhaps you are missing some plugin?', name)
} else {
log.warn('Can not load "%s"!\n ' + e.stack, name)
log.error('Cannot load reporter "%s"!\n ' + e.stack, name)
}

emitter.emit('load_error', 'reporter', name);
}
})

Expand Down
11 changes: 11 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var Server = function (cliOptions, done) {

this.log = logger.create()

this.loadErrors = []

var config = cfg.parseConfig(cliOptions.configFile, cliOptions)

var modules = [{
Expand Down Expand Up @@ -105,6 +107,10 @@ Server.prototype._start = function (config, launcher, preprocess, fileList, webS
capturedBrowsers, socketServer, executor, done) {
var self = this

this.on('load_error', function(type, name) {
self.loadErrors.push([type, name])
})

config.frameworks.forEach(function (framework) {
self._injector.get('framework:' + framework)
})
Expand Down Expand Up @@ -143,6 +149,11 @@ Server.prototype._start = function (config, launcher, preprocess, fileList, webS
singleRunDoneBrowsers[browserLauncher.id] = false
})
}

if (self.loadErrors.length > 0) {
// At least one plugin failed to load.
process.exit(1)
}
})
}

Expand Down

0 comments on commit 73432eb

Please sign in to comment.