Skip to content

Commit

Permalink
fix(init): generate plugins and frameworks config
Browse files Browse the repository at this point in the history
This basically updates `karma init` to work after the plugin refactorings.
  • Loading branch information
vojtajina committed Apr 2, 2013
1 parent 39f9d66 commit 17798d5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 31 deletions.
10 changes: 10 additions & 0 deletions config.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
basePath = '%BASE_PATH%';


// frameworks to use
frameworks = [%FRAMEWORKS%];


// list of files / patterns to load in the browser
files = [
%FILES%
Expand Down Expand Up @@ -62,3 +66,9 @@ captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;


// plugins to load
plugins = [
%PLUGINS%
];
63 changes: 46 additions & 17 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var path = require('path');
var glob = require('glob');

var helper = require('./helper');
var launcher = require('./launcher');
var logger = require('./logger');
var constant = require('./constants');

Expand Down Expand Up @@ -50,14 +49,29 @@ var validatePattern = function(value) {
};


var validateBrowser = function(value) {
var proto = launcher[value + 'Browser'].prototype;
var defaultCmd = proto.DEFAULT_CMD[process.platform];
var envCmd = process.env[proto.ENV_CMD];
var validateBrowser = function(name) {
var moduleName = 'karma-' + name.toLowerCase().replace('canary', '') + '-launcher';

if (!fs.existsSync(defaultCmd) && (!envCmd || !fs.existsSync(envCmd))) {
log.warn('No binary for %s.\n Create symlink at "%s", or set "%s" env variable.\n' +
colors.NYAN, value, defaultCmd, proto.ENV_CMD);
try {
require(moduleName);
} catch (e) {
log.warn('Missing "%s" plugin.\n npm install %s --save' + colors.NYAN, moduleName, moduleName);
}

// TODO(vojta): check if the path resolves to a binary
};

var validateFramework = function(name) {
try {
require('karma-' + name);
} catch (e) {
log.warn('Missing "karma-%s" plugin.\n npm install karma-%s --save' + colors.NYAN, name, name);
}
};

var validateRequireJs = function(useRequire) {
if (useRequire) {
validateFramework('requirejs');
}
};

Expand All @@ -66,13 +80,15 @@ var questions = [{
id: 'framework',
question: 'Which testing framework do you want to use ?',
hint: 'Press tab to list possible options. Enter to move to the next question.',
options: ['jasmine', 'mocha', 'qunit', '']
options: ['jasmine', 'mocha', 'qunit', ''],
validate: validateFramework
}, {
id: 'requirejs',
question: 'Do you want to use Require.js ?',
hint: 'This will add Require.js adapter into files.\n' +
'Press tab to list possible options. Enter to move to the next question.',
options: ['no', 'yes'],
validate: validateRequireJs,
boolean: true
}, {
id: 'browsers',
Expand Down Expand Up @@ -272,24 +288,37 @@ var getReplacementsFromAnswers = function(answers, basePath) {
files = answers.includedFiles.map(quote).concat(files);
}

if (answers.requirejs) {
files.unshift('REQUIRE_ADAPTER');
files.unshift('REQUIRE');
}
var frameworks = [];

if (answers.framework) {
var framework = answers.framework.toUpperCase();
files.unshift(framework + '_ADAPTER');
files.unshift(framework);
frameworks.push(answers.framework);
}

if (answers.requirejs) {
frameworks.push('requirejs');
}

var plugins = frameworks.map(function(name) {
return 'karma-' + name;
});

answers.browsers.forEach(function(name) {
if (name === 'ChromeCanary') {
return;
}

plugins.push('karma-' + name.toLowerCase() + '-launcher');
});

return {
DATE: new Date(),
BASE_PATH: basePath,
FRAMEWORKS: frameworks.map(quote).join(', '),
FILES: formatFiles(files),
EXCLUDE: answers.exclude ? formatFiles(answers.exclude.map(quote)) : '',
AUTO_WATCH: answers.autoWatch ? 'true' : 'false',
BROWSERS: answers.browsers.map(quote).join(', ')
BROWSERS: answers.browsers.map(quote).join(', '),
PLUGINS: formatFiles(plugins.map(quote))
};
};

Expand Down
46 changes: 32 additions & 14 deletions test/unit/init.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,28 @@ describe 'init', ->
replacements = m.getReplacementsFromAnswers answers {files: ['*.js', 'other/file.js']}
expect(replacements.FILES).to.equal "'*.js',\n 'other/file.js'"

# prepend testing framework files
replacements = m.getReplacementsFromAnswers answers {files: ['*.js'], framework: 'jasmine'}
expect(replacements.FILES).to.equal "JASMINE,\n JASMINE_ADAPTER,\n '*.js'"
replacements = m.getReplacementsFromAnswers answers {files: ['*.js'], framework: 'mocha'}
expect(replacements.FILES).to.equal "MOCHA,\n MOCHA_ADAPTER,\n '*.js'"

it 'should set FRAMEWORKS', ->
replacements = m.getReplacementsFromAnswers answers {
framework: 'mocha',
requirejs: true
}

expect(replacements.FRAMEWORKS).to.equal "'mocha', 'requirejs'"

it 'should add REQUIRE and set files non-included if requirejs used', ->

it 'should add requirejs and set files non-included if requirejs used', ->
replacements = m.getReplacementsFromAnswers answers {
requirejs: true,
includedFiles: [],
files: ['*.js', 'other/file.js']
}

expect(replacements.FILES).to.equal "REQUIRE,\n" +
" REQUIRE_ADAPTER,\n" +
" {pattern: '*.js', included: false},\n" +
" {pattern: 'other/file.js', included: false}"
expect(replacements.FRAMEWORKS).to.contain "'requirejs'"

expect(replacements.FILES).to.equal "" +
"{pattern: '*.js', included: false},\n " +
"{pattern: 'other/file.js', included: false}"


it 'should prepend includedFiles into FILES', ->
Expand All @@ -200,10 +204,9 @@ describe 'init', ->
files: ['*.js']
}

expect(replacements.FILES).to.equal "REQUIRE,\n" +
" REQUIRE_ADAPTER,\n" +
" 'main.js',\n" +
" {pattern: '*.js', included: false}"
expect(replacements.FILES).to.equal "" +
"'main.js',\n " +
"{pattern: '*.js', included: false}"


it 'should set EXCLUDE', ->
Expand All @@ -227,3 +230,18 @@ describe 'init', ->

replacements = m.getReplacementsFromAnswers answers {autoWatch: false}
expect(replacements.AUTO_WATCH).to.equal 'false'


it 'should set PLUGINS', ->
replacements = m.getReplacementsFromAnswers answers {
browsers: ['Chrome', 'Firefox', 'ChromeCanary']
framework: 'jasmine'
requirejs: true
}

# ignore chrome canary
expect(replacements.PLUGINS).to.equal "" +
"'karma-jasmine',\n " +
"'karma-requirejs',\n " +
"'karma-chrome-launcher',\n " +
"'karma-firefox-launcher'"

0 comments on commit 17798d5

Please sign in to comment.