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
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
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
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
47 local ($dir, $path) = @_;
50 local $dir_path = $path . $dir;
53 opendir ( DIR
, ${dir_path
} );
54 local @testdir_contents = readdir( DIR
);
57 foreach (@testdir_contents) {
58 if ( (-d
$_) && ($_ !~ 'CVS') && ( $_ ne '.') && ($_ ne '..')) {
59 @subdirs[$#subdirs+1] = $_;
67 # given a directory, return an array of all the js files that are in it.
70 ( $test_subdir ) = @_;
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
98 sub execute_js_tests
{
99 (@js_file_array) = @_;
101 $js_printed_suitename = 0;
106 foreach $js_test (@js_file_array) {
107 $js_printed_filename = 0;
108 $js_test_bugnumber = 0;
113 # create the test command
116 " -f $test_dir/$suite/shell.js " .
117 " $test_dir/$suite/$subdir/$js_test";
125 $test_path = $test_dir ."/" . $suite ."/". $test_subdir ."/". $js_test;
129 if ( !-e
$test_path ) {
130 &js_print
( " FAILED! file not found\n",
131 "<font color=#990000>", "</font><br>\n");
133 open( RUNNING_TEST
, "$test_command" . ' 2>&1 |');
135 while( <RUNNING_TEST
> ){
137 if ( $js_verbose && !$js_quiet ) {
138 &js_print
($_ ."\n", "", "<br>\n");
140 if ( $_ =~ /PASSED/ && $passed == -1) {
143 if ( $_ =~ /BUGNUMBER/ ) {
144 $js_test_bugnumber = $_;
146 if ( $_ =~ /FAILED/ && $_ =~ /expected/) {
151 local @msg = split ( "FAILED", $_ );
152 &js_print
( $passed ?
"\n" : "" );
153 &js_print
( " " . $msg[0], " <tt>" );
154 &js_print
( "FAILED", "<font color=#990000>", "</font>");
155 &js_print
( $msg[1], "", "</tt><br>\n" );
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 ) {
171 &js_print
( " PASSED!\n ",
172 "<font color=#009900>  ",
175 &js_print
( $runtime_error, "<pre>", "</pre>");
182 &js_print
( " FAILED! ", " <font color=#990000>",
184 &js_print
( " Should have resulted in an error\n",
188 if ( $passed == 1 && !$js_quiet) {
189 &js_print
( " PASSED!\n " , " <font color=#009900>",
196 &js_print
( " FAILED!\n " , " <font color=#990000>",
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
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
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
238 $shell_command = "java";
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.
248 $js_output = &get_output
;
251 print( "Writing results to $js_output\n" );
253 open( JS_OUTPUT
, "> ${js_output}" ) ||
254 die "Can't open log file $js_output\n";
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.
275 if ( $ARGV[$i] eq '--threaded' ) {
277 } elsif ( $ARGV[$i] eq '--d' ) {
279 } elsif ( $ARGV[$i] eq '--14' ) {
281 } elsif ( $ARGV[$i] eq '--v' ) {
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' ) {
289 } elsif ($ARGV[$i] eq '--e' ) {
291 } elsif ($ARGV[$i] eq '--q' ) {
293 } elsif ($ARGV[$i] eq '--h' ) {
295 } elsif ( $ARGV[$i] eq '-E' ) {
296 $js_exe_full_path = $ARGV[$i+1];
305 # if no output options are provided, show some output and write to file
307 if ( !$js_verbose && !$js_output && !$get_output ) {
313 # print the arguments that this script expects
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
330 ($string, $start_tag, $end_tag) = @_;
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";
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
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>" );
361 &js_print
( "Results were written to " . $js_output ."\n",
370 # get the current date and time
374 $now = $year ."/". $mon ."/". $mday ." ". $hour .":".
375 $min .":". $sec ."\n";
380 ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
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);
391 local ($string) = @_;
392 $string = ($string < 10) ?
"0" . $string : $string;
397 # generate an output file name based on the date
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";
414 $output_file_found = 1;
420 sub js_print_suitename
{
421 if ( !$js_printed_suitename ) {
422 &js_print
( "$suite\\$subdir\n", "<hr><font size+=1><b>",
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>" );
440 &js_print
( "$js_bugnumber",
441 "<a href=http://scopus.mcom.com/bugsplat/show_bug.cgi?id=" .
445 $js_printed_bugnumber = 1;