| 1 |
import os |
| 2 |
import subprocess |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from fenrirscreenreader.utils import x_clipboard |
| 8 |
|
| 9 |
|
| 10 |
def command_exists(command): |
| 11 |
if command == "xclip": |
| 12 |
return "/usr/bin/xclip" |
| 13 |
return None |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.unit |
| 17 |
def test_write_text_uses_display_env_without_mutating_process_env( |
| 18 |
monkeypatch, |
| 19 |
): |
| 20 |
monkeypatch.delenv("DISPLAY", raising=False) |
| 21 |
monkeypatch.setattr(x_clipboard.shutil, "which", command_exists) |
| 22 |
run_command = Mock( |
| 23 |
return_value=subprocess.CompletedProcess( |
| 24 |
["xclip"], 0, stdout=b"", stderr=b"" |
| 25 |
) |
| 26 |
) |
| 27 |
monkeypatch.setattr(x_clipboard.subprocess, "run", run_command) |
| 28 |
|
| 29 |
assert x_clipboard.write_text("clipboard text", ":3") is True |
| 30 |
|
| 31 |
command = run_command.call_args.args[0] |
| 32 |
kwargs = run_command.call_args.kwargs |
| 33 |
assert command == ["xclip", "-selection", "clipboard"] |
| 34 |
assert kwargs["input"] == b"clipboard text" |
| 35 |
assert kwargs["env"]["DISPLAY"] == ":3" |
| 36 |
assert kwargs["timeout"] == 2.0 |
| 37 |
assert os.environ.get("DISPLAY") is None |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.unit |
| 41 |
def test_write_text_does_not_capture_daemonized_writer_pipes(monkeypatch): |
| 42 |
monkeypatch.setattr(x_clipboard.shutil, "which", command_exists) |
| 43 |
|
| 44 |
def run_command(command, **kwargs): |
| 45 |
if ( |
| 46 |
kwargs["stdout"] == subprocess.PIPE |
| 47 |
or kwargs["stderr"] == subprocess.PIPE |
| 48 |
): |
| 49 |
raise subprocess.TimeoutExpired(command, 2.0) |
| 50 |
return subprocess.CompletedProcess( |
| 51 |
command, 0, stdout=None, stderr=None |
| 52 |
) |
| 53 |
|
| 54 |
monkeypatch.setattr(x_clipboard.subprocess, "run", run_command) |
| 55 |
|
| 56 |
assert x_clipboard.write_text("clipboard text", ":3") is True |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.unit |
| 60 |
def test_read_text_scans_displays_until_text_is_found(monkeypatch): |
| 61 |
monkeypatch.delenv("DISPLAY", raising=False) |
| 62 |
monkeypatch.setattr(x_clipboard.shutil, "which", command_exists) |
| 63 |
displays = [] |
| 64 |
|
| 65 |
def run_command(command, **kwargs): |
| 66 |
displays.append(kwargs["env"].get("DISPLAY")) |
| 67 |
if kwargs["env"].get("DISPLAY") == ":1": |
| 68 |
return subprocess.CompletedProcess( |
| 69 |
command, 0, stdout=b"from x", stderr=b"" |
| 70 |
) |
| 71 |
return subprocess.CompletedProcess( |
| 72 |
command, 1, stdout=b"", stderr=b"missing display" |
| 73 |
) |
| 74 |
|
| 75 |
monkeypatch.setattr(x_clipboard.subprocess, "run", run_command) |
| 76 |
|
| 77 |
assert x_clipboard.read_text(scan_displays=True) == "from x" |
| 78 |
assert displays == [":0", ":1"] |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.unit |
| 82 |
def test_read_text_empty_success_returns_none(monkeypatch): |
| 83 |
monkeypatch.setenv("DISPLAY", ":2") |
| 84 |
monkeypatch.setattr(x_clipboard.shutil, "which", command_exists) |
| 85 |
monkeypatch.setattr( |
| 86 |
x_clipboard.subprocess, |
| 87 |
"run", |
| 88 |
Mock( |
| 89 |
return_value=subprocess.CompletedProcess( |
| 90 |
["xclip"], 0, stdout=b"", stderr=b"" |
| 91 |
) |
| 92 |
), |
| 93 |
) |
| 94 |
|
| 95 |
assert x_clipboard.read_text() is None |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.unit |
| 99 |
def test_write_text_propagates_clipboard_timeout(monkeypatch): |
| 100 |
monkeypatch.setattr(x_clipboard.shutil, "which", command_exists) |
| 101 |
timeout = subprocess.TimeoutExpired(["xclip"], 2.0) |
| 102 |
monkeypatch.setattr( |
| 103 |
x_clipboard.subprocess, |
| 104 |
"run", |
| 105 |
Mock(side_effect=timeout), |
| 106 |
) |
| 107 |
|
| 108 |
with pytest.raises(subprocess.TimeoutExpired): |
| 109 |
x_clipboard.write_text("clipboard text", ":3") |