Skip to content

Commit 5f7065f

Browse files
committed
extmod/vfs_posix: Fix accidentally passing tests.
These tests test an unrealistic situation and only pass by accident due to a bug. The upcoming fix for the bug would make them fail. The unrealistic situation is that VfsPosix methods are called with relative paths while the current working directory is somewhere outside of the root of the VFS. In the intended use of VFS objects via os.mount() (as opposed to calling methods directly as the tests do), this never happens, as mp_vfs_lookup_path() directs incoming calls to the VFS that contains the CWD. Make the testing situation realistic by changing the working directory to the root of the VFS before calling methods on it, as the subsequent relative path accesses expect. Thanks to the preceding commit, the tests still pass, but still for the wrong reason. The following commit "Fix relative paths on non-root VFS" will make them pass for the correct reason. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent e3ba6f9 commit 5f7065f

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

tests/extmod/vfs_posix.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def write_files_without_closing():
8888

8989
# construct new VfsPosix with path argument
9090
vfs = os.VfsPosix(temp_dir)
91+
# when VfsPosix is used the intended way via os.mount(), it can only be called
92+
# with relative paths when the CWD is inside or at its root, so simulate that
93+
os.chdir(temp_dir)
9194
print(list(i[0] for i in vfs.ilistdir(".")))
9295

9396
# stat, statvfs (statvfs may not exist)
@@ -112,6 +115,9 @@ def write_files_without_closing():
112115
vfs.rmdir("/subdir/micropy_test_dir")
113116
vfs.rmdir("/subdir")
114117

118+
# done with vfs, restore CWD
119+
os.chdir(curdir)
120+
115121
# remove
116122
os.remove(temp_dir + "/test2")
117123
print(os.listdir(temp_dir))

tests/extmod/vfs_posix_ilistdir_del.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111

1212

1313
def test(testdir):
14+
curdir = os.getcwd()
1415
vfs = os.VfsPosix(testdir)
16+
# When VfsPosix is used the intended way via os.mount(), it can only be called
17+
# with relative paths when the CWD is inside or at its root, so simulate that.
18+
# (Although perhaps calling with a relative path was an oversight in this case
19+
# and the respective line below was meant to read `vfs.rmdir("/" + dname)`.)
20+
os.chdir(testdir)
1521
vfs.mkdir("/test_d1")
1622
vfs.mkdir("/test_d2")
1723
vfs.mkdir("/test_d3")
@@ -48,6 +54,9 @@ def test(testdir):
4854
vfs.open("/test", "w").close()
4955
vfs.remove("/test")
5056

57+
# Done with vfs, restore CWD.
58+
os.chdir(curdir)
59+
5160

5261
# We need an empty directory for testing.
5362
# Skip the test if it already exists.

tests/extmod/vfs_posix_ilistdir_filter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111

1212
def test(testdir):
13+
curdir = os.getcwd()
1314
vfs = os.VfsPosix(testdir)
15+
# When VfsPosix is used the intended way via os.mount(), it can only be called
16+
# with relative paths when the CWD is inside or at its root, so simulate that.
17+
os.chdir(testdir)
1418

1519
dirs = [".a", "..a", "...a", "a.b", "a..b"]
1620

@@ -24,6 +28,9 @@ def test(testdir):
2428

2529
print(dirs)
2630

31+
# Done with vfs, restore CWD.
32+
os.chdir(curdir)
33+
2734

2835
# We need an empty directory for testing.
2936
# Skip the test if it already exists.

0 commit comments

Comments
 (0)