Skip to content

[lldb][test] Switch LLDB API tests from vendored unittest2 to unittest #79945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lldb/packages/Python/lldbsuite/test/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@


# Third-party modules
import unittest2
import unittest

# LLDB Modules
import lldbsuite


# The test suite.
suite = unittest2.TestSuite()
suite = unittest.TestSuite()

# The list of categories we said we care about
categories_list = None
Expand Down
34 changes: 17 additions & 17 deletions lldb/packages/Python/lldbsuite/test/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import subprocess

# Third-party modules
import unittest2
import unittest

# LLDB modules
import lldb
Expand Down Expand Up @@ -115,11 +115,11 @@ def _compiler_supports(

def expectedFailureIf(condition, bugnumber=None):
def expectedFailure_impl(func):
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
raise Exception("Decorator can only be used to decorate a test method")

if condition:
return unittest2.expectedFailure(func)
return unittest.expectedFailure(func)
return func

if callable(bugnumber):
Expand All @@ -130,14 +130,14 @@ def expectedFailure_impl(func):

def expectedFailureIfFn(expected_fn, bugnumber=None):
def expectedFailure_impl(func):
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
raise Exception("Decorator can only be used to decorate a test method")

@wraps(func)
def wrapper(*args, **kwargs):
xfail_reason = expected_fn(*args, **kwargs)
if xfail_reason is not None:
xfail_func = unittest2.expectedFailure(func)
xfail_func = unittest.expectedFailure(func)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's something wrong here. With this PR, the tests in lldb/test/API/functionalities/breakpoint/hardware_breakpoints/require_hw_breakpoints/TestRequireHWBreakpoints.py don't xfail, they just fail.

But I presume that expectedFailureIf is working, so perhaps it's this extra wrapper layer doing something different and the behaviour of expectedFailure has changed since.

xfail_func(*args, **kwargs)
else:
func(*args, **kwargs)
Expand All @@ -157,11 +157,11 @@ def wrapper(*args, **kwargs):

def skipTestIfFn(expected_fn, bugnumber=None):
def skipTestIfFn_impl(func):
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
reason = expected_fn()
# The return value is the reason (or None if we don't skip), so
# reason is used for both args.
return unittest2.skipIf(condition=reason, reason=reason)(func)
return unittest.skipIf(condition=reason, reason=reason)(func)

@wraps(func)
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -191,7 +191,7 @@ def wrapper(*args, **kwargs):

def _xfailForDebugInfo(expected_fn, bugnumber=None):
def expectedFailure_impl(func):
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
raise Exception("Decorator can only be used to decorate a test method")

func.__xfail_for_debug_info_cat_fn__ = expected_fn
Expand All @@ -205,7 +205,7 @@ def expectedFailure_impl(func):

def _skipForDebugInfo(expected_fn, bugnumber=None):
def skipImpl(func):
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
raise Exception("Decorator can only be used to decorate a test method")

func.__skip_for_debug_info_cat_fn__ = expected_fn
Expand Down Expand Up @@ -434,7 +434,7 @@ def add_test_categories(cat):
cat = test_categories.validate(cat, True)

def impl(func):
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
raise Exception(
"@add_test_categories can only be used to decorate a test method"
)
Expand Down Expand Up @@ -465,7 +465,7 @@ def should_skip_benchmarks_test():
def no_debug_info_test(func):
"""Decorate the item as a test what don't use any debug info. If this annotation is specified
then the test runner won't generate a separate test for each debug info format."""
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
if isinstance(func, type) and issubclass(func, unittest.TestCase):
raise Exception(
"@no_debug_info_test can only be used to decorate a test method"
)
Expand Down Expand Up @@ -631,7 +631,7 @@ def is_out_of_tree_debugserver():

def skipIfRemote(func):
"""Decorate the item to skip tests if testing remotely."""
return unittest2.skipIf(lldb.remote_platform, "skip on remote platform")(func)
return unittest.skipIf(lldb.remote_platform, "skip on remote platform")(func)


def skipIfNoSBHeaders(func):
Expand Down Expand Up @@ -768,7 +768,7 @@ def skipUnlessDarwin(func):


def skipUnlessTargetAndroid(func):
return unittest2.skipUnless(
return unittest.skipUnless(
lldbplatformutil.target_is_android(), "requires target to be Android"
)(func)

Expand Down Expand Up @@ -809,7 +809,7 @@ def skipIfPlatform(oslist):
"""Decorate the item to skip tests if running on one of the listed platforms."""
# This decorator cannot be ported to `skipIf` yet because it is used on entire
# classes, which `skipIf` explicitly forbids.
return unittest2.skipIf(
return unittest.skipIf(
lldbplatformutil.getPlatform() in oslist, "skip on %s" % (", ".join(oslist))
)

Expand All @@ -818,7 +818,7 @@ def skipUnlessPlatform(oslist):
"""Decorate the item to skip tests unless running on one of the listed platforms."""
# This decorator cannot be ported to `skipIf` yet because it is used on entire
# classes, which `skipIf` explicitly forbids.
return unittest2.skipUnless(
return unittest.skipUnless(
lldbplatformutil.getPlatform() in oslist,
"requires one of %s" % (", ".join(oslist)),
)
Expand Down Expand Up @@ -1078,7 +1078,7 @@ def _get_bool_config(key, fail_value=True):

def _get_bool_config_skip_if_decorator(key):
have = _get_bool_config(key)
return unittest2.skipIf(not have, "requires " + key)
return unittest.skipIf(not have, "requires " + key)


def skipIfCursesSupportMissing(func):
Expand Down Expand Up @@ -1110,7 +1110,7 @@ def skipIfLLVMTargetMissing(target):
found = True
break

return unittest2.skipIf(not found, "requires " + target)
return unittest.skipIf(not found, "requires " + target)


# Call sysctl on darwin to see if a specified hardware feature is available on this machine.
Expand Down
14 changes: 6 additions & 8 deletions lldb/packages/Python/lldbsuite/test/dotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import tempfile

# Third-party modules
import unittest2
import unittest

# LLDB Modules
import lldbsuite
Expand Down Expand Up @@ -658,7 +658,7 @@ def iter_filters():
for filterspec in iter_filters():
filtered = True
print("adding filter spec %s to module %s" % (filterspec, repr(module)))
tests = unittest2.defaultTestLoader.loadTestsFromName(filterspec, module)
tests = unittest.defaultTestLoader.loadTestsFromName(filterspec, module)
configuration.suite.addTests(tests)

# Forgo this module if the (base, filterspec) combo is invalid
Expand All @@ -669,9 +669,7 @@ def iter_filters():
# Add the entire file's worth of tests since we're not filtered.
# Also the fail-over case when the filterspec branch
# (base, filterspec) combo doesn't make sense.
configuration.suite.addTests(
unittest2.defaultTestLoader.loadTestsFromName(base)
)
configuration.suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base))


def visit(prefix, dir, names):
Expand Down Expand Up @@ -1032,7 +1030,7 @@ def run_suite():
#

# Install the control-c handler.
unittest2.signals.installHandler()
unittest.signals.installHandler()

#
# Invoke the default TextTestRunner to run the test suite
Expand Down Expand Up @@ -1066,7 +1064,7 @@ def run_suite():

# Invoke the test runner.
if configuration.count == 1:
result = unittest2.TextTestRunner(
result = unittest.TextTestRunner(
stream=sys.stderr,
verbosity=configuration.verbose,
resultclass=test_result.LLDBTestResult,
Expand All @@ -1077,7 +1075,7 @@ def run_suite():
# not enforced.
test_result.LLDBTestResult.__ignore_singleton__ = True
for i in range(configuration.count):
result = unittest2.TextTestRunner(
result = unittest.TextTestRunner(
stream=sys.stderr,
verbosity=configuration.verbose,
resultclass=test_result.LLDBTestResult,
Expand Down
28 changes: 9 additions & 19 deletions lldb/packages/Python/lldbsuite/test/lldbtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import traceback

# Third-party modules
import unittest2
import unittest

# LLDB modules
import lldb
Expand Down Expand Up @@ -517,7 +517,7 @@ def builder_module():
return lldbplatformutil.builder_module()


class Base(unittest2.TestCase):
class Base(unittest.TestCase):
"""
Abstract base for performing lldb (see TestBase) or other generic tests (see
BenchBase for one example). lldbtest.Base works with the test driver to
Expand Down Expand Up @@ -1090,17 +1090,14 @@ def markFailure(self):
# Once by the Python unittest framework, and a second time by us.
print("FAIL", file=sbuf)

def markExpectedFailure(self, err, bugnumber):
def markExpectedFailure(self, err):
"""Callback invoked when an expected failure/error occurred."""
self.__expected__ = True
with recording(self, False) as sbuf:
# False because there's no need to write "expected failure" to the
# stderr twice.
# Once by the Python unittest framework, and a second time by us.
if bugnumber is None:
print("expected failure", file=sbuf)
else:
print("expected failure (problem id:" + str(bugnumber) + ")", file=sbuf)
print("expected failure", file=sbuf)

def markSkippedTest(self):
"""Callback invoked when a test is skipped."""
Expand All @@ -1111,19 +1108,14 @@ def markSkippedTest(self):
# Once by the Python unittest framework, and a second time by us.
print("skipped test", file=sbuf)

def markUnexpectedSuccess(self, bugnumber):
def markUnexpectedSuccess(self):
"""Callback invoked when an unexpected success occurred."""
self.__unexpected__ = True
with recording(self, False) as sbuf:
# False because there's no need to write "unexpected success" to the
# stderr twice.
# Once by the Python unittest framework, and a second time by us.
if bugnumber is None:
print("unexpected success", file=sbuf)
else:
print(
"unexpected success (problem id:" + str(bugnumber) + ")", file=sbuf
)
print("unexpected success", file=sbuf)

def getRerunArgs(self):
return " -f %s.%s" % (self.__class__.__name__, self._testMethodName)
Expand Down Expand Up @@ -1704,13 +1696,11 @@ def test_method(self, attrvalue=attrvalue):

xfail_reason = xfail_for_debug_info_cat_fn(cat)
if xfail_reason:
test_method = unittest2.expectedFailure(xfail_reason)(
test_method
)
test_method = unittest.expectedFailure(test_method)

skip_reason = skip_for_debug_info_cat_fn(cat)
if skip_reason:
test_method = unittest2.skip(skip_reason)(test_method)
test_method = unittest.skip(skip_reason)(test_method)

newattrs[method_name] = test_method

Expand Down Expand Up @@ -2226,7 +2216,7 @@ def completions_match(self, command, completions):
match_strings = lldb.SBStringList()
interp.HandleCompletion(command, len(command), 0, -1, match_strings)
# match_strings is a 1-indexed list, so we have to slice...
self.assertItemsEqual(
self.assertCountEqual(
completions, list(match_strings)[1:], "List of returned completion is wrong"
)

Expand Down
18 changes: 9 additions & 9 deletions lldb/packages/Python/lldbsuite/test/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import traceback

# Third-party modules
import unittest2
import unittest

# LLDB Modules
from . import configuration
from lldbsuite.test_event import build_exception


class LLDBTestResult(unittest2.TextTestResult):
class LLDBTestResult(unittest.TextTestResult):
"""
Enforce a singleton pattern to allow introspection of test progress.

Expand Down Expand Up @@ -243,7 +243,7 @@ def addFailure(self, test, err):
if self.checkExclusion(
configuration.xfail_tests, test.id()
) or self.checkCategoryExclusion(configuration.xfail_categories, test):
self.addExpectedFailure(test, err, None)
self.addExpectedFailure(test, err)
return

configuration.sdir_has_content = True
Expand All @@ -264,12 +264,12 @@ def addFailure(self, test, err):
else:
configuration.failures_per_category[category] = 1

def addExpectedFailure(self, test, err, bugnumber):
def addExpectedFailure(self, test, err):
configuration.sdir_has_content = True
super(LLDBTestResult, self).addExpectedFailure(test, err, bugnumber)
super(LLDBTestResult, self).addExpectedFailure(test, err)
method = getattr(test, "markExpectedFailure", None)
if method:
method(err, bugnumber)
method(err)
self.stream.write(
"XFAIL: LLDB (%s) :: %s\n" % (self._config_string(test), str(test))
)
Expand All @@ -285,12 +285,12 @@ def addSkip(self, test, reason):
% (self._config_string(test), str(test), reason)
)

def addUnexpectedSuccess(self, test, bugnumber):
def addUnexpectedSuccess(self, test):
configuration.sdir_has_content = True
super(LLDBTestResult, self).addUnexpectedSuccess(test, bugnumber)
super(LLDBTestResult, self).addUnexpectedSuccess(test)
method = getattr(test, "markUnexpectedSuccess", None)
if method:
method(bugnumber)
method()
self.stream.write(
"XPASS: LLDB (%s) :: %s\n" % (self._config_string(test), str(test))
)
4 changes: 2 additions & 2 deletions lldb/test/API/commands/expression/test/TestExprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""


import unittest2
import unittest
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
Expand Down Expand Up @@ -46,7 +46,7 @@ def build_and_run(self):

# llvm.org/pr17135 <rdar://problem/14874559>
# APFloat::toString does not identify the correct (i.e. least) precision.
@unittest2.expectedFailure
@unittest.expectedFailure
def test_floating_point_expr_commands(self):
self.build_and_run()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""


import unittest2
import unittest
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
Expand Down
Loading