Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Finished basic test_bot

  • Loading branch information...
commit af52eb7330578747f14f5948aee9c8f540186e9a 1 parent fb64eb9
@theycallmeswift authored
Showing with 91 additions and 3 deletions.
  1. +1 −0  .gitignore
  2. +2 −1  package.json
  3. +10 −2 server.js
  4. +78 −0 test_bot.js
View
1  .gitignore
@@ -3,3 +3,4 @@ node_modules/
*.orig
.DS_Store
*.working
+config.json
View
3  package.json
@@ -17,6 +17,7 @@
"license": "Beerware",
"dependencies": {
"express": "~3.0.0rc5",
- "johnny-five": "~0.5.14"
+ "johnny-five": "~0.5.14",
+ "async": "~0.1.22"
}
}
View
12 server.js
@@ -1,4 +1,5 @@
var util = require('util')
+ , test_bot = require('./test_bot')
, express = require('express')
, app = express()
, port = process.env.PORT || 3000;
@@ -15,10 +16,17 @@ app.post('/github', function(req, res) {
}
var payload = JSON.parse(req.body.payload)
- , url = payload.repository.url
- , lastCommit = payload.commits[0];
+ , name = payload.repository.name
+ , url = "git@github.com:" + payload.repository.owner.name + '/' + name + '.git'
+ , lastCommit = payload.commits[payload.commits.length - 1];
+
+ if(!lastCommit) {
+ util.log("Nothing to commit");
+ return res.send("OK");
+ }
util.log("Testing commit by " + lastCommit.author.name + " for " + url);
+ test_bot.test(name, url, lastCommit.id, console.log);
res.send("OK");
});
View
78 test_bot.js
@@ -0,0 +1,78 @@
+var async = require('async')
+ , exec = require('child_process').exec
+ , spawn = require('child_process').spawn
+ , os = require('os')
+ , util = require('util')
+ , path = require('path')
+ , fs = require('fs')
+ , tmpDir = path.join(os.tmpDir(), 'testuino');
+
+if(!fs.existsSync(tmpDir)) {
+ util.log("Creating tmp dir: " + tmpDir);
+ fs.mkdirSync(tmpDir);
+}
+
+function clearRepo(localPath, cb) {
+ fs.exists(localPath, function(exists) {
+ if(!exists) return cb(null, 'Fresh clone');
+
+ util.log("Wiping " + localPath);
+ exec( 'rm -rf ' + localPath, cb);
+ });
+}
+
+function cloneRepo(url, localPath, cb) {
+ util.log("Cloning " + url);
+ exec('git clone ' + url + ' ' + localPath, { env: process.env }, cb);
+}
+
+function checkoutCommit(localPath, commitHash, cb) {
+ util.log("Checking out " + commitHash);
+ exec('git checkout ' + commitHash, { cwd: localPath, env: process.env }, cb);
+}
+
+function bundleInstall(localPath, cb) {
+ util.log("Installing dependencies");
+ exec('bundle install', { cwd: localPath, env: process.env }, cb);
+}
+
+function runTests(localPath, type, cb) {
+ var tests = spawn('bundle', ['exec', 'rake', type, 'RAILS_ENV=test'], { cwd: localPath, env: process.env });
+
+ tests.on('exit', function (code) {
+ if(code === 0) {
+ return cb(null, true);
+ }
+
+ return cb(null, false);
+ });
+}
+
+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(checkoutCommit, localDir, commit),
+ async.apply(bundleInstall, localDir)
+ ], function(err, messages) {
+ if(err) return cb(err);
+
+ util.log("Running specs for " + name + "/" + commit);
+ runTests(localDir, 'spec', function(err, pass) {
+ if(err) return cb(err);
+ if(!pass) return cb(err, false, "Specs");
+
+ util.log("Running features for " + name + "/" + commit);
+ runTests(localDir, 'cucumber', function(err, pass) {
+ if(err) return cb(err);
+ if(!pass) return cb(err, false, "Features");
+
+ return cb(null, true);
+ });
+ });
+ });
+}
+
+module.exports.test = test;
Please sign in to comment.
Something went wrong with that request. Please try again.