diff --git a/src/fenrirscreenreader/utils/x_clipboard.py b/src/fenrirscreenreader/utils/x_clipboard.py index 5847a4df..ececbe24 100644 --- a/src/fenrirscreenreader/utils/x_clipboard.py +++ b/src/fenrirscreenreader/utils/x_clipboard.py @@ -68,11 +68,14 @@ def _run_command(command, display, input_text=None): input_bytes = None if input_text is not None: input_bytes = input_text.encode(_ENCODING) + output_target = ( + subprocess.PIPE if input_text is None else subprocess.DEVNULL + ) return subprocess.run( command, input=input_bytes, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + stdout=output_target, + stderr=output_target, env=_env_for_display(display), timeout=_CLIPBOARD_TIMEOUT, check=False, @@ -80,7 +83,9 @@ def _run_command(command, display, input_text=None): def _command_error(command, result): - stderr = result.stderr.decode(_ENCODING, errors="replace").strip() + stderr = (result.stderr or b"").decode( + _ENCODING, errors="replace" + ).strip() if stderr: return RuntimeError(stderr) return RuntimeError( diff --git a/tests/unit/test_x_clipboard.py b/tests/unit/test_x_clipboard.py index f361b50b..bee2c77b 100644 --- a/tests/unit/test_x_clipboard.py +++ b/tests/unit/test_x_clipboard.py @@ -37,6 +37,25 @@ def test_write_text_uses_display_env_without_mutating_process_env( assert os.environ.get("DISPLAY") is None +@pytest.mark.unit +def test_write_text_does_not_capture_daemonized_writer_pipes(monkeypatch): + monkeypatch.setattr(x_clipboard.shutil, "which", command_exists) + + def run_command(command, **kwargs): + if ( + kwargs["stdout"] == subprocess.PIPE + or kwargs["stderr"] == subprocess.PIPE + ): + raise subprocess.TimeoutExpired(command, 2.0) + return subprocess.CompletedProcess( + command, 0, stdout=None, stderr=None + ) + + monkeypatch.setattr(x_clipboard.subprocess, "run", run_command) + + assert x_clipboard.write_text("clipboard text", ":3") is True + + @pytest.mark.unit def test_read_text_scans_displays_until_text_is_found(monkeypatch): monkeypatch.delenv("DISPLAY", raising=False)