Skip to content

Commit

Permalink
Code analysis fixes
Browse files Browse the repository at this point in the history
Fix some warnings found through code analysis.
  • Loading branch information
richtera committed Dec 23, 2012
1 parent 6020eee commit cf5bea2
Show file tree
Hide file tree
Showing 8 changed files with 2,951 additions and 94 deletions.
265 changes: 229 additions & 36 deletions .idea/workspace.xml

Large diffs are not rendered by default.

189 changes: 189 additions & 0 deletions JasmineAdapter-1.1.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* @fileoverview Jasmine JsTestDriver Adapter.
* @author [email protected] (Misko Hevery)
* @author [email protected] (Olmo Maldonado)
*/
(function(){


var Env = function(onTestDone, onComplete){
jasmine.Env.call(this);

this.specFilter = function(spec){
if (!this.exclusive) return true;
var blocks = spec.queue.blocks, l = blocks.length;
for (var i = 0; i < l; i++) if (blocks[i].func.exclusive >= this.exclusive) return true;
return false;
};

this.reporter = new Reporter(onTestDone, onComplete);
};
jasmine.util.inherit(Env, jasmine.Env);

// Here we store:
// 0: everyone runs
// 1: run everything under ddescribe
// 2: run only iits (ignore ddescribe)
Env.prototype.exclusive = 0;


Env.prototype.execute = function(){
collectMode = false;
playback();
jasmine.Env.prototype.execute.call(this);
};


var Reporter = function(onTestDone, onComplete){
this.onTestDone = onTestDone;
this.onComplete = onComplete;
this.reset();
};
jasmine.util.inherit(Reporter, jasmine.Reporter);


Reporter.formatStack = function(stack) {
var line, lines = (stack || '').split(/\r?\n/), l = lines.length, frames = [];
for (var i = 0; i < l; i++){
line = lines[i];
if (line.match(/\/jasmine[\.-]/)) continue;
frames.push(line.replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' '));
}
return frames.join('\n');
};


Reporter.prototype.reset = function(){
this.specLog = jstestdriver.console.log_ = [];
};


Reporter.prototype.log = function(str){
this.specLog.push(str);
};


Reporter.prototype.reportSpecStarting = function(){
this.reset();
this.start = +new Date();
};


Reporter.prototype.reportSpecResults = function(spec){
var elapsed = +new Date() - this.start, results = spec.results();

if (results.skipped) return;

var item, state = 'passed', items = results.getItems(), l = items.length, messages = [];
for (var i = 0; i < l; i++){
item = items[i];
if (item.passed()) continue;
state = (item.message.indexOf('AssertionError:') != -1) ? 'error' : 'failed';
messages.push({
message: item + '',
name: item.trace.name,
stack: Reporter.formatStack(item.trace.stack)
});
}

this.onTestDone(new jstestdriver.TestResult(
spec.suite.getFullName(),
spec.description,
state,
jstestdriver.angular.toJson(messages),
this.specLog.join('\n'),
elapsed
));
};


Reporter.prototype.reportRunnerResults = function(){
this.onComplete();
};


var collectMode = true, intercepted = {};

describe = intercept('describe');
beforeEach = intercept('beforeEach');
afterEach = intercept('afterEach');

var JASMINE_TYPE = 'jasmine test case';
TestCase('Jasmine Adapter Tests', null, JASMINE_TYPE);

jstestdriver.pluginRegistrar.register({

name: 'jasmine',

getTestRunsConfigurationFor: function(testCaseInfos, expressions, testRunsConfiguration) {
for (var i = 0; i < testCaseInfos.length; i++) {
if (testCaseInfos[i].getType() == JASMINE_TYPE) {
testRunsConfiguration.push(new jstestdriver.TestRunConfiguration(testCaseInfos[i], []));
}
}
return false; // allow other TestCases to be collected.
},

runTestConfiguration: function(config, onTestDone, onComplete){
if (config.getTestCaseInfo().getType() != JASMINE_TYPE) return false;
(jasmine.currentEnv_ = new Env(onTestDone, onComplete)).execute();
return true;
},

onTestsFinish: function(){
jasmine.currentEnv_ = null;
collectMode = true;
}

});

function intercept(method){
var bucket = intercepted[method] = [], method = window[method];
return function(desc, fn){
if (collectMode) bucket.push(function(){ method(desc, fn); });
else method(desc, fn);
};
}

function playback(){
for (var method in intercepted){
var bucket = intercepted[method];
for (var i = 0, l = bucket.length; i < l; i++) bucket[i]();
}
}

})();

var ddescribe = function(name, fn){
var env = jasmine.getEnv();
if (!env.exclusive) env.exclusive = 1; // run ddescribe only
describe(name, function(){
var oldIt = it;
it = function(name, fn){
fn.exclusive = 1; // run anything under ddescribe
env.it(name, fn);
};

try {
fn.call(this);
} finally {
it = oldIt;
};
});
};

var iit = function(name, fn){
var env = jasmine.getEnv();
env.exclusive = fn.exclusive = 2; // run only iits
env.it(name, fn);
};

// Patch Jasmine for proper stack traces
jasmine.Spec.prototype.fail = function (e) {
var result = new jasmine.ExpectationResult({
passed: false,
message: e ? jasmine.util.formatException(e) : 'Exception'
});
if(e) result.trace = e;
this.results_.addResult(result);
};
90 changes: 45 additions & 45 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,47 @@ var req = require('express/lib/request');
}
};

/**
* Queue flash `msg` of the given `type`.
*
* Examples:
*
* req.flash('info', 'email sent');
* req.flash('error', 'email delivery failed');
* req.flash('info', 'email re-sent');
* // => 2
*
* req.flash('info');
* // => ['email sent', 'email re-sent']
*
* req.flash('info');
* // => []
*
* req.flash();
* // => { error: ['email delivery failed'], info: [] }
*
* Formatting:
*
* Flash notifications also support arbitrary formatting support.
* For example you may pass variable arguments to `req.flash()`
* and use the %s specifier to be replaced by the associated argument:
*
* req.flash('info', 'email has been sent to %s.', userName);
*
* To add custom formatters use the `exports.flashFormatters` object.
*
* @param {String} type
* @param {String} msg
* @return {Array|Object|Number}
* @api public
*/

function miniMarkdown(str){
return String(str)
.replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>')
.replace(/(_|\*)(.*?)\1/g, '<em>$2</em>')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
};
}

/**
* Queue flash `msg` of the given `type`.
*
* Examples:
*
* req.flash('info', 'email sent');
* req.flash('error', 'email delivery failed');
* req.flash('info', 'email re-sent');
* // => 2
*
* req.flash('info');
* // => ['email sent', 'email re-sent']
*
* req.flash('info');
* // => []
*
* req.flash();
* // => { error: ['email delivery failed'], info: [] }
*
* Formatting:
*
* Flash notifications also support arbitrary formatting support.
* For example you may pass variable arguments to `req.flash()`
* and use the %s specifier to be replaced by the associated argument:
*
* req.flash('info', 'email has been sent to %s.', userName);
*
* To add custom formatters use the `exports.flashFormatters` object.
*
* @param {String} type
* @param {String} msg
* @return {Array|Object|Number}
* @api public
*/
req.flash = function(type, msg){
if (this.session === undefined) throw Error('req.flash() requires sessions');
var msgs = this.session.flash = this.session.flash || {};
Expand All @@ -70,6 +69,7 @@ var req = require('express/lib/request');
msg = msg.replace(/%([a-zA-Z])/g, function(_, format){
var formatter = formatters[format];
if (formatter) return formatter(utils.escape(args[i++]));
return null;
});
return (msgs[type] = msgs[type] || []).push(msg);
} else if (type) {
Expand All @@ -90,15 +90,14 @@ try {
}

var rootpath = process.cwd() + '/',
path = require('path'),
fs = require('fs'),
express = require('express'),
nodepath = require('path'),
stylus = require('stylus'),
colors = require('colors'),
calipso = require(path.join(rootpath, 'lib/calipso')),
translate = require(path.join(rootpath, 'i18n/translate')),
logo = require(path.join(rootpath, 'logo')),
nodepath = require('path'),
calipso = require(nodepath.join(rootpath, 'lib/calipso')),
translate = require(nodepath.join(rootpath, 'i18n/translate')),
logo = require(nodepath.join(rootpath, 'logo')),
everyauth = require("everyauth");

// To enable everyauth debugging.
Expand All @@ -119,10 +118,10 @@ function calipsoFindOrCreateUser(user, sess, promise) {
delete sess._pending;
return calipso.lib.user.createUserSession(req, null, user, function(err) {
if(err) { calipso.error("Error saving session: " + err); return promise.fail(err); }
promise.fulfill(user);
return promise.fulfill(user);
});
} else
promise.fulfill(user);
return promise.fulfill(user);
}

User.findOne({username:user.username}, function (err, u) {
Expand All @@ -138,11 +137,12 @@ function calipsoFindOrCreateUser(user, sess, promise) {

calipso.e.pre_emit('USER_CREATE',u);

u.save(function(err) {
return u.save(function(err) {
if (err) return promise.fail(err);
calipso.e.post_emit('USER_CREATE',u);
// If not already redirecting, then redirect
finishUser(u);
return null;
});
});
return promise;
Expand Down Expand Up @@ -326,7 +326,7 @@ function bootApplication(cluster, next) {
app.use(translate.translate(app.config.get('i18n:language'), app.config.get('i18n:languages'), app.config.get('i18n:additive')));

// Core calipso router
calipso.init(app, function() {
return calipso.init(app, function() {

// Add the calipso mw
app.use(calipso.routingFn());
Expand Down
Loading

0 comments on commit cf5bea2

Please sign in to comment.