tor-browser

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

test_create_directories.py (6761B)


      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 from unittest import mock
     10 
     11 import mozunit
     12 import pytest
     13 from conftest import setup_args
     14 from manifestparser import TestManifest
     15 
     16 
     17 # Directly running runTests() is likely not working nor a good idea
     18 # So at least we try to minimize with just:
     19 #  - getActiveTests()
     20 #  - create manifests list
     21 #  - parseAndCreateTestsDirs()
     22 #
     23 # Hopefully, breaking the runTests() calls to parseAndCreateTestsDirs() will
     24 # anyway trigger other tests failures so it would be spotted, and we at least
     25 # ensure some coverage of handling the manifest content, creation of the
     26 # directories and cleanup
     27 @pytest.fixture
     28 def prepareRunTests(setup_test_harness, parser):
     29    setup_test_harness(*setup_args)
     30    runtests = pytest.importorskip("runtests")
     31    md = runtests.MochitestDesktop("plain", {"log_tbpl": "-"})
     32 
     33    options = vars(parser.parse_args([]))
     34 
     35    def inner(**kwargs):
     36        opts = options.copy()
     37        opts.update(kwargs)
     38 
     39        manifest = opts.get("manifestFile")
     40        if isinstance(manifest, str):
     41            md.testRootAbs = os.path.dirname(manifest)
     42        elif isinstance(manifest, TestManifest):
     43            md.testRootAbs = manifest.rootdir
     44 
     45        md._active_tests = None
     46        md.prefs_by_manifest = defaultdict(set)
     47        tests = md.getActiveTests(Namespace(**opts))
     48        manifests = set(t["manifest"] for t in tests)
     49        for m in sorted(manifests):
     50            md.parseAndCreateTestsDirs(m)
     51        return md
     52 
     53    return inner
     54 
     55 
     56 @pytest.fixture
     57 def create_manifest(tmpdir, build_obj):
     58    def inner(string, name="manifest.ini"):
     59        manifest = tmpdir.join(name)
     60        manifest.write(string, ensure=True)
     61        path = str(manifest)
     62        return TestManifest(manifests=(path,), strict=False, rootdir=tmpdir.strpath)
     63 
     64    return inner
     65 
     66 
     67 def create_manifest_empty(create_manifest):
     68    manifest = create_manifest(
     69        dedent(
     70            """
     71    [DEFAULT]
     72    [files/test_pass.html]
     73    [files/test_fail.html]
     74    """
     75        )
     76    )
     77 
     78    return {
     79        "runByManifest": True,
     80        "manifestFile": manifest,
     81    }
     82 
     83 
     84 def create_manifest_one(create_manifest):
     85    manifest = create_manifest(
     86        dedent(
     87            """
     88    [DEFAULT]
     89    test-directories =
     90        .snap_firefox_current_real
     91    [files/test_pass.html]
     92    [files/test_fail.html]
     93    """
     94        )
     95    )
     96 
     97    return {
     98        "runByManifest": True,
     99        "manifestFile": manifest,
    100    }
    101 
    102 
    103 def create_manifest_mult(create_manifest):
    104    manifest = create_manifest(
    105        dedent(
    106            """
    107    [DEFAULT]
    108    test-directories =
    109        .snap_firefox_current_real
    110        .snap_firefox_current_real2
    111    [files/test_pass.html]
    112    [files/test_fail.html]
    113    """
    114        )
    115    )
    116 
    117    return {
    118        "runByManifest": True,
    119        "manifestFile": manifest,
    120    }
    121 
    122 
    123 def test_no_entry(prepareRunTests, create_manifest):
    124    options = create_manifest_empty(create_manifest)
    125    with mock.patch("os.makedirs") as mock_os_makedirs:
    126        _ = prepareRunTests(**options)
    127        mock_os_makedirs.assert_not_called()
    128 
    129 
    130 def test_one_entry(prepareRunTests, create_manifest):
    131    options = create_manifest_one(create_manifest)
    132    with mock.patch("os.makedirs") as mock_os_makedirs:
    133        md = prepareRunTests(**options)
    134        mock_os_makedirs.assert_called_once_with(".snap_firefox_current_real")
    135 
    136        opts = mock.Mock(pidFile="")  # so cleanup() does not check it
    137        with mock.patch("os.path.exists") as mock_os_path_exists, mock.patch(
    138            "shutil.rmtree"
    139        ) as mock_shutil_rmtree:
    140            md.cleanup(opts, False)
    141            mock_os_path_exists.assert_called_once_with(".snap_firefox_current_real")
    142            mock_shutil_rmtree.assert_called_once_with(".snap_firefox_current_real")
    143 
    144 
    145 def test_one_entry_already_exists(prepareRunTests, create_manifest):
    146    options = create_manifest_one(create_manifest)
    147    with mock.patch(
    148        "os.path.exists", return_value=True
    149    ) as mock_os_path_exists, mock.patch("os.makedirs") as mock_os_makedirs:
    150        with pytest.raises(FileExistsError):
    151            _ = prepareRunTests(**options)
    152        mock_os_path_exists.assert_called_once_with(".snap_firefox_current_real")
    153        mock_os_makedirs.assert_not_called()
    154 
    155 
    156 def test_mult_entry(prepareRunTests, create_manifest):
    157    options = create_manifest_mult(create_manifest)
    158    with mock.patch("os.makedirs") as mock_os_makedirs:
    159        md = prepareRunTests(**options)
    160        assert mock_os_makedirs.call_count == 2
    161        mock_os_makedirs.assert_has_calls([
    162            mock.call(".snap_firefox_current_real"),
    163            mock.call(".snap_firefox_current_real2"),
    164        ])
    165 
    166        opts = mock.Mock(pidFile="")  # so cleanup() does not check it
    167        with mock.patch("os.path.exists") as mock_os_path_exists, mock.patch(
    168            "shutil.rmtree"
    169        ) as mock_shutil_rmtree:
    170            md.cleanup(opts, False)
    171 
    172            assert mock_os_path_exists.call_count == 2
    173            mock_os_path_exists.assert_has_calls([
    174                mock.call(".snap_firefox_current_real"),
    175                mock.call().__bool__(),
    176                mock.call(".snap_firefox_current_real2"),
    177                mock.call().__bool__(),
    178            ])
    179 
    180            assert mock_os_makedirs.call_count == 2
    181            mock_shutil_rmtree.assert_has_calls([
    182                mock.call(".snap_firefox_current_real"),
    183                mock.call(".snap_firefox_current_real2"),
    184            ])
    185 
    186 
    187 def test_mult_entry_one_already_exists(prepareRunTests, create_manifest):
    188    options = create_manifest_mult(create_manifest)
    189    with mock.patch(
    190        "os.path.exists", side_effect=[True, False]
    191    ) as mock_os_path_exists, mock.patch("os.makedirs") as mock_os_makedirs:
    192        with pytest.raises(FileExistsError):
    193            _ = prepareRunTests(**options)
    194        mock_os_path_exists.assert_called_once_with(".snap_firefox_current_real")
    195        mock_os_makedirs.assert_not_called()
    196 
    197    with mock.patch(
    198        "os.path.exists", side_effect=[False, True]
    199    ) as mock_os_path_exists, mock.patch("os.makedirs") as mock_os_makedirs:
    200        with pytest.raises(FileExistsError):
    201            _ = prepareRunTests(**options)
    202        assert mock_os_path_exists.call_count == 2
    203        mock_os_path_exists.assert_has_calls([
    204            mock.call(".snap_firefox_current_real"),
    205            mock.call(".snap_firefox_current_real2"),
    206        ])
    207        mock_os_makedirs.assert_not_called()
    208 
    209 
    210 if __name__ == "__main__":
    211    mozunit.main()