A class that manages the generation and validation of unit tests to achieve desired code coverage. This agent coordinates between test generation and validation components, handles file management, and tracks the progress of coverage improvements over multiple iterations.
| 20 | |
| 21 | |
| 22 | class CoverAgent: |
| 23 | """ |
| 24 | A class that manages the generation and validation of unit tests to achieve desired code coverage. |
| 25 | |
| 26 | This agent coordinates between test generation and validation components, handles file management, |
| 27 | and tracks the progress of coverage improvements over multiple iterations. |
| 28 | """ |
| 29 | def __init__( |
| 30 | self, |
| 31 | config: CoverAgentConfig, |
| 32 | agent_completion: AgentCompletionABC=None, |
| 33 | logger: Optional[CustomLogger]=None, |
| 34 | ): |
| 35 | """ |
| 36 | Initialize the CoverAgent instance. |
| 37 | |
| 38 | Args: |
| 39 | config (CoverAgentConfig): Configuration object containing all necessary settings for the agent. |
| 40 | agent_completion (AgentCompletionABC, optional): Custom agent completion object. Defaults to None, |
| 41 | in which case a default completion object is initialized. |
| 42 | logger (Optional[CustomLogger], optional): Custom logger instance. Defaults to None, |
| 43 | in which case a default logger is created. |
| 44 | |
| 45 | Attributes: |
| 46 | logger (CustomLogger): Logger instance for logging messages. |
| 47 | config (CoverAgentConfig): Configuration object for the agent. |
| 48 | agent_completion (AgentCompletionABC): Agent completion object for handling AI interactions. |
| 49 | """ |
| 50 | self.config = config |
| 51 | self.generate_log_files = not config.suppress_log_files |
| 52 | |
| 53 | # Initialize logger with file generation flag |
| 54 | self.logger = logger or CustomLogger.get_logger(__name__, generate_log_files=self.generate_log_files) |
| 55 | if config.suppress_log_files: |
| 56 | self.logger.info("Suppressed all generated log files.") |
| 57 | |
| 58 | self._validate_paths() |
| 59 | self._duplicate_test_file() |
| 60 | |
| 61 | # Configure the AgentCompletion object |
| 62 | if agent_completion: |
| 63 | self.agent_completion = agent_completion |
| 64 | else: |
| 65 | self.ai_caller = self._initialize_ai_caller() |
| 66 | self.agent_completion = DefaultAgentCompletion( |
| 67 | caller=self.ai_caller, generate_log_files=self.generate_log_files |
| 68 | ) |
| 69 | |
| 70 | # Modify test command for a single test execution if needed |
| 71 | test_command = self.config.test_command |
| 72 | new_command_line = None |
| 73 | if hasattr(self.config, "run_each_test_separately") and self.config.run_each_test_separately: |
| 74 | # Calculate a relative path for a test file |
| 75 | test_file_relative_path = os.path.relpath( |
| 76 | self.config.test_file_output_path, self.config.project_root |
| 77 | ) |
| 78 | # Handle pytest commands specifically |
| 79 | if "pytest" in test_command: |
no outgoing calls