Permalink
Browse files

Cleanup server and move arduino logic to own file

  • Loading branch information...
1 parent af52eb7 commit c4163be78a0e70a73674404d1a2685ce2c7623ab @theycallmeswift committed Oct 22, 2012
Showing with 145 additions and 6 deletions.
  1. +14 −4 server.js
  2. +62 −2 test_bot.js
  3. +69 −0 testuino.js
View
@@ -1,8 +1,10 @@
var util = require('util')
, test_bot = require('./test_bot')
+ , Testuino = require('./testuino')
+ , testuino = new Testuino
, express = require('express')
, app = express()
- , port = process.env.PORT || 3000;
+ , port = process.env.PORT || 3000
app.configure(function() {
app.use(express.bodyParser());
@@ -15,6 +17,8 @@ app.post('/github', function(req, res) {
return res.send("ERROR");
}
+ testuino.reset();
+
var payload = JSON.parse(req.body.payload)
, name = payload.repository.name
, url = "git@github.com:" + payload.repository.owner.name + '/' + name + '.git'
@@ -25,12 +29,18 @@ app.post('/github', function(req, res) {
return res.send("OK");
}
+ testuino.pending();
util.log("Testing commit by " + lastCommit.author.name + " for " + url);
- test_bot.test(name, url, lastCommit.id, console.log);
+
+ test_bot.test(name, url, lastCommit.id, function(err, pass) {
+ testuino.finish(!err && pass);
+ });
res.send("OK");
});
-app.listen(port, function() {
- console.log("Listening on port " + port);
+testuino.on('ready', function() {
+ app.listen(port, function() {
+ util.log("Listening on port " + port);
+ });
});
View
@@ -1,3 +1,10 @@
+/* test_bot.js
+ *
+ * This is a *very* simple test bot that clones down a
+ * specified repo, checks out a hash, and runs the tests.
+ * Right now, only ruby apps are supported. The rake
+ * tasks that are run are "rake spec" and "rake cucumber".
+ */
var async = require('async')
, exec = require('child_process').exec
, spawn = require('child_process').spawn
@@ -7,11 +14,19 @@ var async = require('async')
, fs = require('fs')
, tmpDir = path.join(os.tmpDir(), 'testuino');
+// Ensure we have a directory to store the cloned repos
if(!fs.existsSync(tmpDir)) {
util.log("Creating tmp dir: " + tmpDir);
fs.mkdirSync(tmpDir);
}
+/* clearRepo
+ *
+ * Clear any existing files to prevent any wierd error states
+ *
+ * @param localPath path to the local repo
+ * @param cb callback(error, message)
+ */
function clearRepo(localPath, cb) {
fs.exists(localPath, function(exists) {
if(!exists) return cb(null, 'Fresh clone');
@@ -21,21 +36,54 @@ function clearRepo(localPath, cb) {
});
}
-function cloneRepo(url, localPath, cb) {
+/* cloneRepo
+ *
+ * Clone down the latest copy of the repo
+ *
+ * @param localPath path to local repo
+ * @param url url of the git repo to clone
+ * @param cb callback(err, message)
+ */
+function cloneRepo(localPath, url, cb) {
util.log("Cloning " + url);
exec('git clone ' + url + ' ' + localPath, { env: process.env }, cb);
}
+/* checkoutCommit
+ *
+ * Check out the latest commit to test
+ *
+ * @param localPath path to local repo
+ * @param commitHash hash of the commit to be tested
+ * @param cb callback(err, message)
+ */
function checkoutCommit(localPath, commitHash, cb) {
util.log("Checking out " + commitHash);
exec('git checkout ' + commitHash, { cwd: localPath, env: process.env }, cb);
}
+/* bundleInstall
+ *
+ * Install any dependencies via bundler. Right now only
+ * ruby apps that use bundler are supported.
+ *
+ * @param localPath path to local repo
+ * @param cb callback(error, message)
+ */
function bundleInstall(localPath, cb) {
util.log("Installing dependencies");
exec('bundle install', { cwd: localPath, env: process.env }, cb);
}
+/* runTests
+ *
+ * Run the tests of a specified type via rake. Sets the
+ * RAILS_ENV to "test".
+ *
+ * @param localPath path to local repo
+ * @param type rake task to run (spec, cucumber, etc)
+ * @param cb callback(err, message)
+ */
function runTests(localPath, type, cb) {
var tests = spawn('bundle', ['exec', 'rake', type, 'RAILS_ENV=test'], { cwd: localPath, env: process.env });
@@ -48,12 +96,24 @@ function runTests(localPath, type, cb) {
});
}
+/* test
+ *
+ * The public interface for the test bot. Clears any existing
+ * files in the local repo, clones down a fresh copy, checks
+ * out a specified commit hash, bundle installs any dependencies,
+ * and runs the specs and features for the repo.
+ *
+ * @param name name of the repositiory
+ * @param url url to clone from
+ * @param commit commit hash to be tested
+ * @param cb callback(error, pass)
+ */
function test(name, url, commit, cb) {
var localDir = path.join(tmpDir, name);
async.series([
async.apply(clearRepo, localDir),
- async.apply(cloneRepo, url, localDir),
+ async.apply(cloneRepo, localDir, url),
async.apply(checkoutCommit, localDir, commit),
async.apply(bundleInstall, localDir)
], function(err, messages) {
View
@@ -0,0 +1,69 @@
+var five = require("johnny-five")
+ , EventEmitter = require('events').EventEmitter
+ , util = require('util');
+
+function Testuino() {
+ var self = this;
+ self._board = new five.Board()
+ self._board.on("ready", function() {
+ self._loadLeds()
+ self.emit('ready');
+ });
+
+ EventEmitter.call(this);
+}
+
+util.inherits(Testuino, EventEmitter);
+
+Testuino.prototype._loadLeds = function() {
+ this._green = new five.Led(13);
+ this._yellow = new five.Led(12);
+ this._red = new five.Led(11);
+ this._buzz = new five.Led(10);
+};
+
+/* reset
+ *
+ * Turn the Leds and Buzzer off.
+ */
+Testuino.prototype.reset = function() {
+ var self = this;
+ ["_green", "_yellow", "_red", "_buzz"].forEach(function(sensor) {
+ self[sensor].stop().off();
+ });
+};
+
+/* pending
+ *
+ * Strobe the yellow Led because a build is pending
+ */
+Testuino.prototype.pending = function() {
+ this._yellow.strobe(300);
+ this.emit('pending');
+}
+
+/* finish
+ *
+ * Stops the yellow Led from stobing, rings the buzzer for
+ * one second and then turns on the green or red Led depending
+ * if the build passed or failed respectivly.
+ *
+ * @param pass did the build pass or fail
+ */
+Testuino.prototype.finish = function(pass) {
+ var self = this;
+
+ self._yellow.stop().off();
+ self._buzz.on();
+ setTimeout(function() { self._buzz.off() }, 1000);
+
+ if(pass) {
+ self._green.on();
+ } else {
+ self._red.on();
+ }
+
+ self.emit('finished');
+}
+
+module.exports = Testuino;

0 comments on commit c4163be

Please sign in to comment.