Commit c1d3de20 c1d3de208aa9324112beeee9d89ff1e2a589c167 by Nicolas Perriault

refs #132 - include files when running test suites

A new `--includes` option has been added to the `casper test` command,
allowing to pass a list of js/coffee scripts to include before each
suite execution:

    $ casperjs test --includes=foo.js,bar.js my_test_suite.js
1 parent 75286e4d
1 CasperJS Changelog 1 CasperJS Changelog
2 ================== 2 ==================
3 3
4 XXXX-XX-XX, v0.6.11
5 -------------------
6
7 - closed [#132](https://github.com/n1k0/casperjs/issues/132) - added ability to include js/coffee files using a dedicated option when using the `casper test` command
8
4 2012-06-04, v0.6.10 9 2012-06-04, v0.6.10
5 ------------------- 10 -------------------
6 11
......
1 Subproject commit 2a26b9af28b829c83bf374841b851bd0bb7a226a 1 Subproject commit 1429f8f52a334c4bef7a699c07cff43c66d81f29
......
...@@ -50,6 +50,7 @@ var Tester = function Tester(casper, options) { ...@@ -50,6 +50,7 @@ var Tester = function Tester(casper, options) {
50 50
51 this.currentTestFile = null; 51 this.currentTestFile = null;
52 this.exporter = require('xunit').create(); 52 this.exporter = require('xunit').create();
53 this.includes = [];
53 this.running = false; 54 this.running = false;
54 this.suites = []; 55 this.suites = [];
55 this.options = utils.mergeObjects({ 56 this.options = utils.mergeObjects({
...@@ -687,6 +688,9 @@ var Tester = function Tester(casper, options) { ...@@ -687,6 +688,9 @@ var Tester = function Tester(casper, options) {
687 this.runTest = function runTest(testFile) { 688 this.runTest = function runTest(testFile) {
688 this.bar(f('Test file: %s', testFile), 'INFO_BAR'); 689 this.bar(f('Test file: %s', testFile), 'INFO_BAR');
689 this.running = true; // this.running is set back to false with done() 690 this.running = true; // this.running is set back to false with done()
691 this.includes.forEach(function(include) {
692 phantom.injectJs(include);
693 });
690 this.exec(testFile); 694 this.exec(testFile);
691 }; 695 };
692 696
......
...@@ -354,3 +354,25 @@ function serialize(value) { ...@@ -354,3 +354,25 @@ function serialize(value) {
354 return JSON.stringify(value, null, 4); 354 return JSON.stringify(value, null, 4);
355 } 355 }
356 exports.serialize = serialize; 356 exports.serialize = serialize;
357
358 /**
359 * Returns unique values from an array.
360 *
361 * Note: ugly code is ugly, but efficient: http://jsperf.com/array-unique2/8
362 *
363 * @param Array array
364 * @return Array
365 */
366 function unique(array) {
367 var o = {},
368 r = [];
369 for (var i = 0, len = array.length; i !== len; i++) {
370 var d = array[i];
371 if (o[d] !== 1) {
372 o[d] = 1;
373 r[r.length] = d;
374 }
375 }
376 return r;
377 }
378 exports.unique = unique;
......
...@@ -6,15 +6,39 @@ if (!phantom.casperLoaded) { ...@@ -6,15 +6,39 @@ if (!phantom.casperLoaded) {
6 var fs = require('fs'); 6 var fs = require('fs');
7 var utils = require('utils'); 7 var utils = require('utils');
8 var f = utils.format; 8 var f = utils.format;
9 var includes = [];
10 var tests = [];
9 var casper = require('casper').create({ 11 var casper = require('casper').create({
10 exitOnError: false 12 exitOnError: false
11 }); 13 });
12 14
13 // Options from cli 15 // local utils
16 function checkIncludeFile(include) {
17 var absInclude = fs.absolute(include.trim());
18 if (!fs.exists(absInclude)) {
19 casper.warn("%s file not found, can't be included", absInclude);
20 return;
21 }
22 if (!utils.isJsFile(absInclude)) {
23 casper.warn("%s is not a supported file type, can't be included", absInclude);
24 return;
25 }
26 if (fs.isDirectory(absInclude)) {
27 casper.warn("%s is a directory, can't be included", absInclude);
28 return;
29 }
30 if (tests.indexOf(include) > -1 || tests.indexOf(absInclude) > -1) {
31 casper.warn("%s is a test file, can't be included", absInclude);
32 return;
33 }
34 return absInclude;
35 }
36
37 // parse some options from cli
14 casper.options.verbose = casper.cli.get('direct') || false; 38 casper.options.verbose = casper.cli.get('direct') || false;
15 casper.options.logLevel = casper.cli.get('log-level') || "error"; 39 casper.options.logLevel = casper.cli.get('log-level') || "error";
16 40
17 // Overriding Casper.open to prefix all test urls 41 // overriding Casper.open to prefix all test urls
18 casper.setFilter('open.location', function(location) { 42 casper.setFilter('open.location', function(location) {
19 if (!/^http/.test(location)) { 43 if (!/^http/.test(location)) {
20 return f('file://%s/%s', phantom.casperPath, location); 44 return f('file://%s/%s', phantom.casperPath, location);
...@@ -22,8 +46,7 @@ casper.setFilter('open.location', function(location) { ...@@ -22,8 +46,7 @@ casper.setFilter('open.location', function(location) {
22 return location; 46 return location;
23 }); 47 });
24 48
25 var tests = []; 49 // test paths are passed as args
26
27 if (casper.cli.args.length) { 50 if (casper.cli.args.length) {
28 tests = casper.cli.args.filter(function(path) { 51 tests = casper.cli.args.filter(function(path) {
29 return fs.isFile(path) || fs.isDirectory(path); 52 return fs.isFile(path) || fs.isDirectory(path);
...@@ -33,8 +56,21 @@ if (casper.cli.args.length) { ...@@ -33,8 +56,21 @@ if (casper.cli.args.length) {
33 casper.exit(1); 56 casper.exit(1);
34 } 57 }
35 58
59 // includes handling
60 if (casper.cli.has('includes')) {
61 includes = casper.cli.get('includes').split(',').map(function(include) {
62 // we can't use filter() directly because of abspath transformation
63 return checkIncludeFile(include);
64 }).filter(function(include) {
65 return utils.isString(include);
66 });
67 casper.test.includes = utils.unique(includes);
68 }
69
70 // test suites completion listener
36 casper.test.on('tests.complete', function() { 71 casper.test.on('tests.complete', function() {
37 this.renderResults(true, undefined, casper.cli.get('xunit') || undefined); 72 this.renderResults(true, undefined, casper.cli.get('xunit') || undefined);
38 }); 73 });
39 74
75 // run all the suites
40 casper.test.runSuites.apply(casper.test, tests); 76 casper.test.runSuites.apply(casper.test, tests);
......
...@@ -90,4 +90,29 @@ t.comment('mergeObjects()'); ...@@ -90,4 +90,29 @@ t.comment('mergeObjects()');
90 }); 90 });
91 })(); 91 })();
92 92
93 t.comment('unique()');
94 (function() {
95 testCases = [
96 {
97 input: [1,2,3],
98 output: [1,2,3]
99 },
100 {
101 input: [1,2,3,2,1],
102 output: [1,2,3]
103 },
104 {
105 input: ["foo", "bar", "foo"],
106 output: ["foo", "bar"]
107 },
108 {
109 input: [],
110 output: []
111 }
112 ];
113 testCases.forEach(function(testCase) {
114 t.assertEquals(utils.unique(testCase.input), testCase.output, 'unique() computes unique values of an array');
115 });
116 })();
117
93 t.done(); 118 t.done();
......