tor-browser

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

test_toml.py (7590B)


      1 #!/usr/bin/env python
      2 
      3 # This Source Code Form is subject to the terms of the Mozilla Public
      4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 # You can obtain one at http://mozilla.org/MPL/2.0/.
      6 
      7 import os
      8 
      9 import mozunit
     10 import pytest
     11 from manifestparser import ManifestParser
     12 from manifestparser.toml import (
     13    DEFAULT_SECTION,
     14    Carry,
     15    Mode,
     16    add_skip_if,
     17    alphabetize_toml_str,
     18    replace_tbd_skip_if,
     19 )
     20 from tomlkit.toml_document import TOMLDocument
     21 
     22 here = os.path.dirname(os.path.abspath(__file__))
     23 
     24 
     25 def test_add_skip_if(tmp_path):
     26    parser = ManifestParser(use_toml=True, document=True)
     27    filename = "aaa.js"
     28    contents = f'[{DEFAULT_SECTION}]\n\n["{filename}"]'
     29    manifest_path = tmp_path / "add_skip_if.toml"
     30    manifest_path.write_text(contents)
     31    parser.read(str(manifest_path))
     32    document = parser.source_documents[str(manifest_path)]
     33    assert DEFAULT_SECTION in document
     34    skip_if = "true"
     35    bug_reference = "Bug 2003869"
     36    additional_comment, carryover, bug_reference = add_skip_if(
     37        document,
     38        filename,
     39        skip_if,
     40        bug_reference,
     41        None,
     42        Mode.NORMAL,
     43    )
     44    assert bug_reference is not None  # skip-if should not be ignored
     45    manifest_str = alphabetize_toml_str(document)
     46    assert (
     47        manifest_str
     48        == """[DEFAULT]
     49 
     50 ["aaa.js"]
     51 skip-if = [
     52  "true", # Bug 2003869
     53 ]
     54 """
     55    )
     56 
     57 
     58 def test_replace_tbd_skip_if():
     59    parser = ManifestParser(use_toml=True, document=True)
     60    before = "replace-tbd-before.toml"
     61    before_path = os.path.join(here, before)
     62    parser.read(before_path)
     63    assert before_path in parser.source_documents
     64    manifest = parser.source_documents[before_path]
     65    assert manifest is not None
     66    assert isinstance(manifest, TOMLDocument)
     67 
     68    filename = "non-existant.js"
     69    condition = "os == 'android' && asan"
     70    bugid = "100"
     71    updated: bool = False
     72    with pytest.raises(Exception) as e:
     73        updated = replace_tbd_skip_if(manifest, filename, condition, bugid)
     74        assert updated  # Fail here if no exception thrown
     75    assert str(e.value) == "TOML manifest does not contain section: non-existant.js"
     76    filename = "DEFAULT"
     77    with pytest.raises(Exception) as e:
     78        updated = replace_tbd_skip_if(manifest, filename, condition, bugid)
     79        assert updated  # Fail here if no exception thrown
     80    assert (
     81        str(e.value)
     82        == "TOML manifest for section: DEFAULT does not contain a skip-if condition"
     83    )
     84 
     85    filename = "bug_100.js"
     86    updated = replace_tbd_skip_if(manifest, filename, condition, bugid)
     87    assert updated
     88 
     89    filename = "bug_3.js"
     90    condition = "os == 'linux'"
     91    bugid = "33333"
     92    updated = replace_tbd_skip_if(manifest, filename, condition, bugid)
     93    assert updated
     94 
     95    filename = "test_bar.html"
     96    condition = "os == 'linux'"
     97    bugid = "222"
     98    updated = replace_tbd_skip_if(manifest, filename, condition, bugid)
     99    assert updated
    100 
    101    filename = "test_extend_linux.js"
    102    condition = "os == 'mac'"
    103    bugid = "111"
    104    updated = replace_tbd_skip_if(manifest, filename, condition, bugid)
    105    assert updated
    106 
    107    manifest_str = alphabetize_toml_str(manifest)
    108    after = "replace-tbd-after.toml"
    109    after_path = os.path.join(here, after)
    110    after_str = open(after_path, encoding="utf-8").read()
    111    assert manifest_str == after_str
    112 
    113 
    114 @pytest.fixture(scope="session")
    115 def carry():
    116    c = Carry()
    117    yield c
    118 
    119 
    120 @pytest.mark.parametrize(
    121    "test_index, e_condition, condition, expected",  # test_index for convenience
    122    [
    123        (
    124            1,
    125            "os == 'android' && processor == 'x86_64'",
    126            "os == 'android' && os_version == '14' && processor == 'x86_64'",
    127            True,
    128        ),
    129        (
    130            2,
    131            "http3",
    132            "os == 'linux' && os_version == '24.04' && processor == 'x86_64' && display == 'x11' && xorigin",
    133            False,
    134        ),
    135        (
    136            3,
    137            "http3",
    138            "os == 'linux' && os_version == '24.04' && processor == 'x86_64' && display == 'x11' && xorigin && debug",
    139            False,
    140        ),
    141        (
    142            4,
    143            "os == 'android' && debug",
    144            "os == 'android' && os_version == '14' && debug",
    145            True,
    146        ),
    147        (
    148            5,
    149            "os == 'android' && !debug",
    150            "os == 'android' && os_version == '14' && debug",
    151            False,
    152        ),
    153        (
    154            6,
    155            "os == 'android' && debug",
    156            "os == 'android' && os_version == '14' && !debug",
    157            False,
    158        ),
    159        (
    160            7,
    161            "os == 'android'",
    162            "os == 'android' && os_version == '14' && debug",
    163            True,
    164        ),
    165        (
    166            8,
    167            "os == 'android'",
    168            "os == 'android' && os_version == '14' && !debug",
    169            True,
    170        ),
    171        (
    172            9,
    173            "os == 'android' && debug",
    174            "os == 'android' && os_version == '14'",
    175            False,
    176        ),
    177        (
    178            10,
    179            "os == 'android' && !debug",
    180            "os == 'android' && os_version == '14'",
    181            False,
    182        ),
    183        (
    184            11,
    185            "os == 'android' && asan",
    186            "os == 'android' && os_version == '14' && ccov",
    187            True,
    188        ),
    189        (
    190            12,
    191            "os == 'android'",
    192            "os == 'android' && os_version == '14' && ccov",
    193            True,
    194        ),
    195        (
    196            13,
    197            "os == 'android' && tsan",
    198            "os == 'android' && os_version == '14'",
    199            False,
    200        ),
    201        (
    202            14,
    203            "os == 'linux' && debug && socketprocess_networking",
    204            "os == 'android' && debug",
    205            False,
    206        ),
    207        (15, "debug && socketprocess_networking", "os == 'android' && debug", True),
    208        (16, "os == 'linux'", "verify", False),
    209        (17, "os == 'win'", "tsan", False),
    210        (
    211            18,
    212            "os == 'linux' && os_version == '22.04' && debug",
    213            "os == 'linux' && os_version == '24.04' && asan && isolated_debug_process",
    214            False,
    215        ),
    216        (
    217            19,
    218            "os == 'linux' && os_version == '22.04' && isolated_debug_process",
    219            "os == 'linux' && os_version == '24.04' && opt",
    220            True,
    221        ),
    222        (
    223            20,
    224            "os == 'android' && opt",
    225            "os == 'android' && os_version == '14' && !debug",
    226            True,
    227        ),
    228        (
    229            21,
    230            "os == 'android' && !debug",
    231            "os == 'android' && os_version == '14' && opt",
    232            True,
    233        ),
    234        (
    235            22,
    236            "!opt",
    237            "!opt",
    238            True,
    239        ),
    240        (
    241            23,
    242            "!opt",
    243            "asan",
    244            True,
    245        ),
    246        (
    247            24,
    248            "ccov",
    249            "!opt",
    250            True,
    251        ),
    252        (
    253            25,
    254            "debug",
    255            "tsan",
    256            False,
    257        ),
    258        (
    259            26,
    260            "ccov",
    261            "debug",
    262            False,
    263        ),
    264    ],
    265 )
    266 def test_platform_match_for_carryover(
    267    carry: Carry, test_index: int, e_condition: str, condition: str, expected: bool
    268 ):
    269    """
    270    Verify TOML function _condition_is_carryover platform match heuristic
    271    """
    272    assert test_index == carry.test_index()  # help maintain order
    273    assert carry.is_carryover(e_condition, condition) == expected
    274 
    275 
    276 if __name__ == "__main__":
    277    mozunit.main()