lv_micropython/tests/extmod/vfs_posix.py
Christian Walther e3ba6f952b extmod/vfs_posix: Fix relative root path.
A VfsPosix created with a relative root path would get confused when
chdir() was called on it and become unable to properly resolve absolute
paths, because changing directories effectively shifted its root. The
simplest fix for that would be to say "don't do that", but since the
unit tests themselves do it, fix it by making a relative path absolute
before storing it.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
2023-10-19 16:21:08 +02:00

138 lines
2.8 KiB
Python

# Test for VfsPosix
try:
import gc
import os
os.VfsPosix
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit
# We need a directory for testing that doesn't already exist.
# Skip the test if it does exist.
temp_dir = "micropy_test_dir"
try:
import os
os.stat(temp_dir)
print("SKIP")
raise SystemExit
except OSError:
pass
# getcwd and chdir
curdir = os.getcwd()
os.chdir("/")
print(os.getcwd())
os.chdir(curdir)
print(os.getcwd() == curdir)
# stat
print(type(os.stat("/")))
# listdir and ilistdir
print(type(os.listdir("/")))
# mkdir
os.mkdir(temp_dir)
# file create
f = open(temp_dir + "/test", "w")
f.write("hello")
f.close()
# close on a closed file should succeed
f.close()
# construct a file object with a raw fileno
f = open(2)
print(f)
# file read
f = open(temp_dir + "/test", "r")
print(f.read())
f.close()
# file finaliser, also see vfs_fat_finaliser.py
names = [temp_dir + "/x%d" % i for i in range(4)]
basefd = temp_dir + "/nextfd1"
nextfd = temp_dir + "/nextfd2"
with open(basefd, "w") as f:
base_file_no = f.fileno()
for i in range(1024): # move GC head forwards by allocating a lot of single blocks
[]
def write_files_without_closing():
for n in names:
open(n, "w").write(n)
sorted(list(range(128)), key=lambda x: x) # use up Python and C stack so f is really gone
write_files_without_closing()
gc.collect()
with open(nextfd, "w") as f:
next_file_no = f.fileno()
print("next_file_no <= base_file_no", next_file_no <= base_file_no)
for n in names + [basefd, nextfd]:
os.remove(n)
# rename
os.rename(temp_dir + "/test", temp_dir + "/test2")
print(os.listdir(temp_dir))
# construct new VfsPosix with path argument
vfs = os.VfsPosix(temp_dir)
print(list(i[0] for i in vfs.ilistdir(".")))
# stat, statvfs (statvfs may not exist)
print(type(vfs.stat(".")))
if hasattr(vfs, "statvfs"):
assert type(vfs.statvfs(".")) is tuple
# check types of ilistdir with str/bytes arguments
print(type(list(vfs.ilistdir("."))[0][0]))
print(type(list(vfs.ilistdir(b"."))[0][0]))
# chdir should not affect absolute paths (regression test)
vfs.mkdir("/subdir")
vfs.mkdir("/subdir/micropy_test_dir")
with vfs.open("/subdir/micropy_test_dir/test2", "w") as f:
f.write("wrong")
vfs.chdir("/subdir")
with vfs.open("/test2", "r") as f:
print(f.read())
os.chdir(curdir)
vfs.remove("/subdir/micropy_test_dir/test2")
vfs.rmdir("/subdir/micropy_test_dir")
vfs.rmdir("/subdir")
# remove
os.remove(temp_dir + "/test2")
print(os.listdir(temp_dir))
# remove with error
try:
import os
os.remove(temp_dir + "/test2")
except OSError:
print("remove OSError")
# rmdir
os.rmdir(temp_dir)
print(temp_dir in os.listdir())
# rmdir with error
try:
import os
os.rmdir(temp_dir)
except OSError:
print("rmdir OSError")