Debian Codegraph @

rhino/testsrc/tests/runRhinoTests.pl

raw
1 #!/usr/bin/perl5
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6 #
7 # simple script that executes JavaScript tests. you have to build the
8 # stand-alone, js shell executable (which is not the same as the dll that gets
9 # built for mozilla). see the readme at
10 # http://lxr.mozilla.org/mozilla/source/js/src/README.html for instructions on
11 # how to build the jsshell.
12 #
13 # this is just a quick-n-dirty script. for full reporting, you need to run
14 # the test driver, which requires java and is currently not available on
15 # mozilla.org.
16 #
17 # this test looks for an executable JavaScript shell in
18 # %MOZ_SRC/mozilla/js/src/[platform]-[platform-version]-OPT.OBJ/js,
19 # which is the default build location when you build using the instructions
20 # at http://lxr.mozilla.org/mozilla/source/js/src/README.html
21 #
22 #
23 # [email protected]
24 #
25
26 &parse_args;
27 &setup_env;
28 &main_test_loop;
29 &cleanup_env;
30
31 #
32 # given a main directory, assume that there is a file called 'shell.js'
33 # in it. then, open all the subdirectories, and look for js files.
34 # for each test.js that is found, execute the shell, and pass shell.js
35 # and the test.js as file arguments. redirect all process output to a
36 # file.
37 #
38 sub main_test_loop {
39 foreach $suite ( &get_subdirs( $test_dir )) {
40 foreach $subdir (&get_subdirs( $suite, $test_dir )) {
41 @jsfiles = &get_js_files($subdir);
42 execute_js_tests(@jsfiles);
43 }
44 }
45 }
46
47 #
48 # given a directory, return an array of all subdirectories
49 #
50 sub get_subdirs{
51 local ($dir, $path) = @_;
52 local @subdirs;
53
54 local $dir_path = $path . $dir;
55 chdir $dir_path;
56
57 opendir ( DIR, ${dir_path} );
58 local @testdir_contents = readdir( DIR );
59 closedir( DIR );
60
61 foreach (@testdir_contents) {
62 if ( (-d $_) && ($_ !~ 'CVS') && ( $_ ne '.') && ($_ ne '..')) {
63 @subdirs[$#subdirs+1] = $_;
64 }
65 }
66 chdir $path;
67 return @subdirs;
68 }
69
70 #
71 # given a directory, return an array of all the js files that are in it.
72 #
73 sub get_js_files {
74 ( $test_subdir ) = @_;
75 local @js_file_array;
76
77 $current_test_dir = $test_dir ."/". $suite . "/" .$test_subdir;
78 chdir $current_test_dir;
79
80 opendir ( TEST_SUBDIR, ${current_test_dir} );
81 @subdir_files = readdir( TEST_SUBDIR );
82 closedir( TOP_LEVEL_BUILD_DIR );
83
84 foreach ( @subdir_files ) {
85 if ( $_ =~ /\.js$/ ) {
86 $js_file_array[$#js_file_array+1] = $_;
87 }
88 }
89
90 return @js_file_array;
91 }
92
93 #
94 # given an array of test.js files, execute the shell command and pass
95 # the shell.js and test.js files as file arguments. redirect process
96 # output to a file. if $js_verbose is set (not recommended), write all
97 # testcase output to the output file. if $js_quiet is set, only write
98 # failed test case information to the output file. the default setting
99 # is to write a line for each test file, and whether each file passed
100 # or failed.
101 #
102 sub execute_js_tests {
103 (@js_file_array) = @_;
104
105 $js_printed_suitename = 0;
106 if ( !$js_quiet ) {
107 &js_print_suitename;
108 }
109
110 foreach $js_test (@js_file_array) {
111 $js_printed_filename = 0;
112 $js_test_bugnumber = 0;
113 $runtime_error = "";
114
115 local $passed = -1;
116
117 # create the test command
118 $test_command =
119 $shell_command .
120 " -f $test_dir/$suite/shell.js " .
121 " $test_dir/$suite/$subdir/$js_test";
122
123 if ( !$js_quiet ) {
124 &js_print_filename;
125 } else {
126 print '.';
127 }
128
129 $test_path = $test_dir ."/" . $suite ."/". $test_subdir ."/". $js_test;
130
131 local $log = "";
132
133 if ( !-e $test_path ) {
134 &js_print( " FAILED! file not found\n",
135 "<font color=#990000>", "</font><br>\n");
136 } else {
137 open( RUNNING_TEST, "$test_command" . ' 2>&1 |');
138
139 while( <RUNNING_TEST> ){
140 $log .= $_;
141 if ( $js_verbose && !$js_quiet ) {
142 &js_print ($_ ."\n", "", "<br>\n");
143 }
144 if ( $_ =~ /PASSED/ && $passed == -1) {
145 $passed = 1;
146 }
147 if ( $_ =~ /BUGNUMBER/ ) {
148 $js_test_bugnumber = $_;
149 }
150 if ( $_ =~ /FAILED/ && $_ =~ /expected/) {
151 &js_print_suitename;
152 &js_print_filename;
153 &js_print_bugnumber;
154
155 local @msg = split ( "FAILED", $_ );
156 &js_print ( $passed ? "\n" : "" );
157 &js_print( " " . $msg[0], "&nbsp;&nbsp;<tt>" );
158 &js_print( "FAILED", "<font color=#990000>", "</font>");
159 &js_print( $msg[1], "", "</tt><br>\n" );
160 $passed = 0;
161 }
162 if ( $_ =~ /$js_test/ ) {
163 $runtime_error .= $_;
164 }
165 }
166 close( RUNNING_TEST );
167
168 #
169 # figure out whether the test passed or failed. print out an
170 # appropriate level of output based on the value of $js_quiet
171 #
172 if ( $js_test =~ /-n\.js$/ ) {
173 if ( $runtime_error ) {
174 if ( !$js_quiet ) {
175 &js_print( " PASSED!\n ",
176 "<font color=#009900>&nbsp;&nbsp",
177 "</font><br>" );
178 if ( $js_errors ) {
179 &js_print( $runtime_error, "<pre>", "</pre>");
180 }
181 }
182 } else {
183 &js_print_suitename;
184 &js_print_filename;
185 &js_print_bugnumber;
186 &js_print( " FAILED! ", "&nbsp;&nbsp;<font color=#990000>",
187 "</font>");
188 &js_print( " Should have resulted in an error\n",
189 "","<br>" );
190 }
191 } else {
192 if ( $passed == 1 && !$js_quiet) {
193 &js_print( " PASSED!\n " , "&nbsp;&nbsp;<font color=#009900>",
194 "</font><br>" );
195 } else {
196 if ($passed == -1) {
197 &js_print_suitename;
198 &js_print_filename;
199 &js_print_bugnumber;
200 &js_print( " FAILED!\n " , "&nbsp;&nbsp;<font color=#990000>",
201 "</font><br>" );
202 &js_print( " Missing 'PASSED' in output\n", "","<br>" );
203 &js_print( $log, "output:<br><pre>", "</pre>" );
204 }
205 }
206 }
207 }
208 }
209 }
210
211 #
212 # figure out what os we're on, the default name of the object directory
213 #
214 sub setup_env {
215 # MOZ_SRC must be set, so we can figure out where the
216 # JavaScript executable is
217 $moz_src = $ENV{"MOZ_SRC"}
218 || die( "You need to set your MOZ_SRC environment variable.\n" );
219 $src_dir = $moz_src . '/mozilla/js/src/';
220
221 # JS_TEST_DIR must be set so we can figure out where the tests are.
222 $test_dir = $ENV{"JS_TEST_DIR"};
223
224 # if it's not set, look for it relative to $moz_src
225 if ( !test_dir ) {
226 $test_dir = $moz_src . '/mozilla/js/tests/';
227 }
228
229 # make sure that the test dir exists
230 if ( ! -e $test_dir ) {
231 die "The JavaScript Test Library could not be found.\n" .
232 "Check the tests out from /mozilla/js/tests or\n" .
233 "Set the value of your JS_TEST_DIR environment variable\n " .
234 "to the location of the test library.\n";
235 }
236
237 # make sure that the test dir ends with a trailing slash
238 $test_dir .= '/';
239
240 chdir $src_dir;
241
242 $shell_command = "java";
243 if ($js_classpath) {
244 $shell_command .= " -classpath " . $js_classpath;
245 }
246 $shell_command .= " org.mozilla.javascript.tools.shell.Main ";
247
248 # set the output file name. let's base its name on the date and platform,
249 # and give it a sequence number.
250
251 if ( $get_output ) {
252 $js_output = &get_output;
253 }
254 if ($js_output) {
255 print( "Writing results to $js_output\n" );
256 chdir $test_dir;
257 open( JS_OUTPUT, "> ${js_output}" ) ||
258 die "Can't open log file $js_output\n";
259 close JS_OUTPUT;
260 }
261
262 # get the start time
263 $start_time = time;
264
265 # print out some nice stuff
266 $start_date = &get_date;
267 &js_print( "JavaScript tests started: " . $start_date, "<p><tt>", "</tt></p>" );
268
269 &js_print ("Executing all the tests under $test_dir\n against " .
270 "$shell_command\n", "<p><tt>", "</tt></p>" );
271 }
272
273 #
274 # parse arguments. see usage for what arguments are expected.
275 #
276 sub parse_args {
277 $i = 0;
278 while( $i < @ARGV ){
279 if ( $ARGV[$i] eq '--threaded' ) {
280 $js_threaded = 1;
281 } elsif ( $ARGV[$i] eq '--d' ) {
282 $js_debug = 1;
283 } elsif ( $ARGV[$i] eq '--14' ) {
284 $js_version = '14';
285 } elsif ( $ARGV[$i] eq '--v' ) {
286 $js_verbose = 1;
287 } elsif ( $ARGV[$i] eq '-f' ) {
288 $js_output = $ARGV[++$i];
289 } elsif ( $ARGV[$i] eq '-c' ) {
290 $js_classpath = $ARGV[++$i];
291 } elsif ( $ARGV[$i] eq '--o' ) {
292 $get_output = 1;
293 } elsif ($ARGV[$i] eq '--e' ) {
294 $js_errors = 1;
295 } elsif ($ARGV[$i] eq '--q' ) {
296 $js_quiet = 1;
297 } elsif ($ARGV[$i] eq '--h' ) {
298 die &usage;
299 } elsif ( $ARGV[$i] eq '-E' ) {
300 $js_exe_full_path = $ARGV[$i+1];
301 $i++;
302 } else {
303 die &usage;
304 }
305 $i++;
306 }
307
308 #
309 # if no output options are provided, show some output and write to file
310 #
311 if ( !$js_verbose && !$js_output && !$get_output ) {
312 $get_output = 1;
313 }
314 }
315
316 #
317 # print the arguments that this script expects
318 #
319 sub usage {
320 die ("usage: $0\n" .
321 "--q Quiet mode -- only show information for tests that failed\n".
322 "--e Show runtime error messages for negative tests\n" .
323 "--v Verbose output -- show all test cases (not recommended)\n" .
324 "--o Send output to file whose generated name is based on date\n".
325 "--d Look for a debug JavaScript executable (default is optimized)\n" .
326 "-f <file> Redirect output to file named <file>\n"
327 );
328 }
329
330 #
331 # if $js_output is set, print to file as well as stdout
332 #
333 sub js_print {
334 ($string, $start_tag, $end_tag) = @_;
335
336 if ($js_output) {
337 open( JS_OUTPUT, ">> ${js_output}" ) ||
338 die "Can't open log file $js_output\n";
339
340 print JS_OUTPUT "$start_tag $string $end_tag";
341 close JS_OUTPUT;
342 }
343 print $string;
344 }
345
346 #
347 # close open files
348 #
349 sub cleanup_env {
350 # print out some nice stuff
351 $end_date = &get_date;
352 &js_print( "\nTests complete at $end_date", "<hr><tt>", "</tt>" );
353
354 # print out how long it took to complete
355 $end_time = time;
356
357 $test_seconds = ( $end_time - $start_time );
358
359 &js_print( "Start Date: $start_date\n", "<tt><br>" );
360 &js_print( "End Date: $end_date\n", "<br>" );
361 &js_print( "Test Time: $test_seconds seconds\n", "<br>" );
362
363 if ($js_output ) {
364 if ( !$js_verbose) {
365 &js_print( "Results were written to " . $js_output ."\n",
366 "<br>", "</tt>" );
367 }
368 close JS_OUTPUT;
369 }
370 }
371
372
373 #
374 # get the current date and time
375 #
376 sub get_date {
377 &get_localtime;
378 $now = $year ."/". $mon ."/". $mday ." ". $hour .":".
379 $min .":". $sec ."\n";
380 return $now;
381
382 }
383 sub get_localtime {
384 ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
385 localtime;
386 $mon++;
387 $mon = &zero_pad($mon);
388 $year= ($year < 2000) ? "19" . $year : $year;
389 $mday= &zero_pad($mday);
390 $sec = &zero_pad($sec);
391 $min = &zero_pad($min);
392 $hour = &zero_pad($hour);
393 }
394 sub zero_pad {
395 local ($string) = @_;
396 $string = ($string < 10) ? "0" . $string : $string;
397 return $string;
398 }
399
400 #
401 # generate an output file name based on the date
402 #
403 sub get_output {
404 &get_localtime;
405
406 chdir $test_dir;
407
408 $js_output = $test_dir ."/". $year .'-'. $mon .'-'. $mday ."\.1.html";
409
410 $output_file_found = 0;
411
412 while ( !$output_file_found ) {
413 if ( -e $js_output ) {
414 # get the last sequence number - everything after the dot
415 @seq_no = split( /\./, $js_output, 2 );
416 $js_output = $seq_no[0] .".". (++$seq_no[1]) . "\.html";
417 } else {
418 $output_file_found = 1;
419 }
420 }
421 return $js_output;
422 }
423
424 sub js_print_suitename {
425 if ( !$js_printed_suitename ) {
426 &js_print( "$suite\\$subdir\n", "<hr><font size+=1><b>",
427 "</font></b><br>" );
428 }
429 $js_printed_suitename = 1;
430 }
431
432 sub js_print_filename {
433 if ( !$js_printed_filename ) {
434 &js_print( "$js_test\n", "<b>", "</b>" );
435 $js_printed_filename = 1;
436 }
437 }
438
439 sub js_print_bugnumber {
440 if ( !$js_printed_bugnumber ) {
441 if ( $js_bugnumber =~ /^http/ ) {
442 &js_print( "$js_bugnumber", "<a href=$js_bugnumber>", "</a>" );
443 } else {
444 &js_print( "$js_bugnumber",
445 "<a href=http://scopus.mcom.com/bugsplat/show_bug.cgi?id=" .
446 $js_bugnumber .">",
447 "</a>" );
448 }
449 $js_printed_bugnumber = 1;
450 }
451 }