Skip tests on arm/mips that time out now we're running on CLoop
[WebKit-https.git] / LayoutTests / html5lib / generate-test-wrappers.py
1 #!/usr/bin/env python
2 # Copyright (c) 2013 Google 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 are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #
30 # Used for generating LayoutTest-compatible html files to run html5lib *.dat files.
31
32 import os
33 import glob
34
35
36 class WrapperGenerator(object):
37     INPUT_DIRECTORY = "resources"
38     INPUT_SUFFIX = ".dat"
39     OUTPUT_DIRECTORY = "generated"
40     HARNESS_PREFIX = "run-"
41     HARNESS_SUFFIX = ".html"
42     EXPECTAION_SUFFIX = "-expected.txt"
43     HARNESS_TYPES = ("write", "data")
44
45     HARNESS_TEMPLATE = """<!DOCTYPE html>
46 <script>
47 var test_files = [ '%(test_path)s' ]
48 </script>
49 <script src="../../resources/dump-as-markup.js"></script>
50 %(extra_content)s
51 <script src="../resources/runner.js"></script>
52 """
53
54     def _files_in_directory_with_suffix(self, directory, suffix):
55         return glob.glob(os.path.join(directory, '*' + suffix))
56
57     def _last_path_component_removing_suffix(self, path, suffix):
58         return os.path.split(path)[-1][:-len(suffix)]
59
60     def _remove_harness_prefix(self, name):
61         assert(name.startswith(self.HARNESS_PREFIX))
62         return name[len(self.HARNESS_PREFIX):]
63
64     def _remove_harness_type(self, name):
65         parts = name.split('-')
66         assert(parts[-1] in self.HARNESS_TYPES)
67         return "-".join(parts[:-1])
68
69     def _test_name_from_harness_name(self, name):
70         name = self._remove_harness_prefix(name)
71         return self._remove_harness_type(name)
72
73     def _remove_stale_tests(self, test_names):
74         for path in self._files_in_directory_with_suffix(self.OUTPUT_DIRECTORY, self.HARNESS_SUFFIX):
75             name = self._last_path_component_removing_suffix(path, self.HARNESS_SUFFIX)
76             name = self._test_name_from_harness_name(name)
77             if name not in test_names:
78                 print("Removing %s, %s no longer exists." % (path, self._input_path(name)))
79                 os.remove(path)
80
81         for path in self._files_in_directory_with_suffix(self.OUTPUT_DIRECTORY, self.EXPECTAION_SUFFIX):
82             name = self._last_path_component_removing_suffix(path, self.EXPECTAION_SUFFIX)
83             name = self._test_name_from_harness_name(name)
84             if name not in test_names:
85                 print("Removing %s, %s no longer exists." % (path, self._input_path(name)))
86                 os.remove(path)
87
88     def _input_path(self, test_name):
89         return os.path.join(self.INPUT_DIRECTORY, test_name + self.INPUT_SUFFIX)
90
91     def _harness_path(self, test_name, use_write):
92         harness_path = os.path.join(self.OUTPUT_DIRECTORY, self.HARNESS_PREFIX + test_name)
93         if use_write:
94             harness_path += "-write"
95         else:
96             harness_path += "-data"
97         return harness_path + self.HARNESS_SUFFIX
98
99     def _harness_content(self, test_name, use_write):
100         extra_content = ""
101         if not use_write:
102             extra_content = "<script>window.forceDataURLs = true;</script>";
103         return self.HARNESS_TEMPLATE % {
104             # FIXME: .. should be relative to the number of components in OUTPUT_DIRECTORY
105             'test_path': os.path.join('..', self._input_path(test_name)),
106             'extra_content': extra_content,
107         }
108
109     def _write_harness(self, test_name, use_write):
110         harness_file = open(self._harness_path(test_name, use_write), "w")
111         harness_file.write(self._harness_content(test_name, use_write))
112
113     def main(self):
114         test_names = [self._last_path_component_removing_suffix(path, self.INPUT_SUFFIX) for path in self._files_in_directory_with_suffix(self.INPUT_DIRECTORY, self.INPUT_SUFFIX)]
115
116         self._remove_stale_tests(test_names)
117
118         for name in test_names:
119             self._write_harness(name, True)
120             self._write_harness(name, False)
121
122
123 if __name__ == "__main__":
124     WrapperGenerator().main()