import tempfile
import os
import pathlib
import datetime
from freezegun import freeze_time
@freeze_time("2020-06-06 06:06:06")
def foo():
with tempfile.TemporaryDirectory() as testrepo:
fpath = os.path.join(testrepo, "foo.txt")
with open(fpath, "w") as f:
f.write("asdfasdf")
fname = pathlib.Path(fpath)
# get the modified time of the file
lmt = datetime.datetime.fromtimestamp(fname.stat().st_mtime)
print(lmt)
foo()
In this file, lmt is unaffected by freezegun.
I would assume this does not freeze becuase this is calling OS level APIs that are not patched. But with mock_open I wonder if this could be achieved? IE if you could do something like with freeze_time(xxx, mock_file_attrs=True) which takes care of the necessary mocking of file attributes? This may not be possible, just wondering.
Fully reproducible python file:
In this file,
lmt
is unaffected byfreezegun
.I would assume this does not freeze becuase this is calling OS level APIs that are not patched. But with
mock_open
I wonder if this could be achieved? IE if you could do something likewith freeze_time(xxx, mock_file_attrs=True)
which takes care of the necessary mocking of file attributes? This may not be possible, just wondering.