Skip to content

Commit 0e6ef40

Browse files
committed
tests/extmod: Add tests for verifying FAT and littlefs mtime values.
Verifies mtime timestamps on files match the value returned by time.time(). Also update vfs_fat_ramdisk.py so it doesn't check FAT timestamp of the root, because that may change across runs/ports. Signed-off-by: Damien George <[email protected]>
1 parent a909c21 commit 0e6ef40

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed

tests/extmod/vfs_fat_mtime.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Test for VfsFat using a RAM device, mtime feature
2+
3+
try:
4+
import utime, uos
5+
6+
utime.time
7+
utime.sleep
8+
uos.VfsFat
9+
except (ImportError, AttributeError):
10+
print("SKIP")
11+
raise SystemExit
12+
13+
14+
class RAMBlockDevice:
15+
ERASE_BLOCK_SIZE = 512
16+
17+
def __init__(self, blocks):
18+
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
19+
20+
def readblocks(self, block, buf):
21+
addr = block * self.ERASE_BLOCK_SIZE
22+
for i in range(len(buf)):
23+
buf[i] = self.data[addr + i]
24+
25+
def writeblocks(self, block, buf):
26+
addr = block * self.ERASE_BLOCK_SIZE
27+
for i in range(len(buf)):
28+
self.data[addr + i] = buf[i]
29+
30+
def ioctl(self, op, arg):
31+
if op == 4: # block count
32+
return len(self.data) // self.ERASE_BLOCK_SIZE
33+
if op == 5: # block size
34+
return self.ERASE_BLOCK_SIZE
35+
36+
37+
def test(bdev, vfs_class):
38+
print("test", vfs_class)
39+
40+
# Initial format of block device.
41+
vfs_class.mkfs(bdev)
42+
43+
# construction
44+
vfs = vfs_class(bdev)
45+
46+
# Create an empty file, should have a timestamp.
47+
current_time = int(utime.time())
48+
vfs.open("test1", "wt").close()
49+
50+
# Wait 2 seconds so mtime will increase (FAT has 2 second resolution).
51+
utime.sleep(2)
52+
53+
# Create another empty file, should have a timestamp.
54+
vfs.open("test2", "wt").close()
55+
56+
# Stat the files and check mtime is non-zero.
57+
stat1 = vfs.stat("test1")
58+
stat2 = vfs.stat("test2")
59+
print(stat1[8] != 0, stat2[8] != 0)
60+
61+
# Check that test1 has mtime which matches time.time() at point of creation.
62+
# TODO this currently fails on the unix port because FAT stores timestamps
63+
# in localtime and stat() is UTC based.
64+
# print(current_time - 1 <= stat1[8] <= current_time + 1)
65+
66+
# Check that test1 is older than test2.
67+
print(stat1[8] < stat2[8])
68+
69+
# Unmount.
70+
vfs.umount()
71+
72+
73+
bdev = RAMBlockDevice(50)
74+
test(bdev, uos.VfsFat)

tests/extmod/vfs_fat_mtime.py.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test <class 'VfsFat'>
2+
True True
3+
True

tests/extmod/vfs_fat_ramdisk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def ioctl(self, op, arg):
6363
f.write("hello!")
6464
print(list(vfs.ilistdir()))
6565

66-
print("stat root:", vfs.stat("/"))
66+
print("stat root:", vfs.stat("/")[:-3]) # timestamps differ across runs
6767
print("stat file:", vfs.stat("foo_file.txt")[:-3]) # timestamps differ across runs
6868

6969
print(b"FOO_FILETXT" in bdev.data)

tests/extmod/vfs_fat_ramdisk.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
44
getcwd: /
55
True
66
[('foo_file.txt', 32768, 0, 6)]
7-
stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
7+
stat root: (16384, 0, 0, 0, 0, 0, 0)
88
stat file: (32768, 0, 0, 0, 0, 0, 6)
99
True
1010
True

tests/extmod/vfs_lfs_mtime.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
try:
44
import utime, uos
55

6+
utime.time
67
utime.sleep
78
uos.VfsLfs2
89
except (ImportError, AttributeError):
@@ -46,6 +47,7 @@ def test(bdev, vfs_class):
4647
vfs = vfs_class(bdev, mtime=True)
4748

4849
# Create an empty file, should have a timestamp.
50+
current_time = int(utime.time())
4951
vfs.open("test1", "wt").close()
5052

5153
# Wait 1 second so mtime will increase by at least 1.
@@ -54,10 +56,15 @@ def test(bdev, vfs_class):
5456
# Create another empty file, should have a timestamp.
5557
vfs.open("test2", "wt").close()
5658

57-
# Stat the files and check that test1 is older than test2.
59+
# Stat the files and check mtime is non-zero.
5860
stat1 = vfs.stat("test1")
5961
stat2 = vfs.stat("test2")
6062
print(stat1[8] != 0, stat2[8] != 0)
63+
64+
# Check that test1 has mtime which matches time.time() at point of creation.
65+
print(current_time <= stat1[8] <= current_time + 1)
66+
67+
# Check that test1 is older than test2.
6168
print(stat1[8] < stat2[8])
6269

6370
# Wait 1 second so mtime will increase by at least 1.

tests/extmod/vfs_lfs_mtime.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ True True
44
True
55
True
66
True
7+
True
78
mtime=False
89
True
910
True

0 commit comments

Comments
 (0)