blob: ea837ed5c8de20e06bc53593d93ace87f5ebd898 [file] [log] [blame]
commit-queue@webkit.org026ee042018-01-04 07:18:18 +00001#!/usr/bin/env perl
ap@apple.com5c610f62011-09-10 04:00:45 +00002
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
ap@apple.com7c99c182017-05-20 18:15:07 +000026# Script to create a new HTML file using js-test machinery.
ap@apple.com5c610f62011-09-10 04:00:45 +000027
28use strict;
commit-queue@webkit.org026ee042018-01-04 07:18:18 +000029use warnings;
ap@apple.com5c610f62011-09-10 04:00:45 +000030
31use FindBin;
32use lib $FindBin::Bin;
33
34use File::Basename;
35use Getopt::Long;
36use webkitdirs;
37
ap@apple.combb1d1b92012-01-19 22:04:55 +000038sub makePathToSharedSources;
ap@apple.com5c610f62011-09-10 04:00:45 +000039sub openTestInEditor;
40sub writeTestFile;
41
42my $showHelp;
43
44my $result = GetOptions(
45 "help" => \$showHelp,
46);
47
48if (!$result || $showHelp || !scalar(@ARGV) || $ARGV[0] !~ m/\.html$/) {
49 print STDERR "Usage: " . basename($0) . " [-h|--help] pathname\n";
50 print STDERR "\nExamples:\n";
51 print STDERR " " . basename($0) . " new-test.html (will create the test in current directory)\n";
52 print STDERR " " . basename($0) . " fast/loader/new-test.html (a relative path is always from LayoutTests directory)\n";
53 print STDERR " " . basename($0) . " /Volumes/Data/WebKit/LayoutTests/fast/loader/new-test.html\n";
54 exit 1;
55}
56
57my $providedPath = $ARGV[0];
58
59my $testAbsolutePath;
60
61# If only a file name is provided, create the test in current directory.
62$testAbsolutePath = File::Spec->rel2abs($providedPath) if (!(File::Spec->splitpath($providedPath))[1]);
63
64# Otherwise, it's either absolute, or relative to LayoutTests directory.
65chdirWebKit();
66chdir "LayoutTests";
67$testAbsolutePath = File::Spec->rel2abs($providedPath) if (!$testAbsolutePath);
68
69writeTestFile();
70
71print "$testAbsolutePath\n";
72openTestInEditor();
73
74exit 0;
75
ap@apple.combb1d1b92012-01-19 22:04:55 +000076sub makePathToSharedSources
ap@apple.com5c610f62011-09-10 04:00:45 +000077{
78 my $layoutTestsPath = getcwd();
79 $testAbsolutePath =~ m/^$layoutTestsPath/ or die "Path $testAbsolutePath is not in LayoutTests directory.\n";
ap@apple.combb1d1b92012-01-19 22:04:55 +000080 my $isHTTPTest = $testAbsolutePath =~ m/^$layoutTestsPath\/http/;
81 if ($isHTTPTest) {
82 return "/js-test-resources";
83 } else {
mark.lam@apple.com0c04c972013-09-10 02:46:55 +000084 return File::Spec->abs2rel("resources/", dirname($testAbsolutePath));
ap@apple.combb1d1b92012-01-19 22:04:55 +000085 }
ap@apple.com5c610f62011-09-10 04:00:45 +000086}
87
88sub writeTestFile
89{
90 die "Test $testAbsolutePath already exists.\n" if (-e $testAbsolutePath);
91
ap@apple.combb1d1b92012-01-19 22:04:55 +000092 my $pathToSharedSources = makePathToSharedSources();
ap@apple.com5c610f62011-09-10 04:00:45 +000093
94 open TEST, ">", ${testAbsolutePath} or die "Cannot create test file at $testAbsolutePath.\n";
95 print TEST << "EOF";
96<!DOCTYPE html>
97<html>
98<head>
99<meta charset="utf-8">
ap@apple.com7c99c182017-05-20 18:15:07 +0000100<script src="$pathToSharedSources/js-test.js"></script>
ap@apple.com5c610f62011-09-10 04:00:45 +0000101</head>
102<body>
ap@apple.com5c610f62011-09-10 04:00:45 +0000103<script>
104
arv@chromium.org0a48e052011-10-25 23:10:43 +0000105description("TEST DESCRIPTION HERE");
ap@apple.com5c610f62011-09-10 04:00:45 +0000106
arv@chromium.org0a48e052011-10-25 23:10:43 +0000107// Your test script here. Feel free to modify surrounding HTML code if necessary.
ap@apple.com5c610f62011-09-10 04:00:45 +0000108
109</script>
ap@apple.com5c610f62011-09-10 04:00:45 +0000110</body>
111</html>
112EOF
113 close TEST;
114}
115
116sub openTestInEditor()
117{
118 my $editor = $ENV{EDITOR};
119 exec ($editor, $testAbsolutePath) if ($editor);
120}