make-new-script-test 3.8 KB

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