Skip to content

Commit 7a21c41

Browse files
committed
V5.0.1
1 parent 42bc342 commit 7a21c41

File tree

5 files changed

+640
-63
lines changed

5 files changed

+640
-63
lines changed

tests/core/test_log_utils.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def test_check_directory_permissions(self):
366366
# Clean up any existing directory first
367367
if os.path.exists(directory):
368368
try:
369-
os.chmod(directory, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
369+
os.chmod(directory, 0o755)
370370
log_utils.delete_file(directory)
371371
except (OSError, PermissionError):
372372
pass # Continue if cleanup fails
@@ -382,7 +382,7 @@ def test_check_directory_permissions(self):
382382
# Always restore permissions for cleanup, even if test fails
383383
if os.path.exists(directory):
384384
try:
385-
os.chmod(directory, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
385+
os.chmod(directory, 0o755)
386386
log_utils.delete_file(directory)
387387
except (OSError, PermissionError):
388388
pass # Ignore cleanup errors
@@ -392,21 +392,21 @@ def test_check_directory_permissions(self):
392392
# test permission error on creation - use a readonly parent directory
393393
with tempfile.TemporaryDirectory() as temp_dir:
394394
readonly_parent = os.path.join(temp_dir, "readonly")
395-
# Read-only parent 0o555
396-
os.makedirs(readonly_parent, mode=stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
395+
# Read-only parent
396+
os.makedirs(readonly_parent, mode=0o555)
397397
try:
398398
non_existent = os.path.join(readonly_parent, "non-existent-directory")
399399
with pytest.raises(PermissionError) as exec_info:
400400
log_utils.check_directory_permissions(non_existent)
401401
assert type(exec_info.value) is PermissionError
402402
assert "Unable to create directory" in str(exec_info.value)
403403
finally:
404-
# Restore permissions for cleanup 0o755
405-
os.chmod(readonly_parent, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
404+
# Restore permissions for cleanup
405+
os.chmod(readonly_parent, 0o755)
406406

407407
def test_remove_old_logs(self):
408408
directory = os.path.join(tempfile.gettempdir(), "test_remove_logs")
409-
os.makedirs(directory, mode=stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH, exist_ok=True)
409+
os.makedirs(directory, mode=0o755, exist_ok=True)
410410
assert os.path.exists(directory) == True
411411

412412
# Create a file and manually set its modification time to be old
@@ -494,15 +494,14 @@ def test_get_log_path(self):
494494
# This test only works on Unix/Linux/macOS systems with chmod
495495
if sys.platform != "win32":
496496
readonly_dir = os.path.join(temp_dir, "readonly")
497-
# 0o555
498-
os.makedirs(readonly_dir, mode=stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
497+
os.makedirs(readonly_dir, mode=0o555)
499498
try:
500499
with pytest.raises(PermissionError) as exc_info:
501500
log_utils.get_log_path(readonly_dir, test_file)
502501
assert "Unable to access directory" in str(exc_info.value)
503502
finally:
504-
# Cleanup permissions 0o755
505-
os.chmod(readonly_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
503+
# Cleanup permissions
504+
os.chmod(readonly_dir, 0o755)
506505
os.rmdir(readonly_dir)
507506

508507
def test_get_format(self):
@@ -843,8 +842,8 @@ def test_remove_old_logs_file_error(self):
843842
old_time = time.time() - 2*24*60*60 # 2 days old
844843
os.utime(gz_file, (old_time, old_time))
845844

846-
# Make parent directory read-only to trigger deletion error 0o555
847-
os.chmod(temp_dir, stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
845+
# Make parent directory read-only to trigger deletion error
846+
os.chmod(temp_dir, 0o555)
848847

849848
try:
850849
# Capture stderr to verify error was logged
@@ -856,8 +855,8 @@ def test_remove_old_logs_file_error(self):
856855
output = stderr_capture.getvalue()
857856
assert "Unable to delete old log" in output
858857
finally:
859-
# Restore permissions for cleanup 0o755
860-
os.chmod(temp_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
858+
# Restore permissions for cleanup
859+
os.chmod(temp_dir, 0o755)
861860

862861
def test_remove_old_logs_directory_error(self):
863862
"""Test remove_old_logs error handling when directory scan fails"""
@@ -915,8 +914,8 @@ def test_get_log_path_permission_error(self):
915914
# Create a subdirectory and make it read-only
916915
readonly_dir = os.path.join(temp_dir, "readonly")
917916
os.makedirs(readonly_dir)
918-
# Read and execute only 0o555
919-
os.chmod(readonly_dir, stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
917+
# Read and execute only
918+
os.chmod(readonly_dir, 0o555)
920919

921920
try:
922921
with pytest.raises(PermissionError) as exc_info:
@@ -925,8 +924,8 @@ def test_get_log_path_permission_error(self):
925924
assert ("Unable to access directory" in str(exc_info.value) or
926925
"Unable to write to log directory" in str(exc_info.value))
927926
finally:
928-
# Restore for cleanup 0o755
929-
os.chmod(readonly_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
927+
# Restore for cleanup
928+
os.chmod(readonly_dir, 0o755)
930929

931930
@pytest.mark.skipif(sys.platform == "win32", reason="Unix/Linux/macOS-specific chmod test")
932931
def test_gzip_file_io_error(self):
@@ -937,8 +936,8 @@ def test_gzip_file_io_error(self):
937936
with open(test_file, "w") as f:
938937
f.write("test content")
939938

940-
# Make directory read-only to trigger gzip error 0o555
941-
os.chmod(temp_dir, stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
939+
# Make directory read-only to trigger gzip error
940+
os.chmod(temp_dir, 0o555)
942941

943942
try:
944943
stderr_capture = io.StringIO()
@@ -949,8 +948,8 @@ def test_gzip_file_io_error(self):
949948
output = stderr_capture.getvalue()
950949
assert "Unable to gzip log file" in output
951950
finally:
952-
# Restore for cleanup 0o755
953-
os.chmod(temp_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
951+
# Restore for cleanup
952+
os.chmod(temp_dir, 0o755)
954953

955954
@pytest.mark.skipif(sys.platform == "win32", reason="Unix/Linux/macOS-specific chmod test")
956955
def test_gzip_file_deletion_error(self):
@@ -1403,8 +1402,8 @@ def test_get_log_path_write_permission_error(self):
14031402
# Add to cache first to bypass check_directory_permissions
14041403
log_utils._checked_directories.add(test_dir)
14051404

1406-
# Make directory non-writable, Read and execute only 0o555
1407-
os.chmod(test_dir, stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
1405+
# Make directory non-writable, Read and execute only
1406+
os.chmod(test_dir, 0o555)
14081407

14091408
try:
14101409
with pytest.raises(PermissionError) as exc_info:
@@ -1415,7 +1414,7 @@ def test_get_log_path_write_permission_error(self):
14151414

14161415
finally:
14171416
# Restore for cleanup 0o755
1418-
os.chmod(test_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
1417+
os.chmod(test_dir, 0o755)
14191418
log_utils._checked_directories.discard(test_dir)
14201419

14211420
def test_timezone_offset_fallback_exception(self):

tests/core/test_log_utils_windows.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -166,49 +166,37 @@ def test_gzip_file_windows_retry_mechanism(self):
166166
file_handle.write("test content for retry test")
167167
file_handle.close()
168168

169-
# Test both with and without platform mocking to ensure retry works
170-
for mock_platform in [False, True]:
171-
# Mock time.sleep to verify retry mechanism
172-
with patch('pythonLogs.log_utils.time.sleep') as mock_sleep:
173-
# Mock open to raise PermissionError on first call, succeed on second
174-
call_count = 0
175-
original_open = open
176-
177-
def mock_open_side_effect(*args, **kwargs):
178-
nonlocal call_count
179-
call_count += 1
180-
if call_count == 1:
181-
# First call - simulate Windows file locking
182-
raise PermissionError("Permission denied")
183-
else:
184-
# Subsequent calls - use real open
185-
return original_open(*args, **kwargs)
186-
187-
context_manager = (
188-
patch('pythonLogs.log_utils.sys.platform', 'win32') if mock_platform
189-
else patch('pythonLogs.log_utils.open', side_effect=mock_open_side_effect)
190-
)
191-
192-
with context_manager:
193-
if mock_platform:
194-
with patch('pythonLogs.log_utils.open', side_effect=mock_open_side_effect):
195-
result = log_utils.gzip_file_with_sufix(file_path, f"retry_test_{mock_platform}")
196-
else:
197-
result = log_utils.gzip_file_with_sufix(file_path, f"retry_test_{mock_platform}")
169+
# Mock time.sleep to verify retry mechanism
170+
with patch('pythonLogs.log_utils.time.sleep') as mock_sleep:
171+
# Mock open to raise PermissionError on first call, succeed on second
172+
call_count = 0
173+
original_open = open
174+
175+
def mock_open_side_effect(*args, **kwargs):
176+
nonlocal call_count
177+
call_count += 1
178+
if call_count == 1:
179+
# First call - simulate Windows file locking
180+
raise PermissionError("Permission denied")
181+
else:
182+
# Subsequent calls - use real open
183+
return original_open(*args, **kwargs)
184+
185+
# Always mock platform as win32 and open with retry behavior
186+
with patch('pythonLogs.log_utils.sys.platform', 'win32'):
187+
with patch('pythonLogs.log_utils.open', side_effect=mock_open_side_effect):
188+
result = log_utils.gzip_file_with_sufix(file_path, "retry_test")
198189

199190
# Verify retry was attempted (sleep was called)
200191
mock_sleep.assert_called_once_with(0.1)
201192

202193
# Verify the operation eventually succeeded
203194
assert result is not None
204-
assert f"retry_test_{mock_platform}" in result
195+
assert "retry_test" in result
205196

206197
# Clean up the gzipped file
207198
if result and os.path.exists(result):
208199
safe_close_and_delete_file(None, result)
209-
210-
# Reset for next iteration
211-
call_count = 0
212200

213201
finally:
214202
# Clean up the original file

tests/factory/test_factory_examples.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ def test_error_handling_scenarios(self):
194194
if sys.platform != "win32":
195195
with tempfile.TemporaryDirectory() as temp_dir:
196196
readonly_parent = os.path.join(temp_dir, "readonly")
197-
# Read-only parent 0o555
197+
# Read-only parent
198198
os.makedirs(
199199
readonly_parent,
200-
mode=stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH,
200+
mode=0o555,
201201
)
202202
try:
203203
invalid_dir = os.path.join(readonly_parent, "invalid")
@@ -207,8 +207,8 @@ def test_error_handling_scenarios(self):
207207
directory=invalid_dir,
208208
)
209209
finally:
210-
# Restore permissions for cleanup 0o755
211-
os.chmod(readonly_parent, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
210+
# Restore permissions for cleanup
211+
os.chmod(readonly_parent, 0o755)
212212

213213
@pytest.mark.skipif(sys.platform == "win32", reason="Windows file locking issues with TemporaryDirectory - see test_factory_windows.py")
214214
def test_logger_customization_example(self):

0 commit comments

Comments
 (0)