22"""Utility functions and tests for log_utils module."""
33
44import contextlib
5- from contextlib import contextmanager
65import functools
76import io
87import logging
1110import sys
1211import tempfile
1312import time
13+ from contextlib import contextmanager
1414
1515# Add parent directory to path for imports
1616sys .path .insert (0 , os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
2727
2828
2929def skip_if_no_zoneinfo_utc ():
30- """Skip test if zoneinfo or UTC timezone data is not available (common on Windows)."""
31- try :
32- from zoneinfo import ZoneInfo
30+ """Skip test if UTC timezone data is not available (common on Windows without tzdata)."""
31+ from zoneinfo import ZoneInfo
3332
34- ZoneInfo ("UTC" ) # Test if UTC is available
35- except Exception :
36- pytest .skip ("zoneinfo not available or UTC timezone data missing on this system" )
33+ try :
34+ ZoneInfo ("UTC" ) # Test if UTC timezone data is available
35+ except KeyError :
36+ pytest .skip ("UTC timezone data missing on this system (install tzdata package)" )
3737
3838
3939def get_safe_timezone ():
4040 """Get a timezone that works on all platforms."""
41- try :
42- from zoneinfo import ZoneInfo
41+ from zoneinfo import ZoneInfo
4342
44- ZoneInfo ("UTC" ) # Test if UTC is available
43+ try :
44+ ZoneInfo ("UTC" ) # Test if UTC timezone data is available
4545 return "UTC"
46- except Exception :
47- return "localtime" # Fallback to localtime which should always work
46+ except KeyError :
47+ return "localtime" # Fallback to localtime if UTC timezone data is missing
4848
4949
5050def requires_zoneinfo_utc (func ):
@@ -60,16 +60,15 @@ def wrapper(*args, **kwargs):
6060
6161def requires_zoneinfo (timezone ):
6262 """Decorator to skip tests that require a specific timezone."""
63+ from zoneinfo import ZoneInfo
6364
6465 def decorator (func ):
6566 @functools .wraps (func )
6667 def wrapper (* args , ** kwargs ):
6768 try :
68- from zoneinfo import ZoneInfo
69-
70- ZoneInfo (timezone ) # Test if timezone is available
71- except Exception :
72- pytest .skip (f"Timezone '{ timezone } ' not available on this system" )
69+ ZoneInfo (timezone ) # Test if timezone data is available
70+ except KeyError :
71+ pytest .skip (f"Timezone '{ timezone } ' data not available on this system" )
7372 return func (* args , ** kwargs )
7473
7574 return wrapper
@@ -79,13 +78,13 @@ def wrapper(*args, **kwargs):
7978
8079def patch_logger_kwargs_with_safe_timezone (kwargs ):
8180 """Patch logger kwargs to use safe timezone if UTC is specified but not available."""
81+ from zoneinfo import ZoneInfo
82+
8283 if kwargs .get ('timezone' ) == 'UTC' :
8384 try :
84- from zoneinfo import ZoneInfo
85-
86- ZoneInfo ("UTC" ) # Test if UTC is available
87- except Exception :
88- kwargs ['timezone' ] = 'localtime' # Fall back to localtime
85+ ZoneInfo ("UTC" ) # Test if UTC timezone data is available
86+ except KeyError :
87+ kwargs ['timezone' ] = 'localtime' # Fall back to localtime if UTC data is missing
8988 return kwargs
9089
9190
@@ -877,8 +876,8 @@ def test_remove_old_logs_file_error(self):
877876 def test_remove_old_logs_directory_error (self ):
878877 """Test remove_old_logs error handling when directory scan fails"""
879878 # Test with a simulated Path.glob() error by mocking pathlib.Path
880- from pathlib import Path
881879 import unittest .mock
880+ from pathlib import Path
882881
883882 with tempfile .TemporaryDirectory () as temp_dir :
884883 # Create a normal directory first
@@ -1455,8 +1454,8 @@ def test_gzip_file_source_deletion_error_coverage(self):
14551454 f .write ("test content" )
14561455
14571456 # Mock Path.unlink to raise OSError during deletion
1458- from pathlib import Path
14591457 import unittest .mock
1458+ from pathlib import Path
14601459
14611460 original_unlink = Path .unlink
14621461
0 commit comments