Skip to content

Commit aa47d47

Browse files
Updated log and LLM call.
1 parent 1dfcc51 commit aa47d47

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

cover_agent/CoverAgent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, args):
3131
api_base=args.api_base,
3232
use_report_coverage_feature_flag=args.use_report_coverage_feature_flag,
3333
mutation_testing=args.mutation_testing,
34+
more_mutation_logging=args.more_mutation_logging,
3435
)
3536

3637
def _validate_paths(self):
@@ -90,7 +91,8 @@ def run(self):
9091
if self.test_gen.current_coverage < (self.test_gen.desired_coverage / 100):
9192
self.test_gen.run_coverage()
9293

93-
self.test_gen.run_mutations()
94+
if self.args.mutation_testing:
95+
self.test_gen.run_mutations()
9496

9597
if self.test_gen.current_coverage >= (self.test_gen.desired_coverage / 100):
9698
self.logger.info(

cover_agent/UnitTestGenerator.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(
3636
additional_instructions: str = "",
3737
use_report_coverage_feature_flag: bool = False,
3838
mutation_testing: bool = False,
39+
more_mutation_logging: bool = False,
3940
):
4041
"""
4142
Initialize the UnitTestGenerator class with the provided parameters.
@@ -72,6 +73,7 @@ def __init__(
7273
self.language = self.get_code_language(source_file_path)
7374
self.use_report_coverage_feature_flag = use_report_coverage_feature_flag
7475
self.mutation_testing = mutation_testing
76+
self.more_mutation_logging = more_mutation_logging
7577
self.last_coverage_percentages = {}
7678
self.llm_model = llm_model
7779

@@ -770,14 +772,33 @@ def run_mutations(self):
770772

771773
mutation_dict = load_yaml(response)
772774

773-
for mutation in mutation_dict["mutation"]:
775+
for mutation in mutation_dict["mutations"]:
774776
result = self.run_mutation(mutation)
775-
self.logger.info(f"Mutation result: {result}")
777+
778+
# Prepare the log message with banners
779+
log_message = f"Mutation result (return code: {result.returncode}):\n"
780+
if result.returncode == 0:
781+
log_message += "Mutation survived.\n"
782+
else:
783+
log_message += "Mutation caught.\n"
784+
785+
# Add STDOUT to the log message if it's not empty
786+
if result.stdout.strip() and self.more_mutation_logging:
787+
log_message += "\n" + "="*10 + " STDOUT " + "="*10 + "\n"
788+
log_message += result.stdout
789+
790+
# Add STDERR to the log message if it's not empty
791+
if result.stderr.strip() and self.more_mutation_logging:
792+
log_message += "\n" + "="*10 + " STDERR " + "="*10 + "\n"
793+
log_message += result.stderr
794+
795+
796+
self.logger.info(log_message)
776797

777798

778799
def run_mutation(self, mutation):
779-
mutated_code = mutation.get("mutation", None)
780-
line_number = mutation.get("line", None)
800+
mutated_code = mutation.get("mutated_version", None)
801+
line_number = mutation.get("location", None)
781802

782803

783804
# Read the original content

cover_agent/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def parse_args():
106106
action="store_true",
107107
help="Setting this to True enables mutation testing. Default: False.",
108108
)
109+
parser.add_argument(
110+
"--more-mutation-logging",
111+
action="store_true",
112+
help="Setting this to True enables more logging. Default: False.",
113+
)
109114
return parser.parse_args()
110115

111116

cover_agent/settings/mutation_test_prompt.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ Focus on subtle, realistic mutations that challenge the code's resilience while
6464
6565
Example output:
6666
```yaml
67-
source: {{source_file}}
68-
mutation:
69-
line: <line number>
70-
mutation: |
71-
<mutated code>
67+
file: {{source_file}}
68+
mutations:
69+
- method: <function name>
70+
category: <mutation type>
71+
summary: <brief mutation description>
72+
location: <line number>
73+
original: |
74+
<original code>
75+
mutated_version: |
76+
<mutated code with {{language}} comment explaining the change>
7277
```
7378
7479
Use block scalar('|') to format each YAML output.

templated_tests/python_fastapi/test_app.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from app import app
44
from datetime import date
55

6+
import math
67
client = TestClient(app)
78

89
def test_root():
@@ -13,3 +14,27 @@ def test_root():
1314
assert response.status_code == 200
1415
assert response.json() == {"message": "Welcome to the FastAPI application!"}
1516

17+
18+
def test_sqrt_negative_number():
19+
response = client.get("/sqrt/-4")
20+
assert response.status_code == 400
21+
assert response.json() == {"detail": "Cannot take square root of a negative number"}
22+
23+
24+
def test_divide_by_zero():
25+
response = client.get("/divide/10/0")
26+
assert response.status_code == 400
27+
assert response.json() == {"detail": "Cannot divide by zero"}
28+
29+
30+
def test_add():
31+
response = client.get("/add/3/5")
32+
assert response.status_code == 200
33+
assert response.json() == {"result": 8}
34+
35+
36+
def test_current_date():
37+
response = client.get("/current-date")
38+
assert response.status_code == 200
39+
assert response.json() == {"date": date.today().isoformat()}
40+

0 commit comments

Comments
 (0)