Skip to content

Commit

Permalink
extmod/vfs_posix: Fix accidentally passing tests.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cwalther committed Oct 19, 2023
1 parent e3ba6f9 commit 5f7065f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/extmod/vfs_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def write_files_without_closing():

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

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

# done with vfs, restore CWD
os.chdir(curdir)

# remove
os.remove(temp_dir + "/test2")
print(os.listdir(temp_dir))
Expand Down
9 changes: 9 additions & 0 deletions tests/extmod/vfs_posix_ilistdir_del.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@


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

# Done with vfs, restore CWD.
os.chdir(curdir)


# We need an empty directory for testing.
# Skip the test if it already exists.
Expand Down
7 changes: 7 additions & 0 deletions tests/extmod/vfs_posix_ilistdir_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@


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

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

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

print(dirs)

# Done with vfs, restore CWD.
os.chdir(curdir)


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

0 comments on commit 5f7065f

Please sign in to comment.