fix(cli): preserve invalid config when backup fails
This commit is contained in:
1 parent
8003cf39c2
commit
41deb48005
2 files changed
+31
-1
No files matched your search
@@ -219,8 +219,10 @@ def _merge_json_file(
|
|||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"Existing JSON in {file_path} could not be parsed AND the backup to"
|
f"Existing JSON in {file_path} could not be parsed AND the backup to"
|
||||||
f" {backup_path} failed ({exc}); the original content will be overwritten."
|
f" {backup_path} failed ({exc}); refusing to rewrite it."
|
||||||
|
" Fix or remove the file, then re-run the install."
|
||||||
)
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
for remove_path in remove_paths:
|
for remove_path in remove_paths:
|
||||||
parent = data
|
parent = data
|
||||||
|
|||||||
@@ -374,6 +374,34 @@ def test_cli_mcp_install_jsonc_and_backup(tmp_path, monkeypatch):
|
|||||||
assert "INVALID JSON DATA" in backup_file.read_text(encoding="utf-8")
|
assert "INVALID JSON DATA" in backup_file.read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_mcp_install_refuses_rewrite_when_backup_fails(tmp_path, monkeypatch):
|
||||||
|
"""Backup failure for an unparseable config must abort the rewrite instead of destroying it."""
|
||||||
|
monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)
|
||||||
|
monkeypatch.setattr("mcp_server.utils.env_utils.get_project_root", lambda: str(tmp_path))
|
||||||
|
|
||||||
|
cursor_dir = tmp_path / ".cursor"
|
||||||
|
cursor_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
mcp_json = cursor_dir / "mcp.json"
|
||||||
|
original = "INVALID JSON DATA {{{{"
|
||||||
|
mcp_json.write_text(original, encoding="utf-8")
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
real_write_text = pathlib.Path.write_text
|
||||||
|
|
||||||
|
def failing_bak_write(self, *args, **kwargs):
|
||||||
|
if str(self).endswith(".bak"):
|
||||||
|
raise OSError("disk full")
|
||||||
|
return real_write_text(self, *args, **kwargs)
|
||||||
|
|
||||||
|
monkeypatch.setattr("pathlib.Path.write_text", failing_bak_write)
|
||||||
|
|
||||||
|
runner.invoke(app, ["mcp", "--install", "cursor"])
|
||||||
|
|
||||||
|
assert mcp_json.read_text(encoding="utf-8") == original
|
||||||
|
assert not (cursor_dir / "mcp.json.bak").exists()
|
||||||
|
|
||||||
|
|
||||||
def test_cli_mcp_install_openclaw_migrates_legacy_plugin_shape(tmp_path, monkeypatch):
|
def test_cli_mcp_install_openclaw_migrates_legacy_plugin_shape(tmp_path, monkeypatch):
|
||||||
"""Verify reinstalling OpenClaw replaces the obsolete plugin wrapper with mcp.servers."""
|
"""Verify reinstalling OpenClaw replaces the obsolete plugin wrapper with mcp.servers."""
|
||||||
monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)
|
monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)
|
||||||
|
|||||||
Reference in new issue
Block a user