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

View File

@ -326,28 +326,25 @@ class Runner(DiscoverRunner):
def test_imports( def test_imports(
self, test_labels: List[str], suite: Union[TestSuite, ParallelTestSuite] self, test_labels: List[str], suite: Union[TestSuite, ParallelTestSuite]
) -> None: ) -> None:
prefix_old = "unittest.loader.ModuleImportFailure." # Python <= 3.4 prefix = "unittest.loader._FailedTest."
prefix_new = "unittest.loader._FailedTest." # Python > 3.4
error_prefixes = [prefix_old, prefix_new]
for test_name in get_test_names(suite): for test_name in get_test_names(suite):
for prefix in error_prefixes: if test_name.startswith(prefix):
if test_name.startswith(prefix): test_name = test_name[len(prefix) :]
test_name = test_name[len(prefix) :] for label in test_labels:
for label in test_labels: # This code block is for when a test label is
# This code block is for Python 3.5 when test label is # directly provided, for example:
# directly provided, for example: # ./tools/test-backend zerver.tests.test_alert_words.py
# ./tools/test-backend zerver.tests.test_alert_words.py #
# # In this case, the test name is of this form:
# In this case, the test name is of this form: # 'unittest.loader._FailedTest.test_alert_words'
# 'unittest.loader._FailedTest.test_alert_words' #
# # Whereas check_import_error requires test names of
# Whereas check_import_error requires test names of # this form:
# this form: # 'unittest.loader._FailedTest.zerver.tests.test_alert_words'.
# 'unittest.loader._FailedTest.zerver.tests.test_alert_words'. if test_name in label:
if test_name in label: test_name = label
test_name = label break
break check_import_error(test_name)
check_import_error(test_name)
def run_tests( def run_tests(
self, self,
@ -359,23 +356,7 @@ class Runner(DiscoverRunner):
**kwargs: Any, **kwargs: Any,
) -> int: ) -> int:
self.setup_test_environment() self.setup_test_environment()
try: suite = self.build_suite(test_labels, extra_tests)
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
self.test_imports(test_labels, suite) self.test_imports(test_labels, suite)
if self.parallel == 1: if self.parallel == 1:
# We are running in serial mode so create the databases here. # We are running in serial mode so create the databases here.