Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/mineSweeperGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from dialogs.CheckUpdateGui import CheckUpdateGui
from network.githubApi import GitHub, SourceManager
import utils
from utils.app_logger import logger
import ms_toollib as ms
import os
import hashlib
Expand Down Expand Up @@ -1107,6 +1108,10 @@ def try_record_pop(self):
del_items += [13, 14, 15]

if len(del_items) < 9:
try:
self.record_setting.sync()
except OSError as exc:
logger.warning("保存 record.ini 失败,新纪录仍保留在内存中:{}", exc)
ui = gameRecordPop.ui_Form(
del_items, b.bbbv, nf_items, self.mainWindow)
ui.label_16.setText(mode_text)
Expand Down
16 changes: 14 additions & 2 deletions src/superGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PySide6.QtWidgets import QApplication
import configparser
import shutil
import tempfile
from PySide6.QtGui import QPalette, QPixmap, QIcon
from ui.ui_main_board import Ui_MainWindow
from pathlib import Path
Expand Down Expand Up @@ -176,8 +177,19 @@ def sync(self):
"""
将内存中的配置写入文件
"""
with open(self.file_path, 'w', encoding="utf-8") as f:
self.config.write(f)
target = Path(self.file_path)
temp_file = tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", dir=target.parent,
prefix=f".{target.name}.", suffix=".tmp", delete=False,
)
try:
with temp_file as stream:
self.config.write(stream)
stream.flush()
os.fsync(stream.fileno())
os.replace(temp_file.name, target)
finally:
Path(temp_file.name).unlink(missing_ok=True)


class Ui_MainWindow(Ui_MainWindow):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_ini_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import patch

from superGUI import IniConfig


class TestIniSync(TestCase):
def test_failed_save_preserves_records_and_can_be_retried(self):
def fail_mid_write(stream):
stream.write("[B]\n")
raise OSError("partial write")

with TemporaryDirectory() as directory:
path = Path(directory) / "record.ini"
original = b"[B]\nrtime = 10\n"
path.write_bytes(original)
records = IniConfig(path)
records.set_value("B/rtime", 9)

for failure in (
patch.object(records.config, "write", side_effect=fail_mid_write),
patch.object(os, "fsync", side_effect=OSError("flush failed")),
patch.object(os, "replace", side_effect=PermissionError("file in use")),
):
with failure, self.assertRaises(OSError):
records.sync()
self.assertEqual(path.read_bytes(), original)
self.assertEqual(list(path.parent.iterdir()), [path])

records.sync()
self.assertEqual(IniConfig(path).value("B/rtime", value_type=int), 9)