spulec / freezegun

Let your Python tests travel through time
Apache License 2.0
4.19k stars 269 forks source link

potential feature request; flag to apply to file attributes? #392

Open tommyjcarpenter opened 3 years ago

tommyjcarpenter commented 3 years ago

Fully reproducible python file:

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.

zerocewl commented 3 years ago

+1