micropython/tests/extmod/vfs_lfs_mount.py

79 wiersze
1.6 KiB
Python
Czysty Zwykły widok Historia

2019-10-18 06:25:48 +00:00
# Test for VfsLittle using a RAM device, with mount/umount
try:
import uos
2019-10-18 06:25:48 +00:00
uos.VfsLfs1
uos.VfsLfs2
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit
2019-10-18 06:25:48 +00:00
class RAMBlockDevice:
ERASE_BLOCK_SIZE = 1024
def __init__(self, blocks):
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
def readblocks(self, block, buf, off):
addr = block * self.ERASE_BLOCK_SIZE + off
for i in range(len(buf)):
buf[i] = self.data[addr + i]
def writeblocks(self, block, buf, off):
addr = block * self.ERASE_BLOCK_SIZE + off
for i in range(len(buf)):
self.data[addr + i] = buf[i]
def ioctl(self, op, arg):
if op == 4: # block count
2019-10-18 06:25:48 +00:00
return len(self.data) // self.ERASE_BLOCK_SIZE
if op == 5: # block size
2019-10-18 06:25:48 +00:00
return self.ERASE_BLOCK_SIZE
if op == 6: # erase block
2019-10-18 06:25:48 +00:00
return 0
2019-10-18 06:25:48 +00:00
def test(bdev, vfs_class):
print("test", vfs_class)
2019-10-18 06:25:48 +00:00
# mkfs
vfs_class.mkfs(bdev)
# construction
vfs = vfs_class(bdev)
# mount
uos.mount(vfs, "/lfs")
2019-10-18 06:25:48 +00:00
# import
with open("/lfs/lfsmod.py", "w") as f:
2019-10-18 06:25:48 +00:00
f.write('print("hello from lfs")\n')
import lfsmod
# import package
uos.mkdir("/lfs/lfspkg")
with open("/lfs/lfspkg/__init__.py", "w") as f:
2019-10-18 06:25:48 +00:00
f.write('print("package")\n')
import lfspkg
# umount
uos.umount("/lfs")
2019-10-18 06:25:48 +00:00
# clear imported modules
sys.modules.clear()
2019-10-18 06:25:48 +00:00
bdev = RAMBlockDevice(30)
# initialise path
import sys
2019-10-18 06:25:48 +00:00
sys.path.clear()
sys.path.append("/lfs")
2019-10-18 06:25:48 +00:00
# run tests
test(bdev, uos.VfsLfs1)
test(bdev, uos.VfsLfs2)