Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Run each requirejs spec in it's own context so global variables don't…

… accidentally polute over to multiple spec files.
  • Loading branch information...
commit c87197ccbd7ccf36df42b5bd0e28c19230d2e189 1 parent b88e1a9
Matthew Smith authored mhevery committed
View
14 lib/jasmine-node/cli.js
@@ -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,
@@ -61,6 +62,9 @@ while(args.length) {
case '--runWithRequireJs':
useRequireJs = true;
break;
+ case '--nohelpers':
+ useHelpers = false;
+ break;
case '--test-dir':
var dir = args.shift();
@@ -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,
@@ -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"));
View
4 lib/jasmine-node/index.js
@@ -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)
{
@@ -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];
View
69 lib/jasmine-node/requirejs-runner.js
@@ -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);
};
View
65 lib/jasmine-node/requirejs-wrapper-template.js
@@ -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);
View
82 lib/jasmine-node/spec-collection.js
@@ -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;
@@ -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);
@@ -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;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.