Skip to content

Commit

Permalink
feat(init): generate test-main.(js/coffee) for RequireJS projects
Browse files Browse the repository at this point in the history
  • Loading branch information
cironunes authored and vojtajina committed Mar 10, 2014
1 parent 2071184 commit 85900c9
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ var questions = [{
'Enter empty string to move to the next question.',
multiple: true,
validate: validatePattern
}, {
id: 'generateTestMain',
question: 'Do you wanna generate a bootstrap file for RequireJS?',
hint: 'This will generate test-main.js/coffee that configures RequiseJS and starts the tests.',
options: ['no', 'yes'],
boolean: true,
condition: function(answers) {return answers.requirejs;}
}, {
id: 'includedFiles',
question: 'Which files do you want to include with <script> tag ?',
Expand All @@ -133,7 +140,7 @@ var questions = [{
'Enter empty string to move to the next question.',
multiple: true,
validate: validatePattern,
condition: function(answers) {return answers.requirejs;}
condition: function(answers) {return answers.requirejs && !answers.generateTestMain;}
}, {
id: 'autoWatch',
question: 'Do you want Karma to watch all the files and run the tests on change ?',
Expand Down Expand Up @@ -166,14 +173,15 @@ var getBasePath = function(configFilePath, cwd) {
};


var processAnswers = function(answers, basePath) {
var processAnswers = function(answers, basePath, testMainFile) {

var processedAnswers = {
basePath: basePath,
files: answers.files,
onlyServedFiles: [],
exclude: answers.exclude,
autoWatch: answers.autoWatch,
generateTestMain: answers.generateTestMain,
browsers: answers.browsers,
frameworks: [],
preprocessors: {}
Expand All @@ -185,8 +193,12 @@ var processAnswers = function(answers, basePath) {

if (answers.requirejs) {
processedAnswers.frameworks.push('requirejs');
processedAnswers.files = answers.includedFiles;
processedAnswers.files = answers.includedFiles || [];
processedAnswers.onlyServedFiles = answers.files;

if (answers.generateTestMain) {
processedAnswers.files.push(testMainFile);
}
}

var allPatterns = answers.files.concat(answers.includedFiles || []);
Expand Down Expand Up @@ -239,12 +251,20 @@ exports.init = function(config) {
sm.process(questions, function(answers) {
var cwd = process.cwd();
var configFile = config.configFile || 'karma.conf.js';
var testMainFile = (/\.coffee$/).test(configFile) ? 'test-main.coffee' : 'test-main.js';
var formatter = formatters.createForPath(configFile);
var processedAnswers = processAnswers(answers, getBasePath(configFile, cwd));
var processedAnswers = processAnswers(answers, getBasePath(configFile, cwd), testMainFile);
var configFilePath = path.resolve(cwd, configFile);
var testMainFilePath = path.resolve(cwd, testMainFile);

formatter.writeConfigFile(configFilePath, processedAnswers);
if (processedAnswers.generateTestMain) {
formatter.writeRequirejsConfigFile(testMainFilePath);
console.log(colorScheme.success(
'RequireJS bootstrap file generated at "' + testMainFilePath + '".\n'
));
}

formatter.writeConfigFile(configFilePath, processedAnswers);
console.log(colorScheme.success('Config file generated at "' + configFilePath + '".\n'));
});
};
13 changes: 13 additions & 0 deletions lib/init/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var util = require('util');

var JS_TEMPLATE_PATH = __dirname + '/../../config.tpl.js';
var COFFEE_TEMPLATE_PATH = __dirname + '/../../config.tpl.coffee';
var JS_REQUIREJS_TEMPLATE_PATH = __dirname + '/../../requirejs.config.tpl.js';
var COFFEE_REQUIREJS_TEMPLATE_PATH = __dirname + '/../../requirejs.config.tpl.coffee';
var COFFEE_REGEXP = /\.coffee$/;
var LIVE_TEMPLATE_PATH = __dirname + '/../../config.tpl.ls';
var LIVE_REGEXP = /\.ls$/;
Expand Down Expand Up @@ -35,6 +37,7 @@ var JavaScriptFormatter = function() {
};

this.TEMPLATE_FILE_PATH = JS_TEMPLATE_PATH;
this.REQUIREJS_TEMPLATE_FILE = JS_REQUIREJS_TEMPLATE_PATH;

this.formatFiles = function(includedFiles, onlyServedFiles) {
var files = includedFiles.map(quote);
Expand Down Expand Up @@ -84,12 +87,22 @@ var JavaScriptFormatter = function() {
this.writeConfigFile = function(path, answers) {
fs.writeFileSync(path, this.generateConfigFile(answers));
};

this.generateRequirejsConfigFile = function () {
var template = fs.readFileSync(this.REQUIREJS_TEMPLATE_FILE).toString();
return template;
};

this.writeRequirejsConfigFile = function (path) {
fs.writeFileSync(path, this.generateRequirejsConfigFile());
};
};

var CoffeeFormatter = function() {
JavaScriptFormatter.call(this);

this.TEMPLATE_FILE_PATH = COFFEE_TEMPLATE_PATH;
this.REQUIREJS_TEMPLATE_FILE = COFFEE_REQUIREJS_TEMPLATE_PATH;
};

var LiveFormatter = function() {
Expand Down
19 changes: 19 additions & 0 deletions requirejs.config.tpl.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
allTestFiles = []
TEST_REGEXP = /(spec|test)(\.coffee)?(\.js)?$/i
pathToModule = (path) ->
path.replace(/^\/base\//, "").replace(/\.js$/, "").replace(/\.cofee$/, "")

Object.keys(window.__karma__.files).forEach (file) ->
# Normalize paths to RequireJS module names.
allTestFiles.push pathToModule(file) if TEST_REGEXP.test(file)
return

require.config
# Karma serves files under /base, which is the basePath from your config file
baseUrl: "/base"

# dynamically load all test files
deps: allTestFiles

# we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
24 changes: 24 additions & 0 deletions requirejs.config.tpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

var pathToModule = function(path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});

require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base',

// dynamically load all test files
deps: allTestFiles,

// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
48 changes: 48 additions & 0 deletions test/unit/init.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ describe 'init', ->

# excludes
machine.onLine ''
machine.onLine ''

# generate test-main
machine.onLine 'no'

# included files
machine.onLine 'test/main.js'
Expand All @@ -193,6 +197,50 @@ describe 'init', ->
machine.onLine 'yes'


it 'should generate the test-main for requirejs', (done) ->
machine.process m.questions, (answers) ->
basePath = m.getBasePath '../karma.conf.js', '/some/path'
processedAnswers = m.processAnswers answers, basePath, 'test-main.js'
generatedConfigCode = formatter.generateConfigFile processedAnswers
config = evaluateConfigCode generatedConfigCode

# expect correct processedAnswers
expect(processedAnswers.generateTestMain).to.be.ok
expect(processedAnswers.files).to.contain 'test-main.js'

# expect correct configuration
expect(config.frameworks).to.contain 'requirejs'
for pattern in config.files.slice(1)
expect(pattern.included).to.equal false
done()

# frameworks
machine.onLine 'jasmine'
machine.onLine ''

# requirejs
machine.onLine 'yes'

# browsers
machine.onLine 'Chrome'
machine.onLine ''

# files
machine.onLine 'src/**/*.js'
machine.onLine 'test/**/*.js'
machine.onLine ''

# excludes
machine.onLine ''
machine.onLine ''

# generate test-main
machine.onLine 'yes'

# autoWatch
machine.onLine 'yes'


it 'should add coffee preprocessor', (done) ->
machine.process m.questions, (answers) ->
basePath = m.getBasePath 'karma.conf.js', '/cwd'
Expand Down

0 comments on commit 85900c9

Please sign in to comment.