From bde52314cb009dce1a83cb01eae3715ae5f7a1c9 Mon Sep 17 00:00:00 2001 From: Simon Pieters Date: Tue, 19 Jun 2018 18:18:38 +0200 Subject: [PATCH] [wptserve] Put `// META:` scripts after testharness.js (#11344) Some tests, e.g., [1], expect testharness.js to exist in the script. Part of #11269. [1] html/infrastructure/safe-passing-of-structured-data/shared-array-buffers/serialization-via-idb.any.html --- infrastructure/server/order-of-metas.any.js | 10 ++++++ .../server/order-of-metas.window.js | 8 +++++ .../server/resources/expect-global.js | 5 +++ .../resources/expect-seen-testharness.js | 5 +++ .../server/resources/expect-title-meta.js | 11 +++++++ tools/serve/serve.py | 33 +++++++++++++++---- 6 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 infrastructure/server/order-of-metas.any.js create mode 100644 infrastructure/server/order-of-metas.window.js create mode 100644 infrastructure/server/resources/expect-global.js create mode 100644 infrastructure/server/resources/expect-seen-testharness.js create mode 100644 infrastructure/server/resources/expect-title-meta.js diff --git a/infrastructure/server/order-of-metas.any.js b/infrastructure/server/order-of-metas.any.js new file mode 100644 index 00000000000000..20f678475c3484 --- /dev/null +++ b/infrastructure/server/order-of-metas.any.js @@ -0,0 +1,10 @@ +// META: global=window,dedicatedworker,sharedworker +// META: script=resources/expect-seen-testharness.js +// META: timeout=long +// META: title=foo +// META: script=resources/expect-global.js +// META: script=resources/expect-title-meta.js + +test(() => { + assert_array_equals(scripts, ['expect-seen-testharness.js', 'expect-global.js', 'expect-title-meta.js']); +}, "order of scripts"); diff --git a/infrastructure/server/order-of-metas.window.js b/infrastructure/server/order-of-metas.window.js new file mode 100644 index 00000000000000..ec2848056a7b10 --- /dev/null +++ b/infrastructure/server/order-of-metas.window.js @@ -0,0 +1,8 @@ +// META: script=resources/expect-seen-testharness.js +// META: timeout=long +// META: title=foo +// META: script=resources/expect-title-meta.js + +test(() => { + assert_array_equals(scripts, ['expect-seen-testharness.js', 'expect-title-meta.js']); +}, "order of scripts"); diff --git a/infrastructure/server/resources/expect-global.js b/infrastructure/server/resources/expect-global.js new file mode 100644 index 00000000000000..63d4944e61ffc7 --- /dev/null +++ b/infrastructure/server/resources/expect-global.js @@ -0,0 +1,5 @@ +test(() => { + assert_true('GLOBAL' in self); +}, 'GLOBAL exists'); + +scripts.push('expect-global.js'); diff --git a/infrastructure/server/resources/expect-seen-testharness.js b/infrastructure/server/resources/expect-seen-testharness.js new file mode 100644 index 00000000000000..29af1fca55b8df --- /dev/null +++ b/infrastructure/server/resources/expect-seen-testharness.js @@ -0,0 +1,5 @@ +test(() => { + assert_true('add_completion_callback' in self); +}, 'add_completion_callback exists'); + +var scripts = ['expect-seen-testharness.js']; diff --git a/infrastructure/server/resources/expect-title-meta.js b/infrastructure/server/resources/expect-title-meta.js new file mode 100644 index 00000000000000..d17588a18f053e --- /dev/null +++ b/infrastructure/server/resources/expect-title-meta.js @@ -0,0 +1,11 @@ +if (!self.GLOBAL || self.GLOBAL.isWindow()) { + test(() => { + assert_equals(document.title, "foo"); + }, ' exists'); + + test(() => { + assert_equals(document.querySelectorAll("meta[name=timeout][content=long]").length, 1); + }, '<meta name=timeout> exists'); +} + +scripts.push('expect-title-meta.js'); diff --git a/tools/serve/serve.py b/tools/serve/serve.py index 10dc3b3bd6cd0c..d3146553b4061d 100644 --- a/tools/serve/serve.py +++ b/tools/serve/serve.py @@ -65,7 +65,8 @@ def handle_request(self, request, response): if query: query = "?" + query meta = "\n".join(self._get_meta(request)) - response.content = self.wrapper % {"meta": meta, "path": path, "query": query} + script = "\n".join(self._get_script(request)) + response.content = self.wrapper % {"meta": meta, "script": script, "path": path, "query": query} wrap_pipeline(path, request, response) def _get_path(self, path, resource_path): @@ -93,7 +94,7 @@ def _get_path(self, path, resource_path): return path def _get_metadata(self, request): - """Get an iterator over script metadata based on //META comments in the + """Get an iterator over script metadata based on // META comments in the associated js file. :param request: The Request being processed. @@ -105,7 +106,7 @@ def _get_metadata(self, request): def _get_meta(self, request): """Get an iterator over strings to inject into the wrapper document - based on //META comments in the associated js file. + based on // META comments in the associated js file. :param request: The Request being processed. """ @@ -114,6 +115,17 @@ def _get_meta(self, request): if replacement: yield replacement + def _get_script(self, request): + """Get an iterator over strings to inject into the wrapper document + based on // META comments in the associated js file. + + :param request: The Request being processed. + """ + for key, value in self._get_metadata(request): + replacement = self._script_replacement(key, value) + if replacement: + yield replacement + @abc.abstractproperty def path_replace(self): # A list containing a mix of 2 item tuples with (input suffix, output suffix) @@ -159,14 +171,17 @@ def _meta_replacement(self, key, value): if key == b"timeout": if value == b"long": return '<meta name="timeout" content="long">' - if key == b"script": - attribute = value.decode('utf-8').replace("&", "&").replace('"', """) - return '<script src="%s"></script>' % attribute if key == b"title": value = value.decode('utf-8').replace("&", "&").replace("<", "<") return '<title>%s' % value return None + def _script_replacement(self, key, value): + if key == b"script": + attribute = value.decode('utf-8').replace("&", "&").replace('"', """) + return '' % attribute + return None + class WorkersHandler(HtmlWrapperHandler): global_type = b"dedicatedworker" @@ -191,6 +206,7 @@ class WindowHandler(HtmlWrapperHandler): %(meta)s +%(script)s
""" @@ -210,6 +226,7 @@ class AnyHtmlHandler(HtmlWrapperHandler): +%(script)s
""" @@ -260,11 +277,15 @@ class AnyWorkerHandler(WrapperHandler): isWorker: function() { return true; }, }; importScripts("/resources/testharness.js"); +%(script)s importScripts("%(path)s"); done(); """ def _meta_replacement(self, key, value): + return None + + def _script_replacement(self, key, value): if key == b"script": attribute = value.decode('utf-8').replace("\\", "\\\\").replace('"', '\\"') return 'importScripts("%s")' % attribute