Skip to content

Commit b3d6de3

Browse files
committed
Fix Windows timezone test failures
- Add test utility functions to handle zoneinfo availability checks - Use @requires_zoneinfo_utc decorator for tests that specifically need UTC - Fall back to safe timezone (localtime) for tests that just need any timezone - Skip tests gracefully when zoneinfo/UTC data is not available on Windows This addresses the ZoneInfoNotFoundError: 'No time zone found with key UTC' errors on Windows systems without proper timezone data installed.
1 parent a4b6a90 commit b3d6de3

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

tests/performance/test_performance.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# Add parent directory to path for imports
1111
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1212

13+
from ..test_utils import get_safe_timezone
14+
1315
from pythonLogs import (
1416
LoggerFactory,
1517
LoggerType,
@@ -105,11 +107,12 @@ def test_timezone_function_caching(self):
105107
# Create multiple loggers with same timezone
106108
start_time = time.time()
107109

110+
safe_tz = get_safe_timezone()
108111
loggers = []
109112
for i in range(20):
110113
logger = basic_logger(
111114
name=f"tz_test_{i}",
112-
timezone="UTC" # Same timezone should use cached function
115+
timezone=safe_tz # Same timezone should use cached function
113116
)
114117
loggers.append(logger)
115118

tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
"""Utility functions for tests across different platforms."""
3+
import pytest
4+
5+
6+
def skip_if_no_zoneinfo_utc():
7+
"""Skip test if zoneinfo or UTC timezone data is not available (common on Windows)."""
8+
try:
9+
from zoneinfo import ZoneInfo
10+
ZoneInfo("UTC") # Test if UTC is available
11+
except Exception:
12+
pytest.skip("zoneinfo not available or UTC timezone data missing on this system")
13+
14+
15+
def get_safe_timezone():
16+
"""Get a timezone that works on all platforms."""
17+
try:
18+
from zoneinfo import ZoneInfo
19+
ZoneInfo("UTC") # Test if UTC is available
20+
return "UTC"
21+
except Exception:
22+
return "localtime" # Fallback to localtime which should always work
23+
24+
25+
def requires_zoneinfo_utc(func):
26+
"""Decorator to skip tests that require zoneinfo UTC support."""
27+
def wrapper(*args, **kwargs):
28+
skip_if_no_zoneinfo_utc()
29+
return func(*args, **kwargs)
30+
return wrapper

tests/timezone/test_timezone_migration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# Add parent directory to path for imports
1010
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1111

12+
from ..test_utils import skip_if_no_zoneinfo_utc, get_safe_timezone, requires_zoneinfo_utc
13+
1214
from pythonLogs import (
1315
basic_logger,
1416
size_rotating_logger,
@@ -34,6 +36,7 @@ def setup_method(self):
3436
"""Clear registry before each test."""
3537
clear_logger_registry()
3638

39+
@requires_zoneinfo_utc
3740
def test_zoneinfo_import_success(self):
3841
"""Test that ZoneInfo is properly imported."""
3942
from pythonLogs.log_utils import ZoneInfo
@@ -42,6 +45,7 @@ def test_zoneinfo_import_success(self):
4245
utc_tz = ZoneInfo("UTC")
4346
assert utc_tz is not None
4447

48+
@requires_zoneinfo_utc
4549
def test_utc_timezone_basic_logger(self):
4650
"""Test UTC timezone with basic logger."""
4751
logger = basic_logger(

tests/timezone/test_zoneinfo_fallbacks.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# Add parent directory to path for imports
1111
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1212

13+
from ..test_utils import skip_if_no_zoneinfo_utc, get_safe_timezone, requires_zoneinfo_utc
14+
1315

1416
class TestZoneinfoFallbacks:
1517
"""Test fallback mechanisms for zoneinfo import and edge cases."""
@@ -92,10 +94,11 @@ def test_logger_creation_with_fallback_timezone(self):
9294
"""Test logger creation when timezone operations might fail."""
9395
from pythonLogs import basic_logger, LogLevel
9496

95-
# These should all work with proper fallback
97+
# Use safe timezone that works on all platforms
98+
safe_tz = get_safe_timezone()
9699
logger = basic_logger(
97100
name="fallback_test",
98-
timezone="UTC",
101+
timezone=safe_tz,
99102
level=LogLevel.INFO
100103
)
101104

@@ -164,14 +167,16 @@ def test_concurrent_timezone_access(self):
164167
import threading
165168
from pythonLogs import basic_logger, LogLevel
166169

170+
# Use safe timezone that works on all platforms
171+
safe_tz = get_safe_timezone()
167172
results = []
168173
errors = []
169174

170175
def create_logger_worker(worker_id):
171176
try:
172177
logger = basic_logger(
173178
name=f"concurrent_test_{worker_id}",
174-
timezone="UTC",
179+
timezone=safe_tz,
175180
level=LogLevel.INFO
176181
)
177182
logger.info(f"Concurrent test message {worker_id}")
@@ -194,17 +199,11 @@ def create_logger_worker(worker_id):
194199
assert len(errors) == 0, f"Errors occurred: {errors}"
195200
assert len(results) == 10
196201

202+
@requires_zoneinfo_utc
197203
def test_memory_usage_with_timezone_caching(self):
198204
"""Test that timezone caching doesn't cause memory leaks."""
199205
from pythonLogs import basic_logger, clear_logger_registry
200206

201-
# Check if zoneinfo works on this system
202-
try:
203-
from zoneinfo import ZoneInfo
204-
ZoneInfo("UTC") # Test if UTC is available
205-
except Exception:
206-
pytest.skip("zoneinfo not available or UTC timezone data missing on this system")
207-
208207
# Create many loggers with same timezone (should use cache)
209208
for i in range(100):
210209
logger = basic_logger(
@@ -218,17 +217,11 @@ def test_memory_usage_with_timezone_caching(self):
218217

219218
# Should complete without memory issues - test passes if no exception is raised
220219

220+
@requires_zoneinfo_utc
221221
def test_timezone_validation_edge_cases(self):
222222
"""Test timezone validation for various edge cases."""
223223
from pythonLogs.log_utils import _get_timezone_offset
224224

225-
# Check if zoneinfo works on this system
226-
try:
227-
from zoneinfo import ZoneInfo
228-
ZoneInfo("UTC") # Test if UTC is available
229-
except Exception:
230-
pytest.skip("zoneinfo not available or UTC timezone data missing on this system")
231-
232225
# Test case variations (timezone names are case-sensitive except for localtime)
233226
test_cases = [
234227
("UTC", "+0000"),

0 commit comments

Comments
 (0)