Skip to content

Commit

Permalink
Updated log and LLM call.
Browse files Browse the repository at this point in the history
  • Loading branch information
EmbeddedDevops1 committed Sep 15, 2024
1 parent 1dfcc51 commit aa47d47
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cover_agent/CoverAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
29 changes: 25 additions & 4 deletions cover_agent/UnitTestGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cover_agent/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
15 changes: 10 additions & 5 deletions cover_agent/settings/mutation_test_prompt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ Focus on subtle, realistic mutations that challenge the code's resilience while
Example output:
```yaml
source: {{source_file}}
mutation:
line: <line number>
mutation: |
<mutated code>
file: {{source_file}}
mutations:
- method: <function name>
category: <mutation type>
summary: <brief mutation description>
location: <line number>
original: |
<original code>
mutated_version: |
<mutated code with {{language}} comment explaining the change>
```
Use block scalar('|') to format each YAML output.
Expand Down
25 changes: 25 additions & 0 deletions templated_tests/python_fastapi/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from app import app
from datetime import date

import math
client = TestClient(app)

def test_root():
Expand All @@ -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()}

0 comments on commit aa47d47

Please sign in to comment.