@@ -37,24 +37,18 @@ class TestLogUtilsWindows:
3737 @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
3838 def test_check_directory_permissions_windows (self ):
3939 """Test Windows-specific directory permission behavior."""
40- import tempfile
41-
42- # On Windows, create a test in a deeply nested path that doesn't exist
43- # Use a path that's more likely to cause permission issues
4440 with tempfile .TemporaryDirectory () as temp_dir :
4541 # Create a deeply nested path that should trigger directory creation
4642 nested_path = os .path .join (temp_dir , "level1" , "level2" , "level3" , "level4" )
4743
48- # This should succeed and create the directories
49- result = log_utils .check_directory_permissions (nested_path )
50- assert result == True
44+ # This should succeed and create the directories (function returns None)
45+ log_utils .check_directory_permissions (nested_path )
5146 assert os .path .exists (nested_path )
5247
5348 # Test with a path that contains invalid characters (Windows-specific)
5449 try :
5550 invalid_chars_path = os .path .join (temp_dir , "invalid<>:|*?\" path" )
5651 # This might raise different exceptions on different Windows versions
57- # So we'll catch the general case
5852 with pytest .raises ((PermissionError , OSError , ValueError )) as exec_info :
5953 log_utils .check_directory_permissions (invalid_chars_path )
6054 # The specific error message may vary
@@ -160,13 +154,12 @@ def test_gzip_file_with_sufix_windows_safe(self):
160154 if os .path .exists (file_path ):
161155 safe_close_and_delete_file (None , file_path )
162156
157+
163158 @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
164- def test_gzip_file_cross_platform_retry_mechanism (self ):
165- """Test that gzip_file_with_sufix handles Windows file locking with retry (cross-platform) """
159+ def test_gzip_file_windows_retry_mechanism (self ):
160+ """Test that gzip_file_with_sufix handles Windows file locking with retry. """
166161 from unittest .mock import patch
167162
168- # Import test utilities from the same directory
169-
170163 # Create a Windows-safe temporary file
171164 file_handle , file_path = create_windows_safe_temp_file (suffix = ".log" , text = True )
172165
@@ -175,8 +168,8 @@ def test_gzip_file_cross_platform_retry_mechanism(self):
175168 file_handle .write ("test content for retry test" )
176169 file_handle .close ()
177170
178- # Mock sys. platform to simulate Windows
179- with patch ( 'pythonLogs.log_utils.sys.platform' , 'win32' ) :
171+ # Test both with and without platform mocking to ensure retry works
172+ for mock_platform in [ False , True ] :
180173 # Mock time.sleep to verify retry mechanism
181174 with patch ('pythonLogs.log_utils.time.sleep' ) as mock_sleep :
182175 # Mock open to raise PermissionError on first call, succeed on second
@@ -193,71 +186,31 @@ def mock_open_side_effect(*args, **kwargs):
193186 # Subsequent calls - use real open
194187 return original_open (* args , ** kwargs )
195188
196- with patch ('pythonLogs.log_utils.open' , side_effect = mock_open_side_effect ):
197- # This should succeed after retry
198- result = log_utils .gzip_file_with_sufix (file_path , "retry_test" )
189+ context_manager = (
190+ patch ('pythonLogs.log_utils.sys.platform' , 'win32' ) if mock_platform
191+ else patch ('pythonLogs.log_utils.open' , side_effect = mock_open_side_effect )
192+ )
193+
194+ with context_manager :
195+ if mock_platform :
196+ with patch ('pythonLogs.log_utils.open' , side_effect = mock_open_side_effect ):
197+ result = log_utils .gzip_file_with_sufix (file_path , f"retry_test_{ mock_platform } " )
198+ else :
199+ result = log_utils .gzip_file_with_sufix (file_path , f"retry_test_{ mock_platform } " )
199200
200201 # Verify retry was attempted (sleep was called)
201202 mock_sleep .assert_called_once_with (0.1 )
202203
203204 # Verify the operation eventually succeeded
204205 assert result is not None
205- assert result . endswith ( "_retry_test.log.gz" )
206+ assert f"retry_test_ { mock_platform } " in result
206207
207208 # Clean up the gzipped file
208209 if result and os .path .exists (result ):
209210 safe_close_and_delete_file (None , result )
210-
211- finally :
212- # Clean up the original file
213- if os .path .exists (file_path ):
214- safe_close_and_delete_file (None , file_path )
215-
216- @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
217- def test_gzip_file_windows_retry_mechanism (self ):
218- """Test that gzip_file_with_sufix handles Windows file locking with retry."""
219- from unittest .mock import patch
220-
221- # Import test utilities from the same directory
222-
223- # Create a Windows-safe temporary file
224- file_handle , file_path = create_windows_safe_temp_file (suffix = ".log" , text = True )
225-
226- try :
227- # Write content and close properly
228- file_handle .write ("test content for retry test" )
229- file_handle .close ()
230-
231- # Mock time.sleep to verify retry mechanism
232- with patch ('pythonLogs.log_utils.time.sleep' ) as mock_sleep :
233- # Mock open to raise PermissionError on first call, succeed on second
234- call_count = 0
235- original_open = open
236-
237- def mock_open_side_effect (* args , ** kwargs ):
238- nonlocal call_count
239- call_count += 1
240- if call_count == 1 :
241- # First call - simulate Windows file locking
242- raise PermissionError ("Permission denied" )
243- else :
244- # Subsequent calls - use real open
245- return original_open (* args , ** kwargs )
246-
247- with patch ('pythonLogs.log_utils.open' , side_effect = mock_open_side_effect ):
248- # This should succeed after retry
249- result = log_utils .gzip_file_with_sufix (file_path , "retry_test" )
250-
251- # Verify retry was attempted (sleep was called)
252- mock_sleep .assert_called_once_with (0.1 )
253-
254- # Verify the operation eventually succeeded
255- assert result is not None
256- assert result .endswith ("_retry_test.log.gz" )
257-
258- # Clean up the gzipped file
259- if result and os .path .exists (result ):
260- safe_close_and_delete_file (None , result )
211+
212+ # Reset for next iteration
213+ call_count = 0
261214
262215 finally :
263216 # Clean up the original file
@@ -459,44 +412,7 @@ def windows_file_worker(worker_id):
459412 assert result ['gzip_result' ] is not None
460413 assert f"worker_{ result ['worker_id' ]} " in result ['gzip_result' ]
461414
462- @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
463- def test_get_log_path_unix_chmod_behavior (self ):
464- """Test get_log_path behavior with Unix-style chmod (Windows alternative test)."""
465- with tempfile .TemporaryDirectory () as temp_dir :
466- test_file = "test.log"
467- # Test 1: Valid directory should return the correct path
468- result = log_utils .get_log_path (temp_dir , test_file )
469- assert result == os .path .join (temp_dir , test_file )
470-
471- # Test 2: Directory that gets created should work fine
472- new_dir = os .path .join (temp_dir , "newdir" )
473- result = log_utils .get_log_path (new_dir , test_file )
474- assert result == os .path .join (new_dir , test_file )
475- assert os .path .exists (new_dir ) # Should have been created
476-
477- # Test 3: On Windows, we don't test chmod-based permission errors
478- # since Windows permission model is different from Unix
479- # This test would be: readonly directory + PermissionError expectation
480- # But Windows handles this differently, so we skip it
481- pytest .skip ("Unix-style chmod permission test not applicable on Windows" )
482415
483- @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
484- def test_get_log_path_permission_error_windows (self ):
485- """Test get_log_path permission handling specific to Windows."""
486- with tempfile .TemporaryDirectory () as temp_dir :
487- # On Windows, we test different permission scenarios
488- # Windows doesn't use Unix-style chmod, so we test other error conditions
489-
490- # Test with invalid path characters (Windows-specific)
491- try :
492- invalid_path = os .path .join (temp_dir , "invalid<>path" )
493- # This might succeed on some Windows systems, so we don't assert failure
494- result = log_utils .get_log_path (invalid_path , "test.log" )
495- # If it succeeds, just verify the path is returned
496- assert "test.log" in result
497- except (PermissionError , OSError , ValueError ):
498- # Expected on Windows with invalid characters
499- pass
500416
501417 @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
502418 def test_gzip_file_windows_permission_error (self ):
@@ -532,25 +448,40 @@ def test_gzip_file_windows_permission_error(self):
532448 safe_close_and_delete_file (None , file_path )
533449
534450 @pytest .mark .skipif (sys .platform != "win32" , reason = "Windows-specific tests" )
535- def test_get_log_path_write_permission_windows (self ):
536- """Test get_log_path write permission check specific to Windows ."""
451+ def test_get_log_path_windows_comprehensive (self ):
452+ """Comprehensive test for get_log_path Windows- specific behaviors ."""
537453 with tempfile .TemporaryDirectory () as temp_dir :
538- # Create directory
539- test_dir = os .path .join (temp_dir , "test_write_perm" )
540- os .makedirs (test_dir )
454+ test_file = "test.log"
455+
456+ # Test 1: Valid directory should return the correct path
457+ result = log_utils .get_log_path (temp_dir , test_file )
458+ assert result == os .path .join (temp_dir , test_file )
459+
460+ # Test 2: Directory that gets created should work fine
461+ new_dir = os .path .join (temp_dir , "newdir" )
462+ result = log_utils .get_log_path (new_dir , test_file )
463+ assert result == os .path .join (new_dir , test_file )
464+ assert os .path .exists (new_dir ) # Should have been created
541465
542- # On Windows, we test different permission scenarios
543- # Windows permission model is different from Unix chmod
466+ # Test 3: Test with invalid path characters (Windows-specific)
467+ try :
468+ invalid_path = os .path .join (temp_dir , "invalid<>path" )
469+ result = log_utils .get_log_path (invalid_path , "test.log" )
470+ assert "test.log" in result
471+ except (PermissionError , OSError , ValueError ):
472+ # Expected on Windows with invalid characters
473+ pass
544474
545- # Test normal operation (should succeed)
475+ # Test 4: Test normal operation in created directory
476+ test_dir = os .path .join (temp_dir , "test_write_perm" )
477+ os .makedirs (test_dir )
546478 result = log_utils .get_log_path (test_dir , "test.log" )
547479 assert result == os .path .join (test_dir , "test.log" )
548480
549- # Windows-specific: Test with long path names (Windows limitation)
481+ # Test 5: Windows-specific long path names (Windows limitation)
550482 long_filename = "a" * 200 + ".log" # Very long filename
551483 try :
552484 result = log_utils .get_log_path (test_dir , long_filename )
553- # May succeed or fail depending on Windows version and filesystem
554485 assert long_filename in result
555486 except (OSError , PermissionError ):
556487 # Expected on some Windows systems with path length limitations
0 commit comments