tor-browser

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

test_get_active_tests.py (6471B)


      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 import os
      6 from argparse import Namespace
      7 from collections import defaultdict
      8 from textwrap import dedent
      9 
     10 import mozunit
     11 import pytest
     12 from conftest import setup_args
     13 from manifestparser import TestManifest
     14 
     15 
     16 @pytest.fixture
     17 def get_active_tests(setup_test_harness, parser):
     18    setup_test_harness(*setup_args)
     19    runtests = pytest.importorskip("runtests")
     20    md = runtests.MochitestDesktop("plain", {"log_tbpl": "-"})
     21 
     22    options = vars(parser.parse_args([]))
     23 
     24    def inner(**kwargs):
     25        opts = options.copy()
     26        opts.update(kwargs)
     27 
     28        manifest = opts.get("manifestFile")
     29        if isinstance(manifest, str):
     30            md.testRootAbs = os.path.dirname(manifest)
     31        elif isinstance(manifest, TestManifest):
     32            md.testRootAbs = manifest.rootdir
     33 
     34        md._active_tests = None
     35        md.prefs_by_manifest = defaultdict(set)
     36        return md, md.getActiveTests(Namespace(**opts))
     37 
     38    return inner
     39 
     40 
     41 @pytest.fixture
     42 def create_manifest(tmpdir, build_obj):
     43    def inner(string, name="manifest.ini"):
     44        manifest = tmpdir.join(name)
     45        manifest.write(string, ensure=True)
     46        path = str(manifest)
     47        return TestManifest(manifests=(path,), strict=False, rootdir=tmpdir.strpath)
     48 
     49    return inner
     50 
     51 
     52 def test_args_validation(get_active_tests, create_manifest):
     53    # Test args set in a single manifest.
     54    manifest_relpath = "manifest.ini"
     55    manifest = create_manifest(
     56        dedent(
     57            """
     58    [DEFAULT]
     59    args=
     60      --cheese
     61      --foo=bar
     62      --foo1 bar1
     63 
     64    [files/test_pass.html]
     65    [files/test_fail.html]
     66    """
     67        )
     68    )
     69 
     70    options = {
     71        "runByManifest": True,
     72        "manifestFile": manifest,
     73    }
     74    md, tests = get_active_tests(**options)
     75 
     76    assert len(tests) == 2
     77    assert manifest_relpath in md.args_by_manifest
     78 
     79    args = md.args_by_manifest[manifest_relpath]
     80    assert len(args) == 1
     81    assert args.pop() == "\n--cheese\n--foo=bar\n--foo1 bar1"
     82 
     83    # Test args set with runByManifest disabled.
     84    options["runByManifest"] = False
     85    with pytest.raises(SystemExit):
     86        get_active_tests(**options)
     87 
     88    # Test args set in non-default section.
     89    options["runByManifest"] = True
     90    options["manifestFile"] = create_manifest(
     91        dedent(
     92            """
     93    [files/test_pass.html]
     94    args=--foo2=bar2
     95    [files/test_fail.html]
     96    """
     97        )
     98    )
     99    with pytest.raises(SystemExit):
    100        get_active_tests(**options)
    101 
    102 
    103 def test_args_validation_with_ancestor_manifest(get_active_tests, create_manifest):
    104    # Test args set by an ancestor manifest.
    105    create_manifest(
    106        dedent(
    107            """
    108    [DEFAULT]
    109    args=
    110      --cheese
    111 
    112    [files/test_pass.html]
    113    [files/test_fail.html]
    114    """
    115        ),
    116        name="subdir/manifest.ini",
    117    )
    118 
    119    manifest = create_manifest(
    120        dedent(
    121            """
    122    [DEFAULT]
    123    args =
    124      --foo=bar
    125 
    126    [include:manifest.ini]
    127    [test_foo.html]
    128    """
    129        ),
    130        name="subdir/ancestor-manifest.ini",
    131    )
    132 
    133    options = {
    134        "runByManifest": True,
    135        "manifestFile": manifest,
    136    }
    137 
    138    md, tests = get_active_tests(**options)
    139    assert len(tests) == 3
    140 
    141    key = os.path.join("subdir", "ancestor-manifest.ini")
    142    assert key in md.args_by_manifest
    143    args = md.args_by_manifest[key]
    144    assert len(args) == 1
    145    assert args.pop() == "\n--foo=bar"
    146 
    147    key = "{}:{}".format(
    148        os.path.join("subdir", "ancestor-manifest.ini"),
    149        os.path.join("subdir", "manifest.ini"),
    150    )
    151    assert key in md.args_by_manifest
    152    args = md.args_by_manifest[key]
    153    assert len(args) == 1
    154    assert args.pop() == "\n--foo=bar \n--cheese"
    155 
    156 
    157 def test_prefs_validation(get_active_tests, create_manifest):
    158    # Test prefs set in a single manifest.
    159    manifest_relpath = "manifest.ini"
    160    manifest = create_manifest(
    161        dedent(
    162            """
    163    [DEFAULT]
    164    prefs=
    165      foo=bar
    166      browser.dom.foo=baz
    167 
    168    [files/test_pass.html]
    169    [files/test_fail.html]
    170    """
    171        )
    172    )
    173 
    174    options = {
    175        "runByManifest": True,
    176        "manifestFile": manifest,
    177    }
    178    md, tests = get_active_tests(**options)
    179 
    180    assert len(tests) == 2
    181    assert manifest_relpath in md.prefs_by_manifest
    182 
    183    prefs = md.prefs_by_manifest[manifest_relpath]
    184    assert len(prefs) == 1
    185    assert prefs.pop() == "\nfoo=bar\nbrowser.dom.foo=baz"
    186 
    187    # Test prefs set with runByManifest disabled.
    188    options["runByManifest"] = False
    189    with pytest.raises(SystemExit):
    190        get_active_tests(**options)
    191 
    192    # Test prefs set in non-default section.
    193    options["runByManifest"] = True
    194    options["manifestFile"] = create_manifest(
    195        dedent(
    196            """
    197    [files/test_pass.html]
    198    prefs=foo=bar
    199    [files/test_fail.html]
    200    """
    201        )
    202    )
    203    with pytest.raises(SystemExit):
    204        get_active_tests(**options)
    205 
    206 
    207 def test_prefs_validation_with_ancestor_manifest(get_active_tests, create_manifest):
    208    # Test prefs set by an ancestor manifest.
    209    create_manifest(
    210        dedent(
    211            """
    212    [DEFAULT]
    213    prefs=
    214      foo=bar
    215      browser.dom.foo=baz
    216 
    217    [files/test_pass.html]
    218    [files/test_fail.html]
    219    """
    220        ),
    221        name="subdir/manifest.ini",
    222    )
    223 
    224    manifest = create_manifest(
    225        dedent(
    226            """
    227    [DEFAULT]
    228    prefs =
    229      browser.dom.foo=fleem
    230      flower=rose
    231 
    232    [include:manifest.ini]
    233    [test_foo.html]
    234    """
    235        ),
    236        name="subdir/ancestor-manifest.ini",
    237    )
    238 
    239    options = {
    240        "runByManifest": True,
    241        "manifestFile": manifest,
    242    }
    243 
    244    md, tests = get_active_tests(**options)
    245    assert len(tests) == 3
    246 
    247    key = os.path.join("subdir", "ancestor-manifest.ini")
    248    assert key in md.prefs_by_manifest
    249    prefs = md.prefs_by_manifest[key]
    250    assert len(prefs) == 1
    251    assert prefs.pop() == "\nbrowser.dom.foo=fleem\nflower=rose"
    252 
    253    key = "{}:{}".format(
    254        os.path.join("subdir", "ancestor-manifest.ini"),
    255        os.path.join("subdir", "manifest.ini"),
    256    )
    257    assert key in md.prefs_by_manifest
    258    prefs = md.prefs_by_manifest[key]
    259    assert len(prefs) == 1
    260    assert (
    261        prefs.pop()
    262        == "\nbrowser.dom.foo=fleem\nflower=rose\n\nfoo=bar\nbrowser.dom.foo=baz"
    263    )
    264 
    265 
    266 if __name__ == "__main__":
    267    mozunit.main()