From 712466f77bb88aafc3fc1839ce1dcbd5ba218adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Sat, 28 Dec 2024 13:17:10 -0600 Subject: [PATCH] refactor: Use `io.TextIOWrapper` to redirect stdout and stderr --- singer_sdk/testing/legacy.py | 14 +++++++------- singer_sdk/testing/runners.py | 8 ++++---- tests/samples/test_tap_countries.py | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/singer_sdk/testing/legacy.py b/singer_sdk/testing/legacy.py index 30849abd1..69aed4b07 100644 --- a/singer_sdk/testing/legacy.py +++ b/singer_sdk/testing/legacy.py @@ -111,7 +111,7 @@ def get_standard_target_tests( return [] -def tap_sync_test(tap: Tap) -> tuple[io.StringIO, io.StringIO]: +def tap_sync_test(tap: Tap) -> tuple[io.TextIOWrapper, io.TextIOWrapper]: """Invokes a Tap object and return STDOUT and STDERR results in StringIO buffers. Args: @@ -120,8 +120,8 @@ def tap_sync_test(tap: Tap) -> tuple[io.StringIO, io.StringIO]: Returns: A 2-item tuple with StringIO buffers from the Tap's output: (stdout, stderr) """ - stdout_buf = io.StringIO() - stderr_buf = io.StringIO() + stdout_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") + stderr_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(stdout_buf), redirect_stderr(stderr_buf): tap.sync_all() stdout_buf.seek(0) @@ -186,8 +186,8 @@ def target_sync_test( Returns: A 2-item tuple with StringIO buffers from the Target's output: (stdout, stderr) """ - stdout_buf = io.StringIO() - stderr_buf = io.StringIO() + stdout_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") + stderr_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(stdout_buf), redirect_stderr(stderr_buf): if input is not None: @@ -236,7 +236,7 @@ def sync_end_to_end(tap: Tap, target: Target, *mappers: InlineMapper) -> None: mappers: Zero or more inline mapper to apply in between the tap and target, in order. """ - buf = io.StringIO() + buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(buf): tap.sync_all() @@ -244,7 +244,7 @@ def sync_end_to_end(tap: Tap, target: Target, *mappers: InlineMapper) -> None: mapper_output = buf for mapper in mappers: - buf = io.StringIO() + buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(buf): mapper.listen(mapper_output) diff --git a/singer_sdk/testing/runners.py b/singer_sdk/testing/runners.py index 145f44deb..8c0791a50 100644 --- a/singer_sdk/testing/runners.py +++ b/singer_sdk/testing/runners.py @@ -184,8 +184,8 @@ def _execute_sync(self) -> tuple[str, str]: Returns: A 2-item tuple with StringIO buffers from the Tap's output: (stdout, stderr) """ - stdout_buf = io.StringIO() - stderr_buf = io.StringIO() + stdout_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") + stderr_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(stdout_buf), redirect_stderr(stderr_buf): self.run_sync_dry_run() stdout_buf.seek(0) @@ -294,8 +294,8 @@ def _execute_sync( # noqa: PLR6301 A 2-item tuple with StringIO buffers from the Target's output: (stdout, stderr) """ - stdout_buf = io.StringIO() - stderr_buf = io.StringIO() + stdout_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") + stderr_buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(stdout_buf), redirect_stderr(stderr_buf): if target_input is not None: diff --git a/tests/samples/test_tap_countries.py b/tests/samples/test_tap_countries.py index 5730eed96..b97259262 100644 --- a/tests/samples/test_tap_countries.py +++ b/tests/samples/test_tap_countries.py @@ -116,7 +116,7 @@ def test_batch_mode(outdir): }, ) - buf = io.StringIO() + buf = io.TextIOWrapper(io.BytesIO(), encoding="utf-8") with redirect_stdout(buf): tap.sync_all()