diff --git a/requirements.txt b/requirements.txt index 920fb71..61f7285 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pytest>=4.0.0 tox>=3.14.0 +pywin32; sys_platform == "win32" diff --git a/src/littlefs/context.py b/src/littlefs/context.py index 34d426d..e2df3b7 100644 --- a/src/littlefs/context.py +++ b/src/littlefs/context.py @@ -157,7 +157,7 @@ def __init__(self, disk_path: str) -> None: "Unable to import 'win32file'. This module is required for Windows-specific functionality. Please ensure you are running on a Windows platform or install 'pywin32' using: 'pip install pywin32'." ) self.device = win32file.CreateFile( - disk_path, win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, 0, None + disk_path, win32file.GENERIC_READ | win32file.GENERIC_WRITE, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, 0, None ) if self.device == win32file.INVALID_HANDLE_VALUE: raise IOError("Could not open disk %s" % disk_path) @@ -222,7 +222,7 @@ def erase(self, cfg: "LFSConfig", block: int) -> int: start = block * cfg.block_size win32file.SetFilePointer(self.device, start, win32file.FILE_BEGIN) - win32file.WriteFile(self.device, [0xFF] * cfg.block_size) + win32file.WriteFile(self.device, b'\xff' * cfg.block_size) return 0 def sync(self, cfg: "LFSConfig") -> int: diff --git a/test/test_windisk_context.py b/test/test_windisk_context.py new file mode 100644 index 0000000..1c936fb --- /dev/null +++ b/test/test_windisk_context.py @@ -0,0 +1,60 @@ +"""Tests for UserContextWinDisk on Windows platforms. + +These tests are only run on Windows systems where win32file is available. +They test the Windows disk/file context for basic LittleFS operations. +""" + +import pytest +import sys + +# Only run these tests on Windows +if sys.platform == "win32": + import win32file + +# Import after checking for win32file +from littlefs import LittleFS +from littlefs.context import UserContextWinDisk + + +@pytest.mark.skipif( + sys.platform != "win32", + reason="test must run on Windows" +) +class TestUserContextWinDisk: + """Test suite for UserContextWinDisk""" + + @pytest.fixture + def disk_image_path(self, tmp_path): + """Create a temporary disk image file path""" + # Create a pre-allocated disk image file (2MB with 0xFF fill) + image_path = tmp_path / "disk_image.bin" + with open(image_path, "wb") as f: + # Create a 2MB disk image (0xFF filled) + f.write(b"\xff" * (2 * 1024 * 1024)) + return str(image_path) + + def test_windisk_read_after_remount(self, disk_image_path): + """Test reading file content after remounting""" + block_size = 512 + block_count = 256 + test_content = "Hello, this is persistent data!" + + ctx = UserContextWinDisk(disk_image_path) + try: + fs = LittleFS(context=ctx, block_size=block_size, block_count=block_count) + + # Write file + with fs.open("persistent.txt", "w") as fh: + fh.write(test_content) + + fs.unmount() + + # Remount and read + fs.mount() + with fs.open("persistent.txt", "r") as fh: + content = fh.read() + assert content == test_content + + fs.unmount() + finally: + ctx.__del__()