2020-11-28 01:21:40 +00:00
|
|
|
# Test for VfsPosix
|
|
|
|
|
|
|
|
try:
|
|
|
|
import uos
|
|
|
|
|
|
|
|
uos.VfsPosix
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2021-02-11 12:24:39 +00:00
|
|
|
# We need a directory for testing that doesn't already exist.
|
2021-02-11 11:53:36 +00:00
|
|
|
# Skip the test if it does exist.
|
2021-02-11 12:24:39 +00:00
|
|
|
temp_dir = "micropy_test_dir"
|
2021-02-11 11:53:36 +00:00
|
|
|
try:
|
2021-02-11 12:24:39 +00:00
|
|
|
uos.stat(temp_dir)
|
2021-02-11 11:53:36 +00:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
except OSError:
|
|
|
|
pass
|
2020-11-28 01:21:40 +00:00
|
|
|
|
|
|
|
# getcwd and chdir
|
|
|
|
curdir = uos.getcwd()
|
|
|
|
uos.chdir("/")
|
|
|
|
print(uos.getcwd())
|
|
|
|
uos.chdir(curdir)
|
|
|
|
print(uos.getcwd() == curdir)
|
|
|
|
|
|
|
|
# stat
|
|
|
|
print(type(uos.stat("/")))
|
|
|
|
|
|
|
|
# listdir and ilistdir
|
|
|
|
print(type(uos.listdir("/")))
|
2021-02-11 11:53:36 +00:00
|
|
|
|
2021-02-11 12:24:39 +00:00
|
|
|
# mkdir
|
|
|
|
uos.mkdir(temp_dir)
|
|
|
|
|
2021-02-11 11:53:36 +00:00
|
|
|
# file create
|
2021-02-11 12:24:39 +00:00
|
|
|
f = open(temp_dir + "/test", "w")
|
2021-02-11 11:53:36 +00:00
|
|
|
f.write("hello")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# close on a closed file should succeed
|
|
|
|
f.close()
|
|
|
|
|
2021-02-11 12:24:39 +00:00
|
|
|
# construct a file object using the type constructor, with a raw fileno
|
|
|
|
f = type(f)(2)
|
|
|
|
print(f)
|
|
|
|
|
2021-02-11 11:53:36 +00:00
|
|
|
# file read
|
2021-02-11 12:24:39 +00:00
|
|
|
f = open(temp_dir + "/test", "r")
|
2021-02-11 11:53:36 +00:00
|
|
|
print(f.read())
|
|
|
|
f.close()
|
|
|
|
|
2021-02-11 12:24:39 +00:00
|
|
|
# rename
|
|
|
|
uos.rename(temp_dir + "/test", temp_dir + "/test2")
|
|
|
|
print(uos.listdir(temp_dir))
|
|
|
|
|
|
|
|
# construct new VfsPosix with path argument
|
|
|
|
vfs = uos.VfsPosix(temp_dir)
|
|
|
|
print(list(i[0] for i in vfs.ilistdir(".")))
|
|
|
|
|
2022-03-09 13:41:03 +00:00
|
|
|
# stat, statvfs (statvfs may not exist)
|
2021-02-11 12:24:39 +00:00
|
|
|
print(type(vfs.stat(".")))
|
2022-03-09 13:41:03 +00:00
|
|
|
if hasattr(vfs, "statvfs"):
|
|
|
|
assert type(vfs.statvfs(".")) is tuple
|
2021-02-11 12:24:39 +00:00
|
|
|
|
|
|
|
# check types of ilistdir with str/bytes arguments
|
|
|
|
print(type(list(vfs.ilistdir("."))[0][0]))
|
|
|
|
print(type(list(vfs.ilistdir(b"."))[0][0]))
|
|
|
|
|
2021-02-11 11:53:36 +00:00
|
|
|
# remove
|
2021-02-11 12:24:39 +00:00
|
|
|
uos.remove(temp_dir + "/test2")
|
|
|
|
print(uos.listdir(temp_dir))
|
|
|
|
|
|
|
|
# remove with error
|
|
|
|
try:
|
|
|
|
uos.remove(temp_dir + "/test2")
|
|
|
|
except OSError:
|
|
|
|
print("remove OSError")
|
|
|
|
|
|
|
|
# rmdir
|
|
|
|
uos.rmdir(temp_dir)
|
|
|
|
print(temp_dir in uos.listdir())
|
|
|
|
|
|
|
|
# rmdir with error
|
|
|
|
try:
|
|
|
|
uos.rmdir(temp_dir)
|
|
|
|
except OSError:
|
|
|
|
print("rmdir OSError")
|