Meta: Support importing WPT reftests with multiple references

Collect all match and mismatch links when identifying WPT reftests
instead of rejecting tests with more than one reference.

Map and rewrite every imported reference URL so tests with both match
and mismatch expectations can be imported.
This commit is contained in:
Shannon Booth 2026-06-13 08:55:32 +02:00 committed by Shannon Booth
parent ea72bd3863
commit 06f4ac367e

View file

@ -62,8 +62,8 @@ class ResourceAndType:
test_type = TestType.TEXT
raw_reference_path = None # As specified in the test HTML
reference_path = None # With parent directories
raw_reference_paths = [] # As specified in the test HTML
reference_paths = [] # With parent directories
class LinkedResourceFinder(HTMLParser):
@ -142,27 +142,25 @@ class LinkedResourceFinder(HTMLParser):
class TestTypeIdentifier(HTMLParser):
"""Identifies what kind of test the page is, and stores it in self.test_type
For reference tests, the URL of the reference page is saved as self.reference_path
For reference tests, the URLs of the reference pages are saved as self.reference_paths
"""
def __init__(self, url):
super().__init__()
self.url = url
self.test_type = TestType.TEXT
self.reference_path = None
self.ref_test_link_found = False
self.reference_paths = []
def handle_starttag(self, tag, attrs):
if ":" in tag:
tag = tag.split(":", 1)[-1]
if tag == "link":
attr_dict = dict(attrs)
if "rel" in attr_dict and (attr_dict["rel"] == "match" or attr_dict["rel"] == "mismatch"):
if self.ref_test_link_found:
raise RuntimeError("Ref tests with multiple match or mismatch links are not currently supported")
rel_tokens = (attr_dict.get("rel") or "").split()
href = attr_dict.get("href")
if href is not None and ("match" in rel_tokens or "mismatch" in rel_tokens):
self.test_type = TestType.REF
self.reference_path = attr_dict["href"]
self.ref_test_link_found = True
self.reference_paths.append(href)
def map_to_path(
@ -236,9 +234,8 @@ def modify_sources(files, resources: list[ResourceAndType]) -> None:
new_src_value = parent_folder_path + resource[1::]
page_source = page_source.replace(resource.encode(), new_src_value.encode())
# Look for mentions of the reference page, and update their href
if raw_reference_path is not None:
assert reference_path is not None
# Look for mentions of reference pages, and update their hrefs
for raw_reference_path, reference_path in zip(raw_reference_paths, reference_paths):
new_reference_path = parent_folder_path + "../../expected/wpt-import/" + reference_path[::]
page_source = page_source.replace(raw_reference_path.encode(), new_reference_path.encode())
@ -369,24 +366,24 @@ def main():
# replacing undecodable bytes with U+FFFD is safe and avoids a UnicodeDecodeError.
page = response.read().decode("utf-8", errors="replace")
global test_type, reference_path, raw_reference_path
global test_type, reference_paths, raw_reference_paths
if is_crash_test(url_to_import):
test_type = TestType.CRASH
else:
identifier = TestTypeIdentifier(url_to_import)
identifier.feed(page)
test_type = identifier.test_type
raw_reference_path = identifier.reference_path
raw_reference_paths = identifier.reference_paths
print(f"Identified {url_to_import} as type {test_type}, ref {raw_reference_path}")
print(f"Identified {url_to_import} as type {test_type}, refs {raw_reference_paths}")
main_file = [ResourceAndType(resource_path, ResourceType.INPUT)]
main_paths = map_to_path(main_file, wpt_base_url, False)
if test_type == TestType.REF and raw_reference_path is None:
if test_type == TestType.REF and not raw_reference_paths:
raise RuntimeError("Failed to file reference path in ref test")
if raw_reference_path is not None:
for raw_reference_path in raw_reference_paths:
if raw_reference_path.startswith("/"):
reference_path = raw_reference_path
main_paths.append(
@ -403,6 +400,7 @@ def main():
Path(test_type.expected_path + "/" + reference_path).absolute(),
)
)
reference_paths.append(reference_path)
files_to_modify = download_files(main_paths, wpt_base_url, skip_existing)
download_headers_files(main_paths, wpt_base_url, skip_existing)