2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2013-02-06 06:13:10 +08:00
|
|
|
exports.init = function(grunt, phantomjs) {
|
2013-01-09 03:28:26 +08:00
|
|
|
// node api
|
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path');
|
|
|
|
|
|
|
|
// npm
|
2014-01-29 09:12:51 +08:00
|
|
|
var rimraf = require('rimraf'),
|
|
|
|
_ = require('lodash');
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
var baseDir = '.',
|
|
|
|
tempDir = '.grunt/grunt-contrib-jasmine';
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.writeTempFile = function(dest, contents) {
|
|
|
|
var file = path.join(tempDir,dest);
|
|
|
|
grunt.file.write(file, contents);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.copyTempFile = function(src, dest) {
|
|
|
|
var file = path.join(tempDir,dest);
|
|
|
|
grunt.file.copy(src, file);
|
2012-11-02 13:03:09 +08:00
|
|
|
};
|
|
|
|
|
2013-06-16 02:50:37 +08:00
|
|
|
exports.cleanTemp = function(cb) {
|
|
|
|
rimraf(tempDir, function(){
|
|
|
|
// if this fails, then ./.grunt isn't empty and that's ok.
|
|
|
|
fs.rmdir('.grunt', cb);
|
|
|
|
});
|
2013-01-09 03:28:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.buildSpecrunner = function (src, options){
|
2013-06-19 11:26:34 +08:00
|
|
|
var source = '',
|
|
|
|
outfile = options.outfile,
|
|
|
|
specrunner = path.join(baseDir,outfile),
|
|
|
|
outdir = path.dirname(outfile),
|
|
|
|
gruntfilter = grunt.option("filter"),
|
|
|
|
filteredSpecs = exports.getRelativeFileList(outdir, options.specs);
|
|
|
|
|
|
|
|
// Let's filter through the spec files here,
|
|
|
|
// there's no need to go on if no specs matches
|
2014-01-29 09:12:51 +08:00
|
|
|
if (gruntfilter) {
|
2013-06-19 11:26:34 +08:00
|
|
|
filteredSpecs = specFilter(gruntfilter, filteredSpecs);
|
|
|
|
|
|
|
|
if(filteredSpecs.length === 0) {
|
|
|
|
grunt.log.warn("the --filter flag did not match any spec within " + grunt.task.current.target);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
exports.copyTempFile(__dirname + '/../jasmine/reporters/PhantomReporter.js', 'reporter.js');
|
2013-10-25 11:16:14 +08:00
|
|
|
|
|
|
|
['jasmine.css', 'jasmine.js', 'jasmine-html.js', 'boot.js'].forEach(function(name){
|
|
|
|
var path = __dirname + '/../../vendor/jasmine-' + options.version + '/' + name;
|
|
|
|
if (fs.existsSync(path)) exports.copyTempFile(path, name);
|
|
|
|
});
|
|
|
|
|
2014-02-23 04:05:01 +08:00
|
|
|
exports.copyTempFile(__dirname + '/../../node_modules/es5-shim/es5-shim.js', 'es5-shim.js');
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
var reporters = [
|
|
|
|
tempDir + '/reporter.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var jasmineCss = [
|
|
|
|
tempDir + '/jasmine.css'
|
|
|
|
];
|
|
|
|
|
2013-06-14 11:29:09 +08:00
|
|
|
jasmineCss = jasmineCss.concat(options.styles);
|
2013-03-11 13:15:29 +08:00
|
|
|
|
2013-06-16 00:38:39 +08:00
|
|
|
var polyfills = [
|
2014-02-23 04:05:01 +08:00
|
|
|
tempDir + '/es5-shim.js'
|
2013-06-16 00:38:39 +08:00
|
|
|
];
|
|
|
|
|
2013-01-09 03:28:26 +08:00
|
|
|
var jasmineCore = [
|
|
|
|
tempDir + '/jasmine.js',
|
|
|
|
tempDir + '/jasmine-html.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var context = {
|
|
|
|
temp : tempDir,
|
2014-01-29 09:12:51 +08:00
|
|
|
outfile: outfile,
|
|
|
|
css : exports.getRelativeFileList(outdir, jasmineCss, { nonull : true }),
|
2013-01-09 03:28:26 +08:00
|
|
|
scripts : {
|
2013-06-16 00:38:39 +08:00
|
|
|
polyfills : exports.getRelativeFileList(outdir, polyfills),
|
2014-01-29 09:12:51 +08:00
|
|
|
jasmine : exports.getRelativeFileList(outdir, jasmineCore),
|
|
|
|
helpers : exports.getRelativeFileList(outdir, options.helpers, { nonull : true }),
|
|
|
|
specs : filteredSpecs,
|
|
|
|
src : exports.getRelativeFileList(outdir, src, { nonull : true }),
|
|
|
|
vendor : exports.getRelativeFileList(outdir, options.vendor, { nonull : true }),
|
2013-06-14 11:29:09 +08:00
|
|
|
reporters : exports.getRelativeFileList(outdir, reporters),
|
2014-01-29 09:12:51 +08:00
|
|
|
boot : exports.getRelativeFileList(outdir, tempDir + '/boot.js')
|
2013-01-09 03:28:26 +08:00
|
|
|
},
|
|
|
|
options : options.templateOptions || {}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.template.process) {
|
2013-02-06 06:13:10 +08:00
|
|
|
var task = {
|
|
|
|
writeTempFile : exports.writeTempFile,
|
|
|
|
copyTempFile : exports.copyTempFile,
|
|
|
|
phantomjs : phantomjs
|
|
|
|
};
|
|
|
|
source = options.template.process(grunt, task, context);
|
2013-01-09 03:28:26 +08:00
|
|
|
grunt.file.write(specrunner, source);
|
|
|
|
} else {
|
|
|
|
grunt.file.copy(options.template, specrunner, {
|
|
|
|
process : function(src) {
|
2014-01-29 09:12:51 +08:00
|
|
|
source = _.template(src, context);
|
2013-01-09 03:28:26 +08:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
});
|
2012-11-02 13:03:09 +08:00
|
|
|
}
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
return source;
|
|
|
|
};
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
exports.getRelativeFileList = function(outdir, patterns, options) {
|
2013-06-16 02:15:01 +08:00
|
|
|
patterns = patterns instanceof Array ? patterns : [ patterns ];
|
|
|
|
options = options || {};
|
2013-06-14 11:29:09 +08:00
|
|
|
|
2014-04-23 05:43:59 +08:00
|
|
|
var files = grunt.file.expand(options, grunt.util._(patterns).compact());
|
2013-01-09 03:28:26 +08:00
|
|
|
files = grunt.util._(files).map(function(file){
|
2013-06-14 11:29:09 +08:00
|
|
|
return (/^https?:/).test(file) ? file : path.relative(outdir, file).replace(/\\/g, '/');
|
2013-01-09 03:28:26 +08:00
|
|
|
});
|
|
|
|
return files;
|
|
|
|
};
|
|
|
|
|
2013-06-19 11:26:34 +08:00
|
|
|
// Allows for a spec file to be specified via the command line
|
|
|
|
function specFilter(pattern, files) {
|
|
|
|
var specPattern,
|
|
|
|
patternArray,
|
|
|
|
filteredArray = [],
|
|
|
|
scriptSpecs = [],
|
|
|
|
matchPath = function(path) {
|
|
|
|
return !!path.match(specPattern);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(pattern) {
|
|
|
|
// For '*' to work as a wildcard.
|
|
|
|
pattern = pattern.split("*").join("[\\S]*").replace(/\./g, "\\.");
|
|
|
|
// This allows for comma separated strings to which we can match the spec files.
|
|
|
|
patternArray = pattern.split(",");
|
|
|
|
|
|
|
|
while(patternArray.length > 0) {
|
|
|
|
pattern = (patternArray.splice(0, 1)[0]);
|
|
|
|
|
|
|
|
if(pattern.length > 0) {
|
|
|
|
if(pattern.indexOf('/') === -1) {
|
|
|
|
specPattern = new RegExp("("+pattern+"[^/]*)(?!/)$", "ig");
|
|
|
|
} else if(pattern.indexOf('/') === 0) {
|
|
|
|
specPattern = new RegExp("("+pattern+"[^/]*)(?=/)", "ig");
|
|
|
|
} else {
|
|
|
|
throw new TypeError("--filter flag seems to be in the wrong format.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// push is usually faster than concat.
|
|
|
|
[].push.apply(scriptSpecs, files.filter(matchPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
filteredArray = _.uniq(scriptSpecs);
|
2013-06-19 11:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return filteredArray;
|
|
|
|
}
|
|
|
|
|
2013-01-09 03:28:26 +08:00
|
|
|
return exports;
|
|
|
|
};
|
2012-11-02 13:03:09 +08:00
|
|
|
|