]>
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' );
5 var fs
= require ( 'fs' );
7 var make_list = function ( val
) {
12 . option ( '-t, --tests <testlist>' , 'Run the specified html-file-based test(s). \' testlist \' should be a comma-separated list' , make_list
, [])
13 . option ( '-a, --print-all' , 'Print all tests, not just the failures' )
14 . option ( '--disable-color' , 'Explicitly disable color' )
15 . option ( '-c, --color' , 'Explicitly enable color (default is to use color when not outputting to a pipe)' )
16 . 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 )
17 . option ( '-p, --provider <name>' , 'Use the given provider (defaults to "casper"). Currently, may be "casper" or "zombie"' , 'casper' )
18 . 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)' )
19 . option ( '-o, --output-html' , 'Instead of running the tests, just output the generated HTML source to STDOUT (should be used with -i)' )
20 . option ( '-d, --debug' , 'Show debug output (the "console" event) from the provider' )
23 if ( program
. tests
. length
=== 0 ) {
24 program
. tests
= fs
. readdirSync ( __dirname
). filter ( function ( f
) { return ( /^test\.(\w|\.|-)+\.js$/ ). test ( f
); });
25 console
. log ( 'using files %s' , program
. tests
);
30 if ( program
. autoInject
) {
31 var temp
= require ( 'temp' );
35 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>" ,
36 script_tag : function ( p
) { return "<script src='" + p
+ "'></script>" ; },
37 footer
: "<script> \n mocha.checkLeaks(); \n mocha.globals(['navigator', 'create', 'ClientUtils', '__utils__']); \n mocha.run(); \n </script> \n </body> \n </html>"
40 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/chai/chai.js' ));
41 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/mocha/mocha.js' ));
42 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/sinon/pkg/sinon.js' ));
43 template
. header
+= " \n " + template
. script_tag ( path
. resolve ( __dirname
, 'node_modules/sinon-chai/lib/sinon-chai.js' ));
44 template
. header
+= " \n <script>mocha.setup('bdd');</script>" ;
47 template
. header
= program
. autoInject
. reduce ( function ( acc
, sn
) {
48 return acc
+ " \n " + template
. script_tag ( path
. resolve ( process
. cwd (), sn
));
51 file_paths
= program
. tests
. map ( function ( jsn
, ind
) {
52 var templ
= template
. header
;
54 templ
+= template
. script_tag ( path
. resolve ( process
. cwd (), jsn
));
55 templ
+= template
. footer
;
57 var tempfile
= temp
. openSync ({ prefix
: 'novnc-zombie-inject-' , suffix
: '-file_num-' + ind
+ '.html' });
58 fs
. writeSync ( tempfile
. fd
, templ
);
59 fs
. closeSync ( tempfile
. fd
);
65 file_paths
= program
. tests
. map ( function ( fn
) {
66 return path
. resolve ( process
. cwd (), fn
);
71 if ( program
. color
) use_ansi
= true ;
72 else if ( program
. disableColor
) use_ansi
= false ;
73 else if ( process
. stdout
. isTTY
) use_ansi
= true ;
75 var cursor
= ansi ( process
. stdout
, { enabled
: use_ansi
});
77 if ( program
. outputHtml
) {
78 file_paths
. forEach ( function ( path
, path_ind
) {
79 fs
. readFile ( path
, function ( err
, data
) {
81 console
. warn ( error
. stack
);
87 . write ( program
. tests
[ path_ind
])
90 . write ( Array ( program
. tests
[ path_ind
]. length
+ 1 ). join ( '=' ))
98 if ( program
. generateHtml
) {
99 file_paths
. forEach ( function ( path
, path_ind
) {
102 . write ( program
. tests
[ path_ind
])
111 if ( program
. generateHtml
) {
112 process
. stdin
. resume (); // pause until C-c
113 process
. on ( 'SIGINT' , function () {
114 process
. stdin
. pause (); // exit
118 if (! program
. outputHtml
&& ! program
. generateHtml
) {
119 var failure_count
= 0 ;
121 var prov
= require ( path
. resolve ( __dirname
, 'run_from_console.' + program
. provider
+ '.js' ));
124 . write ( "Running tests " )
126 . write ( program
. tests
. join ( ', ' ))
129 . write ( ' using provider ' + prov
. name
)
132 //console.log("Running tests %s using provider %s", program.tests.join(', '), prov.name);
134 var provider
= prov
. provide_emitter ( file_paths
);
135 provider
. on ( 'test_ready' , function ( test_json
) {
138 filename
= program
. tests
[ test_json
. file_ind
];
141 console
. log ( 'Results for %s:' , filename
);
142 console
. log ( Array ( 'Results for :' . length
+ filename
. length
+ 1 ). join ( '=' ));
148 . write ( '' + test_json
. num_tests
+ ' tests run, ' )
150 . write ( '' + test_json
. num_passes
+ ' passed' );
151 if ( test_json
. num_slow
> 0 ) {
157 . write ( '' + test_json
. num_slow
+ ' slow' )
166 . write ( '' + test_json
. num_fails
+ ' failed' );
167 if ( test_json
. num_skipped
> 0 ) {
172 . write ( '' + test_json
. num_skipped
+ ' skipped' );
176 . write ( ' -- duration: ' + test_json
. duration
+ "s \n " );
180 if ( test_json
. num_fails
> 0 || program
. printAll
) {
181 var traverse_tree = function ( indentation
, node
) {
182 if ( node
. type
== 'suite' ) {
183 if (! node
. has_subfailures
&& ! program
. printAll
) return ;
185 if ( indentation
=== 0 ) {
187 console
. log ( node
. name
);
188 console
. log ( Array ( node
. name
. length
+ 1 ). join ( '-' ));
193 . write ( Array ( indentation
+ 3 ). join ( '#' ))
195 . write ( ' ' + node
. name
+ ' ' )
197 . write ( Array ( indentation
+ 3 ). join ( '#' ))
203 for ( var i
= 0 ; i
< node
. children
. length
; i
++) {
204 traverse_tree ( indentation
+ 1 , node
. children
[ i
]);
210 console
. log ( '- failed: ' + node
. text
+ test_json
. replay
);
212 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
216 else if ( program
. printAll
) {
220 . write ( '- skipped: ' + node
. text
);
223 if ( node
. slow
) cursor
. yellow ();
227 . write ( '- pass: ' + node
. text
)
229 . write ( ' (' + node
. duration
+ ') ' );
231 /*if (node.slow) cursor.yellow();
232 else cursor.green();*/
234 //.write(test_json.replay)
242 for ( var i
= 0 ; i
< test_json
. suites
. length
; i
++) {
243 traverse_tree ( 0 , test_json
. suites
[ i
]);
247 if ( test_json
. num_fails
=== 0 ) {
249 console
. log ( 'all tests passed :-)' );
255 provider
. on ( 'console' , function ( line
) {
260 /*gprom.finally(function(ph) {
262 // exit with a status code that actually gives information
263 if (program.exitWithFailureCount) process.exit(failure_count);