mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 23:13:25 +00:00
python: Reformat with Black, except quotes.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
5028c081cb
commit
11741543da
@@ -31,17 +31,21 @@ from zerver.lib.test_helpers import append_instrumentation_data, write_instrumen
|
||||
# below hack, which fails 1/10000000 of the time.
|
||||
random_id_range_start = str(random.randint(1, 10000000))
|
||||
|
||||
def get_database_id(worker_id: Optional[int]=None) -> str:
|
||||
|
||||
def get_database_id(worker_id: Optional[int] = None) -> str:
|
||||
if worker_id:
|
||||
return f"{random_id_range_start}_{worker_id}"
|
||||
return random_id_range_start
|
||||
|
||||
|
||||
# The root directory for this run of the test suite.
|
||||
TEST_RUN_DIR = get_or_create_dev_uuid_var_path(
|
||||
os.path.join('test-backend', f'run_{get_database_id()}'))
|
||||
os.path.join('test-backend', f'run_{get_database_id()}')
|
||||
)
|
||||
|
||||
_worker_id = 0 # Used to identify the worker process.
|
||||
|
||||
|
||||
class TextTestResult(runner.TextTestResult):
|
||||
"""
|
||||
This class has unpythonic function names because base class follows
|
||||
@@ -84,6 +88,7 @@ class TextTestResult(runner.TextTestResult):
|
||||
)
|
||||
self.stream.flush()
|
||||
|
||||
|
||||
class RemoteTestResult(django_runner.RemoteTestResult):
|
||||
"""
|
||||
The class follows the unpythonic style of function names of the
|
||||
@@ -100,13 +105,16 @@ class RemoteTestResult(django_runner.RemoteTestResult):
|
||||
|
||||
self.events.append(('addInstrumentation', self.test_index, data))
|
||||
|
||||
|
||||
def process_instrumented_calls(func: Callable[[Dict[str, Any]], None]) -> None:
|
||||
for call in test_helpers.INSTRUMENTED_CALLS:
|
||||
func(call)
|
||||
|
||||
|
||||
SerializedSubsuite = Tuple[Type[TestSuite], List[str]]
|
||||
SubsuiteArgs = Tuple[Type['RemoteTestRunner'], int, SerializedSubsuite, bool]
|
||||
|
||||
|
||||
def run_subsuite(args: SubsuiteArgs) -> Tuple[int, Any]:
|
||||
# Reset the accumulated INSTRUMENTED_CALLS before running this subsuite.
|
||||
test_helpers.INSTRUMENTED_CALLS = []
|
||||
@@ -123,6 +131,7 @@ def run_subsuite(args: SubsuiteArgs) -> Tuple[int, Any]:
|
||||
process_instrumented_calls(partial(result.addInstrumentation, None))
|
||||
return subsuite_index, result.events
|
||||
|
||||
|
||||
# Monkey-patch django.test.runner to allow using multiprocessing
|
||||
# inside tests without a “daemonic processes are not allowed to have
|
||||
# children” error.
|
||||
@@ -130,9 +139,11 @@ class NoDaemonContext(multiprocessing.context.ForkContext):
|
||||
class Process(multiprocessing.context.ForkProcess):
|
||||
daemon = cast(bool, property(lambda self: False, lambda self, value: None))
|
||||
|
||||
|
||||
django_runner.multiprocessing = NoDaemonContext()
|
||||
|
||||
def destroy_test_databases(worker_id: Optional[int]=None) -> None:
|
||||
|
||||
def destroy_test_databases(worker_id: Optional[int] = None) -> None:
|
||||
for alias in connections:
|
||||
connection = connections[alias]
|
||||
|
||||
@@ -151,7 +162,9 @@ def destroy_test_databases(worker_id: Optional[int]=None) -> None:
|
||||
query = f"DROP DATABASE IF EXISTS {quoted_name}"
|
||||
cursor.execute(query)
|
||||
|
||||
with mock.patch.object(connection.creation, '_destroy_test_db', monkey_patched_destroy_test_db):
|
||||
with mock.patch.object(
|
||||
connection.creation, '_destroy_test_db', monkey_patched_destroy_test_db
|
||||
):
|
||||
# In the parallel mode, the test databases are created
|
||||
# through the N=self.parallel child processes, and in the
|
||||
# parent process (which calls `destroy_test_databases`),
|
||||
@@ -174,6 +187,7 @@ def destroy_test_databases(worker_id: Optional[int]=None) -> None:
|
||||
else:
|
||||
connection.creation.destroy_test_db()
|
||||
|
||||
|
||||
def create_test_databases(worker_id: int) -> None:
|
||||
database_id = get_database_id(worker_id)
|
||||
for alias in connections:
|
||||
@@ -191,6 +205,7 @@ def create_test_databases(worker_id: int) -> None:
|
||||
connection.settings_dict.update(settings_dict)
|
||||
connection.close()
|
||||
|
||||
|
||||
def init_worker(counter: "multiprocessing.sharedctypes._Value") -> None:
|
||||
"""
|
||||
This function runs only under parallel mode. It initializes the
|
||||
@@ -208,6 +223,7 @@ def init_worker(counter: "multiprocessing.sharedctypes._Value") -> None:
|
||||
|
||||
# Clear the cache
|
||||
from zerver.lib.cache import get_cache_backend
|
||||
|
||||
cache = get_cache_backend(None)
|
||||
cache.clear()
|
||||
|
||||
@@ -220,9 +236,11 @@ def init_worker(counter: "multiprocessing.sharedctypes._Value") -> None:
|
||||
|
||||
# We manually update the upload directory path in the URL regex.
|
||||
from zproject.dev_urls import avatars_url
|
||||
|
||||
new_root = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars")
|
||||
avatars_url.default_args['document_root'] = new_root
|
||||
|
||||
|
||||
class ParallelTestSuite(django_runner.ParallelTestSuite):
|
||||
run_subsuite = run_subsuite
|
||||
init_worker = init_worker
|
||||
@@ -236,6 +254,7 @@ class ParallelTestSuite(django_runner.ParallelTestSuite):
|
||||
assert not isinstance(self.subsuites, SubSuiteList)
|
||||
self.subsuites: Union[SubSuiteList, List[TestSuite]] = SubSuiteList(self.subsuites)
|
||||
|
||||
|
||||
def check_import_error(test_name: str) -> None:
|
||||
try:
|
||||
# Directly using __import__ is not recommended, but here it gives
|
||||
@@ -255,12 +274,16 @@ def initialize_worker_path(worker_id: int) -> None:
|
||||
# Every process should upload to a separate directory so that
|
||||
# race conditions can be avoided.
|
||||
settings.LOCAL_UPLOADS_DIR = get_or_create_dev_uuid_var_path(
|
||||
os.path.join("test-backend",
|
||||
os.path.basename(TEST_RUN_DIR),
|
||||
os.path.basename(worker_path),
|
||||
"test_uploads"))
|
||||
os.path.join(
|
||||
"test-backend",
|
||||
os.path.basename(TEST_RUN_DIR),
|
||||
os.path.basename(worker_path),
|
||||
"test_uploads",
|
||||
)
|
||||
)
|
||||
settings.SENDFILE_ROOT = os.path.join(settings.LOCAL_UPLOADS_DIR, "files")
|
||||
|
||||
|
||||
class Runner(DiscoverRunner):
|
||||
parallel_test_suite = ParallelTestSuite
|
||||
|
||||
@@ -298,9 +321,7 @@ class Runner(DiscoverRunner):
|
||||
|
||||
# Write the template database ids to a file that we can
|
||||
# reference for cleaning them up if they leak.
|
||||
filepath = os.path.join(get_dev_uuid_var_path(),
|
||||
TEMPLATE_DATABASE_DIR,
|
||||
get_database_id())
|
||||
filepath = os.path.join(get_dev_uuid_var_path(), TEMPLATE_DATABASE_DIR, get_database_id())
|
||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||||
with open(filepath, "w") as f:
|
||||
if self.parallel > 1:
|
||||
@@ -332,9 +353,7 @@ class Runner(DiscoverRunner):
|
||||
destroy_test_databases()
|
||||
|
||||
# Clean up our record of which databases this process created.
|
||||
filepath = os.path.join(get_dev_uuid_var_path(),
|
||||
TEMPLATE_DATABASE_DIR,
|
||||
get_database_id())
|
||||
filepath = os.path.join(get_dev_uuid_var_path(), TEMPLATE_DATABASE_DIR, get_database_id())
|
||||
os.remove(filepath)
|
||||
|
||||
# Clean up our test runs root directory.
|
||||
@@ -344,14 +363,16 @@ class Runner(DiscoverRunner):
|
||||
print("Unable to clean up the test run's directory.")
|
||||
return super().teardown_test_environment(*args, **kwargs)
|
||||
|
||||
def test_imports(self, test_labels: List[str], suite: Union[TestSuite, ParallelTestSuite]) -> None:
|
||||
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]
|
||||
for test_name in get_test_names(suite):
|
||||
for prefix in error_prefixes:
|
||||
if test_name.startswith(prefix):
|
||||
test_name = test_name[len(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:
|
||||
@@ -368,11 +389,14 @@ class Runner(DiscoverRunner):
|
||||
break
|
||||
check_import_error(test_name)
|
||||
|
||||
def run_tests(self, test_labels: List[str],
|
||||
extra_tests: Optional[List[TestCase]]=None,
|
||||
full_suite: bool=False,
|
||||
include_webhooks: bool=False,
|
||||
**kwargs: Any) -> Tuple[bool, List[str]]:
|
||||
def run_tests(
|
||||
self,
|
||||
test_labels: List[str],
|
||||
extra_tests: Optional[List[TestCase]] = None,
|
||||
full_suite: bool = False,
|
||||
include_webhooks: bool = False,
|
||||
**kwargs: Any,
|
||||
) -> Tuple[bool, List[str]]:
|
||||
self.setup_test_environment()
|
||||
try:
|
||||
suite = self.build_suite(test_labels, extra_tests)
|
||||
@@ -415,6 +439,7 @@ class Runner(DiscoverRunner):
|
||||
write_instrumentation_reports(full_suite=full_suite, include_webhooks=include_webhooks)
|
||||
return failed, result.failed_tests
|
||||
|
||||
|
||||
def get_test_names(suite: Union[TestSuite, ParallelTestSuite]) -> List[str]:
|
||||
if isinstance(suite, ParallelTestSuite):
|
||||
# suite is ParallelTestSuite. It will have a subsuites parameter of
|
||||
@@ -427,6 +452,7 @@ def get_test_names(suite: Union[TestSuite, ParallelTestSuite]) -> List[str]:
|
||||
else:
|
||||
return [t.id() for t in get_tests_from_suite(suite)]
|
||||
|
||||
|
||||
def get_tests_from_suite(suite: TestSuite) -> TestCase:
|
||||
for test in suite:
|
||||
if isinstance(test, TestSuite):
|
||||
@@ -434,9 +460,11 @@ def get_tests_from_suite(suite: TestSuite) -> TestCase:
|
||||
else:
|
||||
yield test
|
||||
|
||||
|
||||
def serialize_suite(suite: TestSuite) -> Tuple[Type[TestSuite], List[str]]:
|
||||
return type(suite), get_test_names(suite)
|
||||
|
||||
|
||||
def deserialize_suite(args: Tuple[Type[TestSuite], List[str]]) -> TestSuite:
|
||||
suite_class, test_names = args
|
||||
suite = suite_class()
|
||||
@@ -445,9 +473,11 @@ def deserialize_suite(args: Tuple[Type[TestSuite], List[str]]) -> TestSuite:
|
||||
suite.addTest(test)
|
||||
return suite
|
||||
|
||||
|
||||
class RemoteTestRunner(django_runner.RemoteTestRunner):
|
||||
resultclass = RemoteTestResult
|
||||
|
||||
|
||||
class SubSuiteList(List[Tuple[Type[TestSuite], List[str]]]):
|
||||
"""
|
||||
This class allows us to avoid changing the main logic of
|
||||
|
||||
Reference in New Issue
Block a user