The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(test): mock subprocess.Popen for npm engine-failure watcher path
Zak B. Elep31337b388b17
diff --git a/tests/hermes_cli/test_cmd_update.py b/tests/hermes_cli/test_cmd_update.pyindex 7f3ae53ef38..6dd937d10f8 100644--- a/tests/hermes_cli/test_cmd_update.py+++ b/tests/hermes_cli/test_cmd_update.py@@ -822,9 +822,36 @@ class TestUpdateNodeDependencies:if call.args and "npm" in str(call.args[0][0])]- @patch("subprocess.run")+ def _make_popen(self, calls, returncode=0, stderr_lines=()):+ """Fake subprocess.Popen recording each invocation's cmd/kwargs.++ _update_node_dependencies always runs npm with capture_output=False,+ which routes through the Popen-based stderr-teeing path in+ _run_npm_watching_for_engine_failure rather than subprocess.run.+ """++ class _FakeProc:+ def __init__(self, cmd, **kwargs):+ calls.append({"cmd": cmd, "kwargs": kwargs})+ self.stderr = iter(stderr_lines)++ def __enter__(self):+ return self++ def __exit__(self, *exc_info):+ return False++ def wait(self):+ return returncode++ return _FakeProc++ def _popen_npm_calls(self, calls):+ return [c["cmd"] for c in calls if c["cmd"] and "npm" in str(c["cmd"][0])]++ @patch("subprocess.Popen")@patch("shutil.which", return_value="/usr/bin/npm")- def test_install_names_ui_tui_and_web_workspaces(self, _which, mock_run, tmp_path, monkeypatch):+ def test_install_names_ui_tui_and_web_workspaces(self, _which, mock_popen, tmp_path, monkeypatch):"""Regression for #43564: install ui-tui + web directly. apps/desktopmust never appear, so its Electron postinstall is never triggered."""@@ -834,11 +861,12 @@ class TestUpdateNodeDependencies:(tmp_path / "package-lock.json").write_text("{}")monkeypatch.setattr(hm, "PROJECT_ROOT", tmp_path)monkeypatch.setattr(hm, "_npm_lockfile_changed", lambda root: True)- mock_run.return_value = subprocess.CompletedProcess([], 0, stdout="", stderr="")+ popen_calls = []+ mock_popen.side_effect = self._make_popen(popen_calls)hm._update_node_dependencies()- calls = self._npm_calls(mock_run)+ calls = self._popen_npm_calls(popen_calls)assert len(calls) == 1, f"expected exactly 1 npm call, got: {calls}"joined = " ".join(str(a) for a in calls[0])assert "--workspace ui-tui" in joined and "--workspace web" in joined, (@@ -851,9 +879,9 @@ class TestUpdateNodeDependencies:f"no root-only deps remain to protect; --workspaces=false is unnecessary now; actual: {calls[0]}")- @patch("subprocess.run")+ @patch("subprocess.Popen")@patch("shutil.which", return_value="/usr/bin/npm")- def test_install_preserves_standard_flags(self, _which, mock_run, tmp_path, monkeypatch):+ def test_install_preserves_standard_flags(self, _which, mock_popen, tmp_path, monkeypatch):"""--no-fund, --no-audit, --progress=false must survive."""from hermes_cli import main as hm@@ -861,11 +889,12 @@ class TestUpdateNodeDependencies:(tmp_path / "package-lock.json").write_text("{}")monkeypatch.setattr(hm, "PROJECT_ROOT", tmp_path)monkeypatch.setattr(hm, "_npm_lockfile_changed", lambda root: True)- mock_run.return_value = subprocess.CompletedProcess([], 0, stdout="", stderr="")+ popen_calls = []+ mock_popen.side_effect = self._make_popen(popen_calls)hm._update_node_dependencies()- calls = self._npm_calls(mock_run)+ calls = self._popen_npm_calls(popen_calls)assert len(calls) == 1joined = " ".join(str(a) for a in calls[0])for flag in ("--no-fund", "--no-audit", "--progress=false"):@@ -888,9 +917,9 @@ class TestUpdateNodeDependencies:"npm must not run when _npm_lockfile_changed reports no change")- @patch("subprocess.run")+ @patch("subprocess.Popen")@patch("shutil.which", return_value="/usr/bin/npm")- def test_runs_install_when_lockfile_changed(self, _which, mock_run, tmp_path, monkeypatch):+ def test_runs_install_when_lockfile_changed(self, _which, mock_popen, tmp_path, monkeypatch):"""When _npm_lockfile_changed reports a change, npm must run."""from hermes_cli import main as hm@@ -898,16 +927,17 @@ class TestUpdateNodeDependencies:(tmp_path / "package-lock.json").write_text("{}")monkeypatch.setattr(hm, "PROJECT_ROOT", tmp_path)monkeypatch.setattr(hm, "_npm_lockfile_changed", lambda root: True)- mock_run.return_value = subprocess.CompletedProcess([], 0, stdout="", stderr="")+ popen_calls = []+ mock_popen.side_effect = self._make_popen(popen_calls)hm._update_node_dependencies()- calls = self._npm_calls(mock_run)+ calls = self._popen_npm_calls(popen_calls)assert len(calls) == 1, f"expected npm to run when lockfile changed; got: {calls}"- @patch("subprocess.run")+ @patch("subprocess.Popen")@patch("shutil.which", return_value="/usr/bin/npm")- def test_records_lockfile_hash_only_on_success(self, _which, mock_run, tmp_path, monkeypatch):+ def test_records_lockfile_hash_only_on_success(self, _which, mock_popen, tmp_path, monkeypatch):"""A failed install must not record the lockfile hash (so the nextrun retries instead of wrongly believing deps are up to date)."""from hermes_cli import main as hm@@ -918,18 +948,16 @@ class TestUpdateNodeDependencies:monkeypatch.setattr(hm, "_npm_lockfile_changed", lambda root: True)recorded = []monkeypatch.setattr(hm, "_record_npm_lockfile_hash", lambda root: recorded.append(root))- mock_run.return_value = subprocess.CompletedProcess(- [], 1, stdout="", stderr="npm ERR!"- )+ mock_popen.side_effect = self._make_popen([], returncode=1, stderr_lines=["npm ERR!\n"])hm._update_node_dependencies()assert not recorded, "lockfile hash must not be recorded when npm install fails"- @patch("subprocess.run")+ @patch("subprocess.Popen")@patch("shutil.which", return_value="/usr/bin/npm")def test_warms_npx_agent_browser_cache_regardless_of_install_result(- self, _which, mock_run, tmp_path, monkeypatch+ self, _which, mock_popen, tmp_path, monkeypatch):"""The npx warm-up must fire even when the workspace install fails —it's independent of ui-tui/web dependency state (#43564)."""@@ -939,9 +967,7 @@ class TestUpdateNodeDependencies:(tmp_path / "package-lock.json").write_text("{}")monkeypatch.setattr(hm, "PROJECT_ROOT", tmp_path)monkeypatch.setattr(hm, "_npm_lockfile_changed", lambda root: True)- mock_run.return_value = subprocess.CompletedProcess(- [], 1, stdout="", stderr="npm ERR!"- )+ mock_popen.side_effect = self._make_popen([], returncode=1, stderr_lines=["npm ERR!\n"])with patch("tools.browser_tool.warm_agent_browser_npx_cache", return_value=True@@ -975,9 +1001,9 @@ class TestUpdateNodeDependencies:mock_run.assert_not_called()- @patch("subprocess.run")+ @patch("subprocess.Popen")@patch("shutil.which", return_value="/usr/bin/npm")- def test_install_runs_from_project_root(self, _which, mock_run, tmp_path, monkeypatch):+ def test_install_runs_from_project_root(self, _which, mock_popen, tmp_path, monkeypatch):"""npm install must execute from PROJECT_ROOT, not a workspace subdir."""from hermes_cli import main as hm@@ -985,17 +1011,16 @@ class TestUpdateNodeDependencies:(tmp_path / "package-lock.json").write_text("{}")monkeypatch.setattr(hm, "PROJECT_ROOT", tmp_path)- cwd_calls = []-- def capture(cmd, **kwargs):- if "npm" in str(cmd[0]):- cwd_calls.append(kwargs.get("cwd"))- return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")-- mock_run.side_effect = capture+ popen_calls = []+ mock_popen.side_effect = self._make_popen(popen_calls)hm._update_node_dependencies()+ cwd_calls = [+ c["kwargs"].get("cwd")+ for c in popen_calls+ if c["cmd"] and "npm" in str(c["cmd"][0])+ ]assert cwd_calls, "expected at least one npm call"for cwd in cwd_calls:assert cwd == tmp_path, f"npm must run from PROJECT_ROOT; got cwd={cwd}"