Bug 1705431 - [mochitest] Refactor MessageLogger.TEST_PATH_PREFIXES into regexes and catch https://mochi.test urls, r=marco

Using regexes will be a little less brittle in the event we change things like
ports or prefix paths.

Differential Revision: https://phabricator.services.mozilla.com/D112354
This commit is contained in:
Andrew Halberstadt 2021-04-19 12:57:12 +00:00
parent 3d4ba1d105
commit d0cd0fcc69
2 changed files with 51 additions and 15 deletions

View File

@ -144,11 +144,12 @@ class MessageLogger(object):
"buffering_off",
]
)
# Regexes that will be replaced with an empty string if found in a test
# name. We do this to normalize test names which may contain URLs and test
# package prefixes.
TEST_PATH_PREFIXES = [
"/tests/",
"chrome://mochitests/content/a11y/",
"chrome://mochitests/content/browser/",
"chrome://mochitests/content/chrome/",
r"^/tests/",
r"^\w+://[\w\.]+(:\d+)?(/\w+)?/(tests?|a11y|chrome|browser)/",
]
def __init__(self, logger, buffering=True, structured=True):
@ -190,9 +191,10 @@ class MessageLogger(object):
"""Normalize a logged test path to match the relative path from the sourcedir."""
if message.get("test") is not None:
test = message["test"]
for prefix in MessageLogger.TEST_PATH_PREFIXES:
if test.startswith(prefix):
message["test"] = test[len(prefix) :]
for pattern in MessageLogger.TEST_PATH_PREFIXES:
test = re.sub(pattern, "", test)
if test != message["test"]:
message["test"] = test
break
def _fix_message_format(self, message):

View File

@ -53,7 +53,7 @@ def get_message_logger(setup_test_harness, logger):
message["message"] = "foobar"
message.update(**extra)
return self.process_message(message)
return self.write(json.dumps(message))
def inner(**kwargs):
ml = runtests.MessageLogger(logger, **kwargs)
@ -66,22 +66,31 @@ def get_message_logger(setup_test_harness, logger):
@pytest.fixture
def assert_actions(logger):
def get_lines(logger):
buf = logger.handlers[0].stream
def inner(expected):
if isinstance(expected, string_types):
expected = [expected]
def inner():
lines = buf.getvalue().splitlines()
actions = [json.loads(l)["action"] for l in lines]
assert actions == expected
buf.truncate(0)
# Python3 will not reposition the buffer position after
# truncate and will extend the buffer with null bytes.
# Force the buffer position to the start of the buffer
# to prevent null bytes from creeping in.
buf.seek(0)
return lines
return inner
@pytest.fixture
def assert_actions(get_lines):
def inner(expected):
if isinstance(expected, string_types):
expected = [expected]
lines = get_lines()
actions = [json.loads(l)["action"] for l in lines]
assert actions == expected
return inner
@ -155,5 +164,30 @@ def test_buffering_off(get_message_logger, assert_actions):
assert_actions(["test_end"])
@pytest.mark.parametrize(
"name,expected",
(
("/tests/test_foo.html", "test_foo.html"),
("chrome://mochitests/content/a11y/test_foo.html", "test_foo.html"),
("chrome://mochitests/content/browser/test_foo.html", "test_foo.html"),
("chrome://mochitests/content/chrome/test_foo.html", "test_foo.html"),
(
"https://example.org:443/tests/netwerk/test_foo.html",
"netwerk/test_foo.html",
),
("http://mochi.test:8888/tests/test_foo.html", "test_foo.html"),
("http://mochi.test:8888/content/dom/browser/test_foo.html", None),
),
)
def test_test_names_fixed_to_be_relative(name, expected, get_message_logger, get_lines):
ml = get_message_logger(buffering=False)
ml.fake_message("test_start", test=name)
lines = get_lines()
if expected is None:
expected = name
assert json.loads(lines[0])["test"] == expected
if __name__ == "__main__":
mozunit.main()