fix(screenshot): recover adb after empty screencap output
Windows Package CI / package-fresh-install (push) Waiting to run
Windows Package CI / package-fresh-install (push) Waiting to run
This commit is contained in:
1 parent
329b7756f4
commit
60306dd4e0
2 files changed
+125
-45
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from io import BytesIO
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -132,3 +133,55 @@ def test_capture_screenshot_direct_tries_stdout_after_invalid_pull_file(tmp_path
|
||||
assert result is True
|
||||
assert (pic_dir / "screenshot.png").read_bytes().startswith(b"\x89PNG")
|
||||
assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)
|
||||
|
||||
|
||||
def test_screenshot_operations_recovers_after_zero_byte_execout(tmp_path, monkeypatch):
|
||||
from douyin.core import path_config
|
||||
from PIL import Image
|
||||
|
||||
from operations import screenshot_operations
|
||||
|
||||
pic_dir = tmp_path / "pic"
|
||||
pic_dir.mkdir()
|
||||
buf = BytesIO()
|
||||
image = Image.new("RGB", (256, 256))
|
||||
image.putdata(
|
||||
[
|
||||
((x * 17 + y * 3) % 255, (y * 19 + x * 5) % 255, (x * y) % 255)
|
||||
for y in range(256)
|
||||
for x in range(256)
|
||||
]
|
||||
)
|
||||
image.save(buf, format="PNG")
|
||||
png_bytes = buf.getvalue()
|
||||
calls: list[list[str]] = []
|
||||
execout_count = 0
|
||||
|
||||
def fake_run(cmd, **_kwargs):
|
||||
nonlocal execout_count
|
||||
calls.append(list(cmd))
|
||||
if cmd[:4] == ["adb", "-s", "MOCK", "exec-out"]:
|
||||
execout_count += 1
|
||||
if execout_count == 1:
|
||||
return SimpleNamespace(returncode=0, stdout=b"", stderr=b"")
|
||||
return SimpleNamespace(returncode=0, stdout=png_bytes, stderr=b"")
|
||||
if cmd == ["adb", "kill-server"] or cmd == ["adb", "start-server"]:
|
||||
return SimpleNamespace(returncode=0, stdout=b"", stderr=b"")
|
||||
raise AssertionError(f"unexpected command: {cmd}")
|
||||
|
||||
monkeypatch.setenv("ADB_EXECUTABLE", "adb")
|
||||
monkeypatch.setattr(
|
||||
path_config,
|
||||
"get_pic_dir",
|
||||
lambda: str(pic_dir),
|
||||
raising=False,
|
||||
)
|
||||
monkeypatch.setattr(screenshot_operations.subprocess, "run", fake_run)
|
||||
monkeypatch.setattr(screenshot_operations, "_mirror_screenshot_to_external", lambda *_: None)
|
||||
|
||||
result = screenshot_operations.ScreenshotOperations().get_screenshot("MOCK", use_service=False)
|
||||
|
||||
assert result is True
|
||||
assert (pic_dir / "screenshot.png").read_bytes().startswith(b"\x89PNG")
|
||||
assert ["adb", "kill-server"] in calls
|
||||
assert ["adb", "start-server"] in calls
|
||||
Reference in new issue
Block a user