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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
# 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/.
import os
from argparse import Namespace
from collections import defaultdict
from textwrap import dedent
import mozunit
import pytest
from conftest import setup_args
from manifestparser import TestManifest
@pytest.fixture
def get_active_tests(setup_test_harness, parser):
setup_test_harness(*setup_args)
runtests = pytest.importorskip("runtests")
md = runtests.MochitestDesktop("plain", {"log_tbpl": "-"})
options = vars(parser.parse_args([]))
def inner(**kwargs):
opts = options.copy()
opts.update(kwargs)
manifest = opts.get("manifestFile")
if isinstance(manifest, str):
md.testRootAbs = os.path.dirname(manifest)
elif isinstance(manifest, TestManifest):
md.testRootAbs = manifest.rootdir
md._active_tests = None
md.prefs_by_manifest = defaultdict(set)
return md, md.getActiveTests(Namespace(**opts))
return inner
@pytest.fixture
def create_manifest(tmpdir, build_obj):
def inner(string, name="manifest.ini"):
manifest = tmpdir.join(name)
manifest.write(string, ensure=True)
path = str(manifest)
return TestManifest(manifests=(path,), strict=False, rootdir=tmpdir.strpath)
return inner
def test_args_validation(get_active_tests, create_manifest):
# Test args set in a single manifest.
manifest_relpath = "manifest.ini"
manifest = create_manifest(
dedent(
"""
[DEFAULT]
args=
--cheese
--foo=bar
--foo1 bar1
[files/test_pass.html]
[files/test_fail.html]
"""
)
)
options = {
"runByManifest": True,
"manifestFile": manifest,
}
md, tests = get_active_tests(**options)
assert len(tests) == 2
assert manifest_relpath in md.args_by_manifest
args = md.args_by_manifest[manifest_relpath]
assert len(args) == 1
assert args.pop() == "\n--cheese\n--foo=bar\n--foo1 bar1"
# Test args set with runByManifest disabled.
options["runByManifest"] = False
with pytest.raises(SystemExit):
get_active_tests(**options)
# Test args set in non-default section.
options["runByManifest"] = True
options["manifestFile"] = create_manifest(
dedent(
"""
[files/test_pass.html]
args=--foo2=bar2
[files/test_fail.html]
"""
)
)
with pytest.raises(SystemExit):
get_active_tests(**options)
def test_args_validation_with_ancestor_manifest(get_active_tests, create_manifest):
# Test args set by an ancestor manifest.
create_manifest(
dedent(
"""
[DEFAULT]
args=
--cheese
[files/test_pass.html]
[files/test_fail.html]
"""
),
name="subdir/manifest.ini",
)
manifest = create_manifest(
dedent(
"""
[DEFAULT]
args =
--foo=bar
[include:manifest.ini]
[test_foo.html]
"""
),
name="subdir/ancestor-manifest.ini",
)
options = {
"runByManifest": True,
"manifestFile": manifest,
}
md, tests = get_active_tests(**options)
assert len(tests) == 3
key = os.path.join("subdir", "ancestor-manifest.ini")
assert key in md.args_by_manifest
args = md.args_by_manifest[key]
assert len(args) == 1
assert args.pop() == "\n--foo=bar"
key = "{}:{}".format(
os.path.join("subdir", "ancestor-manifest.ini"),
os.path.join("subdir", "manifest.ini"),
)
assert key in md.args_by_manifest
args = md.args_by_manifest[key]
assert len(args) == 1
assert args.pop() == "\n--foo=bar \n--cheese"
def test_prefs_validation(get_active_tests, create_manifest):
# Test prefs set in a single manifest.
manifest_relpath = "manifest.ini"
manifest = create_manifest(
dedent(
"""
[DEFAULT]
prefs=
foo=bar
browser.dom.foo=baz
[files/test_pass.html]
[files/test_fail.html]
"""
)
)
options = {
"runByManifest": True,
"manifestFile": manifest,
}
md, tests = get_active_tests(**options)
assert len(tests) == 2
assert manifest_relpath in md.prefs_by_manifest
prefs = md.prefs_by_manifest[manifest_relpath]
assert len(prefs) == 1
assert prefs.pop() == "\nfoo=bar\nbrowser.dom.foo=baz"
# Test prefs set with runByManifest disabled.
options["runByManifest"] = False
with pytest.raises(SystemExit):
get_active_tests(**options)
# Test prefs set in non-default section.
options["runByManifest"] = True
options["manifestFile"] = create_manifest(
dedent(
"""
[files/test_pass.html]
prefs=foo=bar
[files/test_fail.html]
"""
)
)
with pytest.raises(SystemExit):
get_active_tests(**options)
def test_prefs_validation_with_ancestor_manifest(get_active_tests, create_manifest):
# Test prefs set by an ancestor manifest.
create_manifest(
dedent(
"""
[DEFAULT]
prefs=
foo=bar
browser.dom.foo=baz
[files/test_pass.html]
[files/test_fail.html]
"""
),
name="subdir/manifest.ini",
)
manifest = create_manifest(
dedent(
"""
[DEFAULT]
prefs =
browser.dom.foo=fleem
flower=rose
[include:manifest.ini]
[test_foo.html]
"""
),
name="subdir/ancestor-manifest.ini",
)
options = {
"runByManifest": True,
"manifestFile": manifest,
}
md, tests = get_active_tests(**options)
assert len(tests) == 3
key = os.path.join("subdir", "ancestor-manifest.ini")
assert key in md.prefs_by_manifest
prefs = md.prefs_by_manifest[key]
assert len(prefs) == 1
assert prefs.pop() == "\nbrowser.dom.foo=fleem\nflower=rose"
key = "{}:{}".format(
os.path.join("subdir", "ancestor-manifest.ini"),
os.path.join("subdir", "manifest.ini"),
)
assert key in md.prefs_by_manifest
prefs = md.prefs_by_manifest[key]
assert len(prefs) == 1
assert (
prefs.pop()
== "\nbrowser.dom.foo=fleem\nflower=rose\n\nfoo=bar\nbrowser.dom.foo=baz"
)
if __name__ == "__main__":
mozunit.main()
|