]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/run_from_console.js
2 var ansi
= require ( 'ansi' );
3 var program
= require ( 'commander' );
4 var path
= require ( 'path' );
6 var make_list = function ( val
) {
11 . option ( '-t, --tests <testlist>' , 'Run the specified html-file-based test(s). \' testlist \' should be a comma-separated list' , make_list
, [])
12 . option ( '-a, --print-all' , 'Print all tests, not just the failures' )
13 . option ( '--disable-color' , 'Explicitly disable color' )
14 . option ( '-c, --color' , 'Explicitly enable color (default is to use color when not outputting to a pipe)' )
15 . option ( '-i, --auto-inject <includefiles>' , 'Treat the test list as a set of mocha JS files, and automatically generate HTML files with which to test test. \' includefiles \' should be a comma-separated list of paths to javascript files to include in each of the generated HTML files' , make_list
, null )
16 . option ( '-p, --provider <name>' , 'Use the given provider (defaults to "casper"). Currently, may be "casper" or "zombie"' , 'casper' )
17 . option ( '-g, --generate-html' , 'Instead of running the tests, just return the path to the generated HTML file, then wait for user interaction to exit (should be used with -i)' )
18 . option ( '-o, --output-html' , 'Instead of running the tests, just output the generated HTML source to STDOUT (should be used with -i)' )
19 . option ( '-d, --debug' , 'Show debug output (the "console" event) from the provider' )
24 if ( program
. autoInject
) {
25 var temp
= require ( 'temp' );
26 var fs
= require ( 'fs' );
30 header
: "<html> \n <head> \n <meta charset='utf-8' /> \n <link rel='stylesheet' href='" + path
. resolve ( __dirname
, 'node_modules/mocha/mocha.css' ) + "'/> \n </head> \n <body><div id='mocha'></div>" ,
31 script_tag : function ( p
) { return "<script src='" + p
+ "'></script>" ; },
32 footer
: "<script> \n mocha.checkLeaks(); \n mocha.globals(['navigator', 'create', 'ClientUtils', '__utils__']); \n mocha.run(); \n </script> \n </body> \n </html>"
35 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/chai/chai.js' ));
36 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/mocha/mocha.js' ));
37 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/sinon/pkg/sinon.js' ));
38 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/sinon-chai/lib/sinon-chai.js' ));
39 template
. header
+= " \n <script>mocha.setup('bdd');</script>" ;
42 template
. header
= program
. autoInject
. reduce ( function ( acc
, sn
) {
43 return acc
+ " \n " + template
. script_tag ( path
. resolve ( process
. cwd (), sn
));
46 file_paths
= program
. tests
. map ( function ( jsn
, ind
) {
47 var templ
= template
. header
;
49 templ
+= template
. script_tag ( path
. resolve ( process
. cwd (), jsn
));
50 templ
+= template
. footer
;
52 var tempfile
= temp
. openSync ({ prefix
: 'novnc-zombie-inject-' , suffix
: '-file_num-' + ind
+ '.html' });
53 fs
. writeSync ( tempfile
. fd
, templ
);
54 fs
. closeSync ( tempfile
. fd
);
60 file_paths
= program
. tests
. map ( function ( fn
) {
61 return path
. resolve ( process
. cwd (), fn
);
66 if ( program
. color
) use_ansi
= true ;
67 else if ( program
. disableColor
) use_ansi
= false ;
68 else if ( process
. stdout
. isTTY
) use_ansi
= true ;
70 var cursor
= ansi ( process
. stdout
, { enabled
: use_ansi
});
72 if ( program
. outputHtml
) {
73 var fs
= require ( 'fs' );
74 file_paths
. forEach ( function ( path
, path_ind
) {
75 fs
. readFile ( path
, function ( err
, data
) {
77 console
. warn ( error
. stack
);
83 . write ( program
. tests
[ path_ind
])
86 . write ( Array ( program
. tests
[ path_ind
]. length
+ 1 ). join ( '=' ))
94 if ( program
. generateHtml
) {
95 file_paths
. forEach ( function ( path
, path_ind
) {
98 . write ( program
. tests
[ path_ind
])
107 if ( program
. generateHtml
) {
108 process
. stdin
. resume (); // pause until C-c
109 process
. on ( 'SIGINT' , function () {
110 process
. stdin
. pause (); // exit
114 if (! program
. outputHtml
&& ! program
. generateHtml
) {
115 var failure_count
= 0 ;
117 var prov
= require ( path
. resolve ( __dirname
, 'run_from_console.' + program
. provider
+ '.js' ));
120 . write ( "Running tests " )
122 . write ( program
. tests
. join ( ', ' ))
125 . write ( ' using provider ' + prov
. name
)
128 //console.log("Running tests %s using provider %s", program.tests.join(', '), prov.name);
130 var provider
= prov
. provide_emitter ( file_paths
);
131 provider
. on ( 'test_ready' , function ( test_json
) {
134 filename
= program
. tests
[ test_json
. file_ind
];
137 console
. log ( 'Results for %s:' , filename
);
138 console
. log ( Array ( 'Results for :' . length
+ filename
. length
+ 1 ). join ( '=' ));
144 . write ( '' + test_json
. num_tests
+ ' tests run, ' )
146 . write ( '' + test_json
. num_passes
+ ' passed' );
147 if ( test_json
. num_slow
> 0 ) {
153 . write ( '' + test_json
. num_slow
+ ' slow' )
162 . write ( '' + test_json
. num_fails
+ ' failed' );
163 if ( test_json
. num_skipped
> 0 ) {
168 . write ( '' + test_json
. num_skipped
+ ' skipped' );
172 . write ( ' -- duration: ' + test_json
. duration
+ "s \n " );
176 if ( test_json
. num_fails
> 0 || program
. printAll
) {
177 var traverse_tree = function ( indentation
, node
) {
178 if ( node
. type
== 'suite' ) {
179 if (! node
. has_subfailures
&& ! program
. printAll
) return ;
181 if ( indentation
=== 0 ) {
183 console
. log ( node
. name
);
184 console
. log ( Array ( node
. name
. length
+ 1 ). join ( '-' ));
189 . write ( Array ( indentation
+ 3 ). join ( '#' ))
191 . write ( ' ' + node
. name
+ ' ' )
193 . write ( Array ( indentation
+ 3 ). join ( '#' ))
199 for ( var i
= 0 ; i
< node
. children
. length
; i
++) {
200 traverse_tree ( indentation
+ 1 , node
. children
[ i
]);
206 console
. log ( '- failed: ' + node
. text
+ test_json
. replay
);
208 console
. log ( ' ' + node
. error
. split ( " \n " )[ 0 ]); // the split is to avoid a weird thing where in PhantomJS where we get a stack trace too
212 else if ( program
. printAll
) {
216 . write ( '- skipped: ' + node
. text
);
219 if ( node
. slow
) cursor
. yellow ();
223 . write ( '- pass: ' + node
. text
)
225 . write ( ' (' + node
. duration
+ ') ' );
227 /*if (node.slow) cursor.yellow();
228 else cursor.green();*/
230 //.write(test_json.replay)
238 for ( var i
= 0 ; i
< test_json
. suites
. length
; i
++) {
239 traverse_tree ( 0 , test_json
. suites
[ i
]);
243 if ( test_json
. num_fails
=== 0 ) {
245 console
. log ( 'all tests passed :-)' );
251 provider
. on ( 'console' , function ( line
) {
256 /*gprom.finally(function(ph) {
258 // exit with a status code that actually gives information
259 if (program.exitWithFailureCount) process.exit(failure_count);