@@ -420,10 +420,9 @@ def test_is_older_than_x_days(self):
420420 try :
421421 assert os .path .isfile (file_path ) == True
422422
423- # When days=1, it compares against current time, so the file should be "older"
424- # due to the small time difference since creation
423+ # When days=1, it compares against 1 day ago, so newly created file should NOT be older
425424 result = log_utils .is_older_than_x_days (file_path , 1 )
426- assert result == True
425+ assert result == False
427426
428427 # When days=5, it compares against 5 days ago, so newly created file should NOT be older
429428 result = log_utils .is_older_than_x_days (file_path , 5 )
@@ -1555,3 +1554,88 @@ def mock_copyfileobj(*args, **kwargs):
15551554 # Cleanup: remove the test file if it still exists
15561555 if os .path .exists (test_file ):
15571556 os .unlink (test_file )
1557+
1558+ def test_gzip_file_windows_retry_mechanism_coverage (self ):
1559+ """Test gzip_file_with_sufix Windows retry mechanism for coverage."""
1560+ with tempfile .TemporaryDirectory () as temp_dir :
1561+ # Create a test file
1562+ test_file = os .path .join (temp_dir , "test_retry.log" )
1563+ with open (test_file , "w" ) as f :
1564+ f .write ("test content for retry mechanism" )
1565+
1566+ import unittest .mock
1567+
1568+ # Mock to simulate Windows platform and PermissionError on first attempt
1569+ call_count = 0
1570+ original_open = open
1571+
1572+ def mock_open_side_effect (* args , ** kwargs ):
1573+ nonlocal call_count
1574+ call_count += 1
1575+ if args [0 ] == test_file and call_count == 1 :
1576+ # First call - simulate Windows file locking
1577+ raise PermissionError ("The process cannot access the file" )
1578+ else :
1579+ # Subsequent calls - use real open
1580+ return original_open (* args , ** kwargs )
1581+
1582+ # Mock sys.platform to be Windows and time.sleep to verify retry
1583+ with unittest .mock .patch ('pythonLogs.log_utils.sys.platform' , 'win32' ):
1584+ with unittest .mock .patch ('pythonLogs.log_utils.time.sleep' ) as mock_sleep :
1585+ with unittest .mock .patch ('pythonLogs.log_utils.open' , side_effect = mock_open_side_effect ):
1586+ # This should succeed after retry, covering lines 259-261
1587+ result = log_utils .gzip_file_with_sufix (test_file , "retry_coverage" )
1588+
1589+ # Verify retry was attempted (sleep was called) - line 260
1590+ mock_sleep .assert_called_once_with (0.1 )
1591+
1592+ # Verify the operation eventually succeeded
1593+ assert result is not None
1594+ assert result .endswith ("_retry_coverage.log.gz" )
1595+ assert not os .path .exists (test_file ) # Original should be deleted
1596+
1597+ # Clean up the gzipped file
1598+ if result and os .path .exists (result ):
1599+ os .unlink (result )
1600+
1601+ def test_gzip_file_windows_retry_exhausted_coverage (self ):
1602+ """Test gzip_file_with_sufix when Windows retries are exhausted for coverage."""
1603+ with tempfile .TemporaryDirectory () as temp_dir :
1604+ # Create a test file
1605+ test_file = os .path .join (temp_dir , "test_retry_fail.log" )
1606+ with open (test_file , "w" ) as f :
1607+ f .write ("test content for retry exhaustion" )
1608+
1609+ import unittest .mock
1610+
1611+ # Mock to always raise PermissionError to exhaust retries
1612+ def mock_open_side_effect (* args , ** kwargs ):
1613+ if args [0 ] == test_file :
1614+ # Always fail - simulate persistent Windows file locking
1615+ raise PermissionError ("The process cannot access the file - persistent lock" )
1616+ else :
1617+ # Other opens work normally
1618+ return open (* args , ** kwargs )
1619+
1620+ # Mock sys.platform to be Windows and capture stderr
1621+ with unittest .mock .patch ('pythonLogs.log_utils.sys.platform' , 'win32' ):
1622+ with unittest .mock .patch ('pythonLogs.log_utils.time.sleep' ) as mock_sleep :
1623+ with unittest .mock .patch ('pythonLogs.log_utils.open' , side_effect = mock_open_side_effect ):
1624+ # Capture stderr to verify error logging
1625+ stderr_capture = io .StringIO ()
1626+ with contextlib .redirect_stderr (stderr_capture ):
1627+ with pytest .raises (PermissionError ) as exc_info :
1628+ # This should exhaust retries and fail, covering lines 262-264
1629+ log_utils .gzip_file_with_sufix (test_file , "retry_fail" )
1630+
1631+ # Verify retries were attempted (should be called twice for 3 attempts)
1632+ assert mock_sleep .call_count == 2
1633+
1634+ # Verify error was logged to stderr (line 263)
1635+ output = stderr_capture .getvalue ()
1636+ assert "Unable to gzip log file" in output
1637+ assert test_file in output
1638+ assert "persistent lock" in output
1639+
1640+ # Verify the exception was re-raised (line 264)
1641+ assert "persistent lock" in str (exc_info .value )
0 commit comments