Skip to content

Commit

Permalink
Run each requirejs spec in it's own context so global variables don't…
Browse files Browse the repository at this point in the history
… accidentally polute over to multiple spec files.
  • Loading branch information
mtscout6 authored and mhevery committed Jan 11, 2012
1 parent b88e1a9 commit c87197c
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 53 deletions.
14 changes: 11 additions & 3 deletions lib/jasmine-node/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var showColors = true;
var teamcity = process.env.TEAMCITY_PROJECT_NAME || false;
var useRequireJs = false;
var extentions = "js";
var match = '.'
var match = '.';
var useHelpers = true;

var junitreport = {
report: false,
Expand Down Expand Up @@ -61,6 +62,9 @@ while(args.length) {
case '--runWithRequireJs':
useRequireJs = true;
break;
case '--nohelpers':
useHelpers = false;
break;
case '--test-dir':
var dir = args.shift();

Expand Down Expand Up @@ -100,8 +104,11 @@ var onComplete = function(runner, log) {
}
};

jasmine.loadHelpersInFolder(specFolder,
new RegExp("[-_]helper\\.(" + extentions + ")$"));
if(useHelpers){
jasmine.loadHelpersInFolder(specFolder,
new RegExp("[-_]helper\\.(" + extentions + ")$"));
}

jasmine.executeSpecsInFolder(specFolder,
onComplete,
isVerbose,
Expand All @@ -125,6 +132,7 @@ function help(){
, ' --teamcity - converts all console output to teamcity custom test runner commands. (Normally auto detected.)'
, ' --runWithRequireJs - loads all specs using requirejs instead of node\'s native require method'
, ' --test-dir - the absolute root directory path where tests are located'
, ' --nohelpers - does not load helpers.'
, ' -h, --help - display this help and exit'
, ''
].join("\n"));
Expand Down
4 changes: 2 additions & 2 deletions lib/jasmine-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jasmine.loadHelpersInFolder=function(folder, matcher)
helperCollection = require('./spec-collection');

helperCollection.load(folder, matcher);
helpers = helperCollection.getSpecPaths();
helpers = helperCollection.getSpecs();

for (var i = 0, len = helpers.length; i < len; ++i)
{
Expand Down Expand Up @@ -103,7 +103,7 @@ jasmine.executeSpecsInFolder = function(folder,
if (useRequireJs) {
require('./requirejs-runner').executeJsRunner(specs, done, jasmineEnv);
} else {
var specsList = specs.getSpecPaths();
var specsList = specs.getSpecs();

for (var i = 0, len = specsList.length; i < len; ++i) {
var filename = specsList[i];
Expand Down
69 changes: 58 additions & 11 deletions lib/jasmine-node/requirejs-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,69 @@ exports.executeJsRunner = function(specCollection, done, jasmineEnv) {
var specs,
specLoader = require('./requirejs-spec-loader'),
requirejs = require('requirejs'),
fs = require('fs');
vm = require('vm'),
fs = require('fs'),
template = fs.readFileSync(__dirname + '/requirejs-wrapper-template.js', 'utf8'),
buildNewContext = function(){
return {
describe: describe,
it: it,
xdescribe: xdescribe,
xit: xit,
jasmine: jasmine,
expect: expect,
require: require,
console: console,
process: process,
module: module,
specLoader: specLoader
};
},
buildRelativeDirName = function(dir){
var retVal = "",
thisDir = process.cwd();//.replace(/.:/, '\\c').split('\\'),
toDir = dir.split('/'),
index = 0,
colonMatches = __dirname.match(/.:/);

specs = specCollection.getRelativeSpecPaths();
for(var i = 0; i < colonMatches.length; i++){
thisDir = thisDir.replace(colonMatches[i], '\\' + colonMatches[i].substring(0,1));
}

process.chdir(specCollection.getRootPath());
thisDir = thisDir.split('\\');

requirejs.config({
baseUrl: './',
nodeRequire: require
});
for(; index < thisDir.length || index < toDir.length; index++) {
if(thisDir[index] != toDir[index]){
for(var i = index; i < thisDir.length; i++){
retVal += '../';
}

for(var i = index; i < toDir.length; i++){
retVal += toDir[i] + '/';
}

break;
}
}

return retVal.trim('/');
};

specLoader.defineLoader(requirejs);
specCollection.getSpecs().forEach(function(s){
var script = fs.readFileSync(s.path(), 'utf8'),
dir = s.directory(),
colonMatches = dir.match(/.:/),
wrappedScript;

for (var i = 0, len = specs.length; i < len; i++) {
requirejs(specs[i]);
}
for(var i = 0; i < colonMatches.length; i++){
dir = dir.replace(colonMatches[i], '/' + colonMatches[i].substring(0,1));
}

wrappedScript = template.replace(/#REPLACE URL#/, buildRelativeDirName(dir))
.replace(/#REPLACE TEST SCRIPT#/, script);

vm.runInNewContext(wrappedScript, buildNewContext(), s.path());
});

specLoader.executeWhenAllSpecsAreComplete(jasmineEnv);
};
65 changes: 65 additions & 0 deletions lib/jasmine-node/requirejs-wrapper-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var test = function(require, define, undefined) { #REPLACE TEST SCRIPT#
};

var requirejsOrig = require('requirejs'),
ostring = Object.prototype.toString,
path = require('path'),
baseUrl = '#REPLACE URL#',
isArray = function(it){
return ostring.call(it) === '[object Array]';
},
isFunction = function(it){
return ostring.call(it) === '[object Function]';
},
requirejs = function(deps, callback){
var retVal;

if(!isArray(deps) && typeof deps !== 'string'){
if(isArray(callback)){
retVal = requirejsOrig(deps, callback, arguments[2]);
} else {
retVal = requirejsOrig(deps, [], callback);
}
} else {
retVal = requirejsOrig(deps, callback);
}

return retVal;
};

requirejsOrig.config({
baseUrl: baseUrl,
nodeRequire: require
});

for(var key in requirejsOrig) {
requirejs[key] = requirejsOrig[key];
}

requirejs.config = function(config){
var alteredConfig = {};

for(var key in config) {
alteredConfig[key] = config[key];
}

if(alteredConfig.baseUrl){
var base = baseUrl.replace(/\\/g, '/'),
splitUrl = alteredConfig.baseUrl.replace(/\\/g, '/').split('/'),
index = 0;

for(; index < splitUrl.length; index++){
if(splitUrl[index] === '..'){
base = path.dirname(base);
} else {
base += '/' + splitUrl[index];
}
}

alteredConfig.baseUrl = base;
}

return requirejsOrig.config(alteredConfig);
};

test(requirejs, requirejs.define);
82 changes: 45 additions & 37 deletions lib/jasmine-node/spec-collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
var collection = (function() {
var fs = require('fs'),
createSpecObj = function(path, root) {
return {
path: function() { return path; },
relativePath: function() { return path.replace(root, '').replace(/^[\/\\]/, ''); },
directory: function() { return path.replace(/[\/\\][\s\w\.-]*$/, ""); },
relativeDirectory: function() { return relativePath().replace(/[\/\\][\s\w\.-]*$/, ""); },
filename: function() { return path.replace(/^.*[\\\/]/, ''); }
};
},
getFiles = function(dir) {
if(isDir(dir)){
try {
return fs.readdirSync(dir);
} catch (err) {
if(err.code === 'ENOENT') {
return [];
} else {
throw err;
}
}
} else {
return [];
}
},
isFile = function(path) {
var isFile = false;

Expand Down Expand Up @@ -36,19 +59,19 @@ var collection = (function() {

return isDir;
},
getAllSpecFiles = function(path, matcher) {
getAllSpecFiles = function(path, matcher, root) {
var specs = [];

if (fs.statSync(path).isFile() && path.match(matcher)) {
specs.push(path);
} else {
var files = fs.readdirSync(path);
if (isFile(path) && path.match(matcher)) {
specs.push(createSpecObj(path, root));
} else if(isDir(path)) {
var files = getFiles(path);

for (var i = 0, len = files.length; i < len; i++) {
var filename = path + '/' + files[i];

if(isFile(filename) && filename.match(matcher)) {
specs.push(filename);
specs.push(createSpecObj(filename, root));
} else if (isDir(filename)) {
var subFiles = getAllSpecFiles(filename, matcher);

Expand All @@ -61,37 +84,22 @@ var collection = (function() {

return specs;
},
specs = [],
relativeSpecs = [],
root,
publicExposure = {
load: function(path, matcher) {
specs = getAllSpecFiles(path, matcher);
specs = [];

if (fs.statSync(path).isDirectory()) {
root = path;
} else {
root = path.replace(/[\/\\][\s\w\.-]*$/, "");
}
exports.load = function(path, matcher) {
var root;

for (var i = 0, len = specs.length; i < len; i++) {
relativeSpecs.push(specs[i].replace(root, '').replace(/^[\/\\]/, ''));
}
},
getRootPath: function() {
return root;
},
getRelativeSpecPaths: function() {
return relativeSpecs;
},
getSpecPaths: function() {
return specs;
}
};
if (isDir(path)) {
root = path;
} else {
root = path.replace(/[\/\\][\s\w\.-]*$/, "");
}

return publicExposure;
})();
getAllSpecFiles(path, matcher, root).forEach(function(s){
specs.push(s);
});
};

for (var key in collection) {
exports[key] = collection[key];
}
exports.getSpecs = function() {
return specs;
};

0 comments on commit c87197c

Please sign in to comment.