8000 Rework test_runner.py by rdesparbes · Pull Request #281 · CEXT-Dan/PyRx · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rework test_runner.py #281

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 2 commits into from
Apr 18, 2025
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
17 changes: 9 additions & 8 deletions PySamples/tests/sample_project/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,34 @@
from pyrx.commands import command
from pyrx.console import console
from pyrx.PyRxDebug import startListener as PyRxCmd_debug # noqa
from pyrx.utils.test_runner import PytestCmdlineArgsTestRunner, PytestFileArgsTestRunner
from pyrx.utils.test_runner import PytestTestRunner, FileTestArgsProvider, CmdlineTestArgsProvider

BASE_DIR = Path(__file__).parent

file_test_runner = PytestFileArgsTestRunner(
modules_to_reload=("package", "tests"), test_args_file=BASE_DIR / "test_args.txt"
modules_to_reload = ("package", "tests")
file_test_runner = PytestTestRunner(
modules_to_reload, FileTestArgsProvider(BASE_DIR / "test_args.txt")
)
cmd_test_runner = PytestCmdlineArgsTestRunner(modules_to_reload=("package", "tests"))
cmd_test_runner = PytestTestRunner(modules_to_reload, CmdlineTestArgsProvider())


@command
def runtests_file():
def runtests_file() -> None:
with chdir(BASE_DIR), console():
file_test_runner.start()
input("Press Enter to continue...")


@command
def runtests_cmd():
def runtests_cmd() -> None:
with chdir(BASE_DIR), console():
cmd_test_runner.start()
input("Press Enter to continue...")


@command
def settestargs():
cmd_test_runner.set_pytest_args_cmd()
def settestargs() -> None:
cmd_test_runner.test_args_provider.set_pytest_args_cmd()


# Command: RUNTESTS_FILE
Expand Down
6 changes: 3 additions & 3 deletions in_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

from pyrx import command
from pyrx.console import console
from pyrx.utils.test_runner import PytestFileArgsTestRunner
from pyrx.utils.test_runner import PytestTestRunner, FileTestArgsProvider

BASE_DIR = Path(__file__).parent


runner = PytestFileArgsTestRunner(("pyrx", "tests"), BASE_DIR / "pytest_args.txt")
runner = PytestTestRunner(("pyrx", "tests"), FileTestArgsProvider(BASE_DIR / "pytest_args.txt"))


@command
def runtests():
def runtests() -> None:
with console():
runner.start()
input("Press Enter to continue...")
52 changes: 26 additions & 26 deletions pyrx/utils/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,32 @@
import _typeshed as _t


class TestRunnerBase(abc.ABC):
def __init__(self, modules_to_reload: c.Iterable[str]):
class TestArgsProvider(abc.ABC):
@abc.abstractmethod
def get_test_args(self) -> c.Iterable[str]: ...


T = t.TypeVar("T", bound=TestArgsProvider)


class TestRunner(abc.ABC, t.Generic[T]):
def __init__(self, modules_to_reload: c.Iterable[str], test_args_provider: T) -> None:
self.test_args_provider: T = test_args_provider
self.reload_modules = Reloader(*modules_to_reload, reload=False).reload_modules

def start(self):
test_args = self.get_test_args()
def start(self) -> None:
test_args = self.test_args_provider.get_test_args()
self.reload_modules()
return self.run_tests(test_args)

@abc.abstractmethod
def get_test_args(self) -> c.Iterable[str]: ...
self.run_tests(test_args)

@abc.abstractmethod
def run_tests(self, test_args: c.Iterable[str]): ...
def run_tests(self, test_args: c.Iterable[str]) -> None: ...


class FileArgsTestRunner(TestRunnerBase):
class FileTestArgsProvider(TestArgsProvider):
def __init__(
self, modules_to_reload, test_args_file: _t.StrPath, test_args_file_encoding: str = "utf-8"
):
super().__init__(modules_to_reload)
self, test_args_file: _t.StrPath, test_args_file_encoding: str = "utf-8"
) -> None:
self.test_args_file = Path(test_args_file)
self.test_args_file_encoding = test_args_file_encoding

Expand All @@ -46,7 +51,10 @@ def get_test_args(self) -> list[str]:
return file.read_text(self.test_args_file_encoding).splitlines(keepends=False)


class CmdlineArgsTestRunner(TestRunnerBase):
class CmdlineTestArgsProvider(TestArgsProvider):
def __init__(self) -> None:
self._test_args: tuple[str, ...] = ()

def set_pytest_args_cmd(self):
def test_args():
while True:
Expand All @@ -58,17 +66,9 @@ def test_args():
self._test_args = tuple(test_args())

def get_test_args(self) -> tuple[str, ...]:
return getattr(self, "_test_args", ())


class PytestTestRunnerMixin:
def run_tests(self, test_args):
return pytest.main(list(test_args))


class PytestFileArgsTestRunner(PytestTestRunnerMixin, FileArgsTestRunner):
pass
return self._test_args


class PytestCmdlineArgsTestRunner(PytestTestRunnerMixin, CmdlineArgsTestRunner):
pass
class PytestTestRunner(TestRunner[T]):
def run_tests(self, test_args: c.Iterable[str]) -> None:
pytest.main(list(test_args))
14 changes: 6 additions & 8 deletions tests/test_utils/test_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

from pyrx import Ed, Rx
from pyrx.commands import command
from pyrx.utils.test_runner import CmdlineArgsTestRunner, FileArgsTestRunner
from pyrx.utils.test_runner import FileTestArgsProvider, CmdlineTestArgsProvider


class TestFileArgsTestRunner:
def test_get_test_args(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
args_file = tmp_path / "test_args.txt"
args_file.write_text("test1\ntest2\n", encoding="utf-8")
monkeypatch.setattr(FileArgsTestRunner, "__abstractmethods__", set())
runner = FileArgsTestRunner(modules_to_reload=(), test_args_file=str(args_file))
assert runner.get_test_args() == ["test1", "test2"]
args_provider = FileTestArgsProvider(test_args_file=str(args_file))
assert args_provider.get_test_args() == ["test1", "test2"]


class TestCmdlineArgsTestRunner:
def test_test_args(self, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(CmdlineArgsTestRunner, "__abstractmethods__", set())
runner = CmdlineArgsTestRunner(modules_to_reload=())
command(runner.set_pytest_args_cmd, name="test_set_pytest_args_cmd")
args_provider = CmdlineTestArgsProvider()
command(args_provider.set_pytest_args_cmd, name="test_set_pytest_args_cmd")
resbuf = [
(Rx.LispType.kText, "test_set_pytest_args_cmd"),
(Rx.LispType.kText, "test1"),
Expand All @@ -29,7 +27,7 @@ def test_test_args(self, monkeypatch: pytest.MonkeyPatch):
(Rx.LispType.kNone, 0),
]
Ed.Core.cmdS(resbuf)
assert runner.get_test_args() == ("test1", "test2")
assert args_provider.get_test_args() == ("test1", "test2")


if __name__ == "__main__":
Expand Down
Loading
0