Skip to content

Commit a4b6a90

Browse files
committed
Fix test failures on macOS and Windows
- Fix macOS failures by using temporary directories instead of hardcoded paths like /non-existent-directory - Fix Windows failures by checking if zoneinfo/UTC is available before running timezone tests - Use proper permission testing with temporary directories and cleanup
1 parent 02a5288 commit a4b6a90

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

tests/core/test_log_utils.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ def test_check_directory_permissions(self):
5454
log_utils.delete_file(directory)
5555
assert os.path.exists(directory) == False
5656

57-
# test permission error on creation
58-
directory = "/non-existent-directory"
59-
with pytest.raises(PermissionError) as exec_info:
60-
log_utils.check_directory_permissions(directory)
61-
assert type(exec_info.value) is PermissionError
62-
assert "Unable to create directory" in str(exec_info.value)
57+
# test permission error on creation - use a readonly parent directory
58+
with tempfile.TemporaryDirectory() as temp_dir:
59+
readonly_parent = os.path.join(temp_dir, "readonly")
60+
os.makedirs(readonly_parent, mode=0o555) # Read-only parent
61+
try:
62+
non_existent = os.path.join(readonly_parent, "non-existent-directory")
63+
with pytest.raises(PermissionError) as exec_info:
64+
log_utils.check_directory_permissions(non_existent)
65+
assert type(exec_info.value) is PermissionError
66+
assert "Unable to create directory" in str(exec_info.value)
67+
finally:
68+
os.chmod(readonly_parent, 0o755) # Restore permissions for cleanup
6369

6470
def test_remove_old_logs(self):
6571
directory = os.path.join(tempfile.gettempdir(), "test_remove_logs")
@@ -181,11 +187,12 @@ def test_gzip_file_with_sufix(self):
181187
log_utils.delete_file(result)
182188
assert os.path.isfile(result) == False
183189

184-
# test a non-existent file
185-
file_path = "/non-existent-directory/test2.log"
186-
sufix = "test2"
187-
result = log_utils.gzip_file_with_sufix(file_path, sufix)
188-
assert result is None
190+
# test a non-existent file - use tempfile path that doesn't exist
191+
with tempfile.TemporaryDirectory() as temp_dir:
192+
file_path = os.path.join(temp_dir, "non-existent-directory", "test2.log")
193+
sufix = "test2"
194+
result = log_utils.gzip_file_with_sufix(file_path, sufix)
195+
assert result is None
189196

190197
def test_get_timezone_function(self):
191198
timezone = "UTC"

tests/factory/test_factory_examples.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,18 @@ def test_error_handling_scenarios(self):
192192
create_logger("nonexistent_type", name="error_test")
193193

194194
# Invalid directory (should raise PermissionError when trying to create)
195-
with pytest.raises(PermissionError):
196-
size_rotating_logger(
197-
name="permission_test",
198-
directory="/invalid/directory/path"
199-
)
195+
with tempfile.TemporaryDirectory() as temp_dir:
196+
readonly_parent = os.path.join(temp_dir, "readonly")
197+
os.makedirs(readonly_parent, mode=0o555) # Read-only parent
198+
try:
199+
invalid_dir = os.path.join(readonly_parent, "invalid")
200+
with pytest.raises(PermissionError):
201+
size_rotating_logger(
202+
name="permission_test",
203+
directory=invalid_dir
204+
)
205+
finally:
206+
os.chmod(readonly_parent, 0o755) # Restore permissions for cleanup
200207

201208
def test_logger_customization_example(self):
202209
"""Test logger with extensive customization."""

tests/timezone/test_zoneinfo_fallbacks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ def test_memory_usage_with_timezone_caching(self):
198198
"""Test that timezone caching doesn't cause memory leaks."""
199199
from pythonLogs import basic_logger, clear_logger_registry
200200

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+
201208
# Create many loggers with same timezone (should use cache)
202209
for i in range(100):
203210
logger = basic_logger(
@@ -215,6 +222,13 @@ def test_timezone_validation_edge_cases(self):
215222
"""Test timezone validation for various edge cases."""
216223
from pythonLogs.log_utils import _get_timezone_offset
217224

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+
218232
# Test case variations (timezone names are case-sensitive except for localtime)
219233
test_cases = [
220234
("UTC", "+0000"),

0 commit comments

Comments
 (0)