|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from .platform_test import skipif_not_win32 |
| 7 | + |
| 8 | +# Set module-level skips |
| 9 | +pytestmark = [skipif_not_win32] |
| 10 | + |
| 11 | + |
| 12 | +def test_syncfile_basic(): |
| 13 | + """Integration: SyncFile creates file and writes data correctly.""" |
| 14 | + from ...platform.windows import SyncFile |
| 15 | + |
| 16 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 17 | + path = os.path.join(tmpdir, "testfile") |
| 18 | + with SyncFile(path, binary=True) as sf: |
| 19 | + sf.write(b"hello borg") |
| 20 | + with open(path, "rb") as f: |
| 21 | + assert f.read() == b"hello borg" |
| 22 | + |
| 23 | + |
| 24 | +def test_syncfile_file_exists_error(): |
| 25 | + """SyncFile raises FileExistsError if file already exists.""" |
| 26 | + from ...platform.windows import SyncFile |
| 27 | + |
| 28 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 29 | + path = os.path.join(tmpdir, "testfile") |
| 30 | + open(path, "w").close() |
| 31 | + with pytest.raises(FileExistsError): |
| 32 | + SyncFile(path, binary=True) |
| 33 | + |
| 34 | + |
| 35 | +def test_syncfile_text_mode(): |
| 36 | + """SyncFile works in text mode.""" |
| 37 | + from ...platform.windows import SyncFile |
| 38 | + |
| 39 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 40 | + path = os.path.join(tmpdir, "testfile.txt") |
| 41 | + with SyncFile(path) as sf: |
| 42 | + sf.write("hello text") |
| 43 | + with open(path, "r") as f: |
| 44 | + assert f.read() == "hello text" |
| 45 | + |
| 46 | + |
| 47 | +def test_syncfile_fd_fallback(): |
| 48 | + """SyncFile with fd falls back to base implementation (mirrors SaveFile usage).""" |
| 49 | + from ...platform.windows import SyncFile |
| 50 | + |
| 51 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 52 | + fd, path = tempfile.mkstemp(dir=tmpdir) |
| 53 | + with SyncFile(path, fd=fd, binary=True) as sf: |
| 54 | + sf.write(b"fallback test") |
| 55 | + with open(path, "rb") as f: |
| 56 | + assert f.read() == b"fallback test" |
| 57 | + |
| 58 | + |
| 59 | +def test_syncfile_sync(): |
| 60 | + """Explicit sync() does not raise.""" |
| 61 | + from ...platform.windows import SyncFile |
| 62 | + |
| 63 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 64 | + path = os.path.join(tmpdir, "testfile") |
| 65 | + with SyncFile(path, binary=True) as sf: |
| 66 | + sf.write(b"sync test data") |
| 67 | + sf.sync() |
| 68 | + |
| 69 | + |
| 70 | +def test_syncfile_uses_write_through(monkeypatch): |
| 71 | + """Verify CreateFileW is called with FILE_FLAG_WRITE_THROUGH.""" |
| 72 | + from ...platform import windows |
| 73 | + |
| 74 | + calls = [] |
| 75 | + original = windows._CreateFileW |
| 76 | + |
| 77 | + def mock_create(*args): |
| 78 | + calls.append(args) |
| 79 | + return original(*args) |
| 80 | + |
| 81 | + monkeypatch.setattr(windows, "_CreateFileW", mock_create) |
| 82 | + |
| 83 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 84 | + path = os.path.join(tmpdir, "testfile") |
| 85 | + with windows.SyncFile(path, binary=True) as sf: |
| 86 | + sf.write(b"write-through test") |
| 87 | + |
| 88 | + assert len(calls) == 1 |
| 89 | + flags_attrs = calls[0][5] # 6th arg: dwFlagsAndAttributes |
| 90 | + assert flags_attrs & windows.FILE_FLAG_WRITE_THROUGH |
0 commit comments