summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/vfs_fat_fileio.py
diff options
context:
space:
mode:
authorAlex March <alex.march.dev@gmail.com>2016-10-13 10:48:54 +0100
committerDamien George <damien.p.george@gmail.com>2016-10-24 12:49:19 +1100
commitcb20d999bc2d4f7e842f3e0b26e8fdc484acf82a (patch)
tree320dc870216f394a899cb333f66bfc81fb55da95 /tests/extmod/vfs_fat_fileio.py
parentb6c22c42abe23560c9452511168c158d5cc8fe4c (diff)
downloadmicropython-cb20d999bc2d4f7e842f3e0b26e8fdc484acf82a.tar.gz
micropython-cb20d999bc2d4f7e842f3e0b26e8fdc484acf82a.zip
tests/extmod/vfs_fat: Improve VFS test coverage.
Covered case: - Stat cases - Invalid read/write/flush/close - Invalid mkdir/rmdir/remove/getcwd - File seek/tell, modes a/x/+, t/b - Writing to a full disk - Full path rename, slash trim - Rename cases - Bytestring listdir - File object printing
Diffstat (limited to 'tests/extmod/vfs_fat_fileio.py')
-rw-r--r--tests/extmod/vfs_fat_fileio.py160
1 files changed, 160 insertions, 0 deletions
diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py
new file mode 100644
index 0000000000..26fec78281
--- /dev/null
+++ b/tests/extmod/vfs_fat_fileio.py
@@ -0,0 +1,160 @@
+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(48)
+except MemoryError:
+ print("SKIP")
+ sys.exit()
+
+uos.VfsFat.mkfs(bdev)
+vfs = uos.VfsFat(bdev, "/ramdisk")
+
+# file IO
+f = vfs.open("foo_file.txt", "w")
+print(str(f)[:17], str(f)[-1:])
+f.write("hello!")
+f.flush()
+f.close()
+try:
+ f.write("world!")
+except OSError as e:
+ print(e.args[0] == uerrno.EINVAL)
+
+try:
+ f.read()
+except OSError as e:
+ print(e.args[0] == uerrno.EINVAL)
+
+try:
+ f.flush()
+except OSError as e:
+ print(e.args[0] == uerrno.EINVAL)
+
+try:
+ f.close()
+except OSError as e:
+ print(e.args[0] == uerrno.EINVAL)
+
+try:
+ vfs.open("foo_file.txt", "x")
+except OSError as e:
+ print(e.args[0] == uerrno.EEXIST)
+
+with vfs.open("foo_file.txt", "a") as f:
+ f.write("world!")
+
+with vfs.open("foo_file.txt") as f2:
+ print(f2.read())
+ print(f2.tell())
+
+ f2.seek(0, 0) # SEEK_SET
+ print(f2.read(1))
+
+ f2.seek(0, 1) # SEEK_CUR
+ print(f2.read(1))
+ try:
+ f2.seek(1, 1) # SEEK_END
+ except OSError as e:
+ print(e.args[0] == uerrno.EOPNOTSUPP)
+
+ f2.seek(-2, 2) # SEEK_END
+ print(f2.read(1))
+
+# dirs
+vfs.mkdir("foo_dir")
+
+try:
+ vfs.rmdir("foo_file.txt")
+except OSError as e:
+ print(e.args[0] == 20) # uerrno.ENOTDIR
+
+try:
+ vfs.mkdir("foo_dir")
+except OSError as e:
+ print(e.args[0] == uerrno.EEXIST)
+
+try:
+ vfs.remove("foo_dir")
+except OSError as e:
+ print(e.args[0] == uerrno.EISDIR)
+
+try:
+ vfs.remove("no_file.txt")
+except OSError as e:
+ print(e.args[0] == uerrno.ENOENT)
+
+try:
+ vfs.rename("foo_dir", "/null")
+except OSError as e:
+ print(e.args[0] == uerrno.ENODEV)
+
+# file in dir
+with vfs.open("foo_dir/file-in-dir.txt", "w+t") as f:
+ f.write("data in file")
+
+with vfs.open("foo_dir/file-in-dir.txt", "r+b") as f:
+ print(f.read())
+
+with vfs.open("foo_dir/sub_file.txt", "w") as f:
+ f.write("subdir file")
+
+# directory not empty
+try:
+ vfs.rmdir("foo_dir")
+except OSError as e:
+ print(e.args[0] == uerrno.EACCES)
+
+# trim full path
+vfs.rename("foo_dir/file-in-dir.txt", "/ramdisk/foo_dir/file.txt")
+print(vfs.listdir("foo_dir"))
+
+vfs.rename("foo_dir/file.txt", "moved-to-root.txt")
+print(vfs.listdir())
+
+# valid removes
+vfs.remove("foo_dir/sub_file.txt")
+vfs.remove("foo_file.txt")
+vfs.rmdir("foo_dir")
+print(vfs.listdir())
+
+# disk full
+try:
+ bsize = vfs.statvfs("/ramdisk")[0]
+ free = vfs.statvfs("/ramdisk")[2] + 1
+ f = vfs.open("large_file.txt", "wb")
+ f.write(bytearray(bsize * free))
+except OSError as e:
+ print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC