From 73432eb67f76a2d403d3878d4190ea01bde78209 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Thu, 6 Nov 2014 19:31:06 -0800 Subject: [PATCH] feat: Fail on launcher-, reporter-, or preprocessor-load errors. 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 #855 --- lib/launcher.js | 9 +++++---- lib/preprocessor.js | 35 +++++++++++++++++++---------------- lib/reporter.js | 8 +++++--- lib/server.js | 11 +++++++++++ 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/lib/launcher.js b/lib/launcher.js index bebdefdb6..f9a69eb40 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -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) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index c11689f33..e76314aec 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -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) @@ -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; } } @@ -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 diff --git a/lib/reporter.js b/lib/reporter.js index ebf6dc207..5636af4e1 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -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); } }) diff --git a/lib/server.js b/lib/server.js index 6edd381bf..5af706bb5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -50,6 +50,8 @@ var Server = function (cliOptions, done) { this.log = logger.create() + this.loadErrors = [] + var config = cfg.parseConfig(cliOptions.configFile, cliOptions) var modules = [{ @@ -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) }) @@ -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) + } }) }