Skip to content

Commit 8ef1fe4

Browse files
authored
Merge pull request #158 from amgross/add_UserContextWinDisk_test
add test for testing UserContextWinDisk to ensure it won't get broken
2 parents 626f309 + 6f9e3dc commit 8ef1fe4

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pytest>=4.0.0
22
tox>=3.14.0
3+
pywin32; sys_platform == "win32"

src/littlefs/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def __init__(self, disk_path: str) -> None:
157157
"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'."
158158
)
159159
self.device = win32file.CreateFile(
160-
disk_path, win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, 0, None
160+
disk_path, win32file.GENERIC_READ | win32file.GENERIC_WRITE, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, 0, None
161161
)
162162
if self.device == win32file.INVALID_HANDLE_VALUE:
163163
raise IOError("Could not open disk %s" % disk_path)
@@ -222,7 +222,7 @@ def erase(self, cfg: "LFSConfig", block: int) -> int:
222222
start = block * cfg.block_size
223223

224224
win32file.SetFilePointer(self.device, start, win32file.FILE_BEGIN)
225-
win32file.WriteFile(self.device, [0xFF] * cfg.block_size)
225+
win32file.WriteFile(self.device, b'\xff' * cfg.block_size)
226226
return 0
227227

228228
def sync(self, cfg: "LFSConfig") -> int:

test/test_windisk_context.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Tests for UserContextWinDisk on Windows platforms.
2+
3+
These tests are only run on Windows systems where win32file is available.
4+
They test the Windows disk/file context for basic LittleFS operations.
5+
"""
6+
7+
import pytest
8+
import sys
9+
10+
# Only run these tests on Windows
11+
if sys.platform == "win32":
12+
import win32file
13+
14+
# Import after checking for win32file
15+
from littlefs import LittleFS
16+
from littlefs.context import UserContextWinDisk
17+
18+
19+
@pytest.mark.skipif(
20+
sys.platform != "win32",
21+
reason="test must run on Windows"
22+
)
23+
class TestUserContextWinDisk:
24+
"""Test suite for UserContextWinDisk"""
25+
26+
@pytest.fixture
27+
def disk_image_path(self, tmp_path):
28+
"""Create a temporary disk image file path"""
29+
# Create a pre-allocated disk image file (2MB with 0xFF fill)
30+
image_path = tmp_path / "disk_image.bin"
31+
with open(image_path, "wb") as f:
32+
# Create a 2MB disk image (0xFF filled)
33+
f.write(b"\xff" * (2 * 1024 * 1024))
34+
return str(image_path)
35+
36+
def test_windisk_read_after_remount(self, disk_image_path):
37+
"""Test reading file content after remounting"""
38+
block_size = 512
39+
block_count = 256
40+
test_content = "Hello, this is persistent data!"
41+
42+
ctx = UserContextWinDisk(disk_image_path)
43+
try:
44+
fs = LittleFS(context=ctx, block_size=block_size, block_count=block_count)
45+
46+
# Write file
47+
with fs.open("persistent.txt", "w") as fh:
48+
fh.write(test_content)
49+
50+
fs.unmount()
51+
52+
# Remount and read
53+
fs.mount()
54+
with fs.open("persistent.txt", "r") as fh:
55+
content = fh.read()
56+
assert content == test_content
57+
58+
fs.unmount()
59+
finally:
60+
ctx.__del__()

0 commit comments

Comments
 (0)