mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-10 15:42:10 +03:00
This commit adds a new test file, `tests/test_cli.py`, containing unit tests for the `_os_timezone` function in `netsleuth.cli`. The tests cover: - Happy paths (TZ variable, /etc/localtime symlink, /etc/timezone file) - The edge case of an `OSError` exception being thrown when reading the /etc/timezone file. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
import os
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from netsleuth.cli import _os_timezone
|
|
|
|
|
|
def test_os_timezone_tz_env():
|
|
with patch.dict(os.environ, {"TZ": "Europe/Berlin"}):
|
|
assert _os_timezone() == "Europe/Berlin"
|
|
|
|
def test_os_timezone_localtime_symlink():
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
with patch("netsleuth.cli.Path") as MockPath:
|
|
mock_localtime = MagicMock()
|
|
mock_localtime.is_symlink.return_value = True
|
|
mock_localtime.readlink.return_value = "/usr/share/zoneinfo/America/New_York"
|
|
|
|
# Make Path("/etc/localtime") return mock_localtime
|
|
MockPath.return_value = mock_localtime
|
|
|
|
assert _os_timezone() == "America/New_York"
|
|
MockPath.assert_called_once_with("/etc/localtime")
|
|
|
|
def test_os_timezone_timezone_file():
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
with patch("netsleuth.cli.Path") as MockPath:
|
|
mock_localtime = MagicMock()
|
|
mock_localtime.is_symlink.return_value = False
|
|
|
|
mock_timezone = MagicMock()
|
|
mock_timezone.read_text.return_value = "Asia/Tokyo\n"
|
|
|
|
def side_effect(path):
|
|
if path == "/etc/localtime":
|
|
return mock_localtime
|
|
elif path == "/etc/timezone":
|
|
return mock_timezone
|
|
return MagicMock()
|
|
|
|
MockPath.side_effect = side_effect
|
|
|
|
assert _os_timezone() == "Asia/Tokyo"
|
|
|
|
def test_os_timezone_oserror():
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
with patch("netsleuth.cli.Path") as MockPath:
|
|
mock_localtime = MagicMock()
|
|
mock_localtime.is_symlink.return_value = False
|
|
|
|
mock_timezone = MagicMock()
|
|
mock_timezone.read_text.side_effect = OSError("Permission denied")
|
|
|
|
def side_effect(path):
|
|
if path == "/etc/localtime":
|
|
return mock_localtime
|
|
elif path == "/etc/timezone":
|
|
return mock_timezone
|
|
return MagicMock()
|
|
|
|
MockPath.side_effect = side_effect
|
|
|
|
assert _os_timezone() is None
|