fix(config): never autosave defaults over an unreadable config file

Additional changes
The migration copies the old file to the backup instead of moving it and replaces the file atomically, only after the migration succeeded.
An explicit save keeps a copy of a file that could not be loaded and serializes the configuration before it opens the file.
The configuration guide describes the migration backup and what happens to a file that cannot be loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
Christin
2026-11-20 18:39:14 +01:00
committed by Bobby Noelte
co-authored by Claude Opus 5.5
parent 887bae551b
commit f3773de8f8
5 changed files with 236 additions and 32 deletions
+46
View File
@@ -1,9 +1,12 @@
# Test data directory and known migration pairs
import copy
import json
import os
import shutil
import stat
from pathlib import Path
from typing import Any
from unittest.mock import Mock
import pytest
@@ -144,6 +147,49 @@ class TestConfigMigration:
assert result is True
assert not backup_file.exists(), "No backup should be made if config is already current."
def test_migrate_config_file_backs_up_by_copy(self, tmp_config_file: Path) -> None:
"""The backup is a copy of the old file, the migrated file keeps its permissions."""
tmp_config_file.chmod(0o640)
mode = stat.S_IMODE(tmp_config_file.stat().st_mode)
original = tmp_config_file.read_bytes()
backup_file = tmp_config_file.with_suffix(".bak")
assert configmigrate.migrate_config_file(tmp_config_file, backup_file) is True
assert backup_file.read_bytes() == original
migrated_data = json.loads(tmp_config_file.read_text(encoding="utf-8"))
assert migrated_data["general"]["version"] == __version__
assert stat.S_IMODE(tmp_config_file.stat().st_mode) == mode
assert not list(tmp_config_file.parent.glob("*.tmp"))
def test_migrate_config_file_failure_keeps_original(
self, tmp_config_file: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A migration that raises leaves the old file in place and needs no backup."""
original = tmp_config_file.read_bytes()
backup_file = tmp_config_file.with_suffix(".bak")
monkeypatch.setattr(
configmigrate, "migrate_config_data", Mock(side_effect=RuntimeError("migration failed"))
)
assert configmigrate.migrate_config_file(tmp_config_file, backup_file) is False
assert tmp_config_file.read_bytes() == original
assert not backup_file.exists()
def test_migrate_config_file_write_failure_keeps_original(
self, tmp_config_file: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A migrated file that can not replace the old one leaves the old file in place."""
original = tmp_config_file.read_bytes()
backup_file = tmp_config_file.with_suffix(".bak")
monkeypatch.setattr(os, "replace", Mock(side_effect=OSError("replace failed")))
assert configmigrate.migrate_config_file(tmp_config_file, backup_file) is False
assert tmp_config_file.read_bytes() == original
assert not list(tmp_config_file.parent.glob("*.tmp"))
@pytest.mark.parametrize("old_file, expected_file", MIGRATION_PAIRS)
def test_migrate_old_version_config(self, tmp_path: Path, old_file: Path, expected_file: Path):