From c565e35142378b8cd216749c577460fa65c379bc Mon Sep 17 00:00:00 2001 From: "Thomas.A" Date: Wed, 19 Jun 2013 13:26:34 +1000 Subject: [PATCH] Added a filter flag, to filter through the *Spec files This allows to filter through the *Spec.js files, without modifying Gruntfile.js --- tasks/jasmine.js | 13 ++++++--- tasks/lib/jasmine.js | 63 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/tasks/jasmine.js b/tasks/jasmine.js index c31d599..0ba5622 100644 --- a/tasks/jasmine.js +++ b/tasks/jasmine.js @@ -54,7 +54,10 @@ module.exports = function(grunt) { setup(options); - jasmine.buildSpecrunner(this.filesSrc,options); + // The filter returned no spec, let's skip phantom. + if(!jasmine.buildSpecrunner(this.filesSrc, options)) { + return removePhantomListeners(); + } // If we're just building (e.g. for web), skip phantom. if (this.flags.build) return; @@ -100,8 +103,7 @@ module.exports = function(grunt) { } function teardown(options, cb) { - phantomjs.removeAllListeners(); - phantomjs.listenersAny().length = 0; + removePhantomListeners(); if (!options.keepRunner && fs.statSync(options.outfile).isFile()) fs.unlink(options.outfile); if (!options.keepRunner) { @@ -111,6 +113,11 @@ module.exports = function(grunt) { } } + function removePhantomListeners() { + phantomjs.removeAllListeners(); + phantomjs.listenersAny().length = 0; + } + function setup(options) { var thisRun = {}; diff --git a/tasks/lib/jasmine.js b/tasks/lib/jasmine.js index 9ca7a20..80ccc9a 100644 --- a/tasks/lib/jasmine.js +++ b/tasks/lib/jasmine.js @@ -32,6 +32,23 @@ exports.init = function(grunt, phantomjs) { }; exports.buildSpecrunner = function (src, options){ + 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 + if(gruntfilter) { + 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; + } + } exports.copyTempFile(__dirname + '/../jasmine/reporters/PhantomReporter.js', 'reporter.js'); exports.copyTempFile(__dirname + '/../../vendor/jasmine-' + options.version + '/jasmine.css', 'jasmine.css'); @@ -48,11 +65,6 @@ exports.init = function(grunt, phantomjs) { tempDir + '/jasmine.css' ]; - var source = '', - outfile = options.outfile, - specrunner = path.join(baseDir,outfile), - outdir = path.dirname(outfile); - jasmineCss = jasmineCss.concat(options.styles); var polyfills = [ @@ -73,7 +85,7 @@ exports.init = function(grunt, phantomjs) { polyfills : exports.getRelativeFileList(outdir, polyfills), jasmine : exports.getRelativeFileList(outdir, jasmineCore), helpers : exports.getRelativeFileList(outdir, options.helpers, { nonull : true }), - specs : exports.getRelativeFileList(outdir, options.specs), + specs : filteredSpecs, src : exports.getRelativeFileList(outdir, src, { nonull : true }), vendor : exports.getRelativeFileList(outdir, options.vendor, { nonull : true }), reporters : exports.getRelativeFileList(outdir, reporters), @@ -116,6 +128,45 @@ exports.init = function(grunt, phantomjs) { return files; }; + // 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)); + } + } + + filteredArray = grunt.util._.uniq(scriptSpecs); + } + + return filteredArray; + } + return exports; };