Skip to content

Commit 86bcbc6

Browse files
authored
Merge branch 'main' into fix/invalid-api-key/stop-optimization
2 parents ef6d6ab + de9837a commit 86bcbc6

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

codeflash/code_utils/code_utils.py

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

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

codeflash/optimization/function_optimizer.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
replace_function_definitions_in_module,
3131
)
3232
from codeflash.code_utils.code_utils import (
33-
ImportErrorPattern,
3433
cleanup_paths,
3534
create_rank_dictionary_compact,
3635
diff_length,
36+
extract_unique_errors,
3737
file_name_from_test_module_name,
3838
get_run_tmp_file,
3939
module_name_from_file_path,
@@ -1576,11 +1576,14 @@ def establish_original_code_baseline(
15761576
)
15771577
if not behavioral_results:
15781578
logger.warning(
1579-
f"force_lsp|Couldn't run any tests for original function {self.function_to_optimize.function_name}. SKIPPING OPTIMIZING THIS FUNCTION."
1579+
f"force_lsp|Couldn't run any tests for original function {self.function_to_optimize.function_name}. Skipping optimization."
15801580
)
15811581
console.rule()
15821582
return Failure("Failed to establish a baseline for the original code - bevhavioral tests failed.")
15831583
if not coverage_critic(coverage_results, self.args.test_framework):
1584+
did_pass_all_tests = all(result.did_pass for result in behavioral_results)
1585+
if not did_pass_all_tests:
1586+
return Failure("Tests failed to pass for the original code.")
15841587
return Failure(
15851588
f"Test coverage is {coverage_results.coverage}%, which is below the required threshold of {COVERAGE_THRESHOLD}%."
15861589
)
@@ -1944,12 +1947,19 @@ def run_and_parse_tests(
19441947
f"stdout: {run_result.stdout}\n"
19451948
f"stderr: {run_result.stderr}\n"
19461949
)
1947-
if "ModuleNotFoundError" in run_result.stdout:
1950+
1951+
unique_errors = extract_unique_errors(run_result.stdout)
1952+
1953+
if unique_errors:
19481954
from rich.text import Text
19491955

1950-
match = ImportErrorPattern.search(run_result.stdout).group()
1951-
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
1952-
console.print(panel)
1956+
for error in unique_errors:
1957+
if is_LSP_enabled():
1958+
lsp_log(LspCodeMessage(code=error, file_name="errors"))
1959+
else:
1960+
panel = Panel(Text.from_markup(f"⚠️ {error} ", style="bold red"), expand=False)
1961+
console.print(panel)
1962+
19531963
if testing_type in {TestingMode.BEHAVIOR, TestingMode.PERFORMANCE}:
19541964
results, coverage_results = parse_test_results(
19551965
test_xml_path=result_file_path,

0 commit comments

Comments
 (0)