| 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 |
" -f $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 |
|
| 132 |
if ( !-e $test_path ) { |
| 133 |
&js_print( " FAILED! file not found\n", |
| 134 |
"<font color=#990000>", "</font><br>\n"); |
| 135 |
} else { |
| 136 |
open( RUNNING_TEST, "$test_command" . ' 2>&1 |'); |
| 137 |
|
| 138 |
|
| 139 |
# this is where we want the tests to provide a lot more information |
| 140 |
# that this script must parse so that we can |
| 141 |
|
| 142 |
while( <RUNNING_TEST> ){ |
| 143 |
if ( $js_verbose && !$js_quiet ) { |
| 144 |
&js_print ($_ ."\n", "", "<br>\n"); |
| 145 |
} |
| 146 |
if ( $_ =~ /BUGNUMBER/ ) { |
| 147 |
$js_test_bugnumber = $_; |
| 148 |
} |
| 149 |
if ( $_ =~ /PASSED/ && $passed == -1 ) { |
| 150 |
$passed = 1; |
| 151 |
} |
| 152 |
if ( $_ =~ /FAILED/ && $_ =~ /expected/) { |
| 153 |
&js_print_suitename; |
| 154 |
&js_print_filename; |
| 155 |
&js_print_bugnumber; |
| 156 |
|
| 157 |
local @msg = split ( "FAILED", $_ ); |
| 158 |
&js_print ( $passed ? "\n" : "" ); |
| 159 |
&js_print( " " . $msg[0], " <tt>" ); |
| 160 |
&js_print( "FAILED", "<font color=#990000>", "</font>"); |
| 161 |
&js_print( $msg[1], "", "</tt><br>\n" ); |
| 162 |
$passed = 0; |
| 163 |
} |
| 164 |
if ( $_ =~ /$js_test/ ) { |
| 165 |
$runtime_error .= $_; |
| 166 |
} |
| 167 |
} |
| 168 |
close( RUNNING_TEST ); |
| 169 |
|
| 170 |
# |
| 171 |
# figure out whether the test passed or failed. print out an |
| 172 |
# appropriate level of output based on the value of $js_quiet |
| 173 |
# |
| 174 |
if ( $js_test =~ /-n\.js$/ ) { |
| 175 |
if ( $runtime_error ) { |
| 176 |
if ( !$js_quiet ) { |
| 177 |
&js_print( " PASSED!\n ", |
| 178 |
"<font color=#009900>  ", |
| 179 |
"</font><br>" ); |
| 180 |
if ( $js_errors ) { |
| 181 |
&js_print( $runtime_error, "<pre>", "</pre>"); |
| 182 |
} |
| 183 |
} |
| 184 |
} else { |
| 185 |
&js_print_suitename; |
| 186 |
&js_print_filename; |
| 187 |
&js_print_bugnumber; |
| 188 |
&js_print( " FAILED! ", " <font color=#990000>", |
| 189 |
"</font>"); |
| 190 |
&js_print( " Should have resulted in an error\n", |
| 191 |
"","<br>" ); |
| 192 |
} |
| 193 |
} else { |
| 194 |
if ( $passed == 1 && !$js_quiet) { |
| 195 |
&js_print( " PASSED!\n " , " <font color=#009900>", |
| 196 |
"</font><br>" ); |
| 197 |
} else { |
| 198 |
if ($passed == -1) { |
| 199 |
&js_print_suitename; |
| 200 |
&js_print_filename; |
| 201 |
&js_print_bugnumber; |
| 202 |
&js_print( " FAILED!\n " , " <font color=#990000>", |
| 203 |
"</font><br>" ); |
| 204 |
&js_print( " Missing 'PASSED' in output\n", "","<br>" ); |
| 205 |
&js_print( $log, "output:<br><pre>", "</pre>" ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
# |
| 215 |
# figure out what os we're on, the default name of the object directory |
| 216 |
# |
| 217 |
sub setup_env { |
| 218 |
# MOZ_SRC must be set, so we can figure out where the |
| 219 |
# JavaScript executable is |
| 220 |
$moz_src = $ENV{"MOZ_SRC"} |
| 221 |
|| die( "You need to set your MOZ_SRC environment variable.\n" ); |
| 222 |
$src_dir = $moz_src . '/mozilla/js/src/'; |
| 223 |
|
| 224 |
# JS_TEST_DIR must be set so we can figure out where the tests are. |
| 225 |
$test_dir = $ENV{"JS_TEST_DIR"}; |
| 226 |
|
| 227 |
# if it's not set, look for it relative to $moz_src |
| 228 |
if ( !$test_dir ) { |
| 229 |
$test_dir = $moz_src . '/mozilla/js/tests/'; |
| 230 |
} |
| 231 |
|
| 232 |
# make sure that the test dir exists |
| 233 |
if ( ! -e $test_dir ) { |
| 234 |
die "The JavaScript Test Library could not be found at $test_dir.\n" . |
| 235 |
"Check the tests out from /mozilla/js/tests or\n" . |
| 236 |
"Set the value of your JS_TEST_DIR environment variable\n " . |
| 237 |
"to the location of the test library.\n"; |
| 238 |
} |
| 239 |
|
| 240 |
# make sure that the test dir ends with a trailing slash |
| 241 |
$test_dir .= '/'; |
| 242 |
|
| 243 |
chdir $src_dir; |
| 244 |
|
| 245 |
# figure out which platform we're on, and figure out where the object |
| 246 |
# directory is |
| 247 |
|
| 248 |
$machine_os = `uname -s`; |
| 249 |
|
| 250 |
if ( $machine_os =~ /WIN/ ) { |
| 251 |
$machine_os = 'WIN'; |
| 252 |
$object_dir = ($js_debug) ? 'Debug' : 'Release'; |
| 253 |
$js_exe = 'jsshell.exe'; |
| 254 |
} else { |
| 255 |
chop $machine_os; |
| 256 |
$js_exe = 'js'; |
| 257 |
|
| 258 |
# figure out what the object directory is. on all platforms, |
| 259 |
# it's the directory that ends in OBJ. if $js_debug is set, |
| 260 |
# look the directory that ends with or DBG.OBJ; otherwise |
| 261 |
# look for the directory that ends with OPT.OBJ |
| 262 |
|
| 263 |
opendir ( SRC_DIR_FILES, $src_dir ); |
| 264 |
@src_dir_files = readdir( SRC_DIR_FILES ); |
| 265 |
closedir ( SRC_DIR_FILES ); |
| 266 |
|
| 267 |
$object_pattern = $js_debug ? 'DBG.OBJ' : 'OPT.OBJ'; |
| 268 |
|
| 269 |
foreach (@src_dir_files) { |
| 270 |
if ( $_ =~ /$object_pattern/ && $_ =~ $machine_os) { |
| 271 |
$object_dir = $_; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
if ( ! $object_dir ) { |
| 276 |
die( "Couldn't find an object directory in $src_dir.\n" ); |
| 277 |
} |
| 278 |
|
| 279 |
# figure out what the name of the javascript executable should be, and |
| 280 |
# make sure it's there. if it's not there, give a helpful message so |
| 281 |
# the user can figure out what they need to do next. |
| 282 |
|
| 283 |
|
| 284 |
if ( ! $js_exe_full_path ) { |
| 285 |
$shell_command = $src_dir . $object_dir .'/'. $js_exe; |
| 286 |
} else { |
| 287 |
$shell_command = $js_exe_full_path; |
| 288 |
} |
| 289 |
|
| 290 |
if ( !-e $shell_command ) { |
| 291 |
die ("Could not find JavaScript shell executable $shell_command.\n" . |
| 292 |
"Check the value of your MOZ_SRC environment variable.\n" . |
| 293 |
"Currently, MOZ_SRC is set to $ENV{\"MOZ_SRC\"}\n". |
| 294 |
"See the readme at http://lxr.mozilla.org/mozilla/src/js/src/ " . |
| 295 |
"for instructions on building the JavaScript shell.\n" ); |
| 296 |
} |
| 297 |
|
| 298 |
# set the output file name. let's base its name on the date and platform, |
| 299 |
# and give it a sequence number. |
| 300 |
|
| 301 |
if ( $get_output ) { |
| 302 |
$js_output = &get_output; |
| 303 |
} |
| 304 |
if ($js_output) { |
| 305 |
print( "Writing results to $js_output\n" ); |
| 306 |
chdir $test_dir; |
| 307 |
open( JS_OUTPUT, "> ${js_output}" ) || |
| 308 |
die "Can't open log file $js_output\n"; |
| 309 |
close JS_OUTPUT; |
| 310 |
} |
| 311 |
|
| 312 |
# get the start time |
| 313 |
$start_time = time; |
| 314 |
|
| 315 |
# print out some nice stuff |
| 316 |
$start_date = &get_date; |
| 317 |
&js_print( "JavaScript tests started: " . $start_date, "<p><tt>", "</tt></p>" ); |
| 318 |
|
| 319 |
&js_print ("Executing all the tests under $test_dir\n against " . |
| 320 |
"$shell_command\n", "<p><tt>", "</tt></p>" ); |
| 321 |
} |
| 322 |
|
| 323 |
# |
| 324 |
# parse arguments. see usage for what arguments are expected. |
| 325 |
# |
| 326 |
sub parse_args { |
| 327 |
$i = 0; |
| 328 |
while( $i < @ARGV ){ |
| 329 |
if ( $ARGV[$i] eq '--threaded' ) { |
| 330 |
$js_threaded = 1; |
| 331 |
} elsif ( $ARGV[$i] eq '--d' ) { |
| 332 |
$js_debug = 1; |
| 333 |
} elsif ( $ARGV[$i] eq '--14' ) { |
| 334 |
$js_version = '14'; |
| 335 |
} elsif ( $ARGV[$i] eq '--v' ) { |
| 336 |
$js_verbose = 1; |
| 337 |
} elsif ( $ARGV[$i] eq '-f' ) { |
| 338 |
$js_output = $ARGV[++$i]; |
| 339 |
} elsif ( $ARGV[$i] eq '--o' ) { |
| 340 |
$get_output = 1; |
| 341 |
} elsif ($ARGV[$i] eq '--e' ) { |
| 342 |
$js_errors = 1; |
| 343 |
} elsif ($ARGV[$i] eq '--q' ) { |
| 344 |
$js_quiet = 1; |
| 345 |
} elsif ($ARGV[$i] eq '--h' ) { |
| 346 |
die &usage; |
| 347 |
} elsif ( $ARGV[$i] eq '-E' ) { |
| 348 |
$js_exe_full_path = $ARGV[$i+1]; |
| 349 |
$i++; |
| 350 |
} else { |
| 351 |
die &usage; |
| 352 |
} |
| 353 |
$i++; |
| 354 |
} |
| 355 |
|
| 356 |
# |
| 357 |
# if no output options are provided, show some output and write to file |
| 358 |
# |
| 359 |
if ( !$js_verbose && !$js_output && !$get_output ) { |
| 360 |
$get_output = 1; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
# |
| 365 |
# print the arguments that this script expects |
| 366 |
# |
| 367 |
sub usage { |
| 368 |
die ("usage: $0\n" . |
| 369 |
"--q Quiet mode -- only show information for tests that failed\n". |
| 370 |
"--e Show runtime error messages for negative tests\n" . |
| 371 |
"--v Verbose output -- show all test cases (not recommended)\n" . |
| 372 |
"--o Send output to file whose generated name is based on date\n". |
| 373 |
"--d Look for a debug JavaScript executable (default is optimized)\n" . |
| 374 |
"-f <file> Redirect output to file named <file>\n" |
| 375 |
); |
| 376 |
} |
| 377 |
|
| 378 |
# |
| 379 |
# if $js_output is set, print to file as well as stdout |
| 380 |
# |
| 381 |
sub js_print { |
| 382 |
($string, $start_tag, $end_tag) = @_; |
| 383 |
|
| 384 |
if ($js_output) { |
| 385 |
open( JS_OUTPUT, ">> ${js_output}" ) || |
| 386 |
die "Can't open log file $js_output\n"; |
| 387 |
|
| 388 |
print JS_OUTPUT "$start_tag $string $end_tag"; |
| 389 |
close JS_OUTPUT; |
| 390 |
} |
| 391 |
print $string; |
| 392 |
} |
| 393 |
|
| 394 |
# |
| 395 |
# close open files |
| 396 |
# |
| 397 |
sub cleanup_env { |
| 398 |
# print out some nice stuff |
| 399 |
$end_date = &get_date; |
| 400 |
&js_print( "\nTests complete at $end_date", "<hr><tt>", "</tt>" ); |
| 401 |
|
| 402 |
# print out how long it took to complete |
| 403 |
$end_time = time; |
| 404 |
|
| 405 |
$test_seconds = ( $end_time - $start_time ); |
| 406 |
|
| 407 |
&js_print( "Start Date: $start_date\n", "<tt><br>" ); |
| 408 |
&js_print( "End Date: $end_date\n", "<br>" ); |
| 409 |
&js_print( "Test Time: $test_seconds seconds\n", "<br>" ); |
| 410 |
|
| 411 |
if ($js_output ) { |
| 412 |
if ( !$js_verbose) { |
| 413 |
&js_print( "Results were written to " . $js_output ."\n", |
| 414 |
"<br>", "</tt>" ); |
| 415 |
} |
| 416 |
close JS_OUTPUT; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
# |
| 422 |
# get the current date and time |
| 423 |
# |
| 424 |
sub get_date { |
| 425 |
&get_localtime; |
| 426 |
$now = $year ."/". $mon ."/". $mday ." ". $hour .":". |
| 427 |
$min .":". $sec ."\n"; |
| 428 |
return $now; |
| 429 |
|
| 430 |
} |
| 431 |
sub get_localtime { |
| 432 |
( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = |
| 433 |
localtime; |
| 434 |
$mon++; |
| 435 |
$mon = &zero_pad($mon); |
| 436 |
$year= ($year < 2000) ? "19" . $year : $year; |
| 437 |
$mday= &zero_pad($mday); |
| 438 |
$sec = &zero_pad($sec); |
| 439 |
$min = &zero_pad($min); |
| 440 |
$hour = &zero_pad($hour); |
| 441 |
} |
| 442 |
sub zero_pad { |
| 443 |
local ($string) = @_; |
| 444 |
$string = ($string < 10) ? "0" . $string : $string; |
| 445 |
return $string; |
| 446 |
} |
| 447 |
|
| 448 |
# |
| 449 |
# generate an output file name based on the date |
| 450 |
# |
| 451 |
sub get_output { |
| 452 |
&get_localtime; |
| 453 |
|
| 454 |
chdir $test_dir; |
| 455 |
|
| 456 |
$js_output = $test_dir ."/". $year .'-'. $mon .'-'. $mday ."\.1.html"; |
| 457 |
|
| 458 |
$output_file_found = 0; |
| 459 |
|
| 460 |
while ( !$output_file_found ) { |
| 461 |
if ( -e $js_output ) { |
| 462 |
# get the last sequence number - everything after the dot |
| 463 |
@seq_no = split( /\./, $js_output, 2 ); |
| 464 |
$js_output = $seq_no[0] .".". (++$seq_no[1]) . "\.html"; |
| 465 |
} else { |
| 466 |
$output_file_found = 1; |
| 467 |
} |
| 468 |
} |
| 469 |
return $js_output; |
| 470 |
} |
| 471 |
|
| 472 |
sub js_print_suitename { |
| 473 |
if ( !$js_printed_suitename ) { |
| 474 |
&js_print( "$suite\\$subdir\n", "<hr><font size+=1><b>", |
| 475 |
"</font></b><br>" ); |
| 476 |
} |
| 477 |
$js_printed_suitename = 1; |
| 478 |
} |
| 479 |
|
| 480 |
sub js_print_filename { |
| 481 |
if ( !$js_printed_filename ) { |
| 482 |
&js_print( "$js_test\n", "<b>", "</b><br>" ); |
| 483 |
$js_printed_filename = 1; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
sub js_print_bugnumber { |
| 488 |
if ( !$js_printed_bugnumber ) { |
| 489 |
if ( $js_bugnumber =~ /^http/ ) { |
| 490 |
&js_print( "$js_bugnumber", "<a href=$js_bugnumber>", "</a>" ); |
| 491 |
} else { |
| 492 |
&js_print( "$js_bugnumber", |
| 493 |
"<a href=http://scopus.mcom.com/bugsplat/show_bug.cgi?id=" . |
| 494 |
$js_bugnumber .">", |
| 495 |
"</a>" ); |
| 496 |
} |
| 497 |
$js_printed_bugnumber = 1; |
| 498 |
} |
| 499 |
} |