Skip to content
This repository has been archived by the owner on Mar 17, 2020. It is now read-only.

Commit

Permalink
Fixed test run trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
benrady committed May 12, 2011
1 parent d2b206f commit 88d5174
Show file tree
Hide file tree
Showing 22 changed files with 1,749 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/jessie
Expand Up @@ -38,4 +38,4 @@ cli.main(function(args, options) {
jessie.run(args, options, function(fail) {
process.exit(fail ? 1 : 0)
})
})
})
10 changes: 10 additions & 0 deletions bin/jezebel
@@ -0,0 +1,10 @@
#!/usr/bin/env node

var path = require('path'),
fs = require('fs');
var binDir = path.dirname(fs.realpathSync(__filename));
var lib = path.join(binDir, '../lib');
require.paths.push(lib);

var jezebel = require(path.join(lib, 'jezebel'));
jezebel.run();
2 changes: 1 addition & 1 deletion lib/jessie.js
Expand Up @@ -9,4 +9,4 @@ exports.include = function(filename) {

exports.run = function(args, options, callback) {
new exports.runner(args, options, callback).run()
};
};
81 changes: 81 additions & 0 deletions lib/jezebel.js
@@ -0,0 +1,81 @@
var fs = require('fs'),
path = require('path'),
watcher = require('jezebel/watcher'),
repl = require('repl'),
childProcess = require('child_process'),
sys = require('sys');
var binDir = fs.realpathSync(path.dirname(__filename) + '/../bin');

var session;

function evalInRepl(statement) {
process.stdin.emit('data', new Buffer(statement));
}

function triggerTestRun() {
evalInRepl("runTests()\n");
}

function fileChanged(path, curr, prev) {
if (curr.mtime.getTime() !== prev.mtime.getTime()) {
triggerTestRun();
}
}

function runTests() {
// This _should_ work, and would be ideal, but I get very strange behavior
//
// var sandbox = {
// jessie: jessie,
// Buffer: Buffer,
// console: console
// };
// var script = "jessie.run(['spec'], {format: 'progress'}, function(fail) {console.log(fail);});"
// vm.runInNewContext(script, sandbox, 'jezebel-script');

// Running the top level script in a new context is kinda tricky :-(
//
//var scriptFile = binDir + '/jessie';
//var script = fs.readFileSync(scriptFile).toString();
//script = script.replace(/#.*/, '');
//var sandbox = {
// require:require,
// __filename: scriptFile,
// __dirname: binDir,
// process: {
// args: ['spec'],
// cwd: process.cwd,
// on: process.on,
// emit: process.emit
// }
//};
//vm.runInNewContext(script, sandbox, scriptFile);

// In process test running. Creates a class reloading problem. :-(
//
//jessie.run(['spec'], {format: 'progress'}, function(fail) {
// process.stdin.emit('data', new Buffer('\n'));
//})

// Out of process test running. Prevents direct access to test results :-(
var child = childProcess.spawn(binDir + '/jessie', ['spec']);
child.stdout.addListener("data", function (chunk) { chunk && sys.print(chunk) });
child.stderr.addListener("data", function (chunk) { chunk && sys.debug(chunk) });
child.on('exit', function() { evalInRepl("\n") });
}

function startRepl() {
session = repl.start("> ");
session.context.runTests = runTests;
}

// FIXME Hackity, hack, hack. For some reason, this module is not cached. Spying on a copy won't work.
exports.repl = repl;

exports.runTests = runTests;
exports.fileChanged = fileChanged;
exports.run = function(args, options) {
watcher.watchFiles(process.cwd(), fileChanged);
startRepl();
triggerTestRun();
};
36 changes: 36 additions & 0 deletions lib/jezebel/watcher.js
@@ -0,0 +1,36 @@
var fs = require('fs');
var fileExtensionPattern = new RegExp(".*\.(js)");

function watchGivenFile (watch, callback) {
fs.watchFile(watch, callback);
}

function watchFiles(path, callback) {
fs.stat(path, function(err, stats){
if (err) {
sys.error('Error retrieving stats for file: ' + path);
} else {
if (stats.isDirectory()) {
fs.readdir(path, function(err, fileNames) {
if(err) {
sys.puts('Error reading path: ' + path);
}
else {
fileNames.forEach(function (fileName) {
watchFiles(path + '/' + fileName, callback);
});
}
});
} else {
if (path.match(fileExtensionPattern)) {
watchGivenFile(path, function (curr, prev) {
callback(path, curr, prev);
});
}
}
}
});
}

exports.watchFiles = watchFiles;

172 changes: 172 additions & 0 deletions node_modules/cli/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions node_modules/cli/cli-min.js

Large diffs are not rendered by default.

0 comments on commit 88d5174

Please sign in to comment.