initial import
[vuplus_webkit] / Tools / Scripts / make-new-script-test
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2011 Apple Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1.  Redistributions of source code must retain the above copyright
10 #     notice, this list of conditions and the following disclaimer. 
11 # 2.  Redistributions in binary form must reproduce the above copyright
12 #     notice, this list of conditions and the following disclaimer in the
13 #     documentation and/or other materials provided with the distribution. 
14 #
15 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 # Script to create a new HTML file for a monolithic test using js-test machinery.
27
28 use strict;
29
30 use FindBin;
31 use lib $FindBin::Bin;
32
33 use File::Basename;
34 use Getopt::Long;
35 use webkitdirs;
36
37 sub makeTestRelativePathToSharedSources;
38 sub openTestInEditor;
39 sub writeTestFile;
40
41 my $showHelp;
42
43 my $result = GetOptions(
44     "help"       => \$showHelp,
45 );
46
47 if (!$result || $showHelp || !scalar(@ARGV) || $ARGV[0] !~ m/\.html$/) {
48     print STDERR "Usage: " . basename($0) . " [-h|--help] pathname\n";
49     print STDERR "\nExamples:\n";
50     print STDERR "    " . basename($0) . " new-test.html (will create the test in current directory)\n";
51     print STDERR "    " . basename($0) . " fast/loader/new-test.html (a relative path is always from LayoutTests directory)\n";
52     print STDERR "    " . basename($0) . " /Volumes/Data/WebKit/LayoutTests/fast/loader/new-test.html\n";
53     exit 1;
54 }
55
56 my $providedPath = $ARGV[0];
57
58 my $testAbsolutePath;
59
60 # If only a file name is provided, create the test in current directory.
61 $testAbsolutePath = File::Spec->rel2abs($providedPath) if (!(File::Spec->splitpath($providedPath))[1]);
62
63 # Otherwise, it's either absolute, or relative to LayoutTests directory.
64 chdirWebKit();
65 chdir "LayoutTests";
66 $testAbsolutePath = File::Spec->rel2abs($providedPath) if (!$testAbsolutePath);
67
68 writeTestFile();
69
70 print "$testAbsolutePath\n";
71 openTestInEditor();
72
73 exit 0;
74
75 sub makeTestRelativePathToSharedSources
76 {
77     my $layoutTestsPath = getcwd();
78     $testAbsolutePath =~ m/^$layoutTestsPath/ or die "Path $testAbsolutePath is not in LayoutTests directory.\n";
79     my $result = File::Spec->abs2rel("fast/js/resources/", dirname($testAbsolutePath));
80     return $result;
81 }
82
83 sub writeTestFile
84 {
85     die "Test $testAbsolutePath already exists.\n" if (-e $testAbsolutePath);
86
87     my $relativePathToSharedSources = makeTestRelativePathToSharedSources();
88
89     open TEST, ">", ${testAbsolutePath} or die "Cannot create test file at $testAbsolutePath.\n";
90     print TEST << "EOF";
91 <!DOCTYPE html>
92 <html>
93 <head>
94 <meta charset="utf-8">
95 <link rel="stylesheet" href="$relativePathToSharedSources/js-test-style.css">
96 <script src="$relativePathToSharedSources/js-test-pre.js"></script>
97 <title></title>
98 </head>
99 <body>
100 <p>TEST DESCRIPTION HERE</p>
101 <div id="console"></div>
102 <script>
103
104 // Your test script here. Feel free to modify surrounding HTML code if necessary.
105
106 var successfullyParsed = true;
107
108 </script>
109 <script src="$relativePathToSharedSources/js-test-post.js"></script>
110 </body>
111 </html>
112 EOF
113     close TEST;
114 }
115
116 sub openTestInEditor()
117 {
118     my $editor = $ENV{EDITOR};
119     exec ($editor, $testAbsolutePath) if ($editor);
120 }