Skip to content

Commit

Permalink
[wptserve] Put // META: scripts after testharness.js (#11344)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zcorpan authored and foolip committed Jun 19, 2018
1 parent 0b687e0 commit bde5231
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
10 changes: 10 additions & 0 deletions infrastructure/server/order-of-metas.any.js
Original file line number Diff line number Diff line change
@@ -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");
8 changes: 8 additions & 0 deletions infrastructure/server/order-of-metas.window.js
Original file line number Diff line number Diff line change
@@ -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");
5 changes: 5 additions & 0 deletions infrastructure/server/resources/expect-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test(() => {
assert_true('GLOBAL' in self);
}, 'GLOBAL exists');

scripts.push('expect-global.js');
5 changes: 5 additions & 0 deletions infrastructure/server/resources/expect-seen-testharness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test(() => {
assert_true('add_completion_callback' in self);
}, 'add_completion_callback exists');

var scripts = ['expect-seen-testharness.js'];
11 changes: 11 additions & 0 deletions infrastructure/server/resources/expect-title-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (!self.GLOBAL || self.GLOBAL.isWindow()) {
test(() => {
assert_equals(document.title, "foo");
}, '<title> exists');

test(() => {
assert_equals(document.querySelectorAll("meta[name=timeout][content=long]").length, 1);
}, '<meta name=timeout> exists');
}

scripts.push('expect-title-meta.js');
33 changes: 27 additions & 6 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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.
"""
Expand All @@ -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)
Expand Down Expand Up @@ -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("&", "&amp;").replace('"', "&quot;")
return '<script src="%s"></script>' % attribute
if key == b"title":
value = value.decode('utf-8').replace("&", "&amp;").replace("<", "&lt;")
return '<title>%s</title>' % value
return None

def _script_replacement(self, key, value):
if key == b"script":
attribute = value.decode('utf-8').replace("&", "&amp;").replace('"', "&quot;")
return '<script src="%s"></script>' % attribute
return None


class WorkersHandler(HtmlWrapperHandler):
global_type = b"dedicatedworker"
Expand All @@ -191,6 +206,7 @@ class WindowHandler(HtmlWrapperHandler):
%(meta)s
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
%(script)s
<div id=log></div>
<script src="%(path)s"></script>
"""
Expand All @@ -210,6 +226,7 @@ class AnyHtmlHandler(HtmlWrapperHandler):
</script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
%(script)s
<div id=log></div>
<script src="%(path)s"></script>
"""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bde5231

Please sign in to comment.