forked from cleveragents/cleveragents-core
123 lines
4.0 KiB
Python
123 lines
4.0 KiB
Python
"""
|
|
Unit tests for __main__.py module
|
|
|
|
Tests the package entry point.
|
|
"""
|
|
|
|
import pytest
|
|
import sys
|
|
import subprocess
|
|
from unittest.mock import patch, Mock
|
|
from pathlib import Path
|
|
|
|
|
|
class TestMainModule:
|
|
"""Test suite for the __main__ module."""
|
|
|
|
def test_main_module_imports(self):
|
|
"""Test that __main__ module can be imported."""
|
|
# Force reimport to ensure coverage tracking
|
|
if 'cleveragents.__main__' in sys.modules:
|
|
del sys.modules['cleveragents.__main__']
|
|
|
|
import cleveragents.__main__
|
|
|
|
assert cleveragents.__main__ is not None
|
|
|
|
def test_main_import_available(self):
|
|
"""Test that main function is imported from cli."""
|
|
# Force reload to track imports
|
|
if 'cleveragents.__main__' in sys.modules:
|
|
import importlib
|
|
importlib.reload(sys.modules['cleveragents.__main__'])
|
|
|
|
import cleveragents.__main__ as main_module
|
|
|
|
# Verify the main function is available
|
|
assert hasattr(main_module, 'main')
|
|
|
|
def test_main_source_execution(self):
|
|
"""Test by executing the __main__ module source directly."""
|
|
import cleveragents.__main__
|
|
from pathlib import Path
|
|
|
|
# Read and execute the source to ensure coverage
|
|
source_file = Path(cleveragents.__main__.__file__)
|
|
with open(source_file, 'r') as f:
|
|
source_code = f.read()
|
|
|
|
# Create a namespace and execute (without __name__ == '__main__')
|
|
namespace = {'__name__': 'test_context'}
|
|
exec(source_code, namespace)
|
|
|
|
# Verify the import happened
|
|
assert 'main' in namespace
|
|
|
|
def test_main_function_is_from_cli(self):
|
|
"""Test that main is actually from cleveragents.cli."""
|
|
import cleveragents.__main__ as main_module
|
|
from cleveragents.cli import main as cli_main
|
|
|
|
# They should be the same function
|
|
assert main_module.main is cli_main
|
|
|
|
def test_module_docstring(self):
|
|
"""Test that the module has appropriate documentation."""
|
|
import cleveragents.__main__ as main_module
|
|
|
|
assert main_module.__doc__ is not None
|
|
assert "entry point" in main_module.__doc__.lower() or "entrypoint" in main_module.__doc__.lower()
|
|
|
|
def test_main_module_name_attribute(self):
|
|
"""Test that __main__ module has expected attributes."""
|
|
import cleveragents.__main__ as main_module
|
|
|
|
# Check that it has the expected module name
|
|
assert main_module.__name__ == 'cleveragents.__main__'
|
|
|
|
@patch('cleveragents.cli.main')
|
|
def test_main_block_calls_main_function(self, mock_main):
|
|
"""Test that the if __name__ == '__main__' block calls main()."""
|
|
from pathlib import Path
|
|
import cleveragents.__main__
|
|
|
|
# Get the source file path
|
|
source_file = Path(cleveragents.__main__.__file__)
|
|
with open(source_file, 'r') as f:
|
|
source_code = f.read()
|
|
|
|
# Execute with __name__ set to '__main__' to trigger the if block
|
|
namespace = {'__name__': '__main__'}
|
|
exec(source_code, namespace)
|
|
|
|
# main() should have been called
|
|
assert mock_main.called, "main() was not called when __name__ == '__main__'"
|
|
assert mock_main.call_count >= 1
|
|
|
|
@patch('cleveragents.cli.main')
|
|
def test_main_module_execution_via_runpy(self, mock_main):
|
|
"""Test executing __main__ module using runpy."""
|
|
import runpy
|
|
|
|
# Clean up any existing __main__ module to avoid warning
|
|
if 'cleveragents.__main__' in sys.modules:
|
|
del sys.modules['cleveragents.__main__']
|
|
|
|
# Mock main to prevent actual CLI execution
|
|
mock_main.return_value = None
|
|
|
|
try:
|
|
# Run the module as __main__
|
|
runpy.run_module('cleveragents', run_name='__main__', alter_sys=False)
|
|
except SystemExit:
|
|
# CLI might call sys.exit(), which is fine
|
|
pass
|
|
|
|
# main() should have been called
|
|
assert mock_main.called, "main() was not called via runpy"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|
|
|