Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / mklistpage.pl
1 #!/usr/bin/perl
2 #
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
10 #
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
15 #
16 # The Original Code is JavaScript Core Tests.
17 #
18 # The Initial Developer of the Original Code is
19 # Netscape Communications Corporation.
20 # Portions created by the Initial Developer are Copyright (C) 1997-1999
21 # the Initial Developer. All Rights Reserved.
22 #
23 # Contributor(s):
24 #   Robert Ginda
25 #   Bob Clary
26 #
27 # Alternatively, the contents of this file may be used under the terms of
28 # either the GNU General Public License Version 2 or later (the "GPL"), or
29 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 # in which case the provisions of the GPL or the LGPL are applicable instead
31 # of those above. If you wish to allow use of your version of this file only
32 # under the terms of either the GPL or the LGPL, and not to allow others to
33 # use your version of this file under the terms of the MPL, indicate your
34 # decision by deleting the provisions above and replace them with the notice
35 # and other provisions required by the GPL or the LGPL. If you do not delete
36 # the provisions above, a recipient may use your version of this file under
37 # the terms of any one of the MPL, the GPL or the LGPL.
38 #
39 # ***** END LICENSE BLOCK *****
40
41 # Creates the meat of a test suite manager page, requites menuhead.html and menufoot.html
42 # to create the complete page.  The test suite manager lets you choose a subset of tests
43 # to run under the runtests2.pl script.
44
45 local $lxr_url = "http://lxr.mozilla.org/mozilla/source/js/tests/";
46 local $suite_path = $ARGV[0] || "./";
47 local $uid = 0;          # radio button unique ID
48 local $html = "";        # html output
49 local $javascript = "";  # script output
50
51 #
52 # automatically exclude spidermonkey-n.tests
53 # XXXbc better to emulate jsDriver.pl's option processing
54 #    and allow -L file1 file2 etc.
55 #
56 local $excludedtest;
57 local @excludedlist = expand_user_test_list('spidermonkey-n.tests');
58 local %excludedhash = {};
59 foreach $excludedtest (@excludedlist)
60 {
61     $excludedhash{"./" . $excludedtest} = 1;
62 }
63
64 &main;
65
66 print (&scriptTag($javascript) . "\n");
67 print ($html);
68
69 sub main {
70     local $i, @suite_list;
71
72     if (!($suite_path =~ /\/$/)) {
73         $suite_path = $suite_path . "/";
74     }
75
76     @suite_list = sort(&get_subdirs ($suite_path));
77
78     $javascript .= "\n";
79     $javascript .= "var suites = {};\n";
80     $javascript .= "function populateSuites()\n";
81     $javascript .= "{\n";
82     $javascript .= "var currSuite;\n";
83     $javascript .= "var currDirectory;\n";
84     $javascript .= "var currTestDirs;\n";
85     $javascript .= "var currTests;\n";
86
87     $html .= "<h3>Test Suites:</h3>\n";
88     $html .= "<center>\n";
89     $html .= "<input type='button' value='Select All' " .
90       "onclick='selectAll();'> \n";
91     $html .= "<input type='button' value='Select None' " .
92       "onclick='selectNone();'> \n";
93
94     # suite menu
95     $html .= "<table border='1' summary='suite menu'>\n";
96     foreach $suite (@suite_list) {
97         local @readme_text = ("No description available.");
98         if (open (README, $suite_path . $suite . "/README")) {
99             @readme_text = <README>;
100             close (README);
101         }
102         $html .= "<tr>\n";
103         $html .= "<td>\n";
104         $html .= "<a href='\#SUITE_$suite'>$suite</a>\n";
105         $html .= "</td>\n";
106         $html .= "<td>@readme_text</td>\n";
107         $html .= "<td>\n";
108         $html .= "<input type='button' value='Select All' " .
109           "onclick='selectAll(\"$suite\");'> \n";
110         $html .= "<input type='button' value='Select None' " .
111           "onclick='selectNone(\"$suite\");'>\n";
112         $html .= "</td>\n";
113         $html .= "<td><input readonly name='SUMMARY_$suite'></td>";
114         $html .= "</tr>";
115     }
116     $html .= "</table>\n";
117     $html .= "<div><input readonly name='TOTAL'></div>\n";
118     $html .= "</center>\n";
119
120     $html .= "<dl>\n";
121     foreach $i (0 .. $#suite_list) {
122         local $prev_href = ($i > 0) ? "\#SUITE_" . $suite_list[$i - 1] : "";
123         local $next_href = ($i < $#suite_list) ? "\#SUITE_" .
124             $suite_list[$i + 1] : "";
125         &process_suite ($suite_path, $suite_list[$i], $prev_href, $next_href);
126     }
127     $html .= "</dl>\n";
128
129     $javascript .= "}\n";
130     $javascript .= "populateSuites();\n";
131 }
132
133 #
134 # Append detail from a 'suite' directory (eg: ecma, ecma_2, js1_1, etc.), calling
135 # process_test_dir for subordinate categories.
136 #
137 sub process_suite {
138     local ($suite_path, $suite, $prev_href, $next_href) = @_;
139     local $i, @test_dir_list;   
140     local $suite_count = 0;
141
142     # suite js object
143     $javascript .= "currSuite = suites[\"$suite\"] = " .
144         "{testDirs:{}, count:0, selected:0};\n";
145     $javascript .= "currTestDirs = currSuite.testDirs;\n";
146
147     @test_dir_list = sort(&get_subdirs ($test_home . $suite));
148
149     # suite header
150
151     $html .= "<dt>\n";
152     $html .= "<a name='SUITE_$suite'></a>$suite " .
153       "(" . ($#test_dir_list + 1) . " Sub-Categories)\n";
154     $html .= "<input type='button' value='Select All' " .
155         "onclick='selectAll(\"$suite\");'>\n";
156     $html .= "<input type='button' value='Select None' " .
157         "onclick='selectNone(\"$suite\");'> \n";
158     $html .= "[ ";
159     $html .= "<a href='\#top_of_page'>Top of page</a> \n";
160     if ($prev_href) {
161         $html .= " | ";
162         $html .= "<a href='$prev_href'>Previous Suite</a> \n";
163     }
164     if ($next_href) {
165         $html .= " | ";
166         $html .= "<a href='$next_href'>Next Suite</a> \n";
167     }
168     $html .= "]\n";
169     $html .= "</dt>\n";
170     $html .= "<dd>\n";
171
172     foreach $i (0 .. $#test_dir_list) {
173         local $prev_href = ($i > 0) ? "\#TESTDIR_" . $suite .
174             $test_dir_list[$i - 1] :
175             "";
176         local $next_href = ($i < $#test_dir_list) ?
177           "\#TESTDIR_" . $suite . $test_dir_list[$i + 1] : "";
178         &process_test_dir ($suite_path . $suite . "/", $test_dir_list[$i],
179                            $suite,
180                            $prev_href, $next_href);
181     }
182     $javascript .= "currSuite.count = $suite_count;\n";
183
184     $html .= "</dd>\n";
185 }
186
187 #
188 # Append detail from a test directory,
189 # calling process_test for subordinate js files
190 #
191 sub process_test_dir {
192     local ($test_dir_path, $test_dir, $suite, $prev_href, $next_href) = @_;
193
194     @test_list = sort(&get_js_files ($test_dir_path . $test_dir));
195
196     if ($#test_list >= 0)
197     {
198         $suite_count += @test_list;
199
200         $javascript .= "currDirectory = currTestDirs[\"$test_dir\"] = " .
201             "{tests:{}};\n";
202         $javascript .= "currTests = currDirectory.tests;\n";
203
204         $html .= "<dl>\n";
205         $html .= "<dt>\n";
206         $html .= "<a name='TESTDIR_$suite$test_dir'></a>\n";
207         $html .= "$test_dir (" . ($#test_list + 1) . " tests)\n";
208         $html .= "<input type='button' value='Select All' " .
209             "onclick='selectAll(\"$suite\", \"$test_dir\");'>\n";
210         $html .= "<input type='button' value='Select None' " .
211             "onclick='selectNone(\"$suite\", \"$test_dir\");'> ";
212         $html .= "[ <a href='\#SUITE_$suite'>Top of $suite Suite</a> ";
213         if ($prev_href) {
214             $html .= "| <a href='$prev_href'>Previous Category</a> ";
215         }
216         if ($next_href) {
217             $html .= " | <a href='$next_href'>Next Category</a> ";
218         }
219         $html .= "]\n";
220         $html .= "</dt>\n";
221
222         $html .= "<dd>\n";
223
224         foreach $test (@test_list) {
225             &process_test ($test_dir_path . $test_dir, $test);
226         }
227
228         $html .= "</dl>\n";
229     }
230 }
231
232
233 #
234 # Append detail from a single JavaScript file.
235 #
236 sub process_test {
237     local ($test_dir_path, $test) = @_;
238     local $title = "";
239
240     # ignore excluded tests
241     if ($excludedhash{"$test_dir_path/$test"})
242     {
243         --$suite_count;
244         return;
245     }
246
247     $uid++;
248
249     open (TESTCASE, $test_dir_path . "/" . $test) ||
250       die ("Error opening " . $test_dir_path . "/" . $test);
251
252     while (<TESTCASE>) {
253         if (/.*(TITLE|summary)\s+\=\s+[\"\'](.*)[\"\']/i) {
254             $title = $2;
255             break;
256         }
257         if (/.*START\([\"\'](.*)[\"\']\);/)
258         {
259             $title = $1;
260             break;
261         }
262     }
263     close (TESTCASE);
264
265     $javascript .= "currTests[\"$test\"] = {id:\"t$uid\"}\n";
266     $html .= "<dd>\n";
267     $html .= " <input type='checkbox' value='$test' name='t$uid' " .
268         "id='$suite:$test_dir:$test' ".
269         "onclick='return onRadioClick(this);'>\n";
270     $html .= "<a href='$lxr_url$suite/$test_dir/$test' target='other_window'>" .
271         "$test</a> $title\n";
272     $html .= "</dd>\n";
273
274 }
275
276 sub scriptTag {
277
278     return ("<script type='text/javascript' language='JavaScript'>@_</script>");
279
280 }
281
282 #
283 # given a directory, return an array of all subdirectories
284 #
285 sub get_subdirs {
286     local ($dir)  = @_;
287     local @subdirs;
288
289     if (!($dir =~ /\/$/)) {
290         $dir = $dir . "/";
291     }
292
293     opendir (DIR, $dir) || die ("couldn't open directory $dir: $!");
294     local @testdir_contents = readdir(DIR);
295     closedir(DIR);
296
297     foreach (@testdir_contents) {
298         if ((-d ($dir . $_)) && ($_ ne 'CVS') && ($_ ne '.') && ($_ ne '..')) {
299             @subdirs[$#subdirs + 1] = $_;
300         }
301     }
302
303     return @subdirs;
304 }
305
306 #
307 # given a directory, return an array of all the js files that are in it.
308 #
309 sub get_js_files {
310     local ($test_subdir) = @_;
311     local @js_file_array;
312
313     opendir ( TEST_SUBDIR, $test_subdir) || die ("couldn't open directory " .
314                                                  "$test_subdir: $!");
315     @subdir_files = readdir( TEST_SUBDIR );
316     closedir( TEST_SUBDIR );
317
318     foreach ( @subdir_files ) {
319 #       print "excluded $test_subdir/$_\n" if $excludedhash{"$test_subdir/$_"};
320         if ( ($_ =~ /\.js$/) && ($_ ne 'shell.js') && ($_ ne 'browser.js') &&
321              (!$excludedhash{"$test_subdir/$_"}) ) {
322             $js_file_array[$#js_file_array+1] = $_;
323         }
324     }
325
326     return @js_file_array;
327 }
328
329
330 # copied from jsDriver.pl
331
332 #
333 # reads $list_file, storing non-comment lines into an array.
334 # lines in the form suite_dir/[*] or suite_dir/test_dir/[*] are expanded
335 # to include all test files under the specified directory
336 #
337 sub expand_user_test_list {
338     my ($list_file) = @_;
339     my @retval = ();
340
341     #
342     # Trim off the leading path separator that begins relative paths on the Mac.
343     # Each path will get concatenated with $opt_suite_path, which ends in one.
344     #
345     # Also note:
346     #
347     # We will call expand_test_list_entry(), which does pattern-matching on $list_file.
348     # This will make the pattern-matching the same as it would be on Linux/Windows -
349     #
350     if ($os_type eq "MAC") {
351         $list_file =~ s/^$path_sep//;
352     }
353
354     if ($list_file =~ /\.js$/ || -d $opt_suite_path . $list_file) {
355
356         push (@retval, &expand_test_list_entry($list_file));
357
358     } else {
359
360         open (TESTLIST, $list_file) ||
361           die("Error opening test list file '$list_file': $!\n");
362
363         while (<TESTLIST>) {
364             s/\r*\n*$//;
365             if (!(/\s*\#/)) {
366                 # It's not a comment, so process it
367                 push (@retval, &expand_test_list_entry($_));
368             }
369         }
370
371         close (TESTLIST);
372
373     }
374
375     return @retval;
376
377 }
378
379
380 #
381 # Currently expect all paths to be RELATIVE to the top-level tests directory.
382 # One day, this should be improved to allow absolute paths as well -
383 #
384 sub expand_test_list_entry {
385     my ($entry) = @_;
386     my @retval;
387
388     if ($entry =~ /\.js$/) {
389         # it's a regular entry, add it to the list
390         if (-f $opt_suite_path . $entry) {
391             push (@retval, $entry);
392         } else {
393             status ("testcase '$entry' not found.");
394         }
395     } elsif ($entry =~ /(.*$path_sep[^\*][^$path_sep]*)$path_sep?\*?$/) {
396         # Entry is in the form suite_dir/test_dir[/*]
397         # so iterate all tests under it
398         my $suite_and_test_dir = $1;
399         my @test_files = &get_js_files ($opt_suite_path .
400                                         $suite_and_test_dir);
401         my $i;
402
403         foreach $i (0 .. $#test_files) {
404             $test_files[$i] = $suite_and_test_dir . $path_sep .
405               $test_files[$i];
406         }
407
408         splice (@retval, $#retval + 1, 0, @test_files);
409
410     } elsif ($entry =~ /([^\*][^$path_sep]*)$path_sep?\*?$/) {
411         # Entry is in the form suite_dir[/*]
412         # so iterate all test dirs and tests under it
413         my $suite = $1;
414         my @test_dirs = &get_subdirs ($opt_suite_path . $suite);
415         my $test_dir;
416
417         foreach $test_dir (@test_dirs) {
418             my @test_files = &get_js_files ($opt_suite_path . $suite .
419                                             $path_sep . $test_dir);
420             my $i;
421
422             foreach $i (0 .. $#test_files) {
423                 $test_files[$i] = $suite . $path_sep . $test_dir . $path_sep .
424                   $test_files[$i];
425             }
426
427             splice (@retval, $#retval + 1, 0, @test_files);
428         }
429
430     } else {
431         die ("Dont know what to do with list entry '$entry'.\n");
432     }
433
434     return @retval;
435
436 }
437
438 sub status {
439
440     print STDERR ("-#- ", @_ , "\n");
441
442 }
443