Skip to content

Commit

Permalink
Fix usage of "pathlib" in implementation breaking unit tests
Browse files Browse the repository at this point in the history
Remove monkeypatching of "os.name" in unit tests (which "pathlib" relies
upon). It simplifies the tests and make them more robust. Mocking all OS
is no longer required now that we're using Github Actions allowing to
run tests on real platforms.
  • Loading branch information
Delgan committed Jun 10, 2022
1 parent e040ede commit 9a13a38
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 231 deletions.
28 changes: 16 additions & 12 deletions loguru/_ctime_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ def load_ctime_functions():
if os.name == "nt":
import win32_setctime

def get_ctime(filepath):
def get_ctime_windows(filepath):
return os.stat(filepath).st_ctime

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

Expand All @@ -17,37 +17,41 @@ def set_ctime(filepath, timestamp):
except (OSError, ValueError):
pass

return get_ctime_windows, set_ctime_windows

elif hasattr(os.stat_result, "st_birthtime"):

def get_ctime(filepath):
def get_ctime_macos(filepath):
return os.stat(filepath).st_birthtime

def set_ctime(filepath, timestamp):
def set_ctime_macos(filepath, timestamp):
pass

return get_ctime_macos, set_ctime_macos

elif hasattr(os, "getxattr") and hasattr(os, "setxattr"):

def get_ctime(filepath):
def get_ctime_linux(filepath):
try:
return float(os.getxattr(filepath, b"user.loguru_crtime"))
except OSError:
return os.stat(filepath).st_mtime

def set_ctime(filepath, timestamp):
def set_ctime_linux(filepath, timestamp):
try:
os.setxattr(filepath, b"user.loguru_crtime", str(timestamp).encode("ascii"))
except OSError:
pass

else:
return get_ctime_linux, set_ctime_linux

def get_ctime(filepath):
return os.stat(filepath).st_mtime
def get_ctime_fallback(filepath):
return os.stat(filepath).st_mtime

def set_ctime(filepath, timestamp):
pass
def set_ctime_fallback(filepath, timestamp):
pass

return get_ctime, set_ctime
return get_ctime_fallback, set_ctime_fallback


get_ctime, set_ctime = load_ctime_functions()
25 changes: 9 additions & 16 deletions tests/test_add_option_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ def patch_colorama(monkeypatch):
@pytest.mark.parametrize("colorize", [True, False, None])
@pytest.mark.parametrize("tty", [True, False])
@pytest.mark.parametrize("replace_original", [True, False])
def test_colorize_stream(patch_colorama, monkeypatch, colorize, tty, replace_original):
@pytest.mark.skipif(os.name == "nt", reason="Colorama is required on Windows")
def test_colorize_stream_linux(patch_colorama, monkeypatch, colorize, tty, replace_original):
stream = Stream(tty)

monkeypatch.delenv("TERM", raising=False)
monkeypatch.delenv("PYCHARM_HOSTED", raising=False)
monkeypatch.setattr(os, "name", "unix")

if replace_original:
monkeypatch.setattr(sys, "__stdout__", stream)

Expand All @@ -100,12 +101,13 @@ def test_colorize_stream(patch_colorama, monkeypatch, colorize, tty, replace_ori
@pytest.mark.parametrize("colorize", [True, False, None])
@pytest.mark.parametrize("tty", [True, False])
@pytest.mark.parametrize("replace_original", [True, False])
@pytest.mark.skipif(os.name != "nt", reason="Only Windows requires Colorama")
def test_colorize_stream_windows(patch_colorama, monkeypatch, colorize, tty, replace_original):
stream = Stream(tty)

monkeypatch.delenv("TERM", raising=False)
monkeypatch.delenv("PYCHARM_HOSTED", raising=False)
monkeypatch.setattr(os, "name", "nt")

if replace_original:
monkeypatch.setattr(sys, "__stdout__", stream)

Expand All @@ -124,8 +126,7 @@ def test_colorize_stream_windows(patch_colorama, monkeypatch, colorize, tty, rep


@pytest.mark.parametrize("colorize", [True, False, None])
def test_isatty_error(monkeypatch, colorize):
monkeypatch.setattr(os, "name", "posix")
def test_isatty_error(colorize):
stream = Stream(None)
logger.add(stream, format="<blue>{message}</blue>", colorize=colorize)
logger.debug("Message")
Expand Down Expand Up @@ -216,32 +217,24 @@ def test_github_actions_ignored(monkeypatch, stream):


@pytest.mark.parametrize("stream", [sys.__stdout__, sys.__stderr__])
@pytest.mark.skipif(os.name != "nt", reason="The fix is applied only on Windows")
def test_mintty_fixed_windows(monkeypatch, stream):
monkeypatch.setattr(os, "name", "nt")
monkeypatch.setitem(os.environ, "TERM", "xterm")
monkeypatch.setattr(stream, "isatty", lambda: False)
assert not stream.isatty()
assert loguru._colorama.should_colorize(stream)


@pytest.mark.parametrize("stream", [None, Stream(False), Stream(None)])
def test_mintty_ignored_windows(monkeypatch, stream):
monkeypatch.setattr(os, "name", "nt")
monkeypatch.setitem(os.environ, "TERM", "xterm")
assert not loguru._colorama.should_colorize(stream)


@pytest.mark.parametrize("stream", [sys.__stdout__, sys.__stderr__])
@pytest.mark.skipif(os.name == "nt", reason="The fix will be applied on Windows")
def test_mintty_not_fixed_linux(monkeypatch, stream):
monkeypatch.setattr(os, "name", "posix")
monkeypatch.setitem(os.environ, "TERM", "xterm")
monkeypatch.setattr(stream, "isatty", lambda: False)
assert not stream.isatty()
assert not loguru._colorama.should_colorize(stream)


@pytest.mark.parametrize("stream", [None, Stream(False), Stream(None)])
def test_mintty_ignored_linux(monkeypatch, stream):
monkeypatch.setattr(os, "name", "posix")
def test_mintty_ignored(monkeypatch, stream):
monkeypatch.setitem(os.environ, "TERM", "xterm")
assert not loguru._colorama.should_colorize(stream)
Loading

0 comments on commit 9a13a38

Please sign in to comment.