tests: Remove compatibility code for Python < 3.5.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-07-21 16:37:06 -07:00 committed by Tim Abbott
parent bca5564c1e
commit 4e645c8ff9
2 changed files with 21 additions and 50 deletions

View File

@ -48,25 +48,15 @@ class TestRuleList(TestCase):
for rule in self.all_rules:
pattern = rule["pattern"]
for line in rule.get("good_lines", []):
# create=True is superfluous when mocking built-ins in Python >= 3.5
with patch(
"builtins.open",
return_value=StringIO(line + "\n\n"),
create=True,
autospec=True,
):
with patch("builtins.open", return_value=StringIO(line + "\n\n"), autospec=True):
self.assertFalse(
RuleList([], [rule]).custom_check_file("foo.bar", "baz", ""),
f"The pattern '{pattern}' matched the line '{line}' while it shouldn't.",
)
for line in rule.get("bad_lines", []):
# create=True is superfluous when mocking built-ins in Python >= 3.5
with patch(
"builtins.open",
return_value=StringIO(line + "\n\n"),
create=True,
autospec=True,
"builtins.open", return_value=StringIO(line + "\n\n"), autospec=True
), patch("builtins.print"):
filename = list(rule.get("include_only", {"foo.bar"}))[0]
self.assertTrue(

View File

@ -326,28 +326,25 @@ class Runner(DiscoverRunner):
def test_imports(
self, test_labels: List[str], suite: Union[TestSuite, ParallelTestSuite]
) -> None:
prefix_old = "unittest.loader.ModuleImportFailure." # Python <= 3.4
prefix_new = "unittest.loader._FailedTest." # Python > 3.4
error_prefixes = [prefix_old, prefix_new]
prefix = "unittest.loader._FailedTest."
for test_name in get_test_names(suite):
for prefix in error_prefixes:
if test_name.startswith(prefix):
test_name = test_name[len(prefix) :]
for label in test_labels:
# This code block is for Python 3.5 when test label is
# directly provided, for example:
# ./tools/test-backend zerver.tests.test_alert_words.py
#
# In this case, the test name is of this form:
# 'unittest.loader._FailedTest.test_alert_words'
#
# Whereas check_import_error requires test names of
# this form:
# 'unittest.loader._FailedTest.zerver.tests.test_alert_words'.
if test_name in label:
test_name = label
break
check_import_error(test_name)
if test_name.startswith(prefix):
test_name = test_name[len(prefix) :]
for label in test_labels:
# This code block is for when a test label is
# directly provided, for example:
# ./tools/test-backend zerver.tests.test_alert_words.py
#
# In this case, the test name is of this form:
# 'unittest.loader._FailedTest.test_alert_words'
#
# Whereas check_import_error requires test names of
# this form:
# 'unittest.loader._FailedTest.zerver.tests.test_alert_words'.
if test_name in label:
test_name = label
break
check_import_error(test_name)
def run_tests(
self,
@ -359,23 +356,7 @@ class Runner(DiscoverRunner):
**kwargs: Any,
) -> int:
self.setup_test_environment()
try:
suite = self.build_suite(test_labels, extra_tests)
except AttributeError:
# We are likely to get here only when running tests in serial
# mode on Python 3.4 or lower.
# test_labels are always normalized to include the correct prefix.
# If we run the command with ./tools/test-backend test_alert_words,
# test_labels will be equal to ['zerver.tests.test_alert_words'].
for test_label in test_labels:
check_import_error(test_label)
# I think we won't reach this line under normal circumstances, but
# for some unforeseen scenario in which the AttributeError was not
# caused by an import error, let's re-raise the exception for
# debugging purposes.
raise
suite = self.build_suite(test_labels, extra_tests)
self.test_imports(test_labels, suite)
if self.parallel == 1:
# We are running in serial mode so create the databases here.