Commit 35a1cff6 35a1cff6c8e7868884badd5ea5b19edafd9a4234 by Nicolas Perriault

introducing casper cli arguments handling

1 parent 602fd2b5
...@@ -32,16 +32,39 @@ ...@@ -32,16 +32,39 @@
32 return Array.prototype.join.call(arguments, this.separator); 32 return Array.prototype.join.call(arguments, this.separator);
33 }; 33 };
34 34
35 // seeking for casperPath passed as an argument 35 phantom.extractCasperArgs = function(cliArgs) {
36 phantom.args.forEach(function(arg) { 36 var extract = { args: [], options: {} };
37 var pathMatch = arg.match(/--casper-path=(.+)/); 37 cliArgs.forEach(function(arg) {
38 if (pathMatch) { 38 if (arg.indexOf('--') === 0) {
39 phantom.casperPath = pathMatch[1]; 39 // named option
40 } 40 var optionMatch = arg.match(/^--(.*)=(.*)/i);
41 }); 41 if (optionMatch) {
42 extract.options[optionMatch[1]] = optionMatch[2];
43 } else {
44 // flag
45 var flagMatch = arg.match(/^--(.*)/);
46 if (flagMatch) {
47 extract.options[flagMatch[1]] = true;
48 }
49 }
50 } else {
51 // positional arg
52 extract.args.push(arg);
53 }
54 });
55 return extract;
56 };
42 57
43 if (!phantom.casperPath || !fs.isDirectory(phantom.casperPath)) { 58 phantom.casperArgs = phantom.extractCasperArgs(phantom.args);
59 phantom.casperPath = phantom.casperArgs.options['casper-path'];
60 //console.log(JSON.stringify(phantom.casperArgs, null, 4))
61
62 if (!phantom.casperPath) {
44 console.log('Cannot find CasperJS home path. Did you set phantom.casperPath or pass the --casper-path option?'); 63 console.log('Cannot find CasperJS home path. Did you set phantom.casperPath or pass the --casper-path option?');
64 phantom.exit(1);
65 } else if (!fs.isDirectory(phantom.casperPath)) {
66 console.log('Invalid CasperJS path: ' + phantom.casperPath);
67 phantom.exit(1);
45 } 68 }
46 69
47 [ 70 [
...@@ -56,6 +79,18 @@ ...@@ -56,6 +79,18 @@
56 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'lib', lib)); 79 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'lib', lib));
57 }); 80 });
58 81
59 phantom.injectJs(phantom.args[1]); 82 if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
83 console.log('Usage: casperjs script.(js|coffee) [options...]');
84 console.log('Read the docs http://n1k0.github.com/casperjs/');
85 phantom.exit(0);
86 }
87
88 phantom.casperScript = phantom.casperArgs.args[0];
89
90 if (!fs.isFile(phantom.casperScript)) {
91 console.log('Unable to open file: ' + phantom.casperScript);
92 phantom.exit(1);
93 }
60 94
95 phantom.injectJs(phantom.casperScript);
61 })(phantom); 96 })(phantom);
......
1 if (!phantom.casperPath || !phantom.Casper) {
2 console.log('This script must be invoked using the casperjs executable');
3 phantom.exit(1);
4 }
5
1 var fs = require('fs'); 6 var fs = require('fs');
2 7
3 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'lib', 'vendors', 'esprima.js')); 8 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'lib', 'vendors', 'esprima.js'));
...@@ -7,13 +12,6 @@ var casper = new phantom.Casper({ ...@@ -7,13 +12,6 @@ var casper = new phantom.Casper({
7 verbose: true 12 verbose: true
8 }); 13 });
9 14
10 var tests = [];
11 if (phantom.args.length > 2 && fs.isFile(phantom.args[2])) {
12 tests = [phantom.args[2]];
13 } else {
14 tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))];
15 }
16
17 // Overriding Casper.open to prefix all test urls 15 // Overriding Casper.open to prefix all test urls
18 phantom.Casper.extend({ 16 phantom.Casper.extend({
19 open: function(location, options) { 17 open: function(location, options) {
...@@ -25,4 +23,12 @@ phantom.Casper.extend({ ...@@ -25,4 +23,12 @@ phantom.Casper.extend({
25 } 23 }
26 }); 24 });
27 25
28 casper.test.runSuites.apply(casper.test, tests); 26 (function(casper) {
27 var tests = [];
28 if (phantom.args.length > 2 && fs.isFile(phantom.args[2])) {
29 tests = [phantom.args[2]];
30 } else {
31 tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))];
32 }
33 casper.test.runSuites.apply(casper.test, tests);
34 })(casper);
......
1 (function(t, phantom) {
2 t.comment('phantom.extractCliArgs()');
3
4 t.assertEquals(phantom.extractCasperArgs([]), {
5 args: [],
6 options: {}
7 }, 'extractCliArgs() returns a formatted object');
8
9 t.assertEquals(phantom.extractCasperArgs(['foo', 'bar']), {
10 args: ['foo', 'bar'],
11 options: {}
12 }, 'extractCliArgs() can extract args');
13
14 t.assertEquals(phantom.extractCasperArgs(['--foo=bar', '--baz']), {
15 args: [],
16 options: { foo: 'bar', baz: true }
17 }, 'extractCliArgs() can extract options and flags');
18
19 t.assertEquals(phantom.extractCasperArgs(['--&é"à=42']), {
20 args: [],
21 options: { '&é"à': '42' }
22 }, 'extractCliArgs() can extract exotic option name');
23
24 t.assertEquals(phantom.extractCasperArgs(['foo', 'bar', '--universe=42', '--chucknorris']), {
25 args: ['foo', 'bar'],
26 options: { universe: '42', chucknorris: true }
27 }, 'extractCliArgs() can extract args, options and flags');
28
29 t.assertEquals(phantom.extractCasperArgs(['bar', '--universe=42', '--chucknorris', 'foo']), {
30 args: ['bar', 'foo'],
31 options: { universe: '42', chucknorris: true }
32 }, 'extractCliArgs() positional args order is preserved, option one has no importance');
33
34 t.done();
35 })(casper.test, phantom);