Official Codebase of 3DCodeVerse
Python
doctor: a torn node_modules package.json is a FAIL row, not a crash of the whole report
gaoypeng91a8ff5545f0
diff --git a/harness/codeverse3d/doctor.py b/harness/codeverse3d/doctor.pyindex 9a1b4a36..a3a0912a 100644--- a/harness/codeverse3d/doctor.py+++ b/harness/codeverse3d/doctor.py@@ -7,12 +7,11 @@ shim is a CLI concern (tests/install imports these checks as documentation factsfrom __future__ import annotationsimport importlib-import jsonimport shutilfrom pathlib import Pathfrom codeverse3d.config import get_settings-from codeverse3d.proc import version_line+from codeverse3d.proc import read_json_or_none, version_lineRow = tuple[str, str, str]_PY_DEPS = ("pydantic", "pydantic_settings", "typer", "rich", "jinja2", "yaml", "numpy", "trimesh", "fcl", "PIL", "pyarrow",@@ -89,19 +88,20 @@ def check_node() -> list[Row]:except RuntimeError as e: # non-editable install without C3D_RUNTIME_JSrows.append(("runtime_js", "FAIL", str(e)))return rows- three = rj / "node_modules" / "three" / "package.json"- pup = rj / "node_modules" / "puppeteer" / "package.json"- if three.is_file():- rows.append(("three", "OK", json.loads(three.read_text()).get("version", "?") + f" ({rj / 'node_modules'})"))+ # a missing, torn or non-object package.json is the same broken install: never a crash+ three = read_json_or_none(rj / "node_modules" / "three" / "package.json")+ pup = read_json_or_none(rj / "node_modules" / "puppeteer" / "package.json")+ if three is not None:+ rows.append(("three", "OK", f"{three.get('version', '?')} ({rj / 'node_modules'})"))else:- rows.append(("three", "FAIL", f"runtime_js/node_modules/three missing — run `npm install` in {rj}"))- if pup.is_file():+ rows.append(("three", "FAIL", f"runtime_js/node_modules/three missing or unreadable — run `npm install` in {rj}"))+ if pup is not None:cache = Path.home() / ".cache" / "puppeteer"chrome = any(cache.rglob("chrome")) if cache.is_dir() else Falserows.append(("puppeteer", "OK" if chrome else "WARN",- f"v{json.loads(pup.read_text()).get('version', '?')}; chrome cache {'found' if chrome else 'missing'} ({cache})"))+ f"v{pup.get('version', '?')}; chrome cache {'found' if chrome else 'missing'} ({cache})"))else:- rows.append(("puppeteer", "FAIL", "not installed in runtime_js"))+ rows.append(("puppeteer", "FAIL", "not installed in runtime_js (or its package.json is unreadable)"))return rowsdiff --git a/harness/tests/core/test_doctor.py b/harness/tests/core/test_doctor.pyindex 5e210296..8ebd0b39 100644--- a/harness/tests/core/test_doctor.py+++ b/harness/tests/core/test_doctor.py@@ -1,4 +1,4 @@-"""``3dcode doctor``'s GPU probe must hand the browser back, never close it."""+"""``3dcode doctor``: the GPU probe hands the browser back, and a broken install is a row, never a crash."""from __future__ import annotations@@ -7,7 +7,7 @@ from pathlib import Pathimport pytest-from codeverse3d.doctor import check_gpu_probe+from codeverse3d.doctor import check_gpu_probe, check_node@pytest.mark.node@@ -24,3 +24,15 @@ def test_the_gpu_probe_releases_the_browser_instead_of_closing_it(tmp_path: Pathmonkeypatch.setenv("C3D_RUNTIME_JS", str(tmp_path))assert check_gpu_probe(timeout_s=60) == [("chrome webgl", "OK", "GPU: fake")]assert mark.read_text() == "release"+++def test_a_torn_package_json_is_a_failed_row_not_a_crash(tmp_path: Path, monkeypatch):+ """Review 2 B9: ``json.loads`` on node_modules/*/package.json was unguarded, so one torn+ file (an interrupted ``npm install``) crashed the whole doctor report."""+ for pkg in ("three", "puppeteer"):+ (tmp_path / "node_modules" / pkg).mkdir(parents=True)+ (tmp_path / "node_modules" / pkg / "package.json").write_text('{"version": "0.18')+ monkeypatch.setenv("C3D_RUNTIME_JS", str(tmp_path))+ rows = {name: (status, detail) for name, status, detail in check_node()}+ assert rows["three"][0] == "FAIL" and "unreadable" in rows["three"][1]+ assert rows["puppeteer"][0] == "FAIL"