-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
extmod/vfs_posix: Fix relative paths on non-root VFS #12137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3ba6f9
extmod/vfs_posix: Fix relative root path.
cwalther 5f7065f
extmod/vfs_posix: Fix accidentally passing tests.
cwalther 0c4fb16
extmod/vfs_posix: Fix relative paths on non-root VFS.
cwalther be28829
extmod/vfs_posix: Fix getcwd() on non-root VFS.
cwalther 7be16e0
extmod/vfs_posix: Additional tests for coverage of error cases.
cwalther File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Test for VfsPosix error conditions | ||
|
||
try: | ||
import os | ||
import sys | ||
|
||
os.VfsPosix | ||
except (ImportError, AttributeError): | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
if sys.platform == "win32": | ||
# Windows doesn't let you delete the current directory, so this cannot be | ||
# tested. | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
# We need an empty directory for testing. | ||
# Skip the test if it already exists. | ||
temp_dir = "vfs_posix_enoent_test_dir" | ||
try: | ||
os.stat(temp_dir) | ||
print("SKIP") | ||
raise SystemExit | ||
except OSError: | ||
pass | ||
|
||
curdir = os.getcwd() | ||
os.mkdir(temp_dir) | ||
os.chdir(temp_dir) | ||
os.rmdir(curdir + "/" + temp_dir) | ||
try: | ||
print("getcwd():", os.getcwd()) | ||
except OSError as e: | ||
# expecting ENOENT = 2 | ||
print("getcwd():", repr(e)) | ||
|
||
try: | ||
print("VfsPosix():", os.VfsPosix("something")) | ||
except OSError as e: | ||
# expecting ENOENT = 2 | ||
print("VfsPosix():", repr(e)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
getcwd(): OSError(2,) | ||
VfsPosix(): OSError(2,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Test for VfsPosix with relative paths | ||
|
||
try: | ||
import os | ||
|
||
os.VfsPosix | ||
except (ImportError, AttributeError): | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
# We need a directory for testing that doesn't already exist. | ||
# Skip the test if it does exist. | ||
temp_dir = "vfs_posix_paths_test_dir" | ||
try: | ||
import os | ||
|
||
os.stat(temp_dir) | ||
print("SKIP") | ||
raise SystemExit | ||
except OSError: | ||
pass | ||
|
||
curdir = os.getcwd() | ||
os.mkdir(temp_dir) | ||
|
||
# construct new VfsPosix with absolute path argument | ||
temp_dir_abs = os.getcwd() + os.sep + temp_dir | ||
vfs = os.VfsPosix(temp_dir_abs) | ||
# 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_abs) | ||
vfs.mkdir("subdir") | ||
vfs.mkdir("subdir/one") | ||
print('listdir("/"):', sorted(i[0] for i in vfs.ilistdir("/"))) | ||
print('listdir("."):', sorted(i[0] for i in vfs.ilistdir("."))) | ||
print('getcwd() in {"", "/"}:', vfs.getcwd() in {"", "/"}) | ||
print('chdir("subdir"):', vfs.chdir("subdir")) | ||
print("getcwd():", vfs.getcwd()) | ||
print('mkdir("two"):', vfs.mkdir("two")) | ||
f = vfs.open("file.py", "w") | ||
f.write("print('hello')") | ||
f.close() | ||
print('listdir("/"):', sorted(i[0] for i in vfs.ilistdir("/"))) | ||
print('listdir("/subdir"):', sorted(i[0] for i in vfs.ilistdir("/subdir"))) | ||
print('listdir("."):', sorted(i[0] for i in vfs.ilistdir("."))) | ||
try: | ||
f = vfs.open("/subdir/file.py", "r") | ||
print(f.read()) | ||
f.close() | ||
except Exception as e: | ||
print(e) | ||
import sys | ||
|
||
sys.path.insert(0, "") | ||
try: | ||
import file | ||
|
||
print(file) | ||
except Exception as e: | ||
print(e) | ||
del sys.path[0] | ||
vfs.remove("file.py") | ||
vfs.rmdir("two") | ||
vfs.rmdir("/subdir/one") | ||
vfs.chdir("/") | ||
vfs.rmdir("/subdir") | ||
|
||
# done with vfs, restore CWD | ||
os.chdir(curdir) | ||
|
||
# some integration tests with a mounted VFS | ||
os.mount(os.VfsPosix(temp_dir_abs), "/mnt") | ||
os.mkdir("/mnt/dir") | ||
print('chdir("/mnt/dir"):', os.chdir("/mnt/dir")) | ||
print("getcwd():", os.getcwd()) | ||
print('chdir("/mnt"):', os.chdir("/mnt")) | ||
print("getcwd():", os.getcwd()) | ||
print('chdir("/"):', os.chdir("/")) | ||
print("getcwd():", os.getcwd()) | ||
print('chdir("/mnt/dir"):', os.chdir("/mnt/dir")) | ||
print("getcwd():", os.getcwd()) | ||
print('chdir(".."):', os.chdir("..")) | ||
print("getcwd():", os.getcwd()) | ||
os.rmdir("/mnt/dir") | ||
os.umount("/mnt") | ||
|
||
# restore CWD | ||
os.chdir(curdir) | ||
|
||
# rmdir | ||
os.rmdir(temp_dir) | ||
print(temp_dir in os.listdir()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
listdir("/"): ['subdir'] | ||
listdir("."): ['subdir'] | ||
getcwd() in {"", "/"}: True | ||
chdir("subdir"): None | ||
getcwd(): /subdir | ||
mkdir("two"): None | ||
listdir("/"): ['subdir'] | ||
listdir("/subdir"): ['file.py', 'one', 'two'] | ||
listdir("."): ['file.py', 'one', 'two'] | ||
print('hello') | ||
hello | ||
<module 'file' from 'file.py'> | ||
chdir("/mnt/dir"): None | ||
getcwd(): /mnt/dir | ||
chdir("/mnt"): None | ||
getcwd(): /mnt | ||
chdir("/"): None | ||
getcwd(): / | ||
chdir("/mnt/dir"): None | ||
getcwd(): /mnt/dir | ||
chdir(".."): None | ||
getcwd(): /mnt | ||
False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.