Skip to content

Commit 77d102f

Browse files
author
Codeflash Bot
committed
log the test results errors to the lsp logs
1 parent 3b522fa commit 77d102f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,19 @@ def exit_with_message(message: str, *, error_on_exit: bool = False) -> None:
353353
paneled_text(message, panel_args={"style": "red"})
354354

355355
sys.exit(1 if error_on_exit else 0)
356+
357+
358+
def extract_unique_errors(pytest_output: str) -> set[str]:
359+
unique_errors = set()
360+
361+
# Regex pattern to match error lines:
362+
# - Start with 'E' followed by optional whitespace
363+
# - Capture the actual error message
364+
pattern = r"^E\s+(.*)$"
365+
366+
for match in re.finditer(pattern, pytest_output, re.MULTILINE):
367+
error_message = match.group(1).strip()
368+
if error_message:
369+
unique_errors.add(error_message)
370+
371+
return unique_errors

codeflash/optimization/function_optimizer.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
cleanup_paths,
3535
create_rank_dictionary_compact,
3636
diff_length,
37+
extract_unique_errors,
3738
file_name_from_test_module_name,
3839
get_run_tmp_file,
3940
module_name_from_file_path,
@@ -1569,11 +1570,14 @@ def establish_original_code_baseline(
15691570
)
15701571
if not behavioral_results:
15711572
logger.warning(
1572-
f"force_lsp|Couldn't run any tests for original function {self.function_to_optimize.function_name}. SKIPPING OPTIMIZING THIS FUNCTION."
1573+
f"force_lsp|Couldn't run any tests for original function {self.function_to_optimize.function_name}. Skipping optimization."
15731574
)
15741575
console.rule()
15751576
return Failure("Failed to establish a baseline for the original code - bevhavioral tests failed.")
15761577
if not coverage_critic(coverage_results, self.args.test_framework):
1578+
did_pass_all_tests = all(result.did_pass for result in behavioral_results)
1579+
if not did_pass_all_tests:
1580+
return Failure("Tests failed to pass for the original code.")
15771581
return Failure(
15781582
f"Test coverage is {coverage_results.coverage}%, which is below the required threshold of {COVERAGE_THRESHOLD}%."
15791583
)
@@ -1601,7 +1605,7 @@ def establish_original_code_baseline(
16011605
)
16021606

16031607
try:
1604-
benchmarking_results, _ = self.run_and_parse_tests(
1608+
benchmarking_results = self.run_and_parse_tests(
16051609
testing_type=TestingMode.PERFORMANCE,
16061610
test_env=test_env,
16071611
test_files=self.test_files,
@@ -1937,6 +1941,12 @@ def run_and_parse_tests(
19371941
f"stdout: {run_result.stdout}\n"
19381942
f"stderr: {run_result.stderr}\n"
19391943
)
1944+
1945+
if is_LSP_enabled():
1946+
unique_errors = extract_unique_errors(run_result.stdout)
1947+
if unique_errors:
1948+
lsp_log(LspCodeMessage(code="\n".join(unique_errors), file_name="errors"))
1949+
19401950
if "ModuleNotFoundError" in run_result.stdout:
19411951
from rich.text import Text
19421952

0 commit comments

Comments
 (0)