Skip to content

Commit

Permalink
(api) watch mode is activated with -w
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Jun 11, 2010
1 parent 1cdfd1c commit 90e0bae
Showing 1 changed file with 57 additions and 43 deletions.
100 changes: 57 additions & 43 deletions bin/vows
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var _reporter = require('vows/reporters/dot-matrix'), reporter = {

var options = {
reporter: reporter,
matcher: /.*/
matcher: /.*/,
watch: false
};

var suites = [];
Expand All @@ -27,6 +28,10 @@ var suites = [];
// ('node' in most cases)
var arg, args = [], argv = process.argv.slice(2);

// Current directory index,
// and path of test folder.
var root, testFolder;

//
// Parse command-line parameters
//
Expand Down Expand Up @@ -60,19 +65,37 @@ while (arg = argv.shift()) {
case 'v':
options.verbose = true;
break;
case 'watch':
case 'w':
options.watch = true;
break;
}
}
}
}

if (args.length === 0) {
if (options.watch) {
options.reporter = reporter = require('vows/reporters/watch');
}

msg('bin', 'argv', args);
msg('bin', 'options', { reporter: options.reporter.name, matcher: options.matcher });

if (args.length > 0) {
if (args.length === 0) {
root = fs.readdirSync('.');

if (root.indexOf('test') !== -1) {
testFolder = 'test';
} else if (root.indexOf('spec') !== -1) {
testFolder = 'spec';
} else {
throw new(Error)("Couldn't find test folder");
}

args = paths(testFolder);
}

if (! options.watch) {
reporter.report = function (data) {
switch (data[0]) {
case 'subject':
Expand Down Expand Up @@ -120,20 +143,10 @@ if (args.length > 0) {
var status,
cue,
current = 0,
testFolder,
running = 0,
lastRun,
colors = ['32m', '33m', '31m'],
timer = setInterval(tick, 100),
root = fs.readdirSync('.');

if (root.indexOf('test') !== -1) {
testFolder = 'test';
} else if (root.indexOf('spec') !== -1) {
testFolder = 'spec';
} else {
throw new(Error)("Couldn't find test folder");
}
timer = setInterval(tick, 100);

process.addListener('uncaughtException', cleanup);
process.addListener('exit', cleanup);
Expand Down Expand Up @@ -215,35 +228,6 @@ if (args.length > 0) {
running --;
});
}
//
// Recursively traverse a hierarchy, returning
// a list of all relevant .js files.
//
function paths(dir) {
var paths = [];

try { fs.statSync(dir) }
catch (e) { return [] }

(function traverse(dir, stack) {
stack.push(dir);
fs.readdirSync(stack.join('/')).forEach(function (file) {
var path = stack.concat([file]).join('/'),
stat = fs.statSync(path);

if (file[0] == '.' || file === 'vendor') {
return;
} else if (stat.isFile() && /\.js$/.test(file)) {
paths.push(path);
} else if (stat.isDirectory()) {
traverse(file, stack);
}
});
stack.pop();
})(dir || '.', []);

return paths;
}

//
// Watch all relevant files in lib/ and src/,
Expand Down Expand Up @@ -288,6 +272,36 @@ function runSuites(suites, callback) {
})(suites, callback);
}

//
// Recursively traverse a hierarchy, returning
// a list of all relevant .js files.
//
function paths(dir) {
var paths = [];

try { fs.statSync(dir) }
catch (e) { return [] }

(function traverse(dir, stack) {
stack.push(dir);
fs.readdirSync(stack.join('/')).forEach(function (file) {
var path = stack.concat([file]).join('/'),
stat = fs.statSync(path);

if (file[0] == '.' || file === 'vendor') {
return;
} else if (stat.isFile() && /\.js$/.test(file)) {
paths.push(path);
} else if (stat.isDirectory()) {
traverse(file, stack);
}
});
stack.pop();
})(dir || '.', []);

return paths;
}

function msg(cmd, subject, str, p) {
if (options.verbose) {
sys[p ? 'print' : 'puts']( stylize('vows ', 'green')
Expand Down

0 comments on commit 90e0bae

Please sign in to comment.