fix: validate optional Python resources and isolate downloads

This commit is contained in:
yudshj committed 2026-10-09 15:52:59 +08:00
1 parent 305021fcd6
commit 4fd85be130
19 files changed
+335 -149

No files matched your search

+69 -8
View File
@@ -332,24 +332,57 @@ def test_runtime_starts_independently_of_office_engine(
assert runtime.bundled_runtime_path() == executable
@pytest.mark.parametrize("function,flag", [
(runtime.download_office, "--download-office"),
(runtime.download_primary_runtime, "--download-primary-runtime"),
@pytest.mark.parametrize("function,resource", [
(runtime.download_office, "office"),
(runtime.download_primary_runtime, "primary"),
])
def test_explicit_download_uses_runtime_without_a_harness_home(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, function, flag: str,
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, function, resource: str,
) -> None:
root = tmp_path / "中文缓存"
root.mkdir()
_resource_sidecars(root / "deepseek-harness-sdk-runtime-linux-x64")
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
script = tmp_path / "download.py"
destination = tmp_path / "downloaded"
script.write_text("import json,sys\n" + f"assert sys.argv[1] == {flag!r}\nprint(json.dumps({str(destination)!r}))\n")
destination = root / "linux-x64/primary-runtime" if resource == "primary" else root
script.write_text("import json,os,sys\n" + f"assert os.environ['DSH_RUNTIME_DOWNLOAD'] == {resource!r}\n"
+ f"sys.stdout.buffer.write(json.dumps({str(destination)!r}, ensure_ascii=False).encode('utf-8'))\n", encoding="utf-8")
monkeypatch.delenv("DSH_HOME", raising=False)
monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: (sys.executable, str(script)))
assert function() == destination
script.write_text("import sys\nprint('checksum mismatch', file=sys.stderr)\nsys.exit(2)\n")
with pytest.raises(RuntimeError, match="checksum mismatch"):
script.write_text("import sys\nsys.stderr.buffer.write('下载失败'.encode('utf-8'))\nsys.exit(2)\n", encoding="utf-8")
with pytest.raises(RuntimeError, match="下载失败"):
function()
@pytest.mark.skipif(os.name == "nt", reason="LC_ALL=C is a POSIX locale fixture")
def test_download_decodes_utf8_in_an_ascii_locale(tmp_path: Path) -> None:
script = tmp_path / "download.py"
destination = tmp_path / "中文缓存"
script.write_text("import sys\nsys.stdout.buffer.write(" + json.dumps(str(destination), ensure_ascii=False).encode('utf-8').__repr__() + ")\n", encoding="utf-8")
code = ("import sys,locale,deepseek_harness_runtime as r; "
"assert locale.getpreferredencoding(False).lower() not in ('utf-8','utf8'); "
f"r.resolve_bundled_launch_args=lambda:(sys.executable,{str(script)!r}); "
f"assert str(r.download_office())=={str(destination)!r}")
result = subprocess.run([sys.executable, "-c", code], capture_output=True, encoding="utf-8", env={
**os.environ, "LC_ALL": "C", "PYTHONUTF8": "0", "PYTHONCOERCECLOCALE": "0",
"PYTHONPATH": str(Path(runtime.__file__).resolve().parents[1]),
}, timeout=30)
assert result.returncode == 0, result.stderr
@pytest.mark.parametrize("missing", ["primary-runtime/runtime.json", "primary-runtime/dependencies/python/bin/python3",
"primary-runtime/dependencies/node/bin/node", "office-skills/scripts/check_office.py"])
def test_primary_download_rejects_an_incomplete_payload(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, missing: str) -> None:
_resource_sidecars(tmp_path / "deepseek-harness-sdk-runtime-linux-x64")
root = tmp_path / "linux-x64"
(root / missing).unlink()
monkeypatch.setattr(runtime, "_download", lambda resource: root / "primary-runtime")
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
with pytest.raises(FileNotFoundError):
runtime.download_primary_runtime()
def test_office_resolution_requires_explicit_download(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from deepseek_harness_runtime._resources import office_launch_args
@@ -366,4 +399,32 @@ def test_office_resolution_requires_explicit_download(tmp_path: Path, monkeypatc
for path in (node, cli):
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
with pytest.raises(FileNotFoundError, match="incomplete"):
office_launch_args(tmp_path / "linux-x64", "linux-x64")
(office / "complete").write_text("wrong", encoding="utf-8")
with pytest.raises(ValueError, match="identity mismatch"):
office_launch_args(tmp_path / "linux-x64", "linux-x64")
(office / "complete").write_text("a" * 64, encoding="utf-8")
assert office_launch_args(tmp_path / "linux-x64", "linux-x64") == (str(node), str(cli))
(office / "complete").unlink()
monkeypatch.setenv("DSH_OFFICE_SIDECAR", str(office))
assert office_launch_args(tmp_path / "linux-x64", "linux-x64") == (str(node), str(cli))
@pytest.mark.parametrize("invalid", ["target", "identity", "office", "pnpm", "skills"])
def test_download_manifest_rejects_invalid_wheel_resources(tmp_path: Path, invalid: str) -> None:
from deepseek_harness_runtime._resources import validate_downloads
_resource_sidecars(tmp_path / "deepseek-harness-sdk-runtime-linux-x64")
root = tmp_path / "linux-x64"
path = root / "downloads.json"
manifest = json.loads(path.read_text(encoding="utf-8"))
if invalid == "skills":
(root / "office-skills/scripts/check_office.py").unlink()
with pytest.raises(FileNotFoundError, match="Office skill"):
validate_downloads(root, "linux-x64")
else:
manifest[invalid] = {"target": "win-x64", "identity": "z" * 64, "office": [], "pnpm": []}[invalid]
path.write_text(json.dumps(manifest), encoding="utf-8")
with pytest.raises(ValueError, match="invalid runtime download manifest"):
validate_downloads(root, "linux-x64")