micropython/tests/extmod/vfs_lfs_mount.py
David Lechner 3dc324d3f1 tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
2020-03-30 13:21:58 +11:00

79 lines
1.6 KiB
Python

# Test for VfsLittle using a RAM device, with mount/umount
try:
import uos
uos.VfsLfs1
uos.VfsLfs2
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit
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
return len(self.data) // self.ERASE_BLOCK_SIZE
if op == 5: # block size
return self.ERASE_BLOCK_SIZE
if op == 6: # erase block
return 0
def test(bdev, vfs_class):
print("test", vfs_class)
# mkfs
vfs_class.mkfs(bdev)
# construction
vfs = vfs_class(bdev)
# mount
uos.mount(vfs, "/lfs")
# import
with open("/lfs/lfsmod.py", "w") as f:
f.write('print("hello from lfs")\n')
import lfsmod
# import package
uos.mkdir("/lfs/lfspkg")
with open("/lfs/lfspkg/__init__.py", "w") as f:
f.write('print("package")\n')
import lfspkg
# umount
uos.umount("/lfs")
# clear imported modules
sys.modules.clear()
bdev = RAMBlockDevice(30)
# initialise path
import sys
sys.path.clear()
sys.path.append("/lfs")
# run tests
test(bdev, uos.VfsLfs1)
test(bdev, uos.VfsLfs2)