Skip to content

Commit 98fd06e

Browse files
author
atollk
committed
Fixes issue #477.
Unit tests for `FS.setinfo` now function correctly, whereas they basically did nothing before. Also, a bug in OSFS was fixed which caused timestamps to lose precision with `setinfo`.
1 parent ac8a91a commit 98fd06e

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

fs/info.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def is_writeable(self, namespace, key):
135135
When creating an `Info` object, you can add a ``_write`` key to
136136
each raw namespace that lists which keys are writable or not.
137137
138+
In general, this means they are compatible with the `setinfo`
139+
function of filesystem objects.
140+
138141
Arguments:
139142
namespace (str): A namespace identifier.
140143
key (str): A key within the namespace.

fs/osfs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,10 @@ def setinfo(self, path, info):
665665
if "details" in info:
666666
details = info["details"]
667667
if "accessed" in details or "modified" in details:
668-
_accessed = typing.cast(int, details.get("accessed"))
669-
_modified = typing.cast(int, details.get("modified", _accessed))
670-
accessed = int(_modified if _accessed is None else _accessed)
671-
modified = int(_modified)
668+
_accessed = typing.cast(float, details.get("accessed"))
669+
_modified = typing.cast(float, details.get("modified", _accessed))
670+
accessed = float(_modified if _accessed is None else _accessed)
671+
modified = float(_modified)
672672
if accessed is not None or modified is not None:
673673
with convert_os_errors("setinfo", path):
674674
os.utime(sys_path, (accessed, modified))

fs/test.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import io
1313
import itertools
1414
import json
15-
import math
1615
import os
1716
import time
1817
import unittest
@@ -1171,15 +1170,21 @@ def test_removetree_root(self):
11711170

11721171
def test_setinfo(self):
11731172
self.fs.create("birthday.txt")
1174-
now = math.floor(time.time())
1173+
now = time.time()
11751174

11761175
change_info = {"details": {"accessed": now + 60, "modified": now + 60 * 60}}
11771176
self.fs.setinfo("birthday.txt", change_info)
1178-
new_info = self.fs.getinfo("birthday.txt", namespaces=["details"]).raw
1179-
if "accessed" in new_info.get("_write", []):
1180-
self.assertEqual(new_info["details"]["accessed"], now + 60)
1181-
if "modified" in new_info.get("_write", []):
1182-
self.assertEqual(new_info["details"]["modified"], now + 60 * 60)
1177+
new_info = self.fs.getinfo("birthday.txt", namespaces=["details"])
1178+
can_write_acccess = new_info.is_writeable("details", "accessed")
1179+
can_write_modified = new_info.is_writeable("details", "modified")
1180+
if can_write_acccess:
1181+
self.assertAlmostEqual(
1182+
new_info.get("details", "accessed"), now + 60, places=4
1183+
)
1184+
if can_write_modified:
1185+
self.assertAlmostEqual(
1186+
new_info.get("details", "modified"), now + 60 * 60, places=4
1187+
)
11831188

11841189
with self.assertRaises(errors.ResourceNotFound):
11851190
self.fs.setinfo("nothing", {})
@@ -1188,10 +1193,11 @@ def test_settimes(self):
11881193
self.fs.create("birthday.txt")
11891194
self.fs.settimes("birthday.txt", accessed=datetime(2016, 7, 5))
11901195
info = self.fs.getinfo("birthday.txt", namespaces=["details"])
1191-
writeable = info.get("details", "_write", [])
1192-
if "accessed" in writeable:
1196+
can_write_acccess = info.is_writeable("details", "accessed")
1197+
can_write_modified = info.is_writeable("details", "modified")
1198+
if can_write_acccess:
11931199
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=pytz.UTC))
1194-
if "modified" in writeable:
1200+
if can_write_modified:
11951201
self.assertEqual(info.modified, datetime(2016, 7, 5, tzinfo=pytz.UTC))
11961202

11971203
def test_touch(self):

0 commit comments

Comments
 (0)