Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching from using diff cover executable to diff cover import #237

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions cover_agent/UnitTestValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import re

from diff_cover.diff_cover_tool import main as diff_cover_main

from cover_agent.AICaller import AICaller
from cover_agent.CoverageProcessor import CoverageProcessor
from cover_agent.CustomLogger import CustomLogger
Expand Down Expand Up @@ -744,17 +746,29 @@ def post_process_coverage_report(self, time_of_test_command):


def generate_diff_coverage_report(self):
# Run the diff-cover command to generate a JSON diff coverage report
coverage_filename = os.path.basename(self.code_coverage_report_path)
coverage_command = f"diff-cover --json-report {self.diff_coverage_report_name} --compare-branch={self.comparison_branch} {coverage_filename}"
# Log and execute the diff coverage command
self.logger.info(f'Running diff coverage command: "{coverage_command}"')
stdout, stderr, exit_code, _ = Runner.run_command(
command=coverage_command, cwd=self.test_command_dir
)

# Ensure the diff command executed successfully
assert exit_code == 0, (
f'Fatal: Error running diff coverage command. Are you sure the command is correct? "{coverage_command}"'
f"\nExit code {exit_code}. \nStdout: \n{stdout} \nStderr: \n{stderr}"
)
"""
Generates a JSON diff coverage report using the diff-cover tool.
This method runs the diff-cover command with the specified arguments to generate
a JSON report that shows the coverage differences between the current branch and
the specified comparison branch.
Args:
None
Returns:
None
Raises:
Exception: If an error occurs while running the diff-cover command.
"""

diff_cover_args = [
"diff-cover",
"--json-report",
self.diff_cover_report_path,
"--compare-branch={}".format(self.comparison_branch),
self.code_coverage_report_path
]

self.logger.info(f'Running diff coverage module with args: "{diff_cover_args}"')
try:
diff_cover_main(diff_cover_args)
except Exception as e:
self.logger.error(f"Error running diff-cover: {e}")
34 changes: 29 additions & 5 deletions tests/test_UnitTestValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_validate_test_pass_no_coverage_increase_with_prompt(self):
result = generator.validate_test(test_to_validate)

assert result["status"] == "FAIL"
assert result["reason"] == "Coverage did not increase"
assert "Coverage did not increase" in result["reason"]
assert result["exit_code"] == 0

def test_initial_test_suite_analysis_with_prompt_builder(self):
Expand Down Expand Up @@ -183,16 +183,40 @@ def test_post_process_coverage_report_without_flags(self):
percentage_covered, coverage_percentages = generator.post_process_coverage_report(datetime.datetime.now())
assert percentage_covered == 0.7

def test_generate_diff_coverage_report(self):

def test_generate_diff_coverage_report_success(self):
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as temp_source_file:
generator = UnitTestValidator(
source_file_path=temp_source_file.name,
test_file_path="test_test.py",
code_coverage_report_path="coverage.xml",
test_command="pytest",
llm_model="gpt-3",
diff_coverage=True
diff_coverage=True,
comparison_branch="main"
)
with patch.object(Runner, 'run_command', return_value=("", "", 0, datetime.datetime.now())):
with patch("cover_agent.UnitTestValidator.diff_cover_main") as mock_diff_cover_main:
generator.generate_diff_coverage_report()
mock_diff_cover_main.assert_called_once_with([
"diff-cover",
"--json-report",
generator.diff_cover_report_path,
"--compare-branch=main",
"coverage.xml"
])

def test_generate_diff_coverage_report_failure(self):
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as temp_source_file:
generator = UnitTestValidator(
source_file_path=temp_source_file.name,
test_file_path="test_test.py",
code_coverage_report_path="coverage.xml",
test_command="pytest",
llm_model="gpt-3",
diff_coverage=True,
comparison_branch="main"
)
with patch("cover_agent.UnitTestValidator.diff_cover_main", side_effect=Exception("Mock exception")), \
patch.object(generator.logger, 'error') as mock_logger_error:
generator.generate_diff_coverage_report()
assert generator.diff_cover_report_path.endswith("diff-cover-report.json")
mock_logger_error.assert_called_once_with("Error running diff-cover: Mock exception")
Loading