Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/branch-24.06' into feat/genera…
Browse files Browse the repository at this point in the history
…lized_patching
  • Loading branch information
vyasr committed Apr 4, 2024
2 parents 505dc47 + 7726daa commit c215163
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
import contextlib
import tempfile
from functools import wraps
from multiprocessing import Process


def run_test_in_subprocess(func):
def redirect_stdout_stderr(func, stdout, stderr, *args, **kwargs):
with open(stdout, "w") as stdout_file, open(stderr, "w") as stderr_file:
with contextlib.redirect_stdout(stdout_file), contextlib.redirect_stderr(
stderr_file
):
func(*args, **kwargs)

@wraps(func)
def wrapper(*args, **kwargs):
p = Process(target=func, args=args, kwargs=kwargs)
p.start()
p.join()
with tempfile.NamedTemporaryFile(
mode="w+"
) as stdout, tempfile.NamedTemporaryFile(mode="w+") as stderr:
p = Process(
target=redirect_stdout_stderr,
args=(func, stdout.name, stderr.name, *args),
kwargs=kwargs,
)
p.start()
p.join()
stdout_log = stdout.file.read()
stderr_log = stderr.file.read()
if p.exitcode != 0:
msg = f"Process exited {p.exitcode}."
if stdout_log:
msg += f"\nstdout:\n{stdout_log}"
if stderr_log:
msg += f"\nstderr:\n{stderr_log}"
raise RuntimeError(msg)

return wrapper

Expand Down

0 comments on commit c215163

Please sign in to comment.