Skip to content

Commit 0894d31

Browse files
authored
Merge pull request #1 from yaleksandr89/fix/windows-initializer
fix: исправлена совместимость initializer с Windows
2 parents 062d157 + 42397e5 commit 0894d31

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎tests/test_initializer.py‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ def append(path: Path, data: bytes) -> None:
7878
stream.write(data)
7979

8080

81+
@pytest.mark.parametrize("kind", ["stable", "changed-ctime", "wrong-inode"])
82+
def test_snapshot_windows_stat_difference(
83+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, kind: str
84+
) -> None:
85+
path = tmp_path / "sample.txt"
86+
path.write_bytes(b"Current content")
87+
original = path.lstat()
88+
real_fstat = os.fstat
89+
calls = 0
90+
91+
def descriptor_stat(fd: int) -> os.stat_result:
92+
nonlocal calls
93+
calls += 1
94+
info = real_fstat(fd)
95+
fields = list(info)
96+
if kind == "wrong-inode":
97+
fields[1] += 1
98+
ctime = original.st_ctime_ns + 1_000_000_000
99+
if kind == "changed-ctime" and calls > 1:
100+
ctime += 1
101+
return os.stat_result(
102+
fields, {"st_mtime_ns": info.st_mtime_ns, "st_ctime_ns": ctime}
103+
)
104+
105+
with monkeypatch.context() as patch:
106+
patch.setattr(sys, "platform", "win32")
107+
patch.setattr(os, "fstat", descriptor_stat)
108+
if kind == "stable":
109+
first = initialize.snapshot(path)
110+
assert first.data == b"Current content"
111+
assert first == initialize.snapshot(path)
112+
else:
113+
with pytest.raises(initialize.InitializationError, match="input changed"):
114+
initialize.snapshot(path)
115+
116+
81117
def test_help_and_preview(project: Path) -> None:
82118
original = files(project)
83119
help_result = run(project, "--help")
@@ -181,7 +217,8 @@ def test_edited_readmes_preserve_current_content(project: Path, eol: bytes) -> N
181217
cover.write_bytes(b"synthetic cover bytes")
182218
originals = {}
183219
for path in READMES:
184-
relative = COVER.as_posix() if path.parent == Path(".") else f"../../../{COVER}"
220+
prefix = "" if path.parent == Path(".") else "../../../"
221+
relative = f"{prefix}{COVER.as_posix()}"
185222
custom = (
186223
(
187224
"# A maintained heading \n\n"

‎tools/initialize.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,21 @@ def snapshot(path: Path) -> Snapshot:
497497
check_directories(path.parent)
498498
initial = signature(safe_stat(path))
499499
with path.open("rb") as stream:
500-
if signature(os.fstat(stream.fileno())) != initial:
500+
opened = signature(os.fstat(stream.fileno()))
501+
# On Windows, path stat and descriptor stat can disagree on ctime.
502+
# Compare ctime only within the same API (CPython issue #157671).
503+
if initial != opened and not (
504+
sys.platform == "win32" and initial[:-1] == opened[:-1]
505+
):
501506
raise InitializationError(f"{path}: input changed during reading.")
502507
data = stream.read(SIZE_LIMIT + 1)
508+
if signature(os.fstat(stream.fileno())) != opened:
509+
raise InitializationError(f"{path}: input changed during reading.")
503510
if len(data) > SIZE_LIMIT:
504511
raise InitializationError(f"{path}: file exceeds the 1 MiB input limit.")
505512
if signature(safe_stat(path)) != initial:
506513
raise InitializationError(f"{path}: input changed during reading.")
507-
return Snapshot(data, initial)
514+
return Snapshot(data, opened)
508515

509516

510517
def build_plan(root: Path, identity: Identity) -> dict[str, Change]:

0 commit comments

Comments
 (0)