summaryrefslogtreecommitdiffstats
path: root/layout/tools/reftest/selftest/conftest.py
blob: 74067e9787f59fef095c0906922fe401f8083268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function

import json
import os
from argparse import Namespace

try:
    # Python2
    from cStringIO import StringIO
except ImportError:
    # Python3
    from io import StringIO

import mozinfo
import pytest
from manifestparser import expression
from moztest.selftest.fixtures import binary, setup_test_harness  # noqa

here = os.path.abspath(os.path.dirname(__file__))
setup_args = [False, "reftest", "reftest"]


@pytest.fixture(scope="module")
def normalize():
    """A function that can take a relative path and append it to the 'files'
    directory which contains the data necessary to run these tests.
    """

    def inner(path):
        if os.path.isabs(path):
            return path
        return os.path.join(here, "files", path)

    return inner


@pytest.fixture
def parser(setup_test_harness):
    setup_test_harness(*setup_args)
    cmdline = pytest.importorskip("reftestcommandline")
    return cmdline.DesktopArgumentsParser()


@pytest.fixture
def get_reftest(setup_test_harness, binary, parser):
    setup_test_harness(*setup_args)
    runreftest = pytest.importorskip("runreftest")
    harness_root = runreftest.SCRIPT_DIRECTORY

    build = parser.build_obj
    options = vars(parser.parse_args([]))
    options.update(
        {
            "app": binary,
            "focusFilterMode": "non-needs-focus",
            "suite": "reftest",
        }
    )

    if not os.path.isdir(build.bindir):
        package_root = os.path.dirname(harness_root)
        options.update(
            {
                "extraProfileFiles": [os.path.join(package_root, "bin", "plugins")],
                "reftestExtensionPath": os.path.join(harness_root, "reftest"),
                "sandboxReadWhitelist": [here, os.environ["PYTHON_TEST_TMP"]],
                "utilityPath": os.path.join(package_root, "bin"),
                "specialPowersExtensionPath": os.path.join(
                    harness_root, "specialpowers"
                ),
            }
        )

        if "MOZ_FETCHES_DIR" in os.environ:
            options["sandboxReadWhitelist"].append(os.environ["MOZ_FETCHES_DIR"])
    else:
        options.update(
            {
                "extraProfileFiles": [os.path.join(build.topobjdir, "dist", "plugins")],
                "sandboxReadWhitelist": [build.topobjdir, build.topsrcdir],
                "specialPowersExtensionPath": os.path.join(
                    build.distdir, "xpi-stage", "specialpowers"
                ),
            }
        )

    def inner(**opts):
        options.update(opts)
        config = Namespace(**options)

        # This is pulled from `runreftest.run_test_harness` minus some error
        # checking that isn't necessary in this context. It should stay roughly
        # in sync.
        reftest = runreftest.RefTest(config.suite)
        parser.validate(config, reftest)

        config.app = reftest.getFullPath(config.app)
        assert os.path.exists(config.app)

        if config.xrePath is None:
            config.xrePath = os.path.dirname(config.app)

        return reftest, config

    return inner


@pytest.fixture  # noqa: F811
def runtests(get_reftest, normalize):
    def inner(*tests, **opts):
        assert len(tests) > 0
        opts["tests"] = map(normalize, tests)

        buf = StringIO()
        opts["log_raw"] = [buf]

        reftest, options = get_reftest(**opts)
        result = reftest.runTests(options.tests, options)

        out = json.loads("[" + ",".join(buf.getvalue().splitlines()) + "]")
        buf.close()
        return result, out

    return inner


@pytest.fixture(autouse=True)  # noqa: F811
def skip_using_mozinfo(request, setup_test_harness):
    """Gives tests the ability to skip based on values from mozinfo.

    Example:
        @pytest.mark.skip_mozinfo("!e10s || os == 'linux'")
        def test_foo():
            pass
    """

    setup_test_harness(*setup_args)
    runreftest = pytest.importorskip("runreftest")
    runreftest.update_mozinfo()

    skip_mozinfo = request.node.get_marker("skip_mozinfo")
    if skip_mozinfo:
        value = skip_mozinfo.args[0]
        if expression.parse(value, **mozinfo.info):
            pytest.skip("skipped due to mozinfo match: \n{}".format(value))