Skip to content

Commit

Permalink
use new coverage in CoverAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
coderustic committed Dec 27, 2024
1 parent f4db2be commit 399b6c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 7 additions & 7 deletions cover_agent/CoverAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,19 @@ def run_test_gen(self, failed_test_runs: List, language: str, test_framework: st

# Check if the desired coverage has been reached
failed_test_runs, language, test_framework, coverage_report = self.test_validator.get_coverage()
if self.test_validator.current_coverage >= (self.test_validator.desired_coverage / 100):
if self.test_validator.current_coverage_report.total_coverage >= (self.test_validator.desired_coverage / 100):
break

# Log the final coverage
if self.test_validator.current_coverage >= (self.test_validator.desired_coverage / 100):
if self.test_validator.current_coverage_report.total_coverage >= (self.test_validator.desired_coverage / 100):
self.logger.info(
f"Reached above target coverage of {self.test_validator.desired_coverage}% (Current Coverage: {round(self.test_validator.current_coverage * 100, 2)}%) in {iteration_count} iterations."
f"Reached above target coverage of {self.test_validator.desired_coverage}% (Current Coverage: {round(self.test_validator.current_coverage_report.total_coverage * 100, 2)}%) in {iteration_count} iterations."
)
elif iteration_count == self.args.max_iterations:
if self.args.diff_coverage:
failure_message = f"Reached maximum iteration limit without achieving desired diff coverage. Current Coverage: {round(self.test_validator.current_coverage * 100, 2)}%"
failure_message = f"Reached maximum iteration limit without achieving desired diff coverage. Current Coverage: {round(self.test_validator.current_coverage_report.total_coverage * 100, 2)}%"
else:
failure_message = f"Reached maximum iteration limit without achieving desired coverage. Current Coverage: {round(self.test_validator.current_coverage * 100, 2)}%"
failure_message = f"Reached maximum iteration limit without achieving desired coverage. Current Coverage: {round(self.test_validator.current_coverage_report.total_coverage * 100, 2)}%"
if self.args.strict_coverage:
# User requested strict coverage (similar to "--cov-fail-under in pytest-cov"). Fail with exist code 2.
self.logger.error(failure_message)
Expand All @@ -237,11 +237,11 @@ def run_test_gen(self, failed_test_runs: List, language: str, test_framework: st
def log_coverage(self):
if self.args.diff_coverage:
self.logger.info(
f"Current Diff Coverage: {round(self.test_validator.current_coverage * 100, 2)}%"
f"Current Diff Coverage: {round(self.test_validator.current_coverage_report.total_coverage * 100, 2)}%"
)
else:
self.logger.info(
f"Current Coverage: {round(self.test_validator.current_coverage * 100, 2)}%"
f"Current Coverage: {round(self.test_validator.current_coverage_report.total_coverage * 100, 2)}%"
)
self.logger.info(f"Desired Coverage: {self.test_validator.desired_coverage}%")

Expand Down
7 changes: 4 additions & 3 deletions tests/test_CoverAgent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cover_agent.CoverAgent import CoverAgent
from cover_agent.coverage.processor import CoverageReport
from cover_agent.main import parse_args
from unittest.mock import patch, MagicMock
import argparse
Expand Down Expand Up @@ -213,7 +214,7 @@ def test_run_max_iterations_strict_coverage(self, mock_test_db, mock_unit_test_v
)
# Mock the methods used in run
validator = mock_unit_test_validator.return_value
validator.current_coverage = 0.5 # below desired coverage
validator.current_coverage_report = CoverageReport(0.5, {}) # below desired coverage
validator.desired_coverage = 90
validator.get_coverage.return_value = [{}, "python", "pytest", ""]
generator = mock_unit_test_generator.return_value
Expand Down Expand Up @@ -277,14 +278,14 @@ def test_run_diff_coverage(self, mock_logger, mock_test_db, mock_test_gen, mock_
diff_coverage=True,
branch="main"
)
mock_test_validator.return_value.current_coverage = 0.5
mock_test_validator.return_value.current_coverage_report = CoverageReport(0.5, {})
mock_test_validator.return_value.desired_coverage = 90
mock_test_validator.return_value.get_coverage.return_value = [{}, "python", "pytest", ""]
mock_test_gen.return_value.generate_tests.return_value = {"new_tests": [{}]}
agent = CoverAgent(args)
agent.run()
mock_logger.get_logger.return_value.info.assert_any_call(
f"Current Diff Coverage: {round(mock_test_validator.return_value.current_coverage * 100, 2)}%"
f"Current Diff Coverage: {round(mock_test_validator.return_value.current_coverage_report.total_coverage * 100, 2)}%"
)

# Clean up the temp files
Expand Down

0 comments on commit 399b6c9

Please sign in to comment.