From ff5afa4c930cee4048d1ebcd69009ba2247b4337 Mon Sep 17 00:00:00 2001 From: amgross Date: Wed, 28 Jan 2026 21:16:01 +0200 Subject: [PATCH 1/5] add test for testing UserContextWinDisk to ensure it won't get broken --- test/test_windisk_context.py | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/test_windisk_context.py diff --git a/test/test_windisk_context.py b/test/test_windisk_context.py new file mode 100644 index 0000000..5b96a2f --- /dev/null +++ b/test/test_windisk_context.py @@ -0,0 +1,63 @@ +"""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 with win32file available +try: + import win32file + HAS_WIN32FILE = True +except ImportError: + HAS_WIN32FILE = False + +# Import after checking for win32file +from littlefs import LittleFS +from littlefs.context import UserContextWinDisk + + +@pytest.mark.skipif( + not HAS_WIN32FILE or sys.platform != "win32", + reason="win32file is required and 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__() From fedce8012dfa7457adba539c9d1c65c1ca96f27e Mon Sep 17 00:00:00 2001 From: amgross Date: Wed, 28 Jan 2026 21:31:46 +0200 Subject: [PATCH 2/5] add in windows dependency on win32file if we are windows --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 920fb71..dfb340c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pytest>=4.0.0 tox>=3.14.0 +win32file; sys_platform == "win32" From 9fdf60f0ddfc10bc0457ca81cb0f43e895810f8b Mon Sep 17 00:00:00 2001 From: amgross Date: Wed, 28 Jan 2026 21:40:18 +0200 Subject: [PATCH 3/5] fix dependency --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index dfb340c..61f7285 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pytest>=4.0.0 tox>=3.14.0 -win32file; sys_platform == "win32" +pywin32; sys_platform == "win32" From 7dc99f9e173cd421f9985df13aeeef0a0f003160 Mon Sep 17 00:00:00 2001 From: amgross Date: Wed, 28 Jan 2026 21:51:41 +0200 Subject: [PATCH 4/5] fix bugs in UserContextWinDisk --- src/littlefs/context.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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: From 6f9e3dc4bee5727cf7db86ba0435d268af7f61fb Mon Sep 17 00:00:00 2001 From: amgross Date: Sat, 31 Jan 2026 22:32:14 +0200 Subject: [PATCH 5/5] win32 must exist on windows when running tests --- test/test_windisk_context.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/test_windisk_context.py b/test/test_windisk_context.py index 5b96a2f..1c936fb 100644 --- a/test/test_windisk_context.py +++ b/test/test_windisk_context.py @@ -7,12 +7,9 @@ import pytest import sys -# Only run these tests on Windows with win32file available -try: +# Only run these tests on Windows +if sys.platform == "win32": import win32file - HAS_WIN32FILE = True -except ImportError: - HAS_WIN32FILE = False # Import after checking for win32file from littlefs import LittleFS @@ -20,8 +17,8 @@ @pytest.mark.skipif( - not HAS_WIN32FILE or sys.platform != "win32", - reason="win32file is required and test must run on Windows" + sys.platform != "win32", + reason="test must run on Windows" ) class TestUserContextWinDisk: """Test suite for UserContextWinDisk"""