]>
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 .js tests).' )
19 . option ( '-o, --open-in-browser' , 'Open the generated HTML files in a web browser using the "open" module (must be used with the "-g"/"--generate-html" option).' )
20 . option ( '--output-html' , 'Instead of running the tests, just output the generated HTML source to STDOUT (should be used with .js tests)' )
21 . option ( '-d, --debug' , 'Show debug output (the "console" event) from the provider' )
22 . option ( '-r, --relative' , 'Use relative paths in the generated HTML file' )
23 . option ( '--debugger <port>' , 'Enable the remote debugger for CasperJS' )
26 if ( program
. tests
. length
=== 0 ) {
27 program
. tests
= fs
. readdirSync ( __dirname
). filter ( function ( f
) { return ( /^test\.(\w|\.|-)+\.js$/ ). test ( f
); });
28 program
. tests
= program
. tests
. map ( function ( f
) { return path
. resolve ( __dirname
, f
); }); // add full paths in
29 console
. log ( 'using files %s' , program
. tests
);
34 var all_js
= program
. tests
. reduce ( function ( a
, e
) { return a
&& e
. slice (- 3 ) == '.js' ; }, true );
36 var get_path = function ( /* arguments */ ) {
37 if ( program
. relative
) {
38 return path
. join
. apply ( null , arguments
);
40 var args
= Array
. prototype . slice
. call ( arguments
);
41 args
. unshift ( __dirname
, '..' );
42 return path
. resolve
. apply ( null , args
);
46 var get_path_cwd = function ( /* arguments */ ) {
47 if ( program
. relative
) {
48 var part_path
= path
. join
. apply ( null , arguments
);
49 return path
. relative ( path
. join ( __dirname
, '..' ), path
. resolve ( process
. cwd (), part_path
));
51 var args
= Array
. prototype . slice
. call ( arguments
);
52 args
. unshift ( process
. cwd ());
53 return path
. resolve
. apply ( null , args
);
57 if ( all_js
&& ! program
. autoInject
) {
60 // uses the first instance of the string 'requires local modules: '
61 program
. tests
. forEach ( function ( testname
) {
62 var full_path
= path
. resolve ( process
. cwd (), testname
);
63 var content
= fs
. readFileSync ( full_path
). toString ();
64 var ind
= content
. indexOf ( 'requires local modules: ' );
66 ind
+= 'requires local modules: ' . length
;
67 var eol
= content
. indexOf ( ' \n ' , ind
);
68 var modules
= content
. slice ( ind
, eol
). split ( /,\s*/ );
69 modules
. forEach ( function ( mod
) {
70 all_modules
[ get_path ( 'include/' , mod
) + '.js' ] = 1 ;
74 var fakes_ind
= content
. indexOf ( 'requires test modules: ' );
76 fakes_ind
+= 'requires test modules: ' . length
;
77 var fakes_eol
= content
. indexOf ( ' \n ' , fakes_ind
);
78 var fakes_modules
= content
. slice ( fakes_ind
, fakes_eol
). split ( /,\s*/ );
79 fakes_modules
. forEach ( function ( mod
) {
80 all_modules
[ get_path ( 'tests/' , mod
) + '.js' ] = 1 ;
85 program
. autoInject
= Object
. keys ( all_modules
);
88 if ( program
. autoInject
) {
89 var temp
= require ( 'temp' );
93 header
: "<html> \n <head> \n <meta charset='utf-8' /> \n <link rel='stylesheet' href='" + get_path ( 'node_modules/mocha/mocha.css' ) + "'/> \n </head> \n <body><div id='mocha'></div>" ,
94 script_tag : function ( p
) { return "<script src='" + p
+ "'></script>" ; },
95 footer
: "<script> \n mocha.checkLeaks(); \n mocha.globals(['navigator', 'create', 'ClientUtils', '__utils__']); \n mocha.run(function () { window.__mocha_done = true; }); \n </script> \n </body> \n </html>"
98 template
. header
+= " \n " + template
. script_tag ( get_path ( 'node_modules/chai/chai.js' ));
99 template
. header
+= " \n " + template
. script_tag ( get_path ( 'node_modules/mocha/mocha.js' ));
100 template
. header
+= " \n " + template
. script_tag ( get_path ( 'node_modules/sinon/pkg/sinon.js' ));
101 template
. header
+= " \n " + template
. script_tag ( get_path ( 'node_modules/sinon-chai/lib/sinon-chai.js' ));
102 template
. header
+= " \n " + template
. script_tag ( get_path ( 'node_modules/sinon-chai/lib/sinon-chai.js' ));
103 template
. header
+= " \n <script>mocha.setup('bdd');</script>" ;
106 template
. header
= program
. autoInject
. reduce ( function ( acc
, sn
) {
107 return acc
+ " \n " + template
. script_tag ( get_path_cwd ( sn
));
110 file_paths
= program
. tests
. map ( function ( jsn
, ind
) {
111 var templ
= template
. header
;
113 templ
+= template
. script_tag ( get_path_cwd ( jsn
));
114 templ
+= template
. footer
;
116 var tempfile
= temp
. openSync ({ prefix
: 'novnc-zombie-inject-' , suffix
: '-file_num-' + ind
+ '.html' });
117 fs
. writeSync ( tempfile
. fd
, templ
);
118 fs
. closeSync ( tempfile
. fd
);
119 return tempfile
. path
;
124 file_paths
= program
. tests
. map ( function ( fn
) {
125 return path
. resolve ( process
. cwd (), fn
);
129 var use_ansi
= false ;
130 if ( program
. color
) use_ansi
= true ;
131 else if ( program
. disableColor
) use_ansi
= false ;
132 else if ( process
. stdout
. isTTY
) use_ansi
= true ;
134 var cursor
= ansi ( process
. stdout
, { enabled
: use_ansi
});
136 if ( program
. outputHtml
) {
137 file_paths
. forEach ( function ( path
, path_ind
) {
138 fs
. readFile ( path
, function ( err
, data
) {
140 console
. warn ( error
. stack
);
147 . write ( program
. tests
[ path_ind
])
150 . write ( Array ( program
. tests
[ path_ind
]. length
+ 1 ). join ( '=' ))
161 if ( program
. generateHtml
) {
163 if ( program
. openInBrowser
) {
164 open_browser
= require ( 'open' );
167 file_paths
. forEach ( function ( path
, path_ind
) {
170 . write ( program
. tests
[ path_ind
])
176 if ( program
. openInBrowser
) {
183 if ( program
. generateHtml
) {
184 process
. stdin
. resume (); // pause until C-c
185 process
. on ( 'SIGINT' , function () {
186 process
. stdin
. pause (); // exit
190 if (! program
. outputHtml
&& ! program
. generateHtml
) {
191 var failure_count
= 0 ;
193 var prov
= require ( path
. resolve ( __dirname
, 'run_from_console.' + program
. provider
+ '.js' ));
196 . write ( "Running tests " )
198 . write ( program
. tests
. join ( ', ' ))
201 . write ( ' using provider ' + prov
. name
)
204 //console.log("Running tests %s using provider %s", program.tests.join(', '), prov.name);
206 var provider
= prov
. provide_emitter ( file_paths
, program
. debugger );
207 provider
. on ( 'test_ready' , function ( test_json
) {
210 filename
= program
. tests
[ test_json
. file_ind
];
213 console
. log ( 'Results for %s:' , filename
);
214 console
. log ( Array ( 'Results for :' . length
+ filename
. length
+ 1 ). join ( '=' ));
220 . write ( '' + test_json
. num_tests
+ ' tests run, ' )
222 . write ( '' + test_json
. num_passes
+ ' passed' );
223 if ( test_json
. num_slow
> 0 ) {
229 . write ( '' + test_json
. num_slow
+ ' slow' )
238 . write ( '' + test_json
. num_fails
+ ' failed' );
239 if ( test_json
. num_skipped
> 0 ) {
244 . write ( '' + test_json
. num_skipped
+ ' skipped' );
248 . write ( ' -- duration: ' + test_json
. duration
+ "s \n " );
252 if ( test_json
. num_fails
> 0 || program
. printAll
) {
253 var extract_error_lines = function ( err
) {
254 // the split is to avoid a weird thing where in PhantomJS where we get a stack trace too
255 var err_lines
= err
. split ( ' \n ' );
256 if ( err_lines
. length
== 1 ) {
260 for ( ind
= 0 ; ind
< err_lines
. length
; ind
++) {
261 var at_ind
= err_lines
[ ind
]. trim (). indexOf ( 'at ' );
267 return err_lines
. slice ( 0 , ind
). join ( ' \n ' );
271 var traverse_tree = function ( indentation
, node
) {
272 if ( node
. type
== 'suite' ) {
273 if (! node
. has_subfailures
&& ! program
. printAll
) return ;
275 if ( indentation
=== 0 ) {
277 console
. log ( node
. name
);
278 console
. log ( Array ( node
. name
. length
+ 1 ). join ( '-' ));
283 . write ( Array ( indentation
+ 3 ). join ( '#' ))
285 . write ( ' ' + node
. name
+ ' ' )
287 . write ( Array ( indentation
+ 3 ). join ( '#' ))
293 for ( var i
= 0 ; i
< node
. children
. length
; i
++) {
294 traverse_tree ( indentation
+ 1 , node
. children
[ i
]);
300 console
. log ( '- failed: ' + node
. text
+ test_json
. replay
);
302 console
. log ( ' ' + extract_error_lines ( node
. error
));
306 else if ( program
. printAll
) {
310 . write ( '- skipped: ' + node
. text
);
313 if ( node
. slow
) cursor
. yellow ();
317 . write ( '- pass: ' + node
. text
)
319 . write ( ' (' + node
. duration
+ ') ' );
321 /*if (node.slow) cursor.yellow();
322 else cursor.green();*/
324 //.write(test_json.replay)
332 for ( var i
= 0 ; i
< test_json
. suites
. length
; i
++) {
333 traverse_tree ( 0 , test_json
. suites
[ i
]);
337 if ( test_json
. num_fails
=== 0 ) {
339 console
. log ( 'all tests passed :-)' );
345 provider
. on ( 'console' , function ( line
) {
351 provider
. on ( 'error' , function ( line
) {
353 console
. error ( 'ERROR: ' + line
);
356 /*gprom.finally(function(ph) {
358 // exit with a status code that actually gives information
359 if (program.exitWithFailureCount) process.exit(failure_count);