tests: Use vfs module instead of os.

Signed-off-by: Damien George <damien@micropython.org>
pull/13584/head
Damien George 2023-11-20 23:04:55 +11:00
rodzic 5804aa0204
commit b87bbaeb43
34 zmienionych plików z 419 dodań i 468 usunięć

Wyświetl plik

@ -1,10 +1,8 @@
# test VFS functionality without any particular filesystem type # test VFS functionality without any particular filesystem type
try: try:
import os import os, vfs
except ImportError:
os.mount
except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -59,11 +57,11 @@ class Filesystem:
# first we umount any existing mount points the target may have # first we umount any existing mount points the target may have
try: try:
os.umount("/") vfs.umount("/")
except OSError: except OSError:
pass pass
for path in os.listdir("/"): for path in os.listdir("/"):
os.umount("/" + path) vfs.umount("/" + path)
# stat root dir # stat root dir
print(os.stat("/")) print(os.stat("/"))
@ -83,7 +81,7 @@ for func in ("chdir", "listdir", "mkdir", "remove", "rmdir", "stat"):
print(func, arg, "OSError") print(func, arg, "OSError")
# basic mounting and listdir # basic mounting and listdir
os.mount(Filesystem(1), "/test_mnt") vfs.mount(Filesystem(1), "/test_mnt")
print(os.listdir()) print(os.listdir())
# ilistdir # ilistdir
@ -103,13 +101,13 @@ print(os.listdir("test_mnt"))
print(os.listdir("/test_mnt")) print(os.listdir("/test_mnt"))
# mounting another filesystem # mounting another filesystem
os.mount(Filesystem(2), "/test_mnt2", readonly=True) vfs.mount(Filesystem(2), "/test_mnt2", readonly=True)
print(os.listdir()) print(os.listdir())
print(os.listdir("/test_mnt2")) print(os.listdir("/test_mnt2"))
# mounting over an existing mount point # mounting over an existing mount point
try: try:
os.mount(Filesystem(3), "/test_mnt2") vfs.mount(Filesystem(3), "/test_mnt2")
except OSError: except OSError:
print("OSError") print("OSError")
@ -139,23 +137,23 @@ open("test_file")
open("test_file", "wb") open("test_file", "wb")
# umount # umount
os.umount("/test_mnt") vfs.umount("/test_mnt")
os.umount("/test_mnt2") vfs.umount("/test_mnt2")
# umount a non-existent mount point # umount a non-existent mount point
try: try:
os.umount("/test_mnt") vfs.umount("/test_mnt")
except OSError: except OSError:
print("OSError") print("OSError")
# root dir # root dir
os.mount(Filesystem(3), "/") vfs.mount(Filesystem(3), "/")
print(os.stat("/")) print(os.stat("/"))
print(os.statvfs("/")) print(os.statvfs("/"))
print(os.listdir()) print(os.listdir())
open("test") open("test")
os.mount(Filesystem(4), "/mnt") vfs.mount(Filesystem(4), "/mnt")
print(os.listdir()) print(os.listdir())
print(os.listdir("/mnt")) print(os.listdir("/mnt"))
os.chdir("/mnt") os.chdir("/mnt")
@ -166,9 +164,9 @@ os.chdir("/subdir")
print(os.listdir()) print(os.listdir())
os.chdir("/") os.chdir("/")
os.umount("/") vfs.umount("/")
print(os.listdir("/")) print(os.listdir("/"))
os.umount("/mnt") vfs.umount("/mnt")
# chdir to a non-existent mount point (current directory should remain unchanged) # chdir to a non-existent mount point (current directory should remain unchanged)
try: try:
@ -178,7 +176,7 @@ except OSError:
print(os.getcwd()) print(os.getcwd())
# chdir to a non-existent subdirectory in a mounted filesystem # chdir to a non-existent subdirectory in a mounted filesystem
os.mount(Filesystem(5, 1), "/mnt") vfs.mount(Filesystem(5, 1), "/mnt")
try: try:
os.chdir("/mnt/subdir") os.chdir("/mnt/subdir")
except OSError: except OSError:

Wyświetl plik

@ -1,10 +1,10 @@
# Test for behaviour of combined standard and extended block device # Test for behaviour of combined standard and extended block device
try: try:
import os import vfs
os.VfsFat vfs.VfsFat
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -65,12 +65,10 @@ def test(bdev, vfs_class):
try: try:
import os
bdev = RAMBlockDevice(50) bdev = RAMBlockDevice(50)
except MemoryError: except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
test(bdev, os.VfsFat) test(bdev, vfs.VfsFat)
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -1,13 +1,8 @@
try: try:
import errno import errno, os, vfs
import os
except ImportError:
print("SKIP")
raise SystemExit
try: vfs.VfsFat
os.VfsFat except (ImportError, AttributeError):
except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -38,13 +33,13 @@ class RAMFS:
try: try:
bdev = RAMFS(50) bdev = RAMFS(50)
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
except MemoryError: except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
vfs = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
os.mount(vfs, "/ramdisk") vfs.mount(fs, "/ramdisk")
os.chdir("/ramdisk") os.chdir("/ramdisk")
# file IO # file IO
@ -99,12 +94,12 @@ with open("foo_file.txt") as f2:
# print(f.read()) # print(f.read())
# dirs # dirs
vfs.mkdir("foo_dir") fs.mkdir("foo_dir")
try: try:
vfs.rmdir("foo_file.txt") fs.rmdir("foo_file.txt")
except OSError as e: except OSError as e:
print(e.errno == 20) # errno.ENOTDIR print(e.errno == 20) # errno.ENOTDIR
vfs.remove("foo_file.txt") fs.remove("foo_file.txt")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))

Wyświetl plik

@ -1,13 +1,8 @@
try: try:
import errno import errno, os, vfs
import os
except ImportError:
print("SKIP")
raise SystemExit
try: vfs.VfsFat
os.VfsFat except (ImportError, AttributeError):
except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -38,32 +33,32 @@ class RAMFS:
try: try:
bdev = RAMFS(50) bdev = RAMFS(50)
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
except MemoryError: except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
vfs = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
os.mount(vfs, "/ramdisk") vfs.mount(fs, "/ramdisk")
os.chdir("/ramdisk") os.chdir("/ramdisk")
try: try:
vfs.mkdir("foo_dir") fs.mkdir("foo_dir")
except OSError as e: except OSError as e:
print(e.errno == errno.EEXIST) print(e.errno == errno.EEXIST)
try: try:
vfs.remove("foo_dir") fs.remove("foo_dir")
except OSError as e: except OSError as e:
print(e.errno == errno.EISDIR) print(e.errno == errno.EISDIR)
try: try:
vfs.remove("no_file.txt") fs.remove("no_file.txt")
except OSError as e: except OSError as e:
print(e.errno == errno.ENOENT) print(e.errno == errno.ENOENT)
try: try:
vfs.rename("foo_dir", "/null/file") fs.rename("foo_dir", "/null/file")
except OSError as e: except OSError as e:
print(e.errno == errno.ENOENT) print(e.errno == errno.ENOENT)
@ -79,34 +74,34 @@ with open("foo_dir/sub_file.txt", "w") as f:
# directory not empty # directory not empty
try: try:
vfs.rmdir("foo_dir") fs.rmdir("foo_dir")
except OSError as e: except OSError as e:
print(e.errno == errno.EACCES) print(e.errno == errno.EACCES)
# trim full path # trim full path
vfs.rename("foo_dir/file-in-dir.txt", "foo_dir/file.txt") fs.rename("foo_dir/file-in-dir.txt", "foo_dir/file.txt")
print(list(vfs.ilistdir("foo_dir"))) print(list(fs.ilistdir("foo_dir")))
vfs.rename("foo_dir/file.txt", "moved-to-root.txt") fs.rename("foo_dir/file.txt", "moved-to-root.txt")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
# check that renaming to existing file will overwrite it # check that renaming to existing file will overwrite it
with 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") fs.rename("temp", "moved-to-root.txt")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
with 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
vfs.remove("foo_dir/sub_file.txt") fs.remove("foo_dir/sub_file.txt")
vfs.rmdir("foo_dir") fs.rmdir("foo_dir")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
# disk full # disk full
try: try:
bsize = vfs.statvfs("/ramdisk")[0] bsize = fs.statvfs("/ramdisk")[0]
free = vfs.statvfs("/ramdisk")[2] + 1 free = fs.statvfs("/ramdisk")[2] + 1
f = 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:

Wyświetl plik

@ -1,9 +1,9 @@
# Test VfsFat class and its finaliser # Test VfsFat class and its finaliser
try: try:
import errno, os import errno, os, vfs
os.VfsFat vfs.VfsFat
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -39,8 +39,8 @@ except MemoryError:
raise SystemExit raise SystemExit
# Format block device and create VFS object # Format block device and create VFS object
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
vfs = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
# Here we test that opening a file with the heap locked fails correctly. This # Here we test that opening a file with the heap locked fails correctly. This
# is a special case because file objects use a finaliser and allocating with a # is a special case because file objects use a finaliser and allocating with a
@ -52,7 +52,7 @@ micropython.heap_lock()
try: try:
import errno, os import errno, os
vfs.open("x", "r") fs.open("x", "r")
except MemoryError: except MemoryError:
print("MemoryError") print("MemoryError")
micropython.heap_unlock() micropython.heap_unlock()
@ -77,10 +77,10 @@ for i in range(1024):
# Only read back N-1 files because the last one may not be finalised due to # Only read back N-1 files because the last one may not be finalised due to
# references to it being left on the C stack. # references to it being left on the C stack.
for n in names: for n in names:
f = vfs.open(n, "w") f = fs.open(n, "w")
f.write(n) f.write(n)
f = None # release f without closing f = None # release f without closing
gc.collect() # should finalise at least the first N-1 files by closing them gc.collect() # should finalise at least the first N-1 files by closing them
for n in names[:-1]: for n in names[:-1]:
with vfs.open(n, "r") as f: with fs.open(n, "r") as f:
print(f.read()) print(f.read())

Wyświetl plik

@ -2,9 +2,9 @@
import gc import gc
try: try:
import os import os, vfs
os.VfsFat vfs.VfsFat
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -37,29 +37,29 @@ class RAMBlockDevice:
def test(bdev, vfs_class): def test(bdev, vfs_class):
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
vfs = vfs_class(bdev) fs = vfs_class(bdev)
vfs.mkdir("/test_d1") fs.mkdir("/test_d1")
vfs.mkdir("/test_d2") fs.mkdir("/test_d2")
vfs.mkdir("/test_d3") fs.mkdir("/test_d3")
for i in range(10): for i in range(10):
print(i) print(i)
# We want to partially iterate the ilistdir iterator to leave it in an # We want to partially iterate the ilistdir iterator to leave it in an
# open state, which will then test the finaliser when it's garbage collected. # open state, which will then test the finaliser when it's garbage collected.
idir = vfs.ilistdir("/") idir = fs.ilistdir("/")
print(any(idir)) print(any(idir))
# Alternate way of partially iterating the ilistdir object, modifying the # Alternate way of partially iterating the ilistdir object, modifying the
# filesystem while it's open. # filesystem while it's open.
for dname, *_ in vfs.ilistdir("/"): for dname, *_ in fs.ilistdir("/"):
vfs.rmdir(dname) fs.rmdir(dname)
break break
vfs.mkdir(dname) fs.mkdir(dname)
# Also create a fully drained iterator and ensure trying to reuse it # Also create a fully drained iterator and ensure trying to reuse it
# throws the correct exception. # throws the correct exception.
idir_emptied = vfs.ilistdir("/") idir_emptied = fs.ilistdir("/")
l = list(idir_emptied) l = list(idir_emptied)
print(len(l)) print(len(l))
try: try:
@ -68,7 +68,7 @@ def test(bdev, vfs_class):
pass pass
gc.collect() gc.collect()
vfs.open("/test", "w").close() fs.open("/test", "w").close()
try: try:
@ -77,4 +77,4 @@ except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
test(bdev, os.VfsFat) test(bdev, vfs.VfsFat)

Wyświetl plik

@ -1,12 +1,8 @@
try: try:
import os import os, vfs
except ImportError:
print("SKIP")
raise SystemExit
try: vfs.VfsFat
os.VfsFat except (ImportError, AttributeError):
except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -44,14 +40,14 @@ except MemoryError:
# first we umount any existing mount points the target may have # first we umount any existing mount points the target may have
try: try:
os.umount("/") vfs.umount("/")
except OSError: except OSError:
pass pass
for path in os.listdir("/"): for path in os.listdir("/"):
os.umount("/" + path) vfs.umount("/" + path)
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
os.mount(bdev, "/") vfs.mount(bdev, "/")
print(os.getcwd()) print(os.getcwd())
@ -94,8 +90,8 @@ for exist in ("", "/", "dir", "/dir", "dir/subdir"):
os.chdir("/") os.chdir("/")
print(os.stat("test5.txt")[:-3]) print(os.stat("test5.txt")[:-3])
os.VfsFat.mkfs(bdev2) vfs.VfsFat.mkfs(bdev2)
os.mount(bdev2, "/sys") vfs.mount(bdev2, "/sys")
print(os.listdir()) print(os.listdir())
print(os.listdir("sys")) print(os.listdir("sys"))
print(os.listdir("/sys")) print(os.listdir("/sys"))
@ -104,7 +100,7 @@ os.rmdir("dir2")
os.remove("test5.txt") os.remove("test5.txt")
print(os.listdir()) print(os.listdir())
os.umount("/") vfs.umount("/")
print(os.getcwd()) print(os.getcwd())
print(os.listdir()) print(os.listdir())
print(os.listdir("sys")) print(os.listdir("sys"))

Wyświetl plik

@ -1,11 +1,11 @@
# Test for VfsFat using a RAM device, mtime feature # Test for VfsFat using a RAM device, mtime feature
try: try:
import time, os import time, os, vfs
time.time time.time
time.sleep time.sleep
os.VfsFat vfs.VfsFat
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -41,21 +41,21 @@ def test(bdev, vfs_class):
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
# construction # construction
vfs = vfs_class(bdev) fs = vfs_class(bdev)
# Create an empty file, should have a timestamp. # Create an empty file, should have a timestamp.
current_time = int(time.time()) current_time = int(time.time())
vfs.open("test1", "wt").close() fs.open("test1", "wt").close()
# Wait 2 seconds so mtime will increase (FAT has 2 second resolution). # Wait 2 seconds so mtime will increase (FAT has 2 second resolution).
time.sleep(2) time.sleep(2)
# Create another empty file, should have a timestamp. # Create another empty file, should have a timestamp.
vfs.open("test2", "wt").close() fs.open("test2", "wt").close()
# Stat the files and check mtime is non-zero. # Stat the files and check mtime is non-zero.
stat1 = vfs.stat("test1") stat1 = fs.stat("test1")
stat2 = vfs.stat("test2") stat2 = fs.stat("test2")
print(stat1[8] != 0, stat2[8] != 0) print(stat1[8] != 0, stat2[8] != 0)
# Check that test1 has mtime which matches time.time() at point of creation. # Check that test1 has mtime which matches time.time() at point of creation.
@ -67,8 +67,8 @@ def test(bdev, vfs_class):
print(stat1[8] < stat2[8]) print(stat1[8] < stat2[8])
# Unmount. # Unmount.
vfs.umount() fs.umount()
bdev = RAMBlockDevice(50) bdev = RAMBlockDevice(50)
test(bdev, os.VfsFat) test(bdev, vfs.VfsFat)

Wyświetl plik

@ -1,13 +1,8 @@
try: try:
import errno import errno, os, vfs
import os
except ImportError:
print("SKIP")
raise SystemExit
try: vfs.VfsFat
os.VfsFat except (ImportError, AttributeError):
except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -41,18 +36,18 @@ except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
vfs = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
os.mount(vfs, "/ramdisk") vfs.mount(fs, "/ramdisk")
# file io # file io
with vfs.open("file.txt", "w") as f: with fs.open("file.txt", "w") as f:
f.write("hello!") f.write("hello!")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
with vfs.open("file.txt", "r") as f: with fs.open("file.txt", "r") as f:
print(f.read()) print(f.read())
vfs.remove("file.txt") fs.remove("file.txt")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))

Wyświetl plik

@ -1,13 +1,8 @@
try: try:
import errno import errno, os, vfs
import os
except ImportError:
print("SKIP")
raise SystemExit
try: vfs.VfsFat
os.VfsFat except (ImportError, AttributeError):
except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -38,7 +33,7 @@ class RAMFS:
try: try:
bdev = RAMFS(50) bdev = RAMFS(50)
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
except MemoryError: except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -46,50 +41,50 @@ except MemoryError:
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 = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
os.mount(vfs, "/ramdisk") vfs.mount(fs, "/ramdisk")
print("statvfs:", vfs.statvfs("/ramdisk")) print("statvfs:", fs.statvfs("/ramdisk"))
print("getcwd:", vfs.getcwd()) print("getcwd:", fs.getcwd())
try: try:
vfs.stat("no_file.txt") fs.stat("no_file.txt")
except OSError as e: except OSError as e:
print(e.errno == errno.ENOENT) print(e.errno == errno.ENOENT)
with vfs.open("foo_file.txt", "w") as f: with fs.open("foo_file.txt", "w") as f:
f.write("hello!") f.write("hello!")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
print("stat root:", vfs.stat("/")[:-3]) # timestamps differ across runs print("stat root:", fs.stat("/")[:-3]) # timestamps differ across runs
print("stat file:", vfs.stat("foo_file.txt")[:-3]) # timestamps differ across runs print("stat file:", fs.stat("foo_file.txt")[:-3]) # timestamps differ across runs
print(b"FOO_FILETXT" in bdev.data) print(b"FOO_FILETXT" in bdev.data)
print(b"hello!" in bdev.data) print(b"hello!" in bdev.data)
vfs.mkdir("foo_dir") fs.mkdir("foo_dir")
vfs.chdir("foo_dir") fs.chdir("foo_dir")
print("getcwd:", vfs.getcwd()) print("getcwd:", fs.getcwd())
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
with vfs.open("sub_file.txt", "w") as f: with fs.open("sub_file.txt", "w") as f:
f.write("subdir file") f.write("subdir file")
try: try:
vfs.chdir("sub_file.txt") fs.chdir("sub_file.txt")
except OSError as e: except OSError as e:
print(e.errno == errno.ENOENT) print(e.errno == errno.ENOENT)
vfs.chdir("..") fs.chdir("..")
print("getcwd:", vfs.getcwd()) print("getcwd:", fs.getcwd())
os.umount(vfs) vfs.umount(fs)
vfs = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
print(list(vfs.ilistdir(b""))) print(list(fs.ilistdir(b"")))
# list a non-existent directory # list a non-existent directory
try: try:
vfs.ilistdir(b"no_exist") fs.ilistdir(b"no_exist")
except OSError as e: except OSError as e:
print("ENOENT:", e.errno == errno.ENOENT) print("ENOENT:", e.errno == errno.ENOENT)

Wyświetl plik

@ -1,14 +1,10 @@
# test making a FAT filesystem on a very large block device # test making a FAT filesystem on a very large block device
try: try:
import os import os, vfs
except ImportError:
print("SKIP")
raise SystemExit
try: vfs.VfsFat
os.VfsFat except (ImportError, AttributeError):
except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -46,24 +42,24 @@ class RAMBDevSparse:
try: try:
bdev = RAMBDevSparse(4 * 1024 * 1024 * 1024 // RAMBDevSparse.SEC_SIZE) bdev = RAMBDevSparse(4 * 1024 * 1024 * 1024 // RAMBDevSparse.SEC_SIZE)
os.VfsFat.mkfs(bdev) vfs.VfsFat.mkfs(bdev)
except MemoryError: except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
vfs = os.VfsFat(bdev) fs = vfs.VfsFat(bdev)
os.mount(vfs, "/ramdisk") vfs.mount(fs, "/ramdisk")
print("statvfs:", vfs.statvfs("/ramdisk")) print("statvfs:", fs.statvfs("/ramdisk"))
f = open("/ramdisk/test.txt", "w") f = open("/ramdisk/test.txt", "w")
f.write("test file") f.write("test file")
f.close() f.close()
print("statvfs:", vfs.statvfs("/ramdisk")) print("statvfs:", fs.statvfs("/ramdisk"))
f = open("/ramdisk/test.txt") f = open("/ramdisk/test.txt")
print(f.read()) print(f.read())
f.close() f.close()
os.umount(vfs) vfs.umount(fs)

Wyświetl plik

@ -1,10 +1,10 @@
# Test for VfsLittle using a RAM device # Test for VfsLittle using a RAM device
try: try:
import os import os, vfs
os.VfsLfs1 vfs.VfsLfs1
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -47,44 +47,44 @@ def test(bdev, vfs_class):
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
# construction # construction
vfs = vfs_class(bdev) fs = vfs_class(bdev)
# statvfs # statvfs
print(vfs.statvfs("/")) print(fs.statvfs("/"))
# open, write close # open, write close
f = vfs.open("test", "w") f = fs.open("test", "w")
f.write("littlefs") f.write("littlefs")
f.close() f.close()
# statvfs after creating a file # statvfs after creating a file
print(vfs.statvfs("/")) print(fs.statvfs("/"))
# ilistdir # ilistdir
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
print(list(vfs.ilistdir("/"))) print(list(fs.ilistdir("/")))
print(list(vfs.ilistdir(b"/"))) print(list(fs.ilistdir(b"/")))
# mkdir, rmdir # mkdir, rmdir
vfs.mkdir("testdir") fs.mkdir("testdir")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
print(sorted(list(vfs.ilistdir("testdir")))) print(sorted(list(fs.ilistdir("testdir"))))
vfs.rmdir("testdir") fs.rmdir("testdir")
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
vfs.mkdir("testdir") fs.mkdir("testdir")
# stat a file # stat a file
print_stat(vfs.stat("test")) print_stat(fs.stat("test"))
# stat a dir (size seems to vary on LFS2 so don't print that) # stat a dir (size seems to vary on LFS2 so don't print that)
print_stat(vfs.stat("testdir"), False) print_stat(fs.stat("testdir"), False)
# read # read
with vfs.open("test", "r") as f: with fs.open("test", "r") as f:
print(f.read()) print(f.read())
# create large file # create large file
with vfs.open("testbig", "w") as f: with fs.open("testbig", "w") as f:
data = "large012" * 32 * 16 data = "large012" * 32 * 16
print("data length:", len(data)) print("data length:", len(data))
for i in range(4): for i in range(4):
@ -92,63 +92,63 @@ def test(bdev, vfs_class):
f.write(data) f.write(data)
# stat after creating large file # stat after creating large file
print(vfs.statvfs("/")) print(fs.statvfs("/"))
# rename # rename
vfs.rename("testbig", "testbig2") fs.rename("testbig", "testbig2")
print(sorted(list(vfs.ilistdir()))) print(sorted(list(fs.ilistdir())))
vfs.chdir("testdir") fs.chdir("testdir")
vfs.rename("/testbig2", "testbig2") fs.rename("/testbig2", "testbig2")
print(sorted(list(vfs.ilistdir()))) print(sorted(list(fs.ilistdir())))
vfs.rename("testbig2", "/testbig2") fs.rename("testbig2", "/testbig2")
vfs.chdir("/") fs.chdir("/")
print(sorted(list(vfs.ilistdir()))) print(sorted(list(fs.ilistdir())))
# remove # remove
vfs.remove("testbig2") fs.remove("testbig2")
print(sorted(list(vfs.ilistdir()))) print(sorted(list(fs.ilistdir())))
# getcwd, chdir # getcwd, chdir
vfs.mkdir("/testdir2") fs.mkdir("/testdir2")
vfs.mkdir("/testdir/subdir") fs.mkdir("/testdir/subdir")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("/testdir") fs.chdir("/testdir")
print(vfs.getcwd()) print(fs.getcwd())
# create file in directory to make sure paths are relative # create file in directory to make sure paths are relative
vfs.open("test2", "w").close() fs.open("test2", "w").close()
print_stat(vfs.stat("test2")) print_stat(fs.stat("test2"))
print_stat(vfs.stat("/testdir/test2")) print_stat(fs.stat("/testdir/test2"))
vfs.remove("test2") fs.remove("test2")
# chdir back to root and remove testdir # chdir back to root and remove testdir
vfs.chdir("/") fs.chdir("/")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("testdir") fs.chdir("testdir")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("..") fs.chdir("..")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("testdir/subdir") fs.chdir("testdir/subdir")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("../..") fs.chdir("../..")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("/./testdir2") fs.chdir("/./testdir2")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("../testdir") fs.chdir("../testdir")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("../..") fs.chdir("../..")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir(".//testdir") fs.chdir(".//testdir")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("subdir/./") fs.chdir("subdir/./")
print(vfs.getcwd()) print(fs.getcwd())
vfs.chdir("/") fs.chdir("/")
print(vfs.getcwd()) print(fs.getcwd())
vfs.rmdir("testdir/subdir") fs.rmdir("testdir/subdir")
vfs.rmdir("testdir") fs.rmdir("testdir")
vfs.rmdir("testdir2") fs.rmdir("testdir2")
bdev = RAMBlockDevice(30) bdev = RAMBlockDevice(30)
test(bdev, os.VfsLfs1) test(bdev, vfs.VfsLfs1)
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -1,10 +1,10 @@
# Test for VfsLittle using a RAM device, testing error handling from corrupt block device # Test for VfsLittle using a RAM device, testing error handling from corrupt block device
try: try:
import os import vfs
os.VfsLfs1 vfs.VfsLfs1
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -47,28 +47,28 @@ def corrupt(bdev, block):
def create_vfs(bdev, vfs_class): def create_vfs(bdev, vfs_class):
bdev.ret = 0 bdev.ret = 0
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
vfs = vfs_class(bdev) fs = vfs_class(bdev)
with vfs.open("f", "w") as f: with fs.open("f", "w") as f:
for i in range(100): for i in range(100):
f.write("test") f.write("test")
return vfs return fs
def test(bdev, vfs_class): def test(bdev, vfs_class):
print("test", vfs_class) print("test", vfs_class)
# statvfs # statvfs
vfs = create_vfs(bdev, vfs_class) fs = create_vfs(bdev, vfs_class)
corrupt(bdev, 0) corrupt(bdev, 0)
corrupt(bdev, 1) corrupt(bdev, 1)
try: try:
print(vfs.statvfs("")) print(fs.statvfs(""))
except OSError: except OSError:
print("statvfs OSError") print("statvfs OSError")
# error during read # error during read
vfs = create_vfs(bdev, vfs_class) fs = create_vfs(bdev, vfs_class)
f = vfs.open("f", "r") f = fs.open("f", "r")
bdev.ret = -5 # EIO bdev.ret = -5 # EIO
try: try:
f.read(10) f.read(10)
@ -76,8 +76,8 @@ def test(bdev, vfs_class):
print("read OSError") print("read OSError")
# error during write # error during write
vfs = create_vfs(bdev, vfs_class) fs = create_vfs(bdev, vfs_class)
f = vfs.open("f", "a") f = fs.open("f", "a")
bdev.ret = -5 # EIO bdev.ret = -5 # EIO
try: try:
f.write("test") f.write("test")
@ -85,8 +85,8 @@ def test(bdev, vfs_class):
print("write OSError") print("write OSError")
# error during close # error during close
vfs = create_vfs(bdev, vfs_class) fs = create_vfs(bdev, vfs_class)
f = vfs.open("f", "w") f = fs.open("f", "w")
f.write("test") f.write("test")
bdev.ret = -5 # EIO bdev.ret = -5 # EIO
try: try:
@ -95,8 +95,8 @@ def test(bdev, vfs_class):
print("close OSError") print("close OSError")
# error during flush # error during flush
vfs = create_vfs(bdev, vfs_class) fs = create_vfs(bdev, vfs_class)
f = vfs.open("f", "w") f = fs.open("f", "w")
f.write("test") f.write("test")
bdev.ret = -5 # EIO bdev.ret = -5 # EIO
try: try:
@ -108,5 +108,5 @@ def test(bdev, vfs_class):
bdev = RAMBlockDevice(30) bdev = RAMBlockDevice(30)
test(bdev, os.VfsLfs1) test(bdev, vfs.VfsLfs1)
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -1,10 +1,10 @@
# Test for VfsLittle using a RAM device, testing error handling # Test for VfsLittle using a RAM device, testing error handling
try: try:
import os import vfs
os.VfsLfs1 vfs.VfsLfs1
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -52,63 +52,63 @@ def test(bdev, vfs_class):
# set up for following tests # set up for following tests
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
vfs = vfs_class(bdev) fs = vfs_class(bdev)
with vfs.open("testfile", "w") as f: with fs.open("testfile", "w") as f:
f.write("test") f.write("test")
vfs.mkdir("testdir") fs.mkdir("testdir")
# ilistdir # ilistdir
try: try:
vfs.ilistdir("noexist") fs.ilistdir("noexist")
except OSError: except OSError:
print("ilistdir OSError") print("ilistdir OSError")
# remove # remove
try: try:
vfs.remove("noexist") fs.remove("noexist")
except OSError: except OSError:
print("remove OSError") print("remove OSError")
# rmdir # rmdir
try: try:
vfs.rmdir("noexist") fs.rmdir("noexist")
except OSError: except OSError:
print("rmdir OSError") print("rmdir OSError")
# rename # rename
try: try:
vfs.rename("noexist", "somethingelse") fs.rename("noexist", "somethingelse")
except OSError: except OSError:
print("rename OSError") print("rename OSError")
# mkdir # mkdir
try: try:
vfs.mkdir("testdir") fs.mkdir("testdir")
except OSError: except OSError:
print("mkdir OSError") print("mkdir OSError")
# chdir to nonexistent # chdir to nonexistent
try: try:
vfs.chdir("noexist") fs.chdir("noexist")
except OSError: except OSError:
print("chdir OSError") print("chdir OSError")
print(vfs.getcwd()) # check still at root print(fs.getcwd()) # check still at root
# chdir to file # chdir to file
try: try:
vfs.chdir("testfile") fs.chdir("testfile")
except OSError: except OSError:
print("chdir OSError") print("chdir OSError")
print(vfs.getcwd()) # check still at root print(fs.getcwd()) # check still at root
# stat # stat
try: try:
vfs.stat("noexist") fs.stat("noexist")
except OSError: except OSError:
print("stat OSError") print("stat OSError")
# error during seek # error during seek
with vfs.open("testfile", "r") as f: with fs.open("testfile", "r") as f:
f.seek(1 << 30) # SEEK_SET f.seek(1 << 30) # SEEK_SET
try: try:
f.seek(1 << 30, 1) # SEEK_CUR f.seek(1 << 30, 1) # SEEK_CUR
@ -117,5 +117,5 @@ def test(bdev, vfs_class):
bdev = RAMBlockDevice(30) bdev = RAMBlockDevice(30)
test(bdev, os.VfsLfs1) test(bdev, vfs.VfsLfs1)
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -1,10 +1,10 @@
# Test for VfsLittle using a RAM device, file IO # Test for VfsLittle using a RAM device, file IO
try: try:
import os import vfs
os.VfsLfs1 vfs.VfsLfs1
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -42,10 +42,10 @@ def test(bdev, vfs_class):
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
# construction # construction
vfs = vfs_class(bdev) fs = vfs_class(bdev)
# create text, print, write, close # create text, print, write, close
f = vfs.open("test.txt", "wt") f = fs.open("test.txt", "wt")
print(f) print(f)
f.write("littlefs") f.write("littlefs")
f.close() f.close()
@ -54,48 +54,48 @@ def test(bdev, vfs_class):
f.close() f.close()
# create binary, print, write, flush, close # create binary, print, write, flush, close
f = vfs.open("test.bin", "wb") f = fs.open("test.bin", "wb")
print(f) print(f)
f.write("littlefs") f.write("littlefs")
f.flush() f.flush()
f.close() f.close()
# create for append # create for append
f = vfs.open("test.bin", "ab") f = fs.open("test.bin", "ab")
f.write("more") f.write("more")
f.close() f.close()
# create exclusive # create exclusive
f = vfs.open("test2.bin", "xb") f = fs.open("test2.bin", "xb")
f.close() f.close()
# create exclusive with error # create exclusive with error
try: try:
vfs.open("test2.bin", "x") fs.open("test2.bin", "x")
except OSError: except OSError:
print("open OSError") print("open OSError")
# read default # read default
with vfs.open("test.txt", "") as f: with fs.open("test.txt", "") as f:
print(f.read()) print(f.read())
# read text # read text
with vfs.open("test.txt", "rt") as f: with fs.open("test.txt", "rt") as f:
print(f.read()) print(f.read())
# read binary # read binary
with vfs.open("test.bin", "rb") as f: with fs.open("test.bin", "rb") as f:
print(f.read()) print(f.read())
# create read and write # create read and write
with vfs.open("test.bin", "r+b") as f: with fs.open("test.bin", "r+b") as f:
print(f.read(8)) print(f.read(8))
f.write("MORE") f.write("MORE")
with vfs.open("test.bin", "rb") as f: with fs.open("test.bin", "rb") as f:
print(f.read()) print(f.read())
# seek and tell # seek and tell
f = vfs.open("test.txt", "r") f = fs.open("test.txt", "r")
print(f.tell()) print(f.tell())
f.seek(3, 0) f.seek(3, 0)
print(f.tell()) print(f.tell())
@ -103,13 +103,13 @@ def test(bdev, vfs_class):
# open nonexistent # open nonexistent
try: try:
vfs.open("noexist", "r") fs.open("noexist", "r")
except OSError: except OSError:
print("open OSError") print("open OSError")
# open multiple files at the same time # open multiple files at the same time
f1 = vfs.open("test.txt", "") f1 = fs.open("test.txt", "")
f2 = vfs.open("test.bin", "b") f2 = fs.open("test.bin", "b")
print(f1.read()) print(f1.read())
print(f2.read()) print(f2.read())
f1.close() f1.close()
@ -117,5 +117,5 @@ def test(bdev, vfs_class):
bdev = RAMBlockDevice(30) bdev = RAMBlockDevice(30)
test(bdev, os.VfsLfs1) test(bdev, vfs.VfsLfs1)
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -2,9 +2,9 @@
import gc import gc
try: try:
import os import vfs
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -37,29 +37,29 @@ class RAMBlockDevice:
def test(bdev, vfs_class): def test(bdev, vfs_class):
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
vfs = vfs_class(bdev) fs = vfs_class(bdev)
vfs.mkdir("/test_d1") fs.mkdir("/test_d1")
vfs.mkdir("/test_d2") fs.mkdir("/test_d2")
vfs.mkdir("/test_d3") fs.mkdir("/test_d3")
for i in range(10): for i in range(10):
print(i) print(i)
# We want to partially iterate the ilistdir iterator to leave it in an # We want to partially iterate the ilistdir iterator to leave it in an
# open state, which will then test the finaliser when it's garbage collected. # open state, which will then test the finaliser when it's garbage collected.
idir = vfs.ilistdir("/") idir = fs.ilistdir("/")
print(any(idir)) print(any(idir))
# Alternate way of partially iterating the ilistdir object, modifying the # Alternate way of partially iterating the ilistdir object, modifying the
# filesystem while it's open. # filesystem while it's open.
for dname, *_ in vfs.ilistdir("/"): for dname, *_ in fs.ilistdir("/"):
vfs.rmdir(dname) fs.rmdir(dname)
break break
vfs.mkdir(dname) fs.mkdir(dname)
# Also create a fully drained iterator and ensure trying to reuse it # Also create a fully drained iterator and ensure trying to reuse it
# throws the correct exception. # throws the correct exception.
idir_emptied = vfs.ilistdir("/") idir_emptied = fs.ilistdir("/")
l = list(idir_emptied) l = list(idir_emptied)
print(len(l)) print(len(l))
try: try:
@ -68,8 +68,8 @@ def test(bdev, vfs_class):
pass pass
gc.collect() gc.collect()
vfs.open("/test", "w").close() fs.open("/test", "w").close()
bdev = RAMBlockDevice(30) bdev = RAMBlockDevice(30)
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -1,10 +1,10 @@
# Test for VfsLittle using a RAM device, with mount/umount # Test for VfsLittle using a RAM device, with mount/umount
try: try:
import os import os, vfs
os.VfsLfs1 vfs.VfsLfs1
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -42,7 +42,7 @@ def test(vfs_class):
# mount bdev unformatted # mount bdev unformatted
try: try:
os.mount(bdev, "/lfs") vfs.mount(bdev, "/lfs")
except Exception as er: except Exception as er:
print(repr(er)) print(repr(er))
@ -50,10 +50,10 @@ def test(vfs_class):
vfs_class.mkfs(bdev) vfs_class.mkfs(bdev)
# construction # construction
vfs = vfs_class(bdev) fs = vfs_class(bdev)
# mount # mount
os.mount(vfs, "/lfs") vfs.mount(fs, "/lfs")
# import # import
with open("/lfs/lfsmod.py", "w") as f: with open("/lfs/lfsmod.py", "w") as f:
@ -73,11 +73,11 @@ def test(vfs_class):
import lfsmod2 import lfsmod2
# umount # umount
os.umount("/lfs") vfs.umount("/lfs")
# mount read-only # mount read-only
vfs = vfs_class(bdev) fs = vfs_class(bdev)
os.mount(vfs, "/lfs", readonly=True) vfs.mount(fs, "/lfs", readonly=True)
# test reading works # test reading works
with open("/lfs/subdir/lfsmod2.py") as f: with open("/lfs/subdir/lfsmod2.py") as f:
@ -90,13 +90,13 @@ def test(vfs_class):
print(repr(er)) print(repr(er))
# umount # umount
os.umount("/lfs") vfs.umount("/lfs")
# mount bdev again # mount bdev again
os.mount(bdev, "/lfs") vfs.mount(bdev, "/lfs")
# umount # umount
os.umount("/lfs") vfs.umount("/lfs")
# clear imported modules # clear imported modules
sys.modules.clear() sys.modules.clear()
@ -110,5 +110,5 @@ sys.path.append("/lfs")
sys.path.append("") sys.path.append("")
# run tests # run tests
test(os.VfsLfs1) test(vfs.VfsLfs1)
test(os.VfsLfs2) test(vfs.VfsLfs2)

Wyświetl plik

@ -1,11 +1,11 @@
# Test for VfsLfs using a RAM device, mtime feature # Test for VfsLfs using a RAM device, mtime feature
try: try:
import time, os import time, vfs
time.time time.time
time.sleep time.sleep
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -44,21 +44,21 @@ def test(bdev, vfs_class):
# construction # construction
print("mtime=True") print("mtime=True")
vfs = vfs_class(bdev, mtime=True) fs = vfs_class(bdev, mtime=True)
# Create an empty file, should have a timestamp. # Create an empty file, should have a timestamp.
current_time = int(time.time()) current_time = int(time.time())
vfs.open("test1", "wt").close() fs.open("test1", "wt").close()
# Wait 1 second so mtime will increase by at least 1. # Wait 1 second so mtime will increase by at least 1.
time.sleep(1) time.sleep(1)
# Create another empty file, should have a timestamp. # Create another empty file, should have a timestamp.
vfs.open("test2", "wt").close() fs.open("test2", "wt").close()
# Stat the files and check mtime is non-zero. # Stat the files and check mtime is non-zero.
stat1 = vfs.stat("test1") stat1 = fs.stat("test1")
stat2 = vfs.stat("test2") stat2 = fs.stat("test2")
print(stat1[8] != 0, stat2[8] != 0) print(stat1[8] != 0, stat2[8] != 0)
# Check that test1 has mtime which matches time.time() at point of creation. # Check that test1 has mtime which matches time.time() at point of creation.
@ -71,34 +71,34 @@ def test(bdev, vfs_class):
time.sleep(1) time.sleep(1)
# Open test1 for reading and ensure mtime did not change. # Open test1 for reading and ensure mtime did not change.
vfs.open("test1", "rt").close() fs.open("test1", "rt").close()
print(vfs.stat("test1") == stat1) print(fs.stat("test1") == stat1)
# Open test1 for writing and ensure mtime increased from the previous value. # Open test1 for writing and ensure mtime increased from the previous value.
vfs.open("test1", "wt").close() fs.open("test1", "wt").close()
stat1_old = stat1 stat1_old = stat1
stat1 = vfs.stat("test1") stat1 = fs.stat("test1")
print(stat1_old[8] < stat1[8]) print(stat1_old[8] < stat1[8])
# Unmount. # Unmount.
vfs.umount() fs.umount()
# Check that remounting with mtime=False can read the timestamps. # Check that remounting with mtime=False can read the timestamps.
print("mtime=False") print("mtime=False")
vfs = vfs_class(bdev, mtime=False) fs = vfs_class(bdev, mtime=False)
print(vfs.stat("test1") == stat1) print(fs.stat("test1") == stat1)
print(vfs.stat("test2") == stat2) print(fs.stat("test2") == stat2)
f = vfs.open("test1", "wt") f = fs.open("test1", "wt")
f.close() f.close()
print(vfs.stat("test1") == stat1) print(fs.stat("test1") == stat1)
vfs.umount() fs.umount()
# Check that remounting with mtime=True still has the timestamps. # Check that remounting with mtime=True still has the timestamps.
print("mtime=True") print("mtime=True")
vfs = vfs_class(bdev, mtime=True) fs = vfs_class(bdev, mtime=True)
print(vfs.stat("test1") == stat1) print(fs.stat("test1") == stat1)
print(vfs.stat("test2") == stat2) print(fs.stat("test2") == stat2)
vfs.umount() fs.umount()
try: try:
@ -107,4 +107,4 @@ except MemoryError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
test(bdev, os.VfsLfs2) test(bdev, vfs.VfsLfs2)

Wyświetl plik

@ -1,9 +1,9 @@
# Test for VfsLfs using a RAM device, when the first superblock does not exist # Test for VfsLfs using a RAM device, when the first superblock does not exist
try: try:
import os import os, vfs
os.VfsLfs2 vfs.VfsLfs2
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -36,12 +36,12 @@ lfs2_data = b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x
bdev = RAMBlockDevice(64, lfs2_data) bdev = RAMBlockDevice(64, lfs2_data)
# Create the VFS explicitly, no auto-detection is needed for this. # Create the VFS explicitly, no auto-detection is needed for this.
vfs = os.VfsLfs2(bdev) fs = vfs.VfsLfs2(bdev)
print(list(vfs.ilistdir())) print(list(fs.ilistdir()))
# Mount the block device directly; this relies on auto-detection. # Mount the block device directly; this relies on auto-detection.
os.mount(bdev, "/userfs") vfs.mount(bdev, "/userfs")
print(os.listdir("/userfs")) print(os.listdir("/userfs"))
# Clean up. # Clean up.
os.umount("/userfs") vfs.umount("/userfs")

Wyświetl plik

@ -1,10 +1,9 @@
# Test for VfsPosix # Test for VfsPosix
try: try:
import gc import gc, os, vfs
import os
os.VfsPosix vfs.VfsPosix
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -13,8 +12,6 @@ except (ImportError, AttributeError):
# Skip the test if it does exist. # Skip the test if it does exist.
temp_dir = "micropy_test_dir" temp_dir = "micropy_test_dir"
try: try:
import os
os.stat(temp_dir) os.stat(temp_dir)
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -87,35 +84,35 @@ os.rename(temp_dir + "/test", temp_dir + "/test2")
print(os.listdir(temp_dir)) print(os.listdir(temp_dir))
# construct new VfsPosix with path argument # construct new VfsPosix with path argument
vfs = os.VfsPosix(temp_dir) fs = vfs.VfsPosix(temp_dir)
# when VfsPosix is used the intended way via os.mount(), it can only be called # when VfsPosix is used the intended way via vfs.mount(), it can only be called
# with relative paths when the CWD is inside or at its root, so simulate that # with relative paths when the CWD is inside or at its root, so simulate that
os.chdir(temp_dir) os.chdir(temp_dir)
print(list(i[0] for i in vfs.ilistdir("."))) print(list(i[0] for i in fs.ilistdir(".")))
# stat, statvfs (statvfs may not exist) # stat, statvfs (statvfs may not exist)
print(type(vfs.stat("."))) print(type(fs.stat(".")))
if hasattr(vfs, "statvfs"): if hasattr(fs, "statvfs"):
assert type(vfs.statvfs(".")) is tuple assert type(fs.statvfs(".")) is tuple
# check types of ilistdir with str/bytes arguments # check types of ilistdir with str/bytes arguments
print(type(list(vfs.ilistdir("."))[0][0])) print(type(list(fs.ilistdir("."))[0][0]))
print(type(list(vfs.ilistdir(b"."))[0][0])) print(type(list(fs.ilistdir(b"."))[0][0]))
# chdir should not affect absolute paths (regression test) # chdir should not affect absolute paths (regression test)
vfs.mkdir("/subdir") fs.mkdir("/subdir")
vfs.mkdir("/subdir/micropy_test_dir") fs.mkdir("/subdir/micropy_test_dir")
with vfs.open("/subdir/micropy_test_dir/test2", "w") as f: with fs.open("/subdir/micropy_test_dir/test2", "w") as f:
f.write("wrong") f.write("wrong")
vfs.chdir("/subdir") fs.chdir("/subdir")
with vfs.open("/test2", "r") as f: with fs.open("/test2", "r") as f:
print(f.read()) print(f.read())
os.chdir(curdir) os.chdir(curdir)
vfs.remove("/subdir/micropy_test_dir/test2") fs.remove("/subdir/micropy_test_dir/test2")
vfs.rmdir("/subdir/micropy_test_dir") fs.rmdir("/subdir/micropy_test_dir")
vfs.rmdir("/subdir") fs.rmdir("/subdir")
# done with vfs, restore CWD # done with fs, restore CWD
os.chdir(curdir) os.chdir(curdir)
# remove # remove

Wyświetl plik

@ -1,10 +1,9 @@
# Test for VfsPosix error conditions # Test for VfsPosix error conditions
try: try:
import os import os, sys, vfs
import sys
os.VfsPosix vfs.VfsPosix
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -36,7 +35,7 @@ except OSError as e:
print("getcwd():", repr(e)) print("getcwd():", repr(e))
try: try:
print("VfsPosix():", os.VfsPosix("something")) print("VfsPosix():", vfs.VfsPosix("something"))
except OSError as e: except OSError as e:
# expecting ENOENT = 2 # expecting ENOENT = 2
print("VfsPosix():", repr(e)) print("VfsPosix():", repr(e))

Wyświetl plik

@ -2,9 +2,9 @@
import gc import gc
try: try:
import os import os, vfs
os.VfsPosix vfs.VfsPosix
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -12,34 +12,34 @@ except (ImportError, AttributeError):
def test(testdir): def test(testdir):
curdir = os.getcwd() curdir = os.getcwd()
vfs = os.VfsPosix(testdir) fs = vfs.VfsPosix(testdir)
# When VfsPosix is used the intended way via os.mount(), it can only be called # When VfsPosix is used the intended way via vfs.mount(), it can only be called
# with relative paths when the CWD is inside or at its root, so simulate that. # with relative paths when the CWD is inside or at its root, so simulate that.
# (Although perhaps calling with a relative path was an oversight in this case # (Although perhaps calling with a relative path was an oversight in this case
# and the respective line below was meant to read `vfs.rmdir("/" + dname)`.) # and the respective line below was meant to read `fs.rmdir("/" + dname)`.)
os.chdir(testdir) os.chdir(testdir)
vfs.mkdir("/test_d1") fs.mkdir("/test_d1")
vfs.mkdir("/test_d2") fs.mkdir("/test_d2")
vfs.mkdir("/test_d3") fs.mkdir("/test_d3")
for i in range(10): for i in range(10):
print(i) print(i)
# We want to partially iterate the ilistdir iterator to leave it in an # We want to partially iterate the ilistdir iterator to leave it in an
# open state, which will then test the finaliser when it's garbage collected. # open state, which will then test the finaliser when it's garbage collected.
idir = vfs.ilistdir("/") idir = fs.ilistdir("/")
print(any(idir)) print(any(idir))
# Alternate way of partially iterating the ilistdir object, modifying the # Alternate way of partially iterating the ilistdir object, modifying the
# filesystem while it's open. # filesystem while it's open.
for dname, *_ in vfs.ilistdir("/"): for dname, *_ in fs.ilistdir("/"):
vfs.rmdir(dname) fs.rmdir(dname)
break break
vfs.mkdir(dname) fs.mkdir(dname)
# Also create a fully drained iterator and ensure trying to reuse it # Also create a fully drained iterator and ensure trying to reuse it
# throws the correct exception. # throws the correct exception.
idir_emptied = vfs.ilistdir("/") idir_emptied = fs.ilistdir("/")
l = list(idir_emptied) l = list(idir_emptied)
print(len(l)) print(len(l))
try: try:
@ -51,10 +51,10 @@ def test(testdir):
# Create and delete a file, try to flush out any filesystem # Create and delete a file, try to flush out any filesystem
# corruption that may be caused over the loops. # corruption that may be caused over the loops.
vfs.open("/test", "w").close() fs.open("/test", "w").close()
vfs.remove("/test") fs.remove("/test")
# Done with vfs, restore CWD. # Done with fs, restore CWD.
os.chdir(curdir) os.chdir(curdir)

Wyświetl plik

@ -1,9 +1,9 @@
# Test ilistdir filter of . and .. for VfsPosix. # Test ilistdir filter of . and .. for VfsPosix.
try: try:
import os import os, vfs
os.VfsPosix vfs.VfsPosix
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -11,24 +11,24 @@ except (ImportError, AttributeError):
def test(testdir): def test(testdir):
curdir = os.getcwd() curdir = os.getcwd()
vfs = os.VfsPosix(testdir) fs = vfs.VfsPosix(testdir)
# When VfsPosix is used the intended way via os.mount(), it can only be called # When VfsPosix is used the intended way via vfs.mount(), it can only be called
# with relative paths when the CWD is inside or at its root, so simulate that. # with relative paths when the CWD is inside or at its root, so simulate that.
os.chdir(testdir) os.chdir(testdir)
dirs = [".a", "..a", "...a", "a.b", "a..b"] dirs = [".a", "..a", "...a", "a.b", "a..b"]
for dir in dirs: for dir in dirs:
vfs.mkdir(dir) fs.mkdir(dir)
dirs = [] dirs = []
for entry in vfs.ilistdir("/"): for entry in fs.ilistdir("/"):
dirs.append(entry[0]) dirs.append(entry[0])
dirs.sort() dirs.sort()
print(dirs) print(dirs)
# Done with vfs, restore CWD. # Done with fs, restore CWD.
os.chdir(curdir) os.chdir(curdir)

Wyświetl plik

@ -1,9 +1,9 @@
# Test for VfsPosix with relative paths # Test for VfsPosix with relative paths
try: try:
import os import os, vfs
os.VfsPosix vfs.VfsPosix
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -12,8 +12,6 @@ except (ImportError, AttributeError):
# Skip the test if it does exist. # Skip the test if it does exist.
temp_dir = "vfs_posix_paths_test_dir" temp_dir = "vfs_posix_paths_test_dir"
try: try:
import os
os.stat(temp_dir) os.stat(temp_dir)
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -25,26 +23,26 @@ os.mkdir(temp_dir)
# construct new VfsPosix with absolute path argument # construct new VfsPosix with absolute path argument
temp_dir_abs = os.getcwd() + os.sep + temp_dir temp_dir_abs = os.getcwd() + os.sep + temp_dir
vfs = os.VfsPosix(temp_dir_abs) fs = vfs.VfsPosix(temp_dir_abs)
# when VfsPosix is used the intended way via os.mount(), it can only be called # when VfsPosix is used the intended way via vfs.mount(), it can only be called
# with relative paths when the CWD is inside or at its root, so simulate that # with relative paths when the CWD is inside or at its root, so simulate that
os.chdir(temp_dir_abs) os.chdir(temp_dir_abs)
vfs.mkdir("subdir") fs.mkdir("subdir")
vfs.mkdir("subdir/one") fs.mkdir("subdir/one")
print('listdir("/"):', sorted(i[0] for i in vfs.ilistdir("/"))) print('listdir("/"):', sorted(i[0] for i in fs.ilistdir("/")))
print('listdir("."):', sorted(i[0] for i in vfs.ilistdir("."))) print('listdir("."):', sorted(i[0] for i in fs.ilistdir(".")))
print('getcwd() in {"", "/"}:', vfs.getcwd() in {"", "/"}) print('getcwd() in {"", "/"}:', fs.getcwd() in {"", "/"})
print('chdir("subdir"):', vfs.chdir("subdir")) print('chdir("subdir"):', fs.chdir("subdir"))
print("getcwd():", vfs.getcwd()) print("getcwd():", fs.getcwd())
print('mkdir("two"):', vfs.mkdir("two")) print('mkdir("two"):', fs.mkdir("two"))
f = vfs.open("file.py", "w") f = fs.open("file.py", "w")
f.write("print('hello')") f.write("print('hello')")
f.close() f.close()
print('listdir("/"):', sorted(i[0] for i in vfs.ilistdir("/"))) print('listdir("/"):', sorted(i[0] for i in fs.ilistdir("/")))
print('listdir("/subdir"):', sorted(i[0] for i in vfs.ilistdir("/subdir"))) print('listdir("/subdir"):', sorted(i[0] for i in fs.ilistdir("/subdir")))
print('listdir("."):', sorted(i[0] for i in vfs.ilistdir("."))) print('listdir("."):', sorted(i[0] for i in fs.ilistdir(".")))
try: try:
f = vfs.open("/subdir/file.py", "r") f = fs.open("/subdir/file.py", "r")
print(f.read()) print(f.read())
f.close() f.close()
except Exception as e: except Exception as e:
@ -59,17 +57,17 @@ try:
except Exception as e: except Exception as e:
print(e) print(e)
del sys.path[0] del sys.path[0]
vfs.remove("file.py") fs.remove("file.py")
vfs.rmdir("two") fs.rmdir("two")
vfs.rmdir("/subdir/one") fs.rmdir("/subdir/one")
vfs.chdir("/") fs.chdir("/")
vfs.rmdir("/subdir") fs.rmdir("/subdir")
# done with vfs, restore CWD # done with fs, restore CWD
os.chdir(curdir) os.chdir(curdir)
# some integration tests with a mounted VFS # some integration tests with a mounted VFS
os.mount(os.VfsPosix(temp_dir_abs), "/mnt") vfs.mount(vfs.VfsPosix(temp_dir_abs), "/mnt")
os.mkdir("/mnt/dir") os.mkdir("/mnt/dir")
print('chdir("/mnt/dir"):', os.chdir("/mnt/dir")) print('chdir("/mnt/dir"):', os.chdir("/mnt/dir"))
print("getcwd():", os.getcwd()) print("getcwd():", os.getcwd())
@ -82,7 +80,7 @@ print("getcwd():", os.getcwd())
print('chdir(".."):', os.chdir("..")) print('chdir(".."):', os.chdir(".."))
print("getcwd():", os.getcwd()) print("getcwd():", os.getcwd())
os.rmdir("/mnt/dir") os.rmdir("/mnt/dir")
os.umount("/mnt") vfs.umount("/mnt")
# restore CWD # restore CWD
os.chdir(curdir) os.chdir(curdir)

Wyświetl plik

@ -4,12 +4,9 @@
import sys import sys
try: try:
import io import io, vfs
io.IOBase io.IOBase
import os
os.mount
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -79,7 +76,7 @@ user_files = {
"/usermod5.py": b"print('in usermod5')", "/usermod5.py": b"print('in usermod5')",
"/usermod6.py": b"print('in usermod6')", "/usermod6.py": b"print('in usermod6')",
} }
os.mount(UserFS(user_files), "/userfs") vfs.mount(UserFS(user_files), "/userfs")
# open and read a file # open and read a file
f = open("/userfs/data.txt") f = open("/userfs/data.txt")
@ -110,5 +107,5 @@ UserFile.buffer_size = 1024
import usermod6 import usermod6
# unmount and undo path addition # unmount and undo path addition
os.umount("/userfs") vfs.umount("/userfs")
sys.path.pop() sys.path.pop()

Wyświetl plik

@ -1,11 +1,10 @@
# Test builtin execfile function using VFS. # Test builtin execfile function using VFS.
try: try:
import io, os import io, os, vfs
execfile execfile
io.IOBase io.IOBase
os.mount
except (ImportError, NameError, AttributeError): except (ImportError, NameError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -44,25 +43,21 @@ class Filesystem:
# First umount any existing mount points the target may have. # First umount any existing mount points the target may have.
try: try:
import io, os vfs.umount("/")
os.umount("/")
except OSError: except OSError:
pass pass
for path in os.listdir("/"): for path in os.listdir("/"):
os.umount("/" + path) vfs.umount("/" + path)
# Create and mount the VFS object. # Create and mount the VFS object.
files = { files = {
"/test.py": "print(123)", "/test.py": "print(123)",
} }
fs = Filesystem(files) fs = Filesystem(files)
os.mount(fs, "/test_mnt") vfs.mount(fs, "/test_mnt")
# Test execfile with a file that doesn't exist. # Test execfile with a file that doesn't exist.
try: try:
import io, os
execfile("/test_mnt/noexist.py") execfile("/test_mnt/noexist.py")
except OSError: except OSError:
print("OSError") print("OSError")
@ -77,4 +72,4 @@ except TypeError:
print("TypeError") print("TypeError")
# Unmount the VFS object. # Unmount the VFS object.
os.umount(fs) vfs.umount(fs)

Wyświetl plik

@ -1,10 +1,9 @@
# test importing of invalid .mpy files # test importing of invalid .mpy files
try: try:
import sys, io, os import sys, io, vfs
io.IOBase io.IOBase
os.mount
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -52,7 +51,7 @@ user_files = {
} }
# create and mount a user filesystem # create and mount a user filesystem
os.mount(UserFS(user_files), "/userfs") vfs.mount(UserFS(user_files), "/userfs")
sys.path.append("/userfs") sys.path.append("/userfs")
# import .mpy files from the user filesystem # import .mpy files from the user filesystem
@ -64,5 +63,5 @@ for i in range(len(user_files)):
print(mod, "ValueError", er) print(mod, "ValueError", er)
# unmount and undo path addition # unmount and undo path addition
os.umount("/userfs") vfs.umount("/userfs")
sys.path.pop() sys.path.pop()

Wyświetl plik

@ -1,11 +1,10 @@
# test importing of .mpy files with native code # test importing of .mpy files with native code
try: try:
import sys, io, os import sys, io, vfs
sys.implementation._mpy sys.implementation._mpy
io.IOBase io.IOBase
os.mount
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -110,7 +109,7 @@ user_files = {
# fmt: on # fmt: on
# create and mount a user filesystem # create and mount a user filesystem
os.mount(UserFS(user_files), "/userfs") vfs.mount(UserFS(user_files), "/userfs")
sys.path.append("/userfs") sys.path.append("/userfs")
# import .mpy files from the user filesystem # import .mpy files from the user filesystem
@ -123,5 +122,5 @@ for i in range(len(user_files)):
print(mod, "ValueError", er) print(mod, "ValueError", er)
# unmount and undo path addition # unmount and undo path addition
os.umount("/userfs") vfs.umount("/userfs")
sys.path.pop() sys.path.pop()

Wyświetl plik

@ -1,11 +1,10 @@
# Test that native code loaded from a .mpy file is retained after a GC. # Test that native code loaded from a .mpy file is retained after a GC.
try: try:
import gc, sys, io, os import gc, sys, io, vfs
sys.implementation._mpy sys.implementation._mpy
io.IOBase io.IOBase
os.mount
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -72,7 +71,7 @@ if sys_implementation_mpy not in features0_file_contents:
user_files = {"/features0.mpy": features0_file_contents[sys_implementation_mpy]} user_files = {"/features0.mpy": features0_file_contents[sys_implementation_mpy]}
# Create and mount a user filesystem. # Create and mount a user filesystem.
os.mount(UserFS(user_files), "/userfs") vfs.mount(UserFS(user_files), "/userfs")
sys.path.append("/userfs") sys.path.append("/userfs")
# Import the native function. # Import the native function.
@ -93,5 +92,5 @@ for i in range(1000):
print(factorial(10)) print(factorial(10))
# Unmount and undo path addition. # Unmount and undo path addition.
os.umount("/userfs") vfs.umount("/userfs")
sys.path.pop() sys.path.pop()

Wyświetl plik

@ -1,8 +1,8 @@
# Test performance of importing an .mpy file many times. # Test performance of importing an .mpy file many times.
import sys, io, os import sys, io, vfs
if not (hasattr(io, "IOBase") and hasattr(os, "mount")): if not hasattr(io, "IOBase"):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -57,7 +57,7 @@ class FS:
def mount(): def mount():
os.mount(FS(), "/__remote") vfs.mount(FS(), "/__remote")
sys.path.insert(0, "/__remote") sys.path.insert(0, "/__remote")

Wyświetl plik

@ -2,9 +2,9 @@
# The first import of a module will intern strings that don't already exist, and # The first import of a module will intern strings that don't already exist, and
# this test should be representative of what happens in a real application. # this test should be representative of what happens in a real application.
import io, os, sys import sys, io, vfs
if not (hasattr(io, "IOBase") and hasattr(os, "mount")): if not hasattr(io, "IOBase"):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
@ -112,7 +112,7 @@ class FS:
def mount(): def mount():
os.mount(FS(), "/__remote") vfs.mount(FS(), "/__remote")
sys.path.insert(0, "/__remote") sys.path.insert(0, "/__remote")

Wyświetl plik

@ -3,7 +3,7 @@ os module test for the CC3200 based boards
""" """
from machine import SD from machine import SD
import os import os, vfs
mch = os.uname().machine mch = os.uname().machine
if "LaunchPad" in mch: if "LaunchPad" in mch:
@ -15,7 +15,7 @@ else:
sd = SD(pins=sd_pins) sd = SD(pins=sd_pins)
os.mount(sd, "/sd") vfs.mount(sd, "/sd")
os.mkfs("/sd") os.mkfs("/sd")
os.chdir("/flash") os.chdir("/flash")
print(os.listdir()) print(os.listdir())
@ -88,7 +88,7 @@ print(os.listdir("/"))
os.unmount("/sd") os.unmount("/sd")
print(os.listdir("/")) print(os.listdir("/"))
os.mkfs(sd) os.mkfs(sd)
os.mount(sd, "/sd") vfs.mount(sd, "/sd")
print(os.listdir("/")) print(os.listdir("/"))
os.chdir("/flash") os.chdir("/flash")
@ -104,12 +104,12 @@ sd.init()
print(os.listdir("/sd")) print(os.listdir("/sd"))
try: try:
os.mount(sd, "/sd") vfs.mount(sd, "/sd")
except: except:
print("Exception") print("Exception")
try: try:
os.mount(sd, "/sd2") vfs.mount(sd, "/sd2")
except: except:
print("Exception") print("Exception")
@ -159,6 +159,6 @@ try:
except: except:
print("Exception") print("Exception")
os.mount(sd, "/sd") vfs.mount(sd, "/sd")
print(os.listdir("/")) print(os.listdir("/"))
os.unmount("/sd") os.unmount("/sd")

Wyświetl plik

@ -30,7 +30,7 @@ TEST_MAPPINGS = {
# Code to allow a target MicroPython to import an .mpy from RAM # Code to allow a target MicroPython to import an .mpy from RAM
injected_import_hook_code = """\ injected_import_hook_code = """\
import sys, os, io import sys, io, vfs
class __File(io.IOBase): class __File(io.IOBase):
def __init__(self): def __init__(self):
self.off = 0 self.off = 0
@ -52,7 +52,7 @@ class __FS:
raise OSError(-2) # ENOENT raise OSError(-2) # ENOENT
def open(self, path, mode): def open(self, path, mode):
return __File() return __File()
os.mount(__FS(), '/__remote') vfs.mount(__FS(), '/__remote')
sys.path.insert(0, '/__remote') sys.path.insert(0, '/__remote')
sys.modules['{}'] = __import__('__injected') sys.modules['{}'] = __import__('__injected')
""" """

Wyświetl plik

@ -59,7 +59,7 @@ os.environ["PYTHONIOENCODING"] = "utf-8"
# Code to allow a target MicroPython to import an .mpy from RAM # Code to allow a target MicroPython to import an .mpy from RAM
injected_import_hook_code = """\ injected_import_hook_code = """\
import sys, os, io import sys, os, io, vfs
class __File(io.IOBase): class __File(io.IOBase):
def __init__(self): def __init__(self):
self.off = 0 self.off = 0
@ -83,7 +83,7 @@ class __FS:
raise OSError(-2) # ENOENT raise OSError(-2) # ENOENT
def open(self, path, mode): def open(self, path, mode):
return __File() return __File()
os.mount(__FS(), '/__vfstest') vfs.mount(__FS(), '/__vfstest')
os.chdir('/__vfstest') os.chdir('/__vfstest')
__import__('__injected_test') __import__('__injected_test')
""" """