8000 extmod/vfs_posix: Add more tests for relative paths, currently failing. · micropython/micropython@0ccc82c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ccc82c

Browse files
committed
extmod/vfs_posix: Add more tests for relative paths, currently failing.
VfsPosix objects with a non-empty root path do not handle relative paths correctly, to be fixed in the following commits. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent 7bc4fff commit 0ccc82c

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

tests/extmod/vfs_posix.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,69 @@ def write_files_without_closing():
130130
except OSError:
131131
print("remove OSError")
132132

133+
# construct new VfsPosix with absolute path argument
134+
temp_dir_abs = os.getcwd() + os.sep + temp_dir
135+
vfs = os.VfsPosix(temp_dir_abs)
136+
# when VfsPosix is used the intended way via os.mount(), it can only be called
137+
# with relative paths when the CWD is inside or at its root, so simulate that
138+
os.chdir(temp_dir_abs)
139+
vfs.mkdir("subdir")
140+
vfs.mkdir("subdir/one")
141+
print('listdir("/"):', sorted(i[0] for i in vfs.ilistdir("/")))
142+
print('listdir("."):', sorted(i[0] for i in vfs.ilistdir(".")))
143+
print('getcwd() in {"", "/"}:', vfs.getcwd() in {"", "/"})
144+
print('chdir("subdir"):', vfs.chdir("subdir"))
145+
print("getcwd():", vfs.getcwd())
146+
print('mkdir("two"):', vfs.mkdir("two"))
147+
f = vfs.open("file.py", "w")
148+
f.write("print('hello')")
149+
f.close()
150+
print('listdir("/"):', sorted(i[0] for i in vfs.ilistdir("/")))
151+
print('listdir("/subdir"):', sorted(i[0] for i in vfs.ilistdir("/subdir")))
152+
print('listdir("."):', sorted(i[0] for i in vfs.ilistdir(".")))
153+
try:
154+
f = vfs.open("/subdir/file.py", "r")
155+
print(f.read())
156+
f.close()
157+
except Exception as e:
158+
print(e)
159+
import sys
160+
161+
sys.path.insert(0, "")
162+
try:
163+
import file
164+
165+
print(file)
166+
except Exception as e:
167+
print(e)
168+
del sys.path[0]
169+
vfs.remove("file.py")
170+
vfs.rmdir("two")
171+
vfs.rmdir("/subdir/one")
172+
vfs.rmdir("/subdir")
173+
174+
# done with vfs, restore CWD
175+
os.chdir(curdir)
176+
177+
# some integration tests with a mounted VFS
178+
os.mount(os.VfsPosix(temp_dir_abs), "/mnt")
179+
os.mkdir("/mnt/dir")
180+
print('chdir("/mnt/dir"):', os.chdir("/mnt/dir"))
181+
print("getcwd():", os.getcwd())
182+
print('chdir("/mnt"):', os.chdir("/mnt"))
183+
print("getcwd():", os.getcwd())
184+
print('chdir("/"):', os.chdir("/"))
185+
print("getcwd():", os.getcwd())
186+
print('chdir("/mnt/dir"):', os.chdir("/mnt/dir"))
187+
print("getcwd():", os.getcwd())
188+
print('chdir(".."):', os.chdir(".."))
189+
print("getcwd():", os.getcwd())
190+
os.rmdir("/mnt/dir")
191+
os.umount("/mnt")
192+
193+
# restore CWD
194+
os.chdir(curdir)
195+
133196
# rmdir
134197
os.rmdir(temp_dir)
135198
print(temp_dir in os.listdir())

tests/extmod/vfs_posix.py.exp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,27 @@ next_file_no <= base_file_no True
1313
hello
1414
[]
1515
remove OSError
16+
listdir("/"): ['subdir']
17+
listdir("."): ['subdir']
18+
getcwd() in {"", "/"}: True
19+
chdir("subdir"): None
20+
getcwd(): /subdir
21+
mkdir("two"): None
22+
listdir("/"): ['subdir']
23+
listdir("/subdir"): ['file.py', 'one', 'two']
24+
listdir("."): ['file.py', 'one', 'two']
25+
print('hello')
26+
hello
27+
<module 'file' from 'file.py'>
28+
chdir("/mnt/dir"): None
29+
getcwd(): /mnt/dir
30+
chdir("/mnt"): None
31+
getcwd(): /mnt
32+
chdir("/"): None
33+
getcwd(): /
34+
chdir("/mnt/dir"): None
35+
getcwd(): /mnt/dir
36+
chdir(".."): None
37+
getcwd(): /mnt
1638
False
1739
rmdir OSError

0 commit comments

Comments
 (0)
0