diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-28 19:22:57 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-28 19:22:57 +0200 |
commit | 7f331ba9492e178de999f010b3420fad8d167495 (patch) | |
tree | 725edbcb05a832be47f9ff965eede5d4bc600b88 /test | |
parent | test: add test timeouts, prettier output (diff) | |
download | cimple-7f331ba9492e178de999f010b3420fad8d167495.tar.gz cimple-7f331ba9492e178de999f010b3420fad8d167495.zip |
test/lib: refactoring
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/process.py | 37 | ||||
-rw-r--r-- | test/lib/test_repo.py | 4 |
2 files changed, 20 insertions, 21 deletions
diff --git a/test/lib/process.py b/test/lib/process.py index be2dc92..b9b24fb 100644 --- a/test/lib/process.py +++ b/test/lib/process.py @@ -11,23 +11,6 @@ import subprocess from threading import Event, Lock, Thread -_COMMON_ARGS = { - 'text': True, - 'stdin': subprocess.DEVNULL, - 'stdout': subprocess.PIPE, - 'stderr': subprocess.STDOUT, -} - - -def run(*args, **kwargs): - try: - return subprocess.run(list(args), check=True, **_COMMON_ARGS, **kwargs) - except subprocess.CalledProcessError as e: - logging.error('Command %s exited with code %s', e.cmd, e.returncode) - logging.error('Output:\n%s', e.output) - raise - - class LoggingEvent(Event): def __init__(self, timeout=10): self.timeout = timeout @@ -120,10 +103,26 @@ class LoggingEventProcessReady(LoggingEvent): class Process(subprocess.Popen): + _COMMON_ARGS = { + 'text': True, + 'stdin': subprocess.DEVNULL, + 'stdout': subprocess.PIPE, + 'stderr': subprocess.STDOUT, + } + + @staticmethod + def run(*args, **kwargs): + try: + return subprocess.run(list(args), check=True, **Process._COMMON_ARGS, **kwargs) + except subprocess.CalledProcessError as e: + logging.error('Command %s exited with code %s', e.cmd, e.returncode) + logging.error('Output:\n%s', e.output) + raise + def __init__(self, cmd_line): self.cmd_line = cmd_line - super().__init__(cmd_line.argv, **_COMMON_ARGS) + super().__init__(cmd_line.argv, **Process._COMMON_ARGS) logging.info('Process %s has started', self.log_id) ready_event = LoggingEventProcessReady(self) @@ -183,7 +182,7 @@ class Runner: cmd_line = self._wrap(cmd_line) cmd_line.log_process_start() - result = run(*cmd_line.argv) + result = Process.run(*cmd_line.argv) return result.returncode, result.stdout @contextmanager diff --git a/test/lib/test_repo.py b/test/lib/test_repo.py index f12b374..820c73f 100644 --- a/test/lib/test_repo.py +++ b/test/lib/test_repo.py @@ -7,7 +7,7 @@ import logging import os import shutil -from .process import run +from .process import Process class Repo: @@ -21,7 +21,7 @@ class Repo: self.run('git', 'config', 'user.email', 'test@example.com') def run(self, *args, **kwargs): - run(*args, cwd=self.path, **kwargs) + Process.run(*args, cwd=self.path, **kwargs) class TestRepo(Repo): |