2025-08-17 19:46:48 -05:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from unittest import TestLoader, TestSuite
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_tests(loader: "TestLoader", standard_tests: "TestSuite", pattern: str):
|
2022-12-11 13:15:23 +01:00
|
|
|
import os
|
|
|
|
|
import unittest
|
2025-08-17 19:46:48 -05:00
|
|
|
import fnmatch
|
2023-10-03 09:47:22 +02:00
|
|
|
from .. import file_path
|
2022-12-11 13:15:23 +01:00
|
|
|
from worlds.AutoWorld import AutoWorldRegister
|
|
|
|
|
|
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
|
suite.addTests(standard_tests)
|
2025-08-17 19:46:48 -05:00
|
|
|
|
|
|
|
|
# pattern hack
|
|
|
|
|
# all tests from within __init__ are always imported, so we need to filter out the folder earlier
|
|
|
|
|
# if the pattern isn't matching a specific world, we don't have much of a solution
|
|
|
|
|
|
|
|
|
|
if pattern.startswith("worlds."):
|
|
|
|
|
if pattern.endswith(".py"):
|
|
|
|
|
pattern = pattern[:-3]
|
|
|
|
|
components = pattern.split(".")
|
|
|
|
|
world_glob = f"worlds.{components[1]}"
|
|
|
|
|
pattern = components[-1]
|
|
|
|
|
|
|
|
|
|
elif pattern.startswith(f"worlds{os.path.sep}") or pattern.startswith(f"worlds{os.path.altsep}"):
|
|
|
|
|
components = pattern.split(os.path.sep)
|
|
|
|
|
if len(components) == 1:
|
|
|
|
|
components = pattern.split(os.path.altsep)
|
|
|
|
|
world_glob = f"worlds.{components[1]}"
|
|
|
|
|
pattern = components[-1]
|
|
|
|
|
else:
|
|
|
|
|
world_glob = "*"
|
|
|
|
|
|
|
|
|
|
|
2022-12-11 13:15:23 +01:00
|
|
|
folders = [os.path.join(os.path.split(world.__file__)[0], "test")
|
2025-08-17 19:46:48 -05:00
|
|
|
for world in AutoWorldRegister.world_types.values()
|
|
|
|
|
if fnmatch.fnmatch(world.__module__, world_glob)]
|
2023-06-01 01:44:54 +02:00
|
|
|
|
|
|
|
|
all_tests = [
|
|
|
|
|
test_case for folder in folders if os.path.exists(folder)
|
2025-08-17 19:46:48 -05:00
|
|
|
for test_collection in loader.discover(folder, top_level_dir=file_path, pattern=pattern)
|
2025-04-19 08:42:20 -05:00
|
|
|
for test_suite in test_collection if isinstance(test_suite, unittest.suite.TestSuite)
|
2023-06-01 01:44:54 +02:00
|
|
|
for test_case in test_suite
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
suite.addTests(sorted(all_tests, key=lambda test: test.__module__))
|
2022-12-11 13:15:23 +01:00
|
|
|
return suite
|