diff --git a/tests/test_initializer.py b/tests/test_initializer.py index 651e4e2..442d2a3 100644 --- a/tests/test_initializer.py +++ b/tests/test_initializer.py @@ -78,6 +78,42 @@ def append(path: Path, data: bytes) -> None: stream.write(data) +@pytest.mark.parametrize("kind", ["stable", "changed-ctime", "wrong-inode"]) +def test_snapshot_windows_stat_difference( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, kind: str +) -> None: + path = tmp_path / "sample.txt" + path.write_bytes(b"Current content") + original = path.lstat() + real_fstat = os.fstat + calls = 0 + + def descriptor_stat(fd: int) -> os.stat_result: + nonlocal calls + calls += 1 + info = real_fstat(fd) + fields = list(info) + if kind == "wrong-inode": + fields[1] += 1 + ctime = original.st_ctime_ns + 1_000_000_000 + if kind == "changed-ctime" and calls > 1: + ctime += 1 + return os.stat_result( + fields, {"st_mtime_ns": info.st_mtime_ns, "st_ctime_ns": ctime} + ) + + with monkeypatch.context() as patch: + patch.setattr(sys, "platform", "win32") + patch.setattr(os, "fstat", descriptor_stat) + if kind == "stable": + first = initialize.snapshot(path) + assert first.data == b"Current content" + assert first == initialize.snapshot(path) + else: + with pytest.raises(initialize.InitializationError, match="input changed"): + initialize.snapshot(path) + + def test_help_and_preview(project: Path) -> None: original = files(project) help_result = run(project, "--help") @@ -181,7 +217,8 @@ def test_edited_readmes_preserve_current_content(project: Path, eol: bytes) -> N cover.write_bytes(b"synthetic cover bytes") originals = {} for path in READMES: - relative = COVER.as_posix() if path.parent == Path(".") else f"../../../{COVER}" + prefix = "" if path.parent == Path(".") else "../../../" + relative = f"{prefix}{COVER.as_posix()}" custom = ( ( "# A maintained heading \n\n" diff --git a/tools/initialize.py b/tools/initialize.py index b2e8888..659783a 100644 --- a/tools/initialize.py +++ b/tools/initialize.py @@ -497,14 +497,21 @@ def snapshot(path: Path) -> Snapshot: check_directories(path.parent) initial = signature(safe_stat(path)) with path.open("rb") as stream: - if signature(os.fstat(stream.fileno())) != initial: + opened = signature(os.fstat(stream.fileno())) + # On Windows, path stat and descriptor stat can disagree on ctime. + # Compare ctime only within the same API (CPython issue #157671). + if initial != opened and not ( + sys.platform == "win32" and initial[:-1] == opened[:-1] + ): raise InitializationError(f"{path}: input changed during reading.") data = stream.read(SIZE_LIMIT + 1) + if signature(os.fstat(stream.fileno())) != opened: + raise InitializationError(f"{path}: input changed during reading.") if len(data) > SIZE_LIMIT: raise InitializationError(f"{path}: file exceeds the 1 MiB input limit.") if signature(safe_stat(path)) != initial: raise InitializationError(f"{path}: input changed during reading.") - return Snapshot(data, initial) + return Snapshot(data, opened) def build_plan(root: Path, identity: Identity) -> dict[str, Change]: