summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/vfs_basic.py
blob: 83c83fd227768a6e885524150e71010aaccb6c2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# test VFS functionality without any particular filesystem type

try:
    try:
        import uos_vfs as uos
        open = uos.vfs_open
    except ImportError:
        import uos
    uos.mount
except (ImportError, AttributeError):
    print("SKIP")
    import sys
    sys.exit()


class Filesystem:
    def __init__(self, id):
        self.id = id
    def mount(self, readonly, mkfs):
        print(self.id, 'mount', readonly, mkfs)
    def umount(self):
        print(self.id, 'umount')
    def listdir(self, dir):
        print(self.id, 'listdir', dir)
        return ['a%d' % self.id]
    def chdir(self, dir):
        print(self.id, 'chdir', dir)
    def getcwd(self):
        print(self.id, 'getcwd')
        return 'dir%d' % self.id
    def mkdir(self, path):
        print(self.id, 'mkdir', path)
    def remove(self, path):
        print(self.id, 'remove', path)
    def rename(self, old_path, new_path):
        print(self.id, 'rename', old_path, new_path)
    def rmdir(self, path):
        print(self.id, 'rmdir', path)
    def stat(self, path):
        print(self.id, 'stat', path)
        return (self.id,)
    def statvfs(self, path):
        print(self.id, 'statvfs', path)
        return (self.id,)
    def open(self, file, mode):
        print(self.id, 'open', file, mode)


# first we umount any existing mount points the target may have
for path in uos.listdir('/'):
    uos.umount('/' + path)

# stat root dir
print(uos.stat('/'))

# getcwd when in root dir
print(uos.getcwd())

# basic mounting and listdir
uos.mount(Filesystem(1), '/test_mnt')
print(uos.listdir())

# referencing the mount point in different ways
print(uos.listdir('test_mnt'))
print(uos.listdir('/test_mnt'))

# mounting another filesystem
uos.mount(Filesystem(2), '/test_mnt2', readonly=True)
print(uos.listdir())
print(uos.listdir('/test_mnt2'))

# mounting over an existing mount point
try:
    uos.mount(Filesystem(3), '/test_mnt2')
except OSError:
    print('OSError')

# mkdir of a mount point
try:
    uos.mkdir('/test_mnt')
except OSError:
    print('OSError')

# rename across a filesystem
try:
    uos.rename('/test_mnt/a', '/test_mnt2/b')
except OSError:
    print('OSError')

# delegating to mounted filesystem
uos.chdir('test_mnt')
print(uos.listdir())
print(uos.getcwd())
uos.mkdir('test_dir')
uos.remove('test_file')
uos.rename('test_file', 'test_file2')
uos.rmdir('test_dir')
print(uos.stat('test_file'))
print(uos.statvfs('/test_mnt'))
open('test_file')
open('test_file', 'wb')

# umount
uos.umount('/test_mnt')
uos.umount('/test_mnt2')

# umount a non-existent mount point
try:
    uos.umount('/test_mnt')
except OSError:
    print('OSError')