Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest>=4.0.0
tox>=3.14.0
pywin32; sys_platform == "win32"
4 changes: 2 additions & 2 deletions src/littlefs/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
63 changes: 63 additions & 0 deletions test/test_windisk_context.py
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove the HAS_WIN32FILE check and just have this only be conditional on if the sys.platform is win32. I don't think we need to import win32file at all within this test and it should all "just work" on all platforms.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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__()
Loading