- Added coverage version 7.10.1 with multiple wheel distributions. - Added iniconfig version 2.1.0 with its wheel distribution. - Added pluggy version 1.6.0 with its wheel distribution. - Added pygments version 2.19.2 with its wheel distribution. - Added pytest version 8.4.1 with its wheel distribution and dependencies. - Added pytest-cov version 6.2.1 with its wheel distribution and dependencies. - Added pytest-mock version 3.14.1 with its wheel distribution and dependencies. - Updated dev-dependencies to include coverage, pytest, pytest-cov, and pytest-mock. - Updated requires-dist to specify minimum versions for coverage, pytest, pytest-cov, and pytest-mock.
259 lines
9.0 KiB
Python
259 lines
9.0 KiB
Python
"""
|
|
Tests for init module.
|
|
"""
|
|
import os
|
|
import tempfile
|
|
import pytest
|
|
from unittest.mock import patch, Mock
|
|
|
|
import sys
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
|
|
|
|
|
class TestInit:
|
|
"""Test cases for the init module."""
|
|
|
|
def test_log_directory_creation(self, temp_log_dir):
|
|
"""Test that log directory is created if it doesn't exist."""
|
|
with patch('init.LOG_PATH', temp_log_dir + '/new_dir'), \
|
|
patch('os.path.exists', return_value=False), \
|
|
patch('os.mkdir') as mock_mkdir:
|
|
|
|
# Re-import to trigger the directory creation logic
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
mock_mkdir.assert_called_once()
|
|
|
|
def test_log_directory_exists(self, temp_log_dir):
|
|
"""Test behavior when log directory already exists."""
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('os.path.exists', return_value=True), \
|
|
patch('os.mkdir') as mock_mkdir:
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
mock_mkdir.assert_not_called()
|
|
|
|
def test_log_directory_creation_error(self, temp_log_dir):
|
|
"""Test handling of errors during log directory creation."""
|
|
with patch('init.LOG_PATH', '/invalid/path'), \
|
|
patch('os.path.exists', return_value=False), \
|
|
patch('os.mkdir', side_effect=PermissionError("Permission denied")), \
|
|
patch('builtins.print') as mock_print:
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
mock_print.assert_called()
|
|
|
|
def test_logger_initialization(self, temp_log_dir):
|
|
"""Test that logger is initialized correctly."""
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('init.LOG_LEVEL', 'INFO'), \
|
|
patch('init.init_logger') as mock_init_logger:
|
|
|
|
mock_logger = Mock()
|
|
mock_init_logger.return_value = mock_logger
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
mock_init_logger.assert_called_once_with('init', testing_mode=False)
|
|
|
|
def test_logger_initialization_debug_mode(self, temp_log_dir):
|
|
"""Test logger initialization in debug mode."""
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('init.LOG_LEVEL', 'DEBUG'), \
|
|
patch('init.init_logger') as mock_init_logger:
|
|
|
|
mock_logger = Mock()
|
|
mock_init_logger.return_value = mock_logger
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
mock_init_logger.assert_called_once_with('init', testing_mode=True)
|
|
|
|
def test_log_files_definition(self, temp_log_dir):
|
|
"""Test that log files tuple is defined correctly."""
|
|
with patch('init.LOG_PATH', temp_log_dir):
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
expected_files = (
|
|
f"{temp_log_dir}/thechart.log",
|
|
f"{temp_log_dir}/thechart.warning.log",
|
|
f"{temp_log_dir}/thechart.error.log",
|
|
)
|
|
|
|
assert init.log_files == expected_files
|
|
|
|
def test_testing_mode_detection(self, temp_log_dir):
|
|
"""Test that testing mode is detected correctly."""
|
|
with patch('init.LOG_PATH', temp_log_dir):
|
|
# Test with DEBUG level
|
|
with patch('init.LOG_LEVEL', 'DEBUG'):
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
assert init.testing_mode is True
|
|
|
|
# Test with non-DEBUG level
|
|
with patch('init.LOG_LEVEL', 'INFO'):
|
|
importlib.reload(sys.modules['init'])
|
|
assert init.testing_mode is False
|
|
|
|
def test_log_clear_true(self, temp_log_dir):
|
|
"""Test log file clearing when LOG_CLEAR is True."""
|
|
# Create some test log files
|
|
log_files = [
|
|
os.path.join(temp_log_dir, "thechart.log"),
|
|
os.path.join(temp_log_dir, "thechart.warning.log"),
|
|
os.path.join(temp_log_dir, "thechart.error.log"),
|
|
]
|
|
|
|
for log_file in log_files:
|
|
with open(log_file, 'w') as f:
|
|
f.write("Old log content")
|
|
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('init.LOG_CLEAR', 'True'), \
|
|
patch('init.log_files', log_files):
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
# Check that files were truncated
|
|
for log_file in log_files:
|
|
with open(log_file, 'r') as f:
|
|
assert f.read() == ""
|
|
|
|
def test_log_clear_false(self, temp_log_dir):
|
|
"""Test that log files are not cleared when LOG_CLEAR is False."""
|
|
# Create some test log files
|
|
log_files = [
|
|
os.path.join(temp_log_dir, "thechart.log"),
|
|
os.path.join(temp_log_dir, "thechart.warning.log"),
|
|
os.path.join(temp_log_dir, "thechart.error.log"),
|
|
]
|
|
|
|
original_content = "Original log content"
|
|
for log_file in log_files:
|
|
with open(log_file, 'w') as f:
|
|
f.write(original_content)
|
|
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('init.LOG_CLEAR', 'False'), \
|
|
patch('init.log_files', log_files):
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
# Check that files were not truncated
|
|
for log_file in log_files:
|
|
with open(log_file, 'r') as f:
|
|
assert f.read() == original_content
|
|
|
|
def test_log_clear_nonexistent_files(self, temp_log_dir):
|
|
"""Test log clearing when some log files don't exist."""
|
|
log_files = [
|
|
os.path.join(temp_log_dir, "thechart.log"),
|
|
os.path.join(temp_log_dir, "nonexistent.log"),
|
|
]
|
|
|
|
# Create only one of the files
|
|
with open(log_files[0], 'w') as f:
|
|
f.write("Content")
|
|
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('init.LOG_CLEAR', 'True'), \
|
|
patch('init.log_files', log_files):
|
|
|
|
# This should not raise an exception
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
def test_log_clear_permission_error(self, temp_log_dir):
|
|
"""Test handling of permission errors during log clearing."""
|
|
log_files = [os.path.join(temp_log_dir, "thechart.log")]
|
|
|
|
with open(log_files[0], 'w') as f:
|
|
f.write("Content")
|
|
|
|
with patch('init.LOG_PATH', temp_log_dir), \
|
|
patch('init.LOG_CLEAR', 'True'), \
|
|
patch('init.log_files', log_files), \
|
|
patch('builtins.open', side_effect=PermissionError("Permission denied")), \
|
|
patch('init.logger') as mock_logger:
|
|
|
|
mock_logger.error = Mock()
|
|
|
|
# Should raise the exception after logging
|
|
with pytest.raises(PermissionError):
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
def test_module_exports(self, temp_log_dir):
|
|
"""Test that module exports expected objects."""
|
|
with patch('init.LOG_PATH', temp_log_dir):
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
# Check that expected objects are available
|
|
assert hasattr(init, 'logger')
|
|
assert hasattr(init, 'log_files')
|
|
assert hasattr(init, 'testing_mode')
|
|
|
|
def test_log_path_printing(self, temp_log_dir):
|
|
"""Test that LOG_PATH is printed when directory is created."""
|
|
with patch('init.LOG_PATH', temp_log_dir + '/new_dir'), \
|
|
patch('os.path.exists', return_value=False), \
|
|
patch('os.mkdir'), \
|
|
patch('builtins.print') as mock_print:
|
|
|
|
import importlib
|
|
if 'init' in sys.modules:
|
|
importlib.reload(sys.modules['init'])
|
|
else:
|
|
import init
|
|
|
|
mock_print.assert_called_with(temp_log_dir + '/new_dir')
|