Skip to content

Commit

Permalink
Capture exceptions potentially raised by "win32_setctime"
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Dec 1, 2019
1 parent b0195f9 commit 364366f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
7 changes: 6 additions & 1 deletion loguru/_ctime_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ def get_ctime(filepath):
return os.stat(filepath).st_ctime

def set_ctime(filepath, timestamp):
if win32_setctime.SUPPORTED:
if not win32_setctime.SUPPORTED:
return

try:
win32_setctime.setctime(filepath, timestamp)
except (OSError, ValueError):
pass


elif hasattr(os.stat_result, "st_birthtime"):
Expand Down
49 changes: 44 additions & 5 deletions tests/test_filesink_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import platform
import builtins
from collections import namedtuple
from unittest.mock import MagicMock
from unittest.mock import MagicMock, PropertyMock
import loguru
from loguru import logger

Expand All @@ -23,6 +23,12 @@ def tmpdir_local(reset_logger):
logger.remove() # Deleting file not possible if still in use by Loguru


def reload_filesink_ctime_functions(monkeypatch):
ctime_functions = importlib.reload(loguru._ctime_functions)
monkeypatch.setattr(loguru._file_sink, "get_ctime", ctime_functions.get_ctime)
monkeypatch.setattr(loguru._file_sink, "set_ctime", ctime_functions.set_ctime)


@pytest.fixture
def monkeypatch_filesystem(monkeypatch):
def monkeypatch_filesystem(raising=None, crtime=None, patch_xattr=False, patch_win32=False):
Expand Down Expand Up @@ -77,9 +83,7 @@ def patched_setctime(filepath, timestamp):
win32_setctime = MagicMock(SUPPORTED=True, setctime=patched_setctime)
monkeypatch.setitem(sys.modules, "win32_setctime", win32_setctime)

ctime_functions = importlib.reload(loguru._ctime_functions)
monkeypatch.setattr(loguru._file_sink, "get_ctime", ctime_functions.get_ctime)
monkeypatch.setattr(loguru._file_sink, "set_ctime", ctime_functions.set_ctime)
reload_filesink_ctime_functions(monkeypatch)

return monkeypatch_filesystem

Expand Down Expand Up @@ -371,9 +375,13 @@ def test_time_rotation_reopening_linux_xattr_attributeerror(
def test_time_rotation_windows_no_setctime(
tmpdir, windows_filesystem, monkeypatch, monkeypatch_date
):
win32_setctime = MagicMock(SUPPORTED=False)
SUPPORTED = PropertyMock(return_value=False)
win32_setctime = MagicMock()
type(win32_setctime).SUPPORTED = SUPPORTED
monkeypatch.setitem(sys.modules, "win32_setctime", win32_setctime)

reload_filesink_ctime_functions(monkeypatch)

monkeypatch_date(2018, 10, 27, 5, 0, 0, 0)
i = logger.add(str(tmpdir.join("test.{time}.log")), format="{message}", rotation="2 h")
logger.info("1")
Expand All @@ -385,9 +393,40 @@ def test_time_rotation_windows_no_setctime(
assert len(tmpdir.listdir()) == 2
assert tmpdir.join("test.2018-10-27_05-00-00_000000.log").read() == "1\n2\n"
assert tmpdir.join("test.2018-10-27_07-30-00_000000.log").read() == "3\n"
assert SUPPORTED.called
assert not win32_setctime.setctime.called


@pytest.mark.parametrize("exception", [ValueError, OSError])
def test_time_rotation_windows_setctime_exception(
tmpdir, windows_filesystem, monkeypatch, monkeypatch_date, exception
):
setctime_called = False

def raising_setctime(filepath, timestamp):
nonlocal setctime_called
setctime_called = True
raise exception("Setctime error")

win32_setctime = MagicMock(SUPPORTED=True, setctime=raising_setctime)
monkeypatch.setitem(sys.modules, "win32_setctime", win32_setctime)

reload_filesink_ctime_functions(monkeypatch)

monkeypatch_date(2018, 10, 27, 5, 0, 0, 0)
i = logger.add(str(tmpdir.join("test.{time}.log")), format="{message}", rotation="2 h")
logger.info("1")
monkeypatch_date(2018, 10, 27, 6, 30, 0, 0)
logger.info("2")
assert len(tmpdir.listdir()) == 1
monkeypatch_date(2018, 10, 27, 7, 30, 0, 0)
logger.info("3")
assert len(tmpdir.listdir()) == 2
assert tmpdir.join("test.2018-10-27_05-00-00_000000.log").read() == "1\n2\n"
assert tmpdir.join("test.2018-10-27_07-30-00_000000.log").read() == "3\n"
assert setctime_called


def test_function_rotation(monkeypatch_date, tmpdir):
monkeypatch_date(2018, 1, 1, 0, 0, 0, 0)
x = iter([False, True, False])
Expand Down

0 comments on commit 364366f

Please sign in to comment.