Commit b9bfaa34 authored by Damien George's avatar Damien George
Browse files

tests/extmod/vfs_fat: Update tests to work with new VFS sub-system.

The vfs_fat_fsusermount test is no longer relevant so has been removed.
parent f9ecd484
import sys import sys
import uos
import uerrno import uerrno
try:
import uos_vfs as uos
open = uos.vfs_open
except ImportError:
import uos
try: try:
uos.VfsFat uos.VfsFat
except AttributeError: except AttributeError:
...@@ -40,10 +44,12 @@ except MemoryError: ...@@ -40,10 +44,12 @@ except MemoryError:
sys.exit() sys.exit()
uos.VfsFat.mkfs(bdev) uos.VfsFat.mkfs(bdev)
vfs = uos.VfsFat(bdev, "/ramdisk") vfs = uos.VfsFat(bdev)
uos.mount(vfs, '/ramdisk')
uos.chdir('/ramdisk')
# file IO # file IO
f = vfs.open("foo_file.txt", "w") f = open("foo_file.txt", "w")
print(str(f)[:17], str(f)[-1:]) print(str(f)[:17], str(f)[-1:])
f.write("hello!") f.write("hello!")
f.flush() f.flush()
...@@ -65,14 +71,14 @@ except OSError as e: ...@@ -65,14 +71,14 @@ except OSError as e:
print(e.args[0] == uerrno.EINVAL) print(e.args[0] == uerrno.EINVAL)
try: try:
vfs.open("foo_file.txt", "x") open("foo_file.txt", "x")
except OSError as e: except OSError as e:
print(e.args[0] == uerrno.EEXIST) print(e.args[0] == uerrno.EEXIST)
with vfs.open("foo_file.txt", "a") as f: with open("foo_file.txt", "a") as f:
f.write("world!") f.write("world!")
with vfs.open("foo_file.txt") as f2: with open("foo_file.txt") as f2:
print(f2.read()) print(f2.read())
print(f2.tell()) print(f2.tell())
...@@ -90,9 +96,10 @@ with vfs.open("foo_file.txt") as f2: ...@@ -90,9 +96,10 @@ with vfs.open("foo_file.txt") as f2:
print(f2.read(1)) print(f2.read(1))
# using constructor of FileIO type to open a file # using constructor of FileIO type to open a file
FileIO = type(f) # no longer working with new VFS sub-system
with FileIO("/ramdisk/foo_file.txt") as f: #FileIO = type(f)
print(f.read()) #with FileIO("/ramdisk/foo_file.txt") as f:
# print(f.read())
# dirs # dirs
vfs.mkdir("foo_dir") vfs.mkdir("foo_dir")
...@@ -123,13 +130,13 @@ except OSError as e: ...@@ -123,13 +130,13 @@ except OSError as e:
print(e.args[0] == uerrno.ENOENT) print(e.args[0] == uerrno.ENOENT)
# file in dir # file in dir
with vfs.open("foo_dir/file-in-dir.txt", "w+t") as f: with open("foo_dir/file-in-dir.txt", "w+t") as f:
f.write("data in file") f.write("data in file")
with vfs.open("foo_dir/file-in-dir.txt", "r+b") as f: with open("foo_dir/file-in-dir.txt", "r+b") as f:
print(f.read()) print(f.read())
with vfs.open("foo_dir/sub_file.txt", "w") as f: with open("foo_dir/sub_file.txt", "w") as f:
f.write("subdir file") f.write("subdir file")
# directory not empty # directory not empty
...@@ -146,11 +153,11 @@ vfs.rename("foo_dir/file.txt", "moved-to-root.txt") ...@@ -146,11 +153,11 @@ vfs.rename("foo_dir/file.txt", "moved-to-root.txt")
print(vfs.listdir()) print(vfs.listdir())
# check that renaming to existing file will overwrite it # check that renaming to existing file will overwrite it
with vfs.open("temp", "w") as f: with open("temp", "w") as f:
f.write("new text") f.write("new text")
vfs.rename("temp", "moved-to-root.txt") vfs.rename("temp", "moved-to-root.txt")
print(vfs.listdir()) print(vfs.listdir())
with vfs.open("moved-to-root.txt") as f: with open("moved-to-root.txt") as f:
print(f.read()) print(f.read())
# valid removes # valid removes
...@@ -163,7 +170,7 @@ print(vfs.listdir()) ...@@ -163,7 +170,7 @@ print(vfs.listdir())
try: try:
bsize = vfs.statvfs("/ramdisk")[0] bsize = vfs.statvfs("/ramdisk")[0]
free = vfs.statvfs("/ramdisk")[2] + 1 free = vfs.statvfs("/ramdisk")[2] + 1
f = vfs.open("large_file.txt", "wb") f = open("large_file.txt", "wb")
f.write(bytearray(bsize * free)) f.write(bytearray(bsize * free))
except OSError as e: except OSError as e:
print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC
...@@ -9,7 +9,6 @@ h ...@@ -9,7 +9,6 @@ h
e e
True True
d d
hello!world!
True True
True True
True True
......
import sys
import uos
import uerrno
try:
uos.VfsFat
except AttributeError:
print("SKIP")
sys.exit()
class RAMFS:
SEC_SIZE = 512
def __init__(self, blocks):
self.data = bytearray(blocks * self.SEC_SIZE)
def readblocks(self, n, buf):
#print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
for i in range(len(buf)):
buf[i] = self.data[n * self.SEC_SIZE + i]
def writeblocks(self, n, buf):
#print("writeblocks(%s, %x)" % (n, id(buf)))
for i in range(len(buf)):
self.data[n * self.SEC_SIZE + i] = buf[i]
def ioctl(self, op, arg):
#print("ioctl(%d, %r)" % (op, arg))
if op == 4: # BP_IOCTL_SEC_COUNT
return len(self.data) // self.SEC_SIZE
if op == 5: # BP_IOCTL_SEC_SIZE
return self.SEC_SIZE
try:
bdev = RAMFS(50)
except MemoryError:
print("SKIP")
sys.exit()
# can't mkfs readonly device
try:
uos.vfs_mkfs(bdev, "/ramdisk", readonly=True)
except OSError as e:
print(e)
# mount before mkfs
try:
uos.vfs_mount(bdev, "/ramdisk")
except OSError as e:
print(e)
# invalid umount
try:
uos.vfs_umount("/ramdisk")
except OSError as e:
print(e.args[0] == uerrno.EINVAL)
try:
uos.vfs_mount(None, "/ramdisk")
except OSError as e:
print(e)
try:
uos.vfs_mkfs(None, "/ramdisk")
except OSError as e:
print(e)
# valid mkfs/mount
uos.vfs_mkfs(bdev, "/ramdisk")
uos.vfs_mount(bdev, "/ramdisk")
# umount by path
uos.vfs_umount("/ramdisk")
# readonly mount
# note: this test doesn't work correctly with new OO FatFs
uos.vfs_mount(bdev, "/ramdisk", readonly=True)
vfs = uos.VfsFat(bdev, "/ramdisk")
try:
f = vfs.open("file.txt", "w")
except OSError as e:
print("EROFS:", e.args[0] == 30) # uerrno.EROFS
# device is None == umount
uos.vfs_mount(None, "/ramdisk")
# max mounted devices
dev = []
try:
for i in range(0,4):
dev.append(RAMFS(50))
uos.vfs_mkfs(dev[i], "/ramdisk" + str(i))
uos.vfs_mount(dev[i], "/ramdisk" + str(i))
except OSError as e:
print(e)
can't mkfs
can't mount
True
can't umount
can't umount
too many devices mounted
import sys import sys
import uos
import uerrno import uerrno
try:
import uos_vfs as uos
except ImportError:
import uos
try: try:
uos.VfsFat uos.VfsFat
uos.vfs_mkfs
uos.vfs_mount
except AttributeError: except AttributeError:
print("SKIP") print("SKIP")
sys.exit() sys.exit()
...@@ -39,11 +40,11 @@ except MemoryError: ...@@ -39,11 +40,11 @@ except MemoryError:
print("SKIP") print("SKIP")
sys.exit() sys.exit()
uos.vfs_mkfs(bdev, "/ramdisk") uos.VfsFat.mkfs(bdev)
uos.vfs_mount(bdev, "/ramdisk") vfs = uos.VfsFat(bdev)
uos.mount(vfs, "/ramdisk")
# file io # file io
vfs = uos.VfsFat(bdev, "/ramdisk")
with vfs.open("file.txt", "w") as f: with vfs.open("file.txt", "w") as f:
f.write("hello!") f.write("hello!")
...@@ -54,6 +55,3 @@ with vfs.open("file.txt", "r") as f: ...@@ -54,6 +55,3 @@ with vfs.open("file.txt", "r") as f:
vfs.remove("file.txt") vfs.remove("file.txt")
print(vfs.listdir()) print(vfs.listdir())
# umount by device
uos.vfs_umount(bdev)
import sys import sys
import uos
import uerrno import uerrno
try:
import uos_vfs as uos
except ImportError:
import uos
try: try:
uos.VfsFat uos.VfsFat
except AttributeError: except AttributeError:
...@@ -44,7 +47,8 @@ uos.VfsFat.mkfs(bdev) ...@@ -44,7 +47,8 @@ uos.VfsFat.mkfs(bdev)
print(b"FOO_FILETXT" not in bdev.data) print(b"FOO_FILETXT" not in bdev.data)
print(b"hello!" not in bdev.data) print(b"hello!" not in bdev.data)
vfs = uos.VfsFat(bdev, "/ramdisk") vfs = uos.VfsFat(bdev)
uos.mount(vfs, "/ramdisk")
print("statvfs:", vfs.statvfs("/ramdisk")) print("statvfs:", vfs.statvfs("/ramdisk"))
print("getcwd:", vfs.getcwd()) print("getcwd:", vfs.getcwd())
...@@ -59,7 +63,6 @@ with vfs.open("foo_file.txt", "w") as f: ...@@ -59,7 +63,6 @@ with vfs.open("foo_file.txt", "w") as f:
print(vfs.listdir()) print(vfs.listdir())
print("stat root:", vfs.stat("/")) print("stat root:", vfs.stat("/"))
print("stat disk:", vfs.stat("/ramdisk/"))
print("stat file:", vfs.stat("foo_file.txt")) print("stat file:", vfs.stat("foo_file.txt"))
print(b"FOO_FILETXT" in bdev.data) print(b"FOO_FILETXT" in bdev.data)
...@@ -81,7 +84,7 @@ except OSError as e: ...@@ -81,7 +84,7 @@ except OSError as e:
vfs.chdir("..") vfs.chdir("..")
print("getcwd:", vfs.getcwd()) print("getcwd:", vfs.getcwd())
vfs.umount() uos.umount(vfs)
vfs = uos.VfsFat(bdev, "/ramdisk") vfs = uos.VfsFat(bdev)
print(vfs.listdir(b"")) print(vfs.listdir(b""))
True True
True True
statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255) statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
getcwd: /ramdisk getcwd: /
True True
['foo_file.txt'] ['foo_file.txt']
stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
stat disk: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
stat file: (32768, 0, 0, 0, 0, 0, 6, -631238400, -631238400, -631238400) stat file: (32768, 0, 0, 0, 0, 0, 6, -631238400, -631238400, -631238400)
True True
True True
getcwd: /ramdisk/foo_dir getcwd: /foo_dir
[] []
True True
getcwd: /ramdisk getcwd: /
[b'foo_file.txt', b'foo_dir'] [b'foo_file.txt', b'foo_dir']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment