diff --git a/cover_agent/CoverAgent.py b/cover_agent/CoverAgent.py index 36999e683..f48d58e46 100644 --- a/cover_agent/CoverAgent.py +++ b/cover_agent/CoverAgent.py @@ -31,6 +31,7 @@ def __init__(self, args): api_base=args.api_base, use_report_coverage_feature_flag=args.use_report_coverage_feature_flag, mutation_testing=args.mutation_testing, + more_mutation_logging=args.more_mutation_logging, ) def _validate_paths(self): @@ -90,7 +91,8 @@ def run(self): if self.test_gen.current_coverage < (self.test_gen.desired_coverage / 100): self.test_gen.run_coverage() - self.test_gen.run_mutations() + if self.args.mutation_testing: + self.test_gen.run_mutations() if self.test_gen.current_coverage >= (self.test_gen.desired_coverage / 100): self.logger.info( diff --git a/cover_agent/UnitTestGenerator.py b/cover_agent/UnitTestGenerator.py index 9c861fc42..6bccd6a3b 100644 --- a/cover_agent/UnitTestGenerator.py +++ b/cover_agent/UnitTestGenerator.py @@ -36,6 +36,7 @@ def __init__( additional_instructions: str = "", use_report_coverage_feature_flag: bool = False, mutation_testing: bool = False, + more_mutation_logging: bool = False, ): """ Initialize the UnitTestGenerator class with the provided parameters. @@ -72,6 +73,7 @@ def __init__( self.language = self.get_code_language(source_file_path) self.use_report_coverage_feature_flag = use_report_coverage_feature_flag self.mutation_testing = mutation_testing + self.more_mutation_logging = more_mutation_logging self.last_coverage_percentages = {} self.llm_model = llm_model @@ -770,14 +772,33 @@ def run_mutations(self): mutation_dict = load_yaml(response) - for mutation in mutation_dict["mutation"]: + for mutation in mutation_dict["mutations"]: result = self.run_mutation(mutation) - self.logger.info(f"Mutation result: {result}") + + # Prepare the log message with banners + log_message = f"Mutation result (return code: {result.returncode}):\n" + if result.returncode == 0: + log_message += "Mutation survived.\n" + else: + log_message += "Mutation caught.\n" + + # Add STDOUT to the log message if it's not empty + if result.stdout.strip() and self.more_mutation_logging: + log_message += "\n" + "="*10 + " STDOUT " + "="*10 + "\n" + log_message += result.stdout + + # Add STDERR to the log message if it's not empty + if result.stderr.strip() and self.more_mutation_logging: + log_message += "\n" + "="*10 + " STDERR " + "="*10 + "\n" + log_message += result.stderr + + + self.logger.info(log_message) def run_mutation(self, mutation): - mutated_code = mutation.get("mutation", None) - line_number = mutation.get("line", None) + mutated_code = mutation.get("mutated_version", None) + line_number = mutation.get("location", None) # Read the original content diff --git a/cover_agent/main.py b/cover_agent/main.py index fccc8e51e..750df9e07 100644 --- a/cover_agent/main.py +++ b/cover_agent/main.py @@ -106,6 +106,11 @@ def parse_args(): action="store_true", help="Setting this to True enables mutation testing. Default: False.", ) + parser.add_argument( + "--more-mutation-logging", + action="store_true", + help="Setting this to True enables more logging. Default: False.", + ) return parser.parse_args() diff --git a/cover_agent/settings/mutation_test_prompt.toml b/cover_agent/settings/mutation_test_prompt.toml index 61963b41d..8e861607d 100644 --- a/cover_agent/settings/mutation_test_prompt.toml +++ b/cover_agent/settings/mutation_test_prompt.toml @@ -64,11 +64,16 @@ Focus on subtle, realistic mutations that challenge the code's resilience while Example output: ```yaml -source: {{source_file}} -mutation: - line: - mutation: | - +file: {{source_file}} +mutations: + - method: + category: + summary: + location: + original: | + + mutated_version: | + ``` Use block scalar('|') to format each YAML output. diff --git a/templated_tests/python_fastapi/test_app.py b/templated_tests/python_fastapi/test_app.py index 8c391f3e9..60be8f415 100644 --- a/templated_tests/python_fastapi/test_app.py +++ b/templated_tests/python_fastapi/test_app.py @@ -3,6 +3,7 @@ from app import app from datetime import date +import math client = TestClient(app) def test_root(): @@ -13,3 +14,27 @@ def test_root(): assert response.status_code == 200 assert response.json() == {"message": "Welcome to the FastAPI application!"} + +def test_sqrt_negative_number(): + response = client.get("/sqrt/-4") + assert response.status_code == 400 + assert response.json() == {"detail": "Cannot take square root of a negative number"} + + +def test_divide_by_zero(): + response = client.get("/divide/10/0") + assert response.status_code == 400 + assert response.json() == {"detail": "Cannot divide by zero"} + + +def test_add(): + response = client.get("/add/3/5") + assert response.status_code == 200 + assert response.json() == {"result": 8} + + +def test_current_date(): + response = client.get("/current-date") + assert response.status_code == 200 + assert response.json() == {"date": date.today().isoformat()} +