From 298c3b212f90e4933c1378c253877c0a443ac9f5 Mon Sep 17 00:00:00 2001 From: Derek Slone-Zhen Date: Sat, 3 Feb 2024 10:25:55 +1100 Subject: [PATCH 1/4] In test cases, use `converter=gmtime` to avoid timezone & daylight savings issues --- tests/unit/test_base_command.py | 6 ++++-- tests/unit/test_logging.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_base_command.py b/tests/unit/test_base_command.py index 44dae384a75..1f24ed91b18 100644 --- a/tests/unit/test_base_command.py +++ b/tests/unit/test_base_command.py @@ -17,9 +17,10 @@ @pytest.fixture def fixed_time() -> Iterator[None]: - with patch("time.time", lambda: 1547704837.040001 + time.timezone): + with patch("time.time", lambda: 1547704837.040001): yield +logging.Formatter.converter = time.gmtime class FakeCommand(Command): _name = "fake" @@ -109,7 +110,8 @@ def test_log_command_success(fixed_time: None, tmpdir: Path) -> None: log_path = os.path.join(tmpdir, "log") cmd.main(["fake", "--log", log_path]) with open(log_path) as f: - assert f.read().rstrip() == "2019-01-17T06:00:37,040 fake" + log_message = f.read().rstrip() + assert log_message == "2019-01-17T06:00:37,040 fake" def test_log_command_error(fixed_time: None, tmpdir: Path) -> None: diff --git a/tests/unit/test_logging.py b/tests/unit/test_logging.py index 9d507d74277..328cbd2b1bf 100644 --- a/tests/unit/test_logging.py +++ b/tests/unit/test_logging.py @@ -15,6 +15,7 @@ logger = logging.getLogger(__name__) +logging.Formatter.converter = time.gmtime class TestIndentingFormatter: """Test ``pip._internal.utils.logging.IndentingFormatter``.""" @@ -23,7 +24,7 @@ def make_record(self, msg: str, level_name: str) -> logging.LogRecord: level_number = getattr(logging, level_name) attrs = { "msg": msg, - "created": 1547704837.040001 + time.timezone, + "created": 1547704837.040001, "msecs": 40, "levelname": level_name, "levelno": level_number, From 9682e2d3f1313d16cf4b3d2b40acbbad528ddd70 Mon Sep 17 00:00:00 2001 From: Derek Slone-Zhen Date: Sat, 3 Feb 2024 10:36:33 +1100 Subject: [PATCH 2/4] Added news file --- news/12508.trivial.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 news/12508.trivial.rst diff --git a/news/12508.trivial.rst b/news/12508.trivial.rst new file mode 100644 index 00000000000..c0c0438e441 --- /dev/null +++ b/news/12508.trivial.rst @@ -0,0 +1,3 @@ +Fix a few test cases to correctly test timestamps generated in log messages +when run in the southern hemisphere in a geography that uses daylight savings +and on a ``timet`` that would fall within daylight savings. \ No newline at end of file From 36f6a7fa1392876d5b1a1febc761587c6f5e0364 Mon Sep 17 00:00:00 2001 From: Derek Slone-Zhen Date: Sat, 3 Feb 2024 11:00:24 +1100 Subject: [PATCH 3/4] Revert cosmetic / debug change --- tests/unit/test_base_command.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/test_base_command.py b/tests/unit/test_base_command.py index 1f24ed91b18..8f0c93402f2 100644 --- a/tests/unit/test_base_command.py +++ b/tests/unit/test_base_command.py @@ -110,8 +110,7 @@ def test_log_command_success(fixed_time: None, tmpdir: Path) -> None: log_path = os.path.join(tmpdir, "log") cmd.main(["fake", "--log", log_path]) with open(log_path) as f: - log_message = f.read().rstrip() - assert log_message == "2019-01-17T06:00:37,040 fake" + assert f.read().rstrip() == "2019-01-17T06:00:37,040 fake" def test_log_command_error(fixed_time: None, tmpdir: Path) -> None: From 1a1db37ecb00f103138882131b51a75ba2687e73 Mon Sep 17 00:00:00 2001 From: Derek Slone-Zhen Date: Sat, 3 Feb 2024 11:05:47 +1100 Subject: [PATCH 4/4] White-space and `black` fixes. --- news/12508.trivial.rst | 4 ++-- tests/unit/test_base_command.py | 2 ++ tests/unit/test_logging.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/news/12508.trivial.rst b/news/12508.trivial.rst index c0c0438e441..fa2cc081f19 100644 --- a/news/12508.trivial.rst +++ b/news/12508.trivial.rst @@ -1,3 +1,3 @@ Fix a few test cases to correctly test timestamps generated in log messages -when run in the southern hemisphere in a geography that uses daylight savings -and on a ``timet`` that would fall within daylight savings. \ No newline at end of file +when run in the southern hemisphere in a geography that uses daylight savings +and on a ``timet`` that would fall within daylight savings. diff --git a/tests/unit/test_base_command.py b/tests/unit/test_base_command.py index 8f0c93402f2..1ff20e21094 100644 --- a/tests/unit/test_base_command.py +++ b/tests/unit/test_base_command.py @@ -20,8 +20,10 @@ def fixed_time() -> Iterator[None]: with patch("time.time", lambda: 1547704837.040001): yield + logging.Formatter.converter = time.gmtime + class FakeCommand(Command): _name = "fake" diff --git a/tests/unit/test_logging.py b/tests/unit/test_logging.py index 328cbd2b1bf..cfe502216ec 100644 --- a/tests/unit/test_logging.py +++ b/tests/unit/test_logging.py @@ -17,6 +17,7 @@ logging.Formatter.converter = time.gmtime + class TestIndentingFormatter: """Test ``pip._internal.utils.logging.IndentingFormatter``."""