Commit 35a1cff6 35a1cff6c8e7868884badd5ea5b19edafd9a4234 by Nicolas Perriault

introducing casper cli arguments handling

1 parent 602fd2b5
......@@ -32,16 +32,39 @@
return Array.prototype.join.call(arguments, this.separator);
};
// seeking for casperPath passed as an argument
phantom.args.forEach(function(arg) {
var pathMatch = arg.match(/--casper-path=(.+)/);
if (pathMatch) {
phantom.casperPath = pathMatch[1];
}
});
phantom.extractCasperArgs = function(cliArgs) {
var extract = { args: [], options: {} };
cliArgs.forEach(function(arg) {
if (arg.indexOf('--') === 0) {
// named option
var optionMatch = arg.match(/^--(.*)=(.*)/i);
if (optionMatch) {
extract.options[optionMatch[1]] = optionMatch[2];
} else {
// flag
var flagMatch = arg.match(/^--(.*)/);
if (flagMatch) {
extract.options[flagMatch[1]] = true;
}
}
} else {
// positional arg
extract.args.push(arg);
}
});
return extract;
};
if (!phantom.casperPath || !fs.isDirectory(phantom.casperPath)) {
phantom.casperArgs = phantom.extractCasperArgs(phantom.args);
phantom.casperPath = phantom.casperArgs.options['casper-path'];
//console.log(JSON.stringify(phantom.casperArgs, null, 4))
if (!phantom.casperPath) {
console.log('Cannot find CasperJS home path. Did you set phantom.casperPath or pass the --casper-path option?');
phantom.exit(1);
} else if (!fs.isDirectory(phantom.casperPath)) {
console.log('Invalid CasperJS path: ' + phantom.casperPath);
phantom.exit(1);
}
[
......@@ -56,6 +79,18 @@
phantom.injectJs(fs.pathJoin(phantom.casperPath, 'lib', lib));
});
phantom.injectJs(phantom.args[1]);
if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
console.log('Usage: casperjs script.(js|coffee) [options...]');
console.log('Read the docs http://n1k0.github.com/casperjs/');
phantom.exit(0);
}
phantom.casperScript = phantom.casperArgs.args[0];
if (!fs.isFile(phantom.casperScript)) {
console.log('Unable to open file: ' + phantom.casperScript);
phantom.exit(1);
}
phantom.injectJs(phantom.casperScript);
})(phantom);
......
if (!phantom.casperPath || !phantom.Casper) {
console.log('This script must be invoked using the casperjs executable');
phantom.exit(1);
}
var fs = require('fs');
phantom.injectJs(fs.pathJoin(phantom.casperPath, 'lib', 'vendors', 'esprima.js'));
......@@ -7,13 +12,6 @@ var casper = new phantom.Casper({
verbose: true
});
var tests = [];
if (phantom.args.length > 2 && fs.isFile(phantom.args[2])) {
tests = [phantom.args[2]];
} else {
tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))];
}
// Overriding Casper.open to prefix all test urls
phantom.Casper.extend({
open: function(location, options) {
......@@ -25,4 +23,12 @@ phantom.Casper.extend({
}
});
casper.test.runSuites.apply(casper.test, tests);
(function(casper) {
var tests = [];
if (phantom.args.length > 2 && fs.isFile(phantom.args[2])) {
tests = [phantom.args[2]];
} else {
tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))];
}
casper.test.runSuites.apply(casper.test, tests);
})(casper);
......
(function(t, phantom) {
t.comment('phantom.extractCliArgs()');
t.assertEquals(phantom.extractCasperArgs([]), {
args: [],
options: {}
}, 'extractCliArgs() returns a formatted object');
t.assertEquals(phantom.extractCasperArgs(['foo', 'bar']), {
args: ['foo', 'bar'],
options: {}
}, 'extractCliArgs() can extract args');
t.assertEquals(phantom.extractCasperArgs(['--foo=bar', '--baz']), {
args: [],
options: { foo: 'bar', baz: true }
}, 'extractCliArgs() can extract options and flags');
t.assertEquals(phantom.extractCasperArgs(['--&é"à=42']), {
args: [],
options: { '&é"à': '42' }
}, 'extractCliArgs() can extract exotic option name');
t.assertEquals(phantom.extractCasperArgs(['foo', 'bar', '--universe=42', '--chucknorris']), {
args: ['foo', 'bar'],
options: { universe: '42', chucknorris: true }
}, 'extractCliArgs() can extract args, options and flags');
t.assertEquals(phantom.extractCasperArgs(['bar', '--universe=42', '--chucknorris', 'foo']), {
args: ['bar', 'foo'],
options: { universe: '42', chucknorris: true }
}, 'extractCliArgs() positional args order is preserved, option one has no importance');
t.done();
})(casper.test, phantom);