fix(utils): preserve exact newlines in atomic text writes (#6475)

* fix(utils): preserve exact newlines in atomic text writes

* test(updates): allow time for confirmed Windows upgrade completion
This commit is contained in:
Quanxin Liu authored and GitHub committed 2026-10-27 13:38:17 +08:00
1 parent 0978ccd50f
commit c4dac649da
3 files changed
+11 -6

No files matched your search

+1 -1
View File
@@ -137,7 +137,7 @@ def atomic_write_text(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
try:
with open(tmp, "x", encoding="utf-8") as handle:
with open(tmp, "x", encoding="utf-8", newline="") as handle:
handle.write(content)
handle.flush()
os.fsync(handle.fileno())
+4 -2
View File
@@ -1539,8 +1539,10 @@ async def test_stale_first_supervisor_cannot_take_new_confirmation(managed_insta
assert not (directory / "approval.json").exists()
assert not (installation_state_dir(spec.baseline.environment) / "stop.json").exists()
# B retains its first supervision budget and completes the explicit retry.
# This path performs a real backup, pip install, restart and snapshot cleanup.
# Parallel Windows runners can exceed the stale-launcher's 40s rejection budget.
try:
completed = await asyncio.to_thread(subprocess.run, [*command, current.id], capture_output=True, timeout=40)
completed = await asyncio.to_thread(subprocess.run, [*command, current.id], capture_output=True, timeout=120)
except subprocess.TimeoutExpired:
# Identify the blocked phase without exposing the confirmation nonce or
# making a slow snapshot indistinguishable from a handoff deadlock.
@@ -1549,7 +1551,7 @@ async def test_stale_first_supervisor_cannot_take_new_confirmation(managed_insta
"mutation_started": status.mutation_started, "message": status.message,
"snapshot_complete": (directory / "program-backup.json").exists(),
"activation_complete": (directory / "activation.json").exists()}
pytest.fail(f"Confirmed supervisor exceeded 40s: {progress}", pytrace=False)
pytest.fail(f"Confirmed supervisor exceeded 120s: {progress}", pytrace=False)
assert completed.returncode == 0, completed.stderr.decode()
assert read_record(directory / "supervision.json")["confirmation_id"] == current.id
assert JobStatus.read(directory).phase == Phase.SUCCEEDED
+6 -3
View File
@@ -194,14 +194,17 @@ def test_write_text_atomic_keeps_file_when_directory_fsync_is_unsupported(
assert len(fsync_calls) == 1
def test_atomic_write_text_creates_parent_and_replaces_exact_content(tmp_path: Path) -> None:
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
def test_atomic_write_text_creates_parent_and_replaces_exact_content(
tmp_path: Path, newline: str,
) -> None:
target = tmp_path / "runs" / "record.json"
atomic_write_text(target, "old")
assert target.read_text(encoding="utf-8") == "old"
content = '{"message": "café"}'
content = f'{{{newline} "message": "café"{newline}}}{newline}'
atomic_write_text(target, content)
assert target.read_text(encoding="utf-8") == content
assert target.read_bytes() == content.encode("utf-8")
atomic_write_text(target, "")
assert target.read_text(encoding="utf-8") == ""