Skip to content

Commit

Permalink
Convert all script metadata to text
Browse files Browse the repository at this point in the history
Otherwise we were getting a different type when loading the manifest
compared to when reading data from the initial file. This caused problems
in Python 3.
  • Loading branch information
jgraham committed May 15, 2020
1 parent be7177f commit e7c8602
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion tools/manifest/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def to_json(self):
if self.quic is not None:
rv[-1]["quic"] = self.quic
if self.script_metadata:
rv[-1]["script_metadata"] = [(k.decode('utf8'), v.decode('utf8')) for (k,v) in self.script_metadata]
rv[-1]["script_metadata"] = [(k, v) for (k,v) in self.script_metadata]
return rv


Expand Down
50 changes: 27 additions & 23 deletions tools/manifest/sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ def read_script_metadata(f, regexp):
if not m:
break

yield (m.groups()[0], m.groups()[1])
yield (m.groups()[0].decode("utf8"), m.groups()[1].decode("utf8"))


_any_variants = {
b"window": {"suffix": ".any.html"},
b"serviceworker": {"force_https": True},
b"sharedworker": {},
b"dedicatedworker": {"suffix": ".any.worker.html"},
b"worker": {"longhand": {b"dedicatedworker", b"sharedworker", b"serviceworker"}},
b"jsshell": {"suffix": ".any.js"},
"window": {"suffix": ".any.html"},
"serviceworker": {"force_https": True},
"sharedworker": {},
"dedicatedworker": {"suffix": ".any.worker.html"},
"worker": {"longhand": {"dedicatedworker", "sharedworker", "serviceworker"}},
"jsshell": {"suffix": ".any.js"},
} # type: Dict[bytes, Dict[str, Any]]


Expand All @@ -88,7 +88,7 @@ def get_any_variants(item):
"""
Returns a set of variants (bytestrings) defined by the given keyword.
"""
assert isinstance(item, binary_type), item
assert isinstance(item, text_type), item

variant = _any_variants.get(item, None)
if variant is None:
Expand All @@ -102,42 +102,46 @@ def get_default_any_variants():
"""
Returns a set of variants (bytestrings) that will be used by default.
"""
return set({b"window", b"dedicatedworker"})
return set({"window", "dedicatedworker"})


def parse_variants(value):
# type: (bytes) -> Set[bytes]
# type: (Text) -> Set[Text]
"""
Returns a set of variants (bytestrings) defined by a comma-separated value.
"""
assert isinstance(value, binary_type), value
assert isinstance(value, text_type), value

if value == b"":
if value == "":
return get_default_any_variants()

globals = set()
for item in value.split(b","):
for item in value.split(","):
item = item.strip()
globals |= get_any_variants(item)
return globals


def global_suffixes(value):
# type: (bytes) -> Set[Tuple[bytes, bool]]
# type: (Text) -> Set[Tuple[Text, bool]]
"""
Yields tuples of the relevant filename suffix (a string) and whether the
variant is intended to run in a JS shell, for the variants defined by the
given comma-separated value.
"""
assert isinstance(value, binary_type), value
try:
assert isinstance(value, text_type), value
except Exception:
import pdb
pdb.post_mortem()

rv = set()

global_types = parse_variants(value)
for global_type in global_types:
variant = _any_variants[global_type]
suffix = variant.get("suffix", ".any.%s.html" % global_type.decode("utf-8"))
rv.add((suffix, global_type == b"jsshell"))
suffix = variant.get("suffix", ".any.%s.html" % global_type)
rv.add((suffix, global_type == "jsshell"))

return rv

Expand Down Expand Up @@ -462,7 +466,7 @@ def timeout_nodes(self):

@cached_property
def script_metadata(self):
# type: () -> Optional[List[Tuple[bytes, bytes]]]
# type: () -> Optional[List[Tuple[Text, Text]]]
if self.name_is_worker or self.name_is_multi_global or self.name_is_window:
regexp = js_meta_re
elif self.name_is_webdriver:
Expand All @@ -479,7 +483,7 @@ def timeout(self):
"""The timeout of a test or reference file. "long" if the file has an extended timeout
or None otherwise"""
if self.script_metadata:
if any(m == (b"timeout", b"long") for m in self.script_metadata):
if any(m == ("timeout", "long") for m in self.script_metadata):
return "long"

if self.root is None:
Expand Down Expand Up @@ -641,8 +645,8 @@ def test_variants(self):
script_metadata = self.script_metadata
assert script_metadata is not None
for (key, value) in script_metadata:
if key == b"variant":
rv.append(value.decode("utf-8"))
if key == "variant":
rv.append(value)
else:
for element in self.variant_nodes:
if "content" in element.attrib:
Expand Down Expand Up @@ -864,11 +868,11 @@ def manifest_items(self):
)]

elif self.name_is_multi_global:
globals = b""
globals = u""
script_metadata = self.script_metadata
assert script_metadata is not None
for (key, value) in script_metadata:
if key == b"global":
if key == "global":
globals = value
break

Expand Down
8 changes: 4 additions & 4 deletions tools/manifest/tests/test_sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_worker_long_timeout():
test()"""

metadata = list(read_script_metadata(BytesIO(contents), js_meta_re))
assert metadata == [(b"timeout", b"long")]
assert metadata == [("timeout", "long")]

s = create("html/test.worker.js", contents=contents)
assert s.name_is_worker
Expand All @@ -197,7 +197,7 @@ def test_window_long_timeout():
test()"""

metadata = list(read_script_metadata(BytesIO(contents), js_meta_re))
assert metadata == [(b"timeout", b"long")]
assert metadata == [("timeout", "long")]

s = create("html/test.window.js", contents=contents)
assert s.name_is_window
Expand Down Expand Up @@ -278,7 +278,7 @@ def test_python_long_timeout():

metadata = list(read_script_metadata(BytesIO(contents),
python_meta_re))
assert metadata == [(b"timeout", b"long")]
assert metadata == [("timeout", "long")]

s = create("webdriver/test.py", contents=contents)
assert s.name_is_webdriver
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_multi_global_long_timeout():
test()"""

metadata = list(read_script_metadata(BytesIO(contents), js_meta_re))
assert metadata == [(b"timeout", b"long")]
assert metadata == [("timeout", "long")]

s = create("html/test.any.js", contents=contents)
assert s.name_is_multi_global
Expand Down
24 changes: 12 additions & 12 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ class HtmlWrapperHandler(WrapperHandler):

def check_exposure(self, request):
if self.global_type:
globals = b""
globals = ""
for (key, value) in self._get_metadata(request):
if key == b"global":
if key == "global":
globals = value
break

Expand All @@ -189,17 +189,17 @@ def check_exposure(self, request):
self.global_type)

def _meta_replacement(self, key, value):
if key == b"timeout":
if value == b"long":
if key == "timeout":
if value == "long":
return '<meta name="timeout" content="long">'
if key == b"title":
value = value.decode('utf-8').replace("&", "&amp;").replace("<", "&lt;")
if key == "title":
value = value.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;")
if key == "script":
attribute = value.replace("&", "&amp;").replace('"', "&quot;")
return '<script src="%s"></script>' % attribute
return None

Expand Down Expand Up @@ -307,11 +307,11 @@ 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('"', '\\"')
if key == "script":
attribute = value.replace("\\", "\\\\").replace('"', '\\"')
return 'importScripts("%s")' % attribute
if key == b"title":
value = value.decode('utf-8').replace("\\", "\\\\").replace('"', '\\"')
if key == "title":
value = value.replace("\\", "\\\\").replace('"', '\\"')
return 'self.META_TITLE = "%s";' % value
return None

Expand Down
2 changes: 1 addition & 1 deletion tools/wptrunner/wptrunner/wpttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def from_manifest(cls, manifest_file, manifest_item, inherit_metadata, test_meta
quic = manifest_item.quic if hasattr(manifest_item, "quic") else False
script_metadata = manifest_item.script_metadata or []
scripts = [v for (k, v) in script_metadata
if k in (b"script", "script")]
if k == "script"]
return cls(manifest_file.tests_root,
manifest_item.url,
inherit_metadata,
Expand Down

0 comments on commit e7c8602

Please sign in to comment.