tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

wpt_path_utils.py (4093B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 """
      6 Utilities for working with Web Platform Test (WPT) paths.
      7 """
      8 
      9 from typing import Dict, Optional  # noqa EP035
     10 
     11 OptJs = Optional[Dict[str, bool]]  # noqa UP006
     12 
     13 WP = "testing/web-platform/"
     14 WPT0 = WP + "tests/infrastructure"
     15 WPT_META0 = WP + "tests/infrastructure/metadata"
     16 WPT_META0_CLASSIC = WP + "meta/infrastructure"
     17 WPT1 = WP + "tests"
     18 WPT_META1 = WPT1.replace("tests", "meta")
     19 WPT2 = WP + "mozilla/tests"
     20 WPT_META2 = WPT2.replace("tests", "meta")
     21 WPT_MOZILLA = "/_mozilla"
     22 
     23 
     24 def resolve_wpt_path(shortpath: str) -> str:
     25    """
     26    Resolves a short WPT path to its full path in the Firefox tree.
     27 
     28    Args:
     29        shortpath: A short WPT path like "/css/foo.html" or "/_mozilla/bar.html"
     30 
     31    Returns:
     32        The full path like "testing/web-platform/tests/css/foo.html"
     33    """
     34    if shortpath.startswith(WPT0):
     35        return shortpath
     36    elif shortpath.startswith(WPT1):
     37        return shortpath
     38    elif shortpath.startswith(WPT2):
     39        return shortpath
     40    elif shortpath.startswith(WPT_MOZILLA):
     41        shortpath = shortpath[len(WPT_MOZILLA) :]
     42        return WPT2 + shortpath
     43    else:
     44        return WPT1 + shortpath
     45 
     46 
     47 def get_wpt_metadata_path(path: str) -> str:
     48    """
     49    Gets the metadata path for a given WPT path.
     50 
     51    Args:
     52        path: A WPT path (can be short or full)
     53 
     54    Returns:
     55        The corresponding metadata path
     56    """
     57    if path.startswith(WPT0):
     58        return path.replace(WPT0, WPT_META0, 1)
     59    elif path.startswith(WPT1):
     60        return path.replace(WPT1, WPT_META1, 1)
     61    elif path.startswith(WPT2):
     62        return path.replace(WPT2, WPT_META2, 1)
     63    elif path.startswith(WPT_MOZILLA):
     64        shortpath = path[len(WPT_MOZILLA) :]
     65        return WPT_META2 + shortpath
     66    else:
     67        return WPT_META1 + path
     68 
     69 
     70 def get_wpt_path_and_metadata(shortpath: str) -> tuple[str, str]:
     71    """
     72    Gets both the resolved WPT path and its metadata path.
     73 
     74    Args:
     75        shortpath: A short or full WPT path
     76 
     77    Returns:
     78        A tuple of (path, metadata_path)
     79    """
     80    path = resolve_wpt_path(shortpath)
     81    meta = get_wpt_metadata_path(shortpath)
     82    return (path, meta)
     83 
     84 
     85 def parse_wpt_path(
     86    shortpath: str, isdir_fn: Optional[callable] = None
     87 ) -> tuple[Optional[str], Optional[str], Optional[str], Optional[str]]:
     88    """
     89    Analyzes the WPT short path for a test and returns detailed information.
     90 
     91    Args:
     92        shortpath: A WPT path that may include query parameters
     93        isdir_fn: Optional function to check if a path is a directory
     94 
     95    Returns:
     96        A tuple of (path, manifest, query, anyjs) where:
     97        - path is the relative path to the test file
     98        - manifest is the relative path to the file metadata
     99        - query is the test file query parameters (or None)
    100        - anyjs is the html test file as reported by mozci (or None)
    101    """
    102    query: Optional[str] = None
    103    anyjs: OptJs = None
    104 
    105    i = shortpath.find("?")
    106    if i > 0:
    107        query = shortpath[i:]
    108        shortpath = shortpath[0:i]
    109 
    110    path, manifest = get_wpt_path_and_metadata(shortpath)
    111 
    112    failure_type = True
    113    if isdir_fn:
    114        failure_type = not isdir_fn(path)
    115 
    116    if failure_type:
    117        i = path.find(".any.")
    118        if i > 0:
    119            anyjs = {path: False}
    120            manifest = manifest.replace(path[i:], ".any.js")
    121            path = path[0:i] + ".any.js"
    122        else:
    123            i = path.find(".window.")
    124            if i > 0:
    125                anyjs = {path: False}
    126                manifest = manifest.replace(path[i:], ".window.js")
    127                path = path[0:i] + ".window.js"
    128            else:
    129                i = path.find(".worker.")
    130                if i > 0:
    131                    anyjs = {path: False}
    132                    manifest = manifest.replace(path[i:], ".worker.js")
    133                    path = path[0:i] + ".worker.js"
    134        manifest += ".ini"
    135 
    136    return (path, manifest, query, anyjs)