Commit dfa753e4 authored by Mike Hommey's avatar Mike Hommey
Browse files

Bug 1839263 - Don't derive assertRaisesFromLine from assertRaises....

Bug 1839263 - Don't derive assertRaisesFromLine from assertRaises. r=firefox-build-system-reviewers,ahochheiden, a=test-only DONTBUILD

In python 3.11 (maybe also 3.10, I haven't tested that version ; 3.9 was
definitely different), by the time the context manager comes back in our
assertRaisesFromLine, the traceback is not available anymore (or yet,
I'm not entirely sure which way it does) to inspect and check the line
numbers we want to check.

And while assertRaises exposes the thrown exception in its `exception`
attribute, it also resets the traceback associated with it, so we can't
find it there either.

So instead, we implement our own context manager for
assertRaisesFromLine such that we can access that traceback.

Differential Revision: https://phabricator.services.mozilla.com/D181425
parent 0f09a132
Loading
Loading
Loading
Loading
+24 −10
Original line number Diff line number Diff line
@@ -2,9 +2,7 @@
# 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 contextlib
import os
import sys
import textwrap
import traceback
import unittest
@@ -19,6 +17,28 @@ test_data_path = mozpath.abspath(mozpath.dirname(__file__))
test_data_path = mozpath.join(test_data_path, "data")


class AssertRaisesFromLine:
    def __init__(self, test_case, expected, path, line):
        self.test_case = test_case
        self.expected = expected
        self.path = path
        self.line = line

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, tb):
        if exc_type is None:
            raise Exception(f"{self.expected.__name__} not raised")
        if not issubclass(exc_type, self.expected):
            return False
        self.exception = exc_value
        self.test_case.assertEqual(
            traceback.extract_tb(tb)[-1][:2], (self.path, self.line)
        )
        return True


class TestLint(unittest.TestCase):
    def lint_test(self, options=[], env={}):
        sandbox = LintSandbox(env, ["configure"] + options)
@@ -30,15 +50,9 @@ class TestLint(unittest.TestCase):
            {os.path.join(test_data_path, "moz.configure"): textwrap.dedent(source)}
        )

    @contextlib.contextmanager
    def assertRaisesFromLine(self, exc_type, line):
        with self.assertRaises(exc_type) as e:
            yield e

        _, _, tb = sys.exc_info()
        self.assertEqual(
            traceback.extract_tb(tb)[-1][:2],
            (mozpath.join(test_data_path, "moz.configure"), line),
        return AssertRaisesFromLine(
            self, exc_type, mozpath.join(test_data_path, "moz.configure"), line
        )

    def test_configure_testcase(self):