summaryrefslogtreecommitdiff
path: root/tests/micropython/builtin_execfile.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-06-29 12:48:42 +1000
committerDamien George <damien@micropython.org>2022-06-29 12:48:42 +1000
commit932556d5fc09a196139170fe52436ea8a02020c1 (patch)
treec8f374f0ae601c7226a31c6a1349dbc50b6f4978 /tests/micropython/builtin_execfile.py
parentafa4d0a4b75bc8d21ae95cc76627952d61df93d0 (diff)
tests/micropython: Add test for builtin execfile() function.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'tests/micropython/builtin_execfile.py')
-rw-r--r--tests/micropython/builtin_execfile.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/micropython/builtin_execfile.py b/tests/micropython/builtin_execfile.py
new file mode 100644
index 000000000..8a8ce79f7
--- /dev/null
+++ b/tests/micropython/builtin_execfile.py
@@ -0,0 +1,70 @@
+# Test builtin execfile function using VFS.
+
+try:
+ import uio, uos
+
+ execfile
+ uio.IOBase
+ uos.mount
+except (ImportError, NameError, AttributeError):
+ print("SKIP")
+ raise SystemExit
+
+
+class File(uio.IOBase):
+ def __init__(self, data):
+ self.data = data
+ self.off = 0
+
+ def ioctl(self, request, arg):
+ return 0
+
+ def readinto(self, buf):
+ buf[:] = memoryview(self.data)[self.off : self.off + len(buf)]
+ self.off += len(buf)
+ return len(buf)
+
+
+class Filesystem:
+ def __init__(self, files):
+ self.files = files
+
+ def mount(self, readonly, mkfs):
+ print("mount", readonly, mkfs)
+
+ def umount(self):
+ print("umount")
+
+ def open(self, file, mode):
+ print("open", file, mode)
+ if file not in self.files:
+ raise OSError(2) # ENOENT
+ return File(self.files[file])
+
+
+# First umount any existing mount points the target may have.
+try:
+ uos.umount("/")
+except OSError:
+ pass
+for path in uos.listdir("/"):
+ uos.umount("/" + path)
+
+# Create and mount the VFS object.
+files = {
+ "/test.py": "print(123)",
+}
+fs = Filesystem(files)
+uos.mount(fs, "/test_mnt")
+
+# Test execfile with a file that doesn't exist.
+try:
+ execfile("/test_mnt/noexist.py")
+except OSError:
+ print("OSError")
+
+# Test execfile with a file that does exist.
+execfile("/test_mnt/test.py")
+
+# Unmount the VFS object.
+uos.umount(fs)