Skip to content

Commit

Permalink
Initial Revision
Browse files Browse the repository at this point in the history
  • Loading branch information
Micah Silverman committed Feb 8, 2012
0 parents commit 10e5896
Show file tree
Hide file tree
Showing 18 changed files with 541 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
node_modules
*.tar.gz
node-*
logs
14 changes: 14 additions & 0 deletions Makefile
@@ -0,0 +1,14 @@
file=*.test.js
files=`find test/unit -name '$(file)' -type f -print0 | xargs -0 echo`

default: all

all: unit peanut

unit:
@NODE_PATH=test:lib expresso -t 250 -I test -I lib -s $(files)

peanut:
@peanut -s

.PHONY: unit peanut
56 changes: 56 additions & 0 deletions app.js
@@ -0,0 +1,56 @@

/**
* Module dependencies.
*/

var express = require('express');

var app = module.exports = express.createServer();

/**
* Globals.
*/

global.inspect = require('eyes').inspector({ maxLength: 1000 });
var envPath = './config/environments/' + app.settings.env;
global.settings = require(envPath + '/settings');

/**
* App configuration.
*/

app.configure(function(){
app.set('views', __dirname + '/lib/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: settings.cookieSecret }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.use(express.errorHandler());
});

/**
* Global Exception Handlers
*/

process.on('uncaughtException', function(err) {
console.error('Uncaught Exception: ' + err);
});

/**
* Require all controllers.
*/

require(__dirname + '/lib/controllers/site_controller')(app);

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
4 changes: 4 additions & 0 deletions config/environments/development/settings.js.sample
@@ -0,0 +1,4 @@
module.exports = {
cookieSecret: "b to the dd"
, tmpPath: "/tmp"
}
4 changes: 4 additions & 0 deletions config/environments/test/settings.js.sample
@@ -0,0 +1,4 @@
module.exports = {
cookieSecret: "b to the dd"
, tmpPath: "/tmp"
}
8 changes: 8 additions & 0 deletions features/home_page.feature
@@ -0,0 +1,8 @@
Feature: Home page
In order to learn BDD
As a node dev
I want to go to the BDD app home page

Scenario: Navigate to home page
When I open bdd.js
Then I should be welcomed by bdd.js
32 changes: 32 additions & 0 deletions features/step_definitions/home_page_steps.js
@@ -0,0 +1,32 @@
var soda = require('soda');

When(/^I open bdd.js$/, function(step) {
var self = this;
this.browser = soda.createClient({
host: 'localhost'
, port: 4444
, url: 'http://localhost:3000'
, browser: "googlechrome"
});

this.browser.session(function(err) {
self.browser.open('/', function(err, body) {
self.browser.waitForPageToLoad(5000, function(err) {
step.done();
})
})
})
});

Then(/^I should be welcomed by bdd.js$/, function(step) {
var self=this;
this.browser.assertElementPresent("xpath=//p[.='Welcome to bdd.js']",
function(err) {
if(err) console.log(err.message)
assert.ok(!err);
self.browser.testComplete(function(error, body) {
step.done();
});
}
);
});
20 changes: 20 additions & 0 deletions lib/controllers/site_controller.js
@@ -0,0 +1,20 @@
/**
* Module dependencies.
*/

/**
* controllers - site.
*/
module.exports = function(app) {
var routes = {
index: '/'
};

app.get(routes.index, function(req, res){
res.render('index', {
title: 'bdd.js'
});
});

return routes;
};
2 changes: 2 additions & 0 deletions lib/views/index.jade
@@ -0,0 +1,2 @@
h1= title
p Welcome to #{title}
6 changes: 6 additions & 0 deletions lib/views/layout.jade
@@ -0,0 +1,6 @@
!!!
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
19 changes: 19 additions & 0 deletions package.json
@@ -0,0 +1,19 @@
{
"name": "bdd.js"
, "version": "0.0.1"
, "description": "just your basic BDD framework for node.js"
, "author": "Didit <development@didit.com>"
, "dependencies": {
"express": "2.4.7"
, "jade": ">= 0.0.1"
, "eyes": "0.1.6"
, "soda": "0.2.4"
, "underscore": "1.2.2"
}
, "devDependencies": {
"jsdom": "0.2.9"
, "should": "0.3.2"
, "sinon": "1.2.0"
}
, "engines": { "node": "0.4.x" }
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
125 changes: 125 additions & 0 deletions setup.sh
@@ -0,0 +1,125 @@
#! /bin/bash

NODE_INSTALL_LOCATION="$HOME/local/node-v$1"
ENV_FILE="$HOME/.bashrc"

NODE_DOWNLOAD_LOCATION="http://nodejs.org/dist"

### Don't edit anything below this line

NODE_VERSION=$1
NODE_FOLDER="node-v"
NODE_VERSION_TARBALL=$NODE_FOLDER$NODE_VERSION.tar.gz

function check_error {
if [ $1 -ne 0 ]; then
echo $2
echo "You may need to cleanup:"
echo -e "\t$NODE_INSTALL_LOCATION/"
echo -e "\t$NODE_FOLDER$NODE_VERSION/"
echo -e "\tnode-$NODE_VERSION.tar.gz"
echo -e "\t$ENV_FILE"
exit 1
fi
}

function check_required {
C=`which $1`

check_error $? "$1 is required."
}

function check_node {
NEED_NODE=1

NODE=`which node`
if [ $? -eq 0 ]; then
CUR_NODE_VERSION=`node --version | cut -c2-`
if [ $CUR_NODE_VERSION = $NODE_VERSION ]; then
NEED_NODE=0
elif [ -d $NODE_INSTALL_LOCATION -a -f $NODE_INSTALL_LOCATION/bin/node ]; then
NEED_NODE=0
fi
fi

if [ $NEED_NODE -eq 1 ]; then
echo "Getting and building node..."
if [ ! -f "$NODE_VERSION_TARBALL" ]; then
wget -q "$NODE_DOWNLOAD_LOCATION/$NODE_VERSION_TARBALL"
if [ $? -ne 0 ]; then
wget -q "$NODE_DOWNLOAD_LOCATION/v$NODE_VERSION/$NODE_VERSION_TARBALL"
check_error $? "unable to download $NODE_VERSION_TARBALL"
fi
fi
tar xvfz $NODE_VERSION_TARBALL
cd $NODE_FOLDER$NODE_VERSION
./configure --prefix=$NODE_INSTALL_LOCATION
check_error $? "node configure failed"
make install
check_error $? "node install failed"
ENV_FIX=`grep .node_home.sh $ENV_FILE`
if [ $? -ne 0 ]; then
echo ". $HOME/.node_home.sh" >> $ENV_FILE
fi
fi
echo export PATH=$NODE_INSTALL_LOCATION/bin:\$PATH > $HOME/.node_home.sh
export PATH=$NODE_INSTALL_LOCATION/bin:$PATH
}

function check_npm {
NPM=`which npm | grep $NODE_VERSION`

if [ $? -ne 0 ]; then
echo "Installing npm..."
curl http://npmjs.org/install.sh | sh
check_error $? "npm install failed"
if [ -d $HOME/.npm ]; then
NOW=`date`
mv $HOME/.npm "$HOME/.npm-$NOW"
fi
fi
}

function check_expresso {
EXPRESSO=`which expresso | grep $NODE_VERSION`

if [ $? -ne 0 ]; then
echo "Installing expresso..."
npm install -g expresso
check_error $? "expresso install failed"
fi
}

function check_peanut {
PEANUT=`which peanut | grep $NODE_VERSION`

if [ $? -ne 0 ]; then
echo "Installing peanut..."
npm install -g peanut
check_error $? "peanut install failed"
fi
}

function check_selenium {
SELENIUM=`which selenium | grep $NODE_VERSION`

if [ $? -ne 0 ]; then
echo "Installing selenium for node..."
npm install -g selenium
fi
}

if [ -z $1 ]; then
echo -e "usage: $0 <node version>\n\tex: $0 0.4.7"
exit 1
fi
check_required curl
check_required wget
check_required tar
check_required make
check_required java
check_node
check_npm
check_expresso
check_peanut
check_selenium
76 changes: 76 additions & 0 deletions test/controller_test_helper.js
@@ -0,0 +1,76 @@
/**
* Module dependencies.
*/

var http = require('http');
var req = http.IncomingMessage.prototype;
var res = http.ServerResponse.prototype;
var views = require('./view_test_helper');

/**
* controller_test_helper
*/

var helper = exports;
var currentRoute;
var currentMiddleware;

var restoreRoute = function() {
if (!currentRoute) {
return;
}
currentRoute.middleware = currentMiddleware;
};

helper.finish = function() {
restoreRoute();
};

var getRoute = helper.getRoute = function(route, app) {
return app.get(route)[0] || app.post(route)[0];
};

helper.stubMiddlewareFor = function(route, app) {
currentRoute = getRoute(route, app);
currentMiddleware = currentRoute.middleware;
currentRoute.middleware = [];
};

helper.setDefaultLocals = function() {
var tools = []
res.local('currentUser', { permissions: { tools: tools } });
res.local('currentClients', []);
res.local('currentClient', {});
res.local('tools', tools);
};

helper.setLocal = function(name, value) {
res.local(name, value);
};

helper.assert = {
get: function(app, route, callback) {
assert.response(app,
{ url: route, method: 'GET' },
{ status: 200, body: /^(?!\w*Error:).+/ },
function(res) {
views.parseDom(res.body, callback);
});
},
getJson: function(app, route, callback) {
assert.response(app,
{ url: route, method: 'GET' },
{ status: 200, body: /^(?!\w*Error:).+/ },
function(res) {
callback(JSON.parse(res.body));
});
},
postJson: function(app, route, callback) {
assert.response(app,
{ url: route, method: 'POST' },
{ status: 200, body: /^(?!\w*Error:).+/ },
function(res) {
callback(JSON.parse(res.body));
});
}
};

0 comments on commit 10e5896

Please sign in to comment.