Always use JSBool (not bool sometimes, JSBool others) as JSVAL_IS_BOOLEAN's native...
[mozilla-central.git] / js / tests / runRhinoTests.pl
blob906323cab336de204dcd61f347fbad73674c0a3d
1 #!/usr/bin/perl5
3 # simple script that executes JavaScript tests. you have to build the
4 # stand-alone, js shell executable (which is not the same as the dll that gets
5 # built for mozilla). see the readme at
6 # http://lxr.mozilla.org/mozilla/source/js/src/README.html for instructions on
7 # how to build the jsshell.
9 # this is just a quick-n-dirty script. for full reporting, you need to run
10 # the test driver, which requires java and is currently not available on
11 # mozilla.org.
13 # this test looks for an executable JavaScript shell in
14 # %MOZ_SRC/mozilla/js/src/[platform]-[platform-version]-OPT.OBJ/js,
15 # which is the default build location when you build using the instructions
16 # at http://lxr.mozilla.org/mozilla/source/js/src/README.html
19 # christine@netscape.com
22 &parse_args;
23 &setup_env;
24 &main_test_loop;
25 &cleanup_env;
28 # given a main directory, assume that there is a file called 'shell.js'
29 # in it. then, open all the subdirectories, and look for js files.
30 # for each test.js that is found, execute the shell, and pass shell.js
31 # and the test.js as file arguments. redirect all process output to a
32 # file.
34 sub main_test_loop {
35 foreach $suite ( &get_subdirs( $test_dir )) {
36 foreach $subdir (&get_subdirs( $suite, $test_dir )) {
37 @jsfiles = &get_js_files($subdir);
38 execute_js_tests(@jsfiles);
44 # given a directory, return an array of all subdirectories
46 sub get_subdirs{
47 local ($dir, $path) = @_;
48 local @subdirs;
50 local $dir_path = $path . $dir;
51 chdir $dir_path;
53 opendir ( DIR, ${dir_path} );
54 local @testdir_contents = readdir( DIR );
55 closedir( DIR );
57 foreach (@testdir_contents) {
58 if ( (-d $_) && ($_ !~ 'CVS') && ( $_ ne '.') && ($_ ne '..')) {
59 @subdirs[$#subdirs+1] = $_;
62 chdir $path;
63 return @subdirs;
67 # given a directory, return an array of all the js files that are in it.
69 sub get_js_files {
70 ( $test_subdir ) = @_;
71 local @js_file_array;
73 $current_test_dir = $test_dir ."/". $suite . "/" .$test_subdir;
74 chdir $current_test_dir;
76 opendir ( TEST_SUBDIR, ${current_test_dir} );
77 @subdir_files = readdir( TEST_SUBDIR );
78 closedir( TOP_LEVEL_BUILD_DIR );
80 foreach ( @subdir_files ) {
81 if ( $_ =~ /\.js$/ ) {
82 $js_file_array[$#js_file_array+1] = $_;
86 return @js_file_array;
90 # given an array of test.js files, execute the shell command and pass
91 # the shell.js and test.js files as file arguments. redirect process
92 # output to a file. if $js_verbose is set (not recommended), write all
93 # testcase output to the output file. if $js_quiet is set, only write
94 # failed test case information to the output file. the default setting
95 # is to write a line for each test file, and whether each file passed
96 # or failed.
98 sub execute_js_tests {
99 (@js_file_array) = @_;
101 $js_printed_suitename = 0;
102 if ( !$js_quiet ) {
103 &js_print_suitename;
106 foreach $js_test (@js_file_array) {
107 $js_printed_filename = 0;
108 $js_test_bugnumber = 0;
109 $runtime_error = "";
111 local $passed = -1;
113 # create the test command
114 $test_command =
115 $shell_command .
116 " -f $test_dir/$suite/shell.js " .
117 " $test_dir/$suite/$subdir/$js_test";
119 if ( !$js_quiet ) {
120 &js_print_filename;
121 } else {
122 print '.';
125 $test_path = $test_dir ."/" . $suite ."/". $test_subdir ."/". $js_test;
127 local $log = "";
129 if ( !-e $test_path ) {
130 &js_print( " FAILED! file not found\n",
131 "<font color=#990000>", "</font><br>\n");
132 } else {
133 open( RUNNING_TEST, "$test_command" . ' 2>&1 |');
135 while( <RUNNING_TEST> ){
136 $log .= $_;
137 if ( $js_verbose && !$js_quiet ) {
138 &js_print ($_ ."\n", "", "<br>\n");
140 if ( $_ =~ /PASSED/ && $passed == -1) {
141 $passed = 1;
143 if ( $_ =~ /BUGNUMBER/ ) {
144 $js_test_bugnumber = $_;
146 if ( $_ =~ /FAILED/ && $_ =~ /expected/) {
147 &js_print_suitename;
148 &js_print_filename;
149 &js_print_bugnumber;
151 local @msg = split ( "FAILED", $_ );
152 &js_print ( $passed ? "\n" : "" );
153 &js_print( " " . $msg[0], "&nbsp;&nbsp;<tt>" );
154 &js_print( "FAILED", "<font color=#990000>", "</font>");
155 &js_print( $msg[1], "", "</tt><br>\n" );
156 $passed = 0;
158 if ( $_ =~ /$js_test/ ) {
159 $runtime_error .= $_;
162 close( RUNNING_TEST );
165 # figure out whether the test passed or failed. print out an
166 # appropriate level of output based on the value of $js_quiet
168 if ( $js_test =~ /-n\.js$/ ) {
169 if ( $runtime_error ) {
170 if ( !$js_quiet ) {
171 &js_print( " PASSED!\n ",
172 "<font color=#009900>&nbsp;&nbsp",
173 "</font><br>" );
174 if ( $js_errors ) {
175 &js_print( $runtime_error, "<pre>", "</pre>");
178 } else {
179 &js_print_suitename;
180 &js_print_filename;
181 &js_print_bugnumber;
182 &js_print( " FAILED! ", "&nbsp;&nbsp;<font color=#990000>",
183 "</font>");
184 &js_print( " Should have resulted in an error\n",
185 "","<br>" );
187 } else {
188 if ( $passed == 1 && !$js_quiet) {
189 &js_print( " PASSED!\n " , "&nbsp;&nbsp;<font color=#009900>",
190 "</font><br>" );
191 } else {
192 if ($passed == -1) {
193 &js_print_suitename;
194 &js_print_filename;
195 &js_print_bugnumber;
196 &js_print( " FAILED!\n " , "&nbsp;&nbsp;<font color=#990000>",
197 "</font><br>" );
198 &js_print( " Missing 'PASSED' in output\n", "","<br>" );
199 &js_print( $log, "output:<br><pre>", "</pre>" );
208 # figure out what os we're on, the default name of the object directory
210 sub setup_env {
211 # MOZ_SRC must be set, so we can figure out where the
212 # JavaScript executable is
213 $moz_src = $ENV{"MOZ_SRC"}
214 || die( "You need to set your MOZ_SRC environment variable.\n" );
215 $src_dir = $moz_src . '/mozilla/js/src/';
217 # JS_TEST_DIR must be set so we can figure out where the tests are.
218 $test_dir = $ENV{"JS_TEST_DIR"};
220 # if it's not set, look for it relative to $moz_src
221 if ( !test_dir ) {
222 $test_dir = $moz_src . '/mozilla/js/tests/';
225 # make sure that the test dir exists
226 if ( ! -e $test_dir ) {
227 die "The JavaScript Test Library could not be found.\n" .
228 "Check the tests out from /mozilla/js/tests or\n" .
229 "Set the value of your JS_TEST_DIR environment variable\n " .
230 "to the location of the test library.\n";
233 # make sure that the test dir ends with a trailing slash
234 $test_dir .= '/';
236 chdir $src_dir;
238 $shell_command = "java";
239 if ($js_classpath) {
240 $shell_command .= " -classpath " . $js_classpath;
242 $shell_command .= " org.mozilla.javascript.tools.shell.Main ";
244 # set the output file name. let's base its name on the date and platform,
245 # and give it a sequence number.
247 if ( $get_output ) {
248 $js_output = &get_output;
250 if ($js_output) {
251 print( "Writing results to $js_output\n" );
252 chdir $test_dir;
253 open( JS_OUTPUT, "> ${js_output}" ) ||
254 die "Can't open log file $js_output\n";
255 close JS_OUTPUT;
258 # get the start time
259 $start_time = time;
261 # print out some nice stuff
262 $start_date = &get_date;
263 &js_print( "JavaScript tests started: " . $start_date, "<p><tt>", "</tt></p>" );
265 &js_print ("Executing all the tests under $test_dir\n against " .
266 "$shell_command\n", "<p><tt>", "</tt></p>" );
270 # parse arguments. see usage for what arguments are expected.
272 sub parse_args {
273 $i = 0;
274 while( $i < @ARGV ){
275 if ( $ARGV[$i] eq '--threaded' ) {
276 $js_threaded = 1;
277 } elsif ( $ARGV[$i] eq '--d' ) {
278 $js_debug = 1;
279 } elsif ( $ARGV[$i] eq '--14' ) {
280 $js_version = '14';
281 } elsif ( $ARGV[$i] eq '--v' ) {
282 $js_verbose = 1;
283 } elsif ( $ARGV[$i] eq '-f' ) {
284 $js_output = $ARGV[++$i];
285 } elsif ( $ARGV[$i] eq '-c' ) {
286 $js_classpath = $ARGV[++$i];
287 } elsif ( $ARGV[$i] eq '--o' ) {
288 $get_output = 1;
289 } elsif ($ARGV[$i] eq '--e' ) {
290 $js_errors = 1;
291 } elsif ($ARGV[$i] eq '--q' ) {
292 $js_quiet = 1;
293 } elsif ($ARGV[$i] eq '--h' ) {
294 die &usage;
295 } elsif ( $ARGV[$i] eq '-E' ) {
296 $js_exe_full_path = $ARGV[$i+1];
297 $i++;
298 } else {
299 die &usage;
301 $i++;
305 # if no output options are provided, show some output and write to file
307 if ( !$js_verbose && !$js_output && !$get_output ) {
308 $get_output = 1;
313 # print the arguments that this script expects
315 sub usage {
316 die ("usage: $0\n" .
317 "--q Quiet mode -- only show information for tests that failed\n".
318 "--e Show runtime error messages for negative tests\n" .
319 "--v Verbose output -- show all test cases (not recommended)\n" .
320 "--o Send output to file whose generated name is based on date\n".
321 "--d Look for a debug JavaScript executable (default is optimized)\n" .
322 "-f <file> Redirect output to file named <file>\n"
327 # if $js_output is set, print to file as well as stdout
329 sub js_print {
330 ($string, $start_tag, $end_tag) = @_;
332 if ($js_output) {
333 open( JS_OUTPUT, ">> ${js_output}" ) ||
334 die "Can't open log file $js_output\n";
336 print JS_OUTPUT "$start_tag $string $end_tag";
337 close JS_OUTPUT;
339 print $string;
343 # close open files
345 sub cleanup_env {
346 # print out some nice stuff
347 $end_date = &get_date;
348 &js_print( "\nTests complete at $end_date", "<hr><tt>", "</tt>" );
350 # print out how long it took to complete
351 $end_time = time;
353 $test_seconds = ( $end_time - $start_time );
355 &js_print( "Start Date: $start_date\n", "<tt><br>" );
356 &js_print( "End Date: $end_date\n", "<br>" );
357 &js_print( "Test Time: $test_seconds seconds\n", "<br>" );
359 if ($js_output ) {
360 if ( !$js_verbose) {
361 &js_print( "Results were written to " . $js_output ."\n",
362 "<br>", "</tt>" );
364 close JS_OUTPUT;
370 # get the current date and time
372 sub get_date {
373 &get_localtime;
374 $now = $year ."/". $mon ."/". $mday ." ". $hour .":".
375 $min .":". $sec ."\n";
376 return $now;
379 sub get_localtime {
380 ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
381 localtime;
382 $mon++;
383 $mon = &zero_pad($mon);
384 $year= ($year < 2000) ? "19" . $year : $year;
385 $mday= &zero_pad($mday);
386 $sec = &zero_pad($sec);
387 $min = &zero_pad($min);
388 $hour = &zero_pad($hour);
390 sub zero_pad {
391 local ($string) = @_;
392 $string = ($string < 10) ? "0" . $string : $string;
393 return $string;
397 # generate an output file name based on the date
399 sub get_output {
400 &get_localtime;
402 chdir $test_dir;
404 $js_output = $test_dir ."/". $year .'-'. $mon .'-'. $mday ."\.1.html";
406 $output_file_found = 0;
408 while ( !$output_file_found ) {
409 if ( -e $js_output ) {
410 # get the last sequence number - everything after the dot
411 @seq_no = split( /\./, $js_output, 2 );
412 $js_output = $seq_no[0] .".". (++$seq_no[1]) . "\.html";
413 } else {
414 $output_file_found = 1;
417 return $js_output;
420 sub js_print_suitename {
421 if ( !$js_printed_suitename ) {
422 &js_print( "$suite\\$subdir\n", "<hr><font size+=1><b>",
423 "</font></b><br>" );
425 $js_printed_suitename = 1;
428 sub js_print_filename {
429 if ( !$js_printed_filename ) {
430 &js_print( "$js_test\n", "<b>", "</b>" );
431 $js_printed_filename = 1;
435 sub js_print_bugnumber {
436 if ( !$js_printed_bugnumber ) {
437 if ( $js_bugnumber =~ /^http/ ) {
438 &js_print( "$js_bugnumber", "<a href=$js_bugnumber>", "</a>" );
439 } else {
440 &js_print( "$js_bugnumber",
441 "<a href=http://scopus.mcom.com/bugsplat/show_bug.cgi?id=" .
442 $js_bugnumber .">",
443 "</a>" );
445 $js_printed_bugnumber = 1;