2020-11-27 20:21:40 -05:00
|
|
|
# Test for VfsPosix
|
|
|
|
|
|
|
|
try:
|
2022-09-12 08:45:39 -04:00
|
|
|
import gc
|
2022-08-18 02:57:45 -04:00
|
|
|
import os
|
2020-11-27 20:21:40 -05:00
|
|
|
|
2022-08-18 02:57:45 -04:00
|
|
|
os.VfsPosix
|
2020-11-27 20:21:40 -05:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2021-02-11 07:24:39 -05:00
|
|
|
# We need a directory for testing that doesn't already exist.
|
2021-02-11 06:53:36 -05:00
|
|
|
# Skip the test if it does exist.
|
2021-02-11 07:24:39 -05:00
|
|
|
temp_dir = "micropy_test_dir"
|
2021-02-11 06:53:36 -05:00
|
|
|
try:
|
2022-08-18 02:57:45 -04:00
|
|
|
import os
|
|
|
|
|
|
|
|
os.stat(temp_dir)
|
2021-02-11 06:53:36 -05:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
except OSError:
|
|
|
|
pass
|
2020-11-27 20:21:40 -05:00
|
|
|
|
|
|
|
# getcwd and chdir
|
2022-08-18 02:57:45 -04:00
|
|
|
curdir = os.getcwd()
|
|
|
|
os.chdir("/")
|
|
|
|
print(os.getcwd())
|
|
|
|
os.chdir(curdir)
|
|
|
|
print(os.getcwd() == curdir)
|
2020-11-27 20:21:40 -05:00
|
|
|
|
|
|
|
# stat
|
2022-08-18 02:57:45 -04:00
|
|
|
print(type(os.stat("/")))
|
2020-11-27 20:21:40 -05:00
|
|
|
|
|
|
|
# listdir and ilistdir
|
2022-08-18 02:57:45 -04:00
|
|
|
print(type(os.listdir("/")))
|
2021-02-11 06:53:36 -05:00
|
|
|
|
2021-02-11 07:24:39 -05:00
|
|
|
# mkdir
|
2022-08-18 02:57:45 -04:00
|
|
|
os.mkdir(temp_dir)
|
2021-02-11 07:24:39 -05:00
|
|
|
|
2021-02-11 06:53:36 -05:00
|
|
|
# file create
|
2021-02-11 07:24:39 -05:00
|
|
|
f = open(temp_dir + "/test", "w")
|
2021-02-11 06:53:36 -05:00
|
|
|
f.write("hello")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# close on a closed file should succeed
|
|
|
|
f.close()
|
|
|
|
|
2022-07-21 23:53:13 -04:00
|
|
|
# construct a file object with a raw fileno
|
|
|
|
f = open(2)
|
2021-02-11 07:24:39 -05:00
|
|
|
print(f)
|
|
|
|
|
2021-02-11 06:53:36 -05:00
|
|
|
# file read
|
2021-02-11 07:24:39 -05:00
|
|
|
f = open(temp_dir + "/test", "r")
|
2021-02-11 06:53:36 -05:00
|
|
|
print(f.read())
|
|
|
|
f.close()
|
|
|
|
|
2022-09-12 08:45:39 -04:00
|
|
|
# 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]:
|
2022-08-18 02:57:45 -04:00
|
|
|
os.remove(n)
|
2022-09-12 08:45:39 -04:00
|
|
|
|
2021-02-11 07:24:39 -05:00
|
|
|
# rename
|
2022-08-18 02:57:45 -04:00
|
|
|
os.rename(temp_dir + "/test", temp_dir + "/test2")
|
|
|
|
print(os.listdir(temp_dir))
|
2021-02-11 07:24:39 -05:00
|
|
|
|
|
|
|
# construct new VfsPosix with path argument
|
2022-08-18 02:57:45 -04:00
|
|
|
vfs = os.VfsPosix(temp_dir)
|
2021-02-11 07:24:39 -05:00
|
|
|
print(list(i[0] for i in vfs.ilistdir(".")))
|
|
|
|
|
2022-03-09 08:41:03 -05:00
|
|
|
# stat, statvfs (statvfs may not exist)
|
2021-02-11 07:24:39 -05:00
|
|
|
print(type(vfs.stat(".")))
|
2022-03-09 08:41:03 -05:00
|
|
|
if hasattr(vfs, "statvfs"):
|
|
|
|
assert type(vfs.statvfs(".")) is tuple
|
2021-02-11 07:24:39 -05: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 06:53:36 -05:00
|
|
|
# remove
|
2022-08-18 02:57:45 -04:00
|
|
|
os.remove(temp_dir + "/test2")
|
|
|
|
print(os.listdir(temp_dir))
|
2021-02-11 07:24:39 -05:00
|
|
|
|
|
|
|
# remove with error
|
|
|
|
try:
|
2022-08-18 02:57:45 -04:00
|
|
|
import os
|
|
|
|
|
|
|
|
os.remove(temp_dir + "/test2")
|
2021-02-11 07:24:39 -05:00
|
|
|
except OSError:
|
|
|
|
print("remove OSError")
|
|
|
|
|
|
|
|
# rmdir
|
2022-08-18 02:57:45 -04:00
|
|
|
os.rmdir(temp_dir)
|
|
|
|
print(temp_dir in os.listdir())
|
2021-02-11 07:24:39 -05:00
|
|
|
|
|
|
|
# rmdir with error
|
|
|
|
try:
|
2022-08-18 02:57:45 -04:00
|
|
|
import os
|
|
|
|
|
|
|
|
os.rmdir(temp_dir)
|
2021-02-11 07:24:39 -05:00
|
|
|
except OSError:
|
|
|
|
print("rmdir OSError")
|