Skip to content

Commit

Permalink
Added ability to load spec files using requirejs instead of node's bu…
Browse files Browse the repository at this point in the history
…ilt in require method.

Removed teamcity reporter methods from the TerminalReporter, I thought I had removed it earlier but must have missed it.
Fixed a possible undefined exception is jasmine, submitted a pull request to jasmine as well; once they pick up the change it may be worth while to update to the latest version of jasmine.
Added the ability to pass an absolute folder location to the location of tests so the current working directory is not used.
  • Loading branch information
mtscout6 committed Oct 19, 2011
1 parent 042bad5 commit b6d8361
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 52 deletions.
29 changes: 26 additions & 3 deletions lib/jasmine-node/cli.js
Expand Up @@ -10,6 +10,7 @@ for (var key in jasmine)
var isVerbose = false;
var showColors = true;
var teamcity = process.env.TEAMCITY_PROJECT_NAME || false;
var useRequireJs = false;
var extentions = "js";
var match = '.'

Expand Down Expand Up @@ -60,6 +61,16 @@ while(args.length) {
case '--teamcity':
teamcity = true;
break;
case '--runWithRequireJs';
useRequireJs = true;
break;
case '--test-dir':
var dir = args.shift();

if(!Path.existsSync(dir));
throw new Error("Test root path '" + dir + "' doesn't exist!");

specFolder = dir; // NOTE: Does not look from current working directory.
default:
if (arg.match(/^--/)) help();
specFolder = Path.join(process.cwd(), arg);
Expand All @@ -80,15 +91,25 @@ function onExit() {
process.exit(exitCode);
}

jasmine.loadHelpersInFolder(specFolder, new RegExp("[-_]helper\\.(" + extentions + ")$"));
jasmine.executeSpecsInFolder(specFolder, function(runner, log){
var onComplete = function(runner, log) {
sys.print('\n');
if (runner.results().failedCount == 0) {
exitCode = 0;
} else {
exitCode = 1;
}
}, isVerbose, showColors, teamcity, new RegExp(match + "spec\\.(" + extentions + ")$", 'i'), junitreport);
};

jasmine.loadHelpersInFolder(specFolder,
new RegExp("[-_]helper\\.(" + extentions + ")$"));
jasmine.executeSpecsInFolder(specFolder,
onComplete,
isVerbose,
showColors,
teamcity,
useRequireJs,
new RegExp(match + "spec\\.(" + extentions + ")$", 'i'),
junitreport);

function help(){
sys.print([
Expand All @@ -103,6 +124,8 @@ function help(){
, ' --coffee - load coffee-script which allows execution .coffee files'
, ' --junitreport - export tests results as junitreport xml format'
, ' --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'
, ''
].join("\n"));

Expand Down
46 changes: 40 additions & 6 deletions lib/jasmine-node/index.js
Expand Up @@ -57,29 +57,63 @@ function removeJasmineFrames(text) {
return lines.join('\n');
}

jasmine.executeSpecsInFolder = function(folder, done, isVerbose, showColors, teamcity, matcher, junitreport){
var fileMatcher = matcher || new RegExp(".(js)$", "i");
var colors = showColors || false;
var specs = [];
jasmine.executeSpecsInFolder = function(folder,
done,
isVerbose,
showColors,
teamcity,
useRequireJs,
matcher,
junitreport){
var fileMatcher = matcher || new RegExp(".(js)$", "i"),
colors = showColors || false,
specs = [],
requirejs = undefined;

if (fs.statSync(folder).isDirectory()) {
specs = jasmine.getAllSpecFiles(folder, fileMatcher);

if (useRequireJs) {
process.chdir(folder);
}
} else {
specs.push(folder);

if (useRequireJs) {
folder = folder.replace(/[\/\\]*$/, "");
process.chdir(folder);
}
}

if (useRequireJs) {
requirejs = require('requirejs');

requirejs.config({
baseUrl: './',
nodeRequire: require
});
}

for (var i = 0, len = specs.length; i < len; ++i){
var filename = specs[i];
require(filename.replace(/\.\w+$/, ""));

if (useRequireJs) {
requirejs(filename.replace(folder + "/", ""));
} else {
require(filename.replace(/\.\w+$/, ""));
}
}

var jasmineEnv = jasmine.getEnv();

if(junitreport.report) {
if(!path.existsSync(junitreport.savePath)) {
sys.puts('creating junit xml report save path: ' + junitreport.savePath);
fs.mkdirSync(junitreport.savePath, "0755");
}
jasmineEnv.addReporter(new jasmine.JUnitXmlReporter(junitreport.savePath, junitreport.consolidate, junitreport.useDotNotation));
jasmineEnv.addReporter(new jasmine.JUnitXmlReporter(junitreport.savePath,
junitreport.consolidate,
junitreport.useDotNotation));
}

if(teamcity){
Expand Down
6 changes: 3 additions & 3 deletions lib/jasmine-node/jasmine-2.0.0.rc1.js
Expand Up @@ -1755,7 +1755,7 @@ jasmine.Queue.prototype.next_ = function() {

while (goAgain) {
goAgain = false;

if (self.index < self.blocks.length && !this.abort) {
var calledSynchronously = true;
var completedSynchronously = false;
Expand All @@ -1766,7 +1766,7 @@ jasmine.Queue.prototype.next_ = function() {
return;
}

if (self.blocks[self.index].abort) {
if (self.blocks[self.index] && self.blocks[self.index].abort) {
self.abort = true;
}

Expand All @@ -1793,7 +1793,7 @@ jasmine.Queue.prototype.next_ = function() {
if (completedSynchronously) {
onComplete();
}

} else {
self.running = false;
if (self.onComplete) {
Expand Down
47 changes: 7 additions & 40 deletions lib/jasmine-node/reporter.js
Expand Up @@ -19,18 +19,6 @@ printRunnerResults = function(runner){
return msg;
};

escapeTeamcityString = function(message) {
return message.replace(/\|/g, "||")
.replace(/\'/g, "|'")
.replace(/\n/g, "|n")
.replace(/\r/g, "|r")
.replace(/\u0085/g, "|x")
.replace(/\u2028/g, "|l")
.replace(/\u2029/g, "|p")
.replace(/\[/g, "|[")
.replace(/]/g, "|]");
};

ANSIColors = {
pass: function() { return '\033[32m'; }, // Green
fail: function() { return '\033[31m'; }, // Red
Expand All @@ -43,6 +31,7 @@ NoColors = {
neutral: function() { return ''; }
};


//
// Reporter implementation
//
Expand All @@ -51,7 +40,6 @@ TerminalReporter = function(config) {
this.isVerbose_ = config.verbose || false;
this.onComplete_ = config.onComplete || noop;
this.color_ = config.color? ANSIColors: NoColors;
this.teamcity_ = config.teamcity;
this.stackFilter = config.stackFilter || function(t) { return t; }

this.columnCounter_ = 0;
Expand Down Expand Up @@ -79,45 +67,24 @@ TerminalReporter.prototype = {
}
var description = path.join(' ');

if (this.isVerbose_ && !this.teamcity_)
if (this.isVerbose_)
this.log_.push('Spec ' + description);

if (this.teamcity_)
this.log_.push("##teamcity[testSuiteStarted name='" + escapeTeamcityString(description) + "]");

outerThis = this;
specResults.items_.forEach(function(spec){
if (spec.failedCount > 0 && spec.description) {
if (!outerThis.isVerbose_ && !outerThis.teamcity_)
if (!outerThis.isVerbose_)
outerThis.log_.push(description);

if (outerThis.teamcity_) {
outerThis.log_.push("##teamcity[testStarted name='" + escapeTeamcityString(spec.description) + "' captureStandardOutput='true']");
} else {
outerThis.log_.push(' it ' + spec.description);
}

outerThis.log_.push(' it ' + spec.description);
spec.items_.forEach(function(result){
if (!result.passed_) {
if(outerThis.teamcity_) {
outerThis.log_.push("##teamcity[testFailed name='" + escapeTeamcityString(spec.description) + "' message='[FAILED]' details='" + escapeTeamcityString(outerThis.stackFilter(outerThis.stackFilter(result.trace.stack))) + "']");
} else {
outerThis.log_.push(' ' + outerThis.stackFilter(result.trace.stack) + '\n');
}
}
if (!result.passed_)
outerThis.log_.push(' ' + outerThis.stackFilter(result.trace.stack) + '\n');
});

if (outerThis.teamcity_)
outerThis.log_.push("##teamcity[testFinished name='" + escapeTeamcityString(spec.description) + "']");

} else {
if (outerThis.isVerbose_ && !outerThis.teamcity_)
if (outerThis.isVerbose_)
outerThis.log_.push(' it ' + spec.description);
}
});

if (this.teamcity_)
this.log_.push("##teamcity[testSuiteFinished name='" + escapeTeamcityString(description) + "]");
},

reportSpecResults: function(spec) {
Expand Down

0 comments on commit b6d8361

Please sign in to comment.