2 # Copyright (c) 2013 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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
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.
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.
30 # Used for generating LayoutTest-compatible html files to run html5lib *.dat files.
36 class WrapperGenerator(object):
37 INPUT_DIRECTORY = "resources"
39 OUTPUT_DIRECTORY = "generated"
40 HARNESS_PREFIX = "run-"
41 HARNESS_SUFFIX = ".html"
42 EXPECTAION_SUFFIX = "-expected.txt"
43 HARNESS_TYPES = ("write", "data")
45 HARNESS_TEMPLATE = """<!DOCTYPE html>
47 var test_files = [ '%(test_path)s' ]
49 <script src="../../resources/dump-as-markup.js"></script>
51 <script src="../resources/runner.js"></script>
54 def _files_in_directory_with_suffix(self, directory, suffix):
55 return glob.glob(os.path.join(directory, '*' + suffix))
57 def _last_path_component_removing_suffix(self, path, suffix):
58 return os.path.split(path)[-1][:-len(suffix)]
60 def _remove_harness_prefix(self, name):
61 assert(name.startswith(self.HARNESS_PREFIX))
62 return name[len(self.HARNESS_PREFIX):]
64 def _remove_harness_type(self, name):
65 parts = name.split('-')
66 assert(parts[-1] in self.HARNESS_TYPES)
67 return "-".join(parts[:-1])
69 def _test_name_from_harness_name(self, name):
70 name = self._remove_harness_prefix(name)
71 return self._remove_harness_type(name)
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))
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))
88 def _input_path(self, test_name):
89 return os.path.join(self.INPUT_DIRECTORY, test_name + self.INPUT_SUFFIX)
91 def _harness_path(self, test_name, use_write):
92 harness_path = os.path.join(self.OUTPUT_DIRECTORY, self.HARNESS_PREFIX + test_name)
94 harness_path += "-write"
96 harness_path += "-data"
97 return harness_path + self.HARNESS_SUFFIX
99 def _harness_content(self, test_name, 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,
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))
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)]
116 self._remove_stale_tests(test_names)
118 for name in test_names:
119 self._write_harness(name, True)
120 self._write_harness(name, False)
123 if __name__ == "__main__":
124 WrapperGenerator().main()