From c5a921167bba3cda146d64f74d7a99678a68e187 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 2 Dec 2024 18:24:35 -0800 Subject: [PATCH 1/2] Removing diff cover execution and using diff cover import instead --- cover_agent/UnitTestValidator.py | 43 +++++++++++++++++++++----------- tests/test_UnitTestValidator.py | 9 ++++--- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/cover_agent/UnitTestValidator.py b/cover_agent/UnitTestValidator.py index 9421ead37..b1bb906c4 100644 --- a/cover_agent/UnitTestValidator.py +++ b/cover_agent/UnitTestValidator.py @@ -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 @@ -744,17 +746,30 @@ 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 + ] + print(diff_cover_args) + + 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}") diff --git a/tests/test_UnitTestValidator.py b/tests/test_UnitTestValidator.py index 147ca91dc..28ec60631 100644 --- a/tests/test_UnitTestValidator.py +++ b/tests/test_UnitTestValidator.py @@ -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): @@ -183,7 +183,7 @@ 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_run_coverage_with_diff_coverage_flag(self): with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as temp_source_file: generator = UnitTestValidator( source_file_path=temp_source_file.name, @@ -194,5 +194,6 @@ def test_generate_diff_coverage_report(self): diff_coverage=True ) with patch.object(Runner, 'run_command', return_value=("", "", 0, datetime.datetime.now())): - generator.generate_diff_coverage_report() - assert generator.diff_cover_report_path.endswith("diff-cover-report.json") \ No newline at end of file + with patch.object(CoverageProcessor, 'process_coverage_report', return_value=([], [], 0.7)): + generator.run_coverage() + assert generator.current_coverage == 0.7 \ No newline at end of file From 83571ca93dc7eea81f0f7bc875d6240e7eb79faa Mon Sep 17 00:00:00 2001 From: James Date: Mon, 2 Dec 2024 18:30:30 -0800 Subject: [PATCH 2/2] Fixing tests and removing print --- cover_agent/UnitTestValidator.py | 1 - tests/test_UnitTestValidator.py | 35 ++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/cover_agent/UnitTestValidator.py b/cover_agent/UnitTestValidator.py index b1bb906c4..f98de5327 100644 --- a/cover_agent/UnitTestValidator.py +++ b/cover_agent/UnitTestValidator.py @@ -766,7 +766,6 @@ def generate_diff_coverage_report(self): "--compare-branch={}".format(self.comparison_branch), self.code_coverage_report_path ] - print(diff_cover_args) self.logger.info(f'Running diff coverage module with args: "{diff_cover_args}"') try: diff --git a/tests/test_UnitTestValidator.py b/tests/test_UnitTestValidator.py index 28ec60631..7af52faff 100644 --- a/tests/test_UnitTestValidator.py +++ b/tests/test_UnitTestValidator.py @@ -183,7 +183,8 @@ 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_run_coverage_with_diff_coverage_flag(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, @@ -191,9 +192,31 @@ def test_run_coverage_with_diff_coverage_flag(self): 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.object(CoverageProcessor, 'process_coverage_report', return_value=([], [], 0.7)): - generator.run_coverage() - assert generator.current_coverage == 0.7 \ No newline at end of file + 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() + mock_logger_error.assert_called_once_with("Error running diff-cover: Mock exception") \ No newline at end of file