- Introduced a new CLI module (`cli.py`) to manage server configurations via command line arguments. - Added script entry point in `pyproject.toml` for easy access to the CLI. - Enhanced `Config` class to load configurations from a YAML file. - Updated `__init__.py` to include `__version__` in the module exports. - Added optional dependencies for development tools in `pyproject.toml`. - Implemented logging improvements and error handling in various modules. - Created tests for the CLI functionality to ensure proper behavior. - Removed the old `run.py` implementation in favor of the new CLI approach.
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import pytest
|
|
from unittest.mock import patch, MagicMock
|
|
from pyserve.cli import main
|
|
|
|
|
|
def test_cli_version():
|
|
with patch('sys.argv', ['pyserve', '--version']):
|
|
with pytest.raises(SystemExit) as exc_info:
|
|
main()
|
|
assert exc_info.value.code == 0
|
|
|
|
|
|
def test_cli_help():
|
|
with patch('sys.argv', ['pyserve', '--help']):
|
|
with pytest.raises(SystemExit) as exc_info:
|
|
main()
|
|
assert exc_info.value.code == 0
|
|
|
|
|
|
@patch('pyserve.cli.PyServeServer')
|
|
@patch('pyserve.cli.Config')
|
|
def test_cli_default_config(mock_config, mock_server):
|
|
mock_config_instance = MagicMock()
|
|
mock_config.return_value = mock_config_instance
|
|
mock_server_instance = MagicMock()
|
|
mock_server.return_value = mock_server_instance
|
|
|
|
with patch('sys.argv', ['pyserve']):
|
|
with patch('pathlib.Path.exists', return_value=False):
|
|
main()
|
|
|
|
mock_config.assert_called_once()
|
|
mock_server.assert_called_once_with(mock_config_instance)
|
|
mock_server_instance.run.assert_called_once()
|
|
|
|
|
|
@patch('pyserve.cli.PyServeServer')
|
|
@patch('pyserve.cli.Config')
|
|
def test_cli_custom_host_port(mock_config, mock_server):
|
|
mock_config_instance = MagicMock()
|
|
mock_config.return_value = mock_config_instance
|
|
mock_server_instance = MagicMock()
|
|
mock_server.return_value = mock_server_instance
|
|
|
|
with patch('sys.argv', ['pyserve', '--host', '127.0.0.1', '--port', '9000']):
|
|
with patch('pathlib.Path.exists', return_value=False):
|
|
main()
|
|
|
|
assert mock_config_instance.server.host == '127.0.0.1'
|
|
assert mock_config_instance.server.port == 9000
|